SystemVerilog support in Pygments
I've used Pygments in the past to do syntax highlighting of Python and C code to drop into documentation. I've wanted SystemVerilog support for a while, but the tool didn't support the language. I eventually got frustrated enough by this to go and add it myself. The motivation was so that I could set up an internal LodgeIt! pastebin to allow colleagues to discuss snippets of code, without having to paste it into emails, or Yammer discussions, both of which always scramble formatting and make discussions harder than they should be. The pastebin that I decided to use, used Pygments as the backend, so I decided to add SystemVerilog support to that project.
The main task was adding the more than 220 keywords in SystemVerilog into the format required for Pygments. I ended up cutting and pasting the keywords from the specification into a text file, then using a quick script to generate the appropriate structures for Pygments. I found that generating it automatically was about the only way to avoid typos and saved quite a bit of time and typing.
If you'd like to try it out, installation is straightforward:
hg clone https://bitbucket.org/birkenfeld/pygments-main pygments cd pygments sudo python setup.py install
After that, you just run the 'pygmentize' command to see formatted source code.
This sample below comes from the UVM source code 'hello_world.sv' example.
import uvm_pkg::*; `include "uvm_macros.svh" `include "packet.sv" `include "producer.sv" `include "consumer.sv" `include "top.sv" top mytop; initial begin $timeformat(-9,0," ns",5); uvm_default_table_printer.knobs.name_width=20; uvm_default_table_printer.knobs.type_width=50; uvm_default_table_printer.knobs.size_width=10; uvm_default_table_printer.knobs.value_width=14; set_config_int("top.producer1","num_packets",2); set_config_int("top.producer2","num_packets",4); set_config_int("*","recording_detail",UVM_LOW); //uvm_default_printer = uvm_default_tree_printer; uvm_default_printer.knobs.reference=0; mytop = new("top"); uvm_default_table_printer.knobs.type_width=20; run_test(); end endmodule
A further challenge is working out how to embed this sort of code snippet into a TypePad blog post. In general, Pygments generates html code with CSS styles and classes. This doesn't play well with TypePad which seems to consume or remove the CSS styles. You can however get Pygments to generate inline styles, which can then be pasted into TypePad. The syntax for this is:
pygmentize -f html -Ofull,noclasses hello_world.sv
And then the output html can be pasted straight into an HTML editor.
There are comments.