GNU Source-highlight 2.2


Next: , Previous: (dir), Up: (dir)


Next: , Previous: Top, Up: Top

1 Introduction

GNU Source-highlight, given a source file, produces a document with syntax highlighting. The colors and the styles can be specified (bold, italics, underline) by means of a configuration file, and some other options can be specified at the command line.

The program already recognizes many programming languages (e.g., C++, Java, Perl, etc.) and file formats (e.g., log files, ChangeLog, etc.), and some output formats (e.g., HTML, ANSI color escape sequences, LaTeX, etc.). Since version 2.0, it allows you to specify your own input source language via a simple syntax described later in this manual (Language Definitions). Since version 2.1, it allows you to specify your own output format language via a simple syntax described later in this manual (Output Language Definitions). Since version 2.2, it is able to generate cross references (e.g., to variable names, field names, etc.) by relying on the program ctags, http://ctags.sourceforge.net (Generating References).

The complete list of languages (indeed, file extensions) natively supported by this version of Source-highlight (2.2), as reported by --lang-list, is the following:

     Supported languages (file extensions)
     and associated language definition files
     
     C = cpp.lang
     H = cpp.lang
     bison = bison.lang
     c = cpp.lang
     caml = caml.lang
     cc = cpp.lang
     changelog = changelog.lang
     cpp = cpp.lang
     diff = diff.lang
     flex = flex.lang
     fortran = fortran.lang
     h = cpp.lang
     hh = cpp.lang
     hpp = cpp.lang
     htm = html.lang
     html = html.lang
     java = java.lang
     javascript = javascript.lang
     js = javascript.lang
     l = flex.lang
     lang = langdef.lang
     langdef = langdef.lang
     latex = latex.lang
     lex = flex.lang
     lgt = logtalk.lang
     ll = flex.lang
     log = syslog.lang
     logtalk = logtalk.lang
     lua = lua.lang
     ml = caml.lang
     mli = caml.lang
     outlang = outlang.lang
     pas = pascal.lang
     pascal = pascal.lang
     patch = diff.lang
     perl = perl.lang
     php = php3.lang
     php3 = php3.lang
     pl = prolog.lang
     pm = perl.lang
     prolog = prolog.lang
     py = python.lang
     python = python.lang
     rb = ruby.lang
     ruby = ruby.lang
     sig = sml.lang
     sml = sml.lang
     style = style.lang
     syslog = syslog.lang
     tex = latex.lang
     y = bison.lang
     yacc = bison.lang
     yy = bison.lang

The complete list of output formats natively supported by this version of Source-highlight (2.2), as reported by --outlang-list, is the following:

     Supported output languages
     and associated language definition files
     
     esc = esc.outlang
     esc-doc = esc.outlang
     html = html.outlang
     html-css = css_common.outlang
     html-css-doc = cssdoc.outlang
     html-doc = htmldoc.outlang
     javadoc = javadoc.outlang
     latex = latex.outlang
     latex-doc = latexdoc.outlang
     latexcolor = latexcolor.outlang
     latexcolor-doc = latexcolordoc.outlang
     texinfo = texinfo.outlang
     xhtml = xhtml.outlang
     xhtml-css = xhtmlcss.outlang
     xhtml-css-doc = xhtmldoc.outlang
     xhtml-doc = xhtmldoc.outlang

The meaning of the suffixes -doc, -css and -css-doc is explained in Output Language map.

Please, keep in mind, that I haven't tested personally all these language definitions: I actually checked that the definition file is correct (with the command line option --check-lang, Invoking source-highlight), but I'm not sure their definition actually respects that language syntax (e.g., I've put up together some language definitions by searching for information in the Internet, but I've never programmed in that language). So, if you find that a language definition is not precise, please let me know. Moreover, if you have a program example in a language that's not included in the tests directory, please send it to me so that I can include it in the test suite.


Next: , Previous: Introduction, Up: Top

2 Installation

See the file INSTALL for detailed building and installation instructions; anyway if you're used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e. untar the file you downloaded in a directory and then:

     cd <source code main directory>
     ./configure
     make
     make install

Note: unless you specify a different install directory by --prefix option of configure (e.g. ./configure --prefix=<your home>), you must be root to run make install.

Files will be installed in the following directories:

Executables
/prefix/bin
docs and samples
/prefix/share/doc/source-highlight
conf files
/prefix/share/source-highlight

Default value for prefix is /usr/local but you may change it with --prefix option to configure.

NOTICE: Originally, instead of Source-highlight, there were two separate programs, namely GNU java2html and GNU cpp2html. There are two shell scripts with the same name that will be installed together with Source-highlight in order to facilitate the migration (however their use is not advised and it is deprecated).


Next: , Previous: Installation, Up: Installation

2.1 Download

You can download it from GNU's ftp site: ftp://ftp.gnu.org/gnu/src-highlite or from one of its mirrors (see http://www.gnu.org/prep/ftp.html).

I do not distribute Windows binaries anymore; since, they can be easily built by using Cygnus C/C++ compiler, available at http://www.cygwin.com. However, if you don't feel like downloading such compiler, you can request such binaries directly to me, by e-mail (find my e-mail at my home page) and I can send them to you. An MS-Windows port of Source-highlight is available from http://gnuwin32.sourceforge.net.

Archives are digitally signed by me (Lorenzo Bettini) with GNU gpg (http://www.gnupg.org). My GPG public key can be found at my home page (http://www.lorenzobettini.it).

You can also get the patches, if they are available for a particular release (see below for patching from a previous version).


Next: , Previous: Download, Up: Installation

2.2 Anonymous CVS Access

This project's CVS repository can be checked out through anonymous (pserver) CVS with the following instruction set. When prompted for a password for anoncvs, simply press the Enter key.

     export CVS_RSH="ssh"
     
     cvs -z3 -d:ext:anoncvs@savannah.gnu.org:/cvsroot/src-highlite co src-highlite

Further instructions can be found at the address:

http://savannah.gnu.org/projects/src-highlite.

Please notice that this way you will get the latest development sources of Source-highlight, which may also be unstable. This solution is the best if you intend to correct/extend this program: you should send me patches against the latest cvs repository sources.

If, on the contrary, you want to get the sources of a given release, through cvs, say, e.g., version X.Y.Z, you must specify the tag rel_X_Y_Z when you run the cvs command or the cvs update command.

When you compile the sources that you get through the cvs repository, before running the configure and make commands, you should, at least the first time, run the command:

     sh reconf

This will run the autotools commands in the correct order, and also copy possibly missing files. You should have installed recent versions of automake and autoconf in order for this to succeed. You will also need flex and bison.

NOTICE: This convention holds since release 2.1.


Next: , Previous: Anonymous CVS Access, Up: Installation

2.3 What you need to build source-highlight

Since version 2.0 Source-highlight relies on regular expressions as provided by boost (http://www.boost.org), so you need to install at least the regex library from boost. Most GNU/Linux distributions provide this library already in a compiled form.

Source-highlight has been developed under GNU/Linux, using gcc (C++), and bison (yacc) and flex (lex), and ported under Win32 with Cygnus C/C++compiler, available at http://www.cygwin.com. I used the excellent GNU Autoconf and GNU Automake. I also used Autotools (ftp://ftp.ugcs.caltech.edu/pub/elef/autotools) which creates a starting source tree (according to GNU standards) with autoconf, automake starting files. Finally I used GNU gengetopt (http://www.gnu.org/software/gengetopt), for command line parsing.

I started to use also doublecpp (http://www.lorenzobettini.it/software/doublecpp) that permits achieving dynamic overloading.

If you want to use a specific version of the Boost regex library, you can use the configure option --with-boost-regex to specify a particular suffix. For instance,

     ./configure --with-boost-regex=boost_regex-gcc-1_31

Actually, apart from the boost regex library, you don't need the other tools above to build source-highlight because I provide generated sources, unless you want to develop source-highlight.

However, if you obtained sources through CVS, you need some other tools, see Anonymous CVS Access.


Next: , Previous: What you need to build source-highlight, Up: Installation

2.4 Patching from a previous version

If you downloaded a patch, say source-highlight-1.3-1.3.1-patch.gz (i.e., the patch to go from version 1.3 to version 1.3.1), cd to the directory with sources from the previous version (source-highlight-1.3) and type:

     gunzip -cd ../source-highlight-1.3-1.3.1.patch.gz | patch -p1

and restart the compilation process (if you had already run configure a simple make should do).


Next: , Previous: Patching from a previous version, Up: Installation

2.5 Using source-highlight with less

This was suggested by Konstantine Serebriany. The script src-hilite-lesspipe.sh will be installed together with source-highlight. You can use the following environment variables:

     export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
     export LESS=' -R '

This way, when you use less to browse a file, if it is a source file handled by source-highlight, it will be automatically highlighted.


Next: , Previous: Using source-highlight with less, Up: Installation

2.6 Building .rpm

Christian W. Zuckschwerdt added support for building an .rpm and an .rpm.src. You can issue the following command

     rpm -tb source-highlight-2.2.tar.gz

for building an .rpm with binaries and

     rpm -ts source-highlight-2.2.tar.gz

for building an .rpm.src with sources.


Previous: Building .rpm, Up: Installation

2.7 Related Software and Links

Martin Gebert is also implementing a KDE interface to source-highlight programs (and he did a wonderful job!), and it is called ksrc2html; if you want to test it: http://murphy.netsolution-net.de.

CGI support was enabled thanks to Robert Wetzel; I haven't tested it personally yet, so you may ask him directly. Moreover he set up some examples at the page http://www.inf.tu-dresden.de/~rw8/java2.html. If you want to use source-highlight as a CGI program, you have to use the executable source-highlight-cgi. You can build such executable by issuing

     make source-highlight-cgi

in the src directory.

Moreover there's also a Java version of java2html, you can find it at http://www.generationjava.com/projects/Java2Html.shtml.


Next: , Previous: Installation, Up: Top

3 Copying Conditions

GNU Source-highlight is free software; you are free to use, share and modify it under the terms of the GNU General Public License that accompanies this software (see COPYING).

GNU source-highlight was written and maintained by Lorenzo Bettini http://www.lorenzobettini.it.


Next: , Previous: Copying, Up: Top

4 Simple Usage

Here are some realistic examples of running source-highlight1.

Source-highlight only does a lexical analysis of the source code, so the program source is assumed to be correct!

Here's how to run source-highlight (for this example we will use C/C++ input files, but this is valid also for other source-highlight input languages):

     source-highlight --src-lang cpp --out-format html \
         --input <C++ file> \
         --output <html file> \
         --style-file <style file> \
         options

For input files, apart from the -i (--input) option and the standard input redirection, you can simply specify some files at the command line and also use regular expressions (for instance *.java). In this case the name for the output files will be formed using the name of the source file with a .<ext> appended, where <ext> is the extension chosen according to the output format specified (in this example it would be .html). The style file (Output format style) contains information on how to format specific language parts (e.g., keywords in blue and boldface, etc.).

If STDOUT string is passed as -o (--output) option, then the output is forced to the standard output anyway.

If -s (--src-lang) is not specified, the source language is inferred by the extension of the input file (this, of course, does not work with standard input redirection).

If -f (--out-format) is not specified, the output will be produced in HTML.

If --style-file is not specified, the default.style, which is included in the distribution, will be used (see Output format style for further information).


Next: , Previous: Simple Usage, Up: Simple Usage

4.1 LaTeX output

When using LaTeX output format you can choose between monochromatic output (by using -f latex) or colored output (by using -f latexcolor). When using colored output, you need the color package (again this should be present in your system). Of course, you are free to define your own LaTeX output format, see Output Language Definitions.


Next: , Previous: LaTeX output, Up: Simple Usage

4.2 Texinfo output

When using the Texinfo output format, you may want to use a dedicated style file, texinfo.style, which comes with the source-highlight distribution, with the option --style-file. For instance, the example in Examples is formatted with this style file.


Previous: Texinfo output, Up: Simple Usage

4.3 ANSI color escape sequences

If you're using this output format, for instance together with less (see Using source-highlight with less), you may want to use the esc.style, which comes with the source-highlight distribution, with the option --style-file. This should result in a more pleasant coloring output.


Next: , Previous: Simple Usage, Up: Top

5 Configuration files

During execution, source-highlight needs some files where it finds directives on how to recognize the source language (if not specified explicitly with --src-lang or --lang-def), on which output format to use (if not specified explicitly with --out-format or --outlang-def), on how to format specific source elements (e.g., keywords, comments, etc.), and source and output language definitions. These files will be explained in the next sections.

If the directory for such files is not explicitly specified with the command line option --data-dir, these files are searched for in the following order:

If you want to be sure about which file is used during the execution, you can use the command line option --verbose.


Next: , Previous: Configuration files, Up: Configuration files

5.1 Output format style

You must specify your options for syntax highlighting in the file default.style2. Here's the one that comes with this distribution (this is formatted by using the style.lang that is shown in Tutorials on Language Definitions):

     keyword blue b ; // for language keywords
     type darkgreen ; // for basic types
     string red f ; // for strings and chars
     specialchar pink f ; // for special chars, e.g., \n, \t, \\
     comment brown i, noref; // for comments
     number purple ;       // for literal numbers
     preproc darkblue b ; // for preproc directives (e.g. #include, import)
     symbol darkred ; // for simbols (e.g. <, >, +)
     function black b; // for function calls and declarations
     cbracket red; // for block brackets (e.g. {, })
     todo b;       // for TODO and FIXME
     
     // line numbers
     linenum black f;
     
     // Internet related
     url blue u, f;
     
     // other elements for ChangeLog and Log files
     date blue b ;
     time darkblue b ;
     ip darkgreen ;
     file darkblue b ;
     name darkgreen ;
     
     // for Prolog, Perl...
     variable darkgreen ;
     
     // explicit for Latex
     italics darkgreen i;
     bold darkgreen b;
     underline darkgreen u;
     fixed green f;
     argument darkgreen;
     optionalargument purple;
     math orange;
     
     // for diffs
     oldfile orange;
     newfile darkgreen;
     difflines blue;

You can specify your own file (it doesn't have to be named default.style) with the command line option --style-file3, see Invoking source-highlight.

You can also specify the color of normal text by adding this line

     normal darkblue ;

As you might see the syntax of this file is quite straightforward:

     b = bold
     i = italics
     u = underline
     f = fixed
     nf = not fixed
     noref = no reference information is generated for these elements

Since version 2.2, the color specification is not required. For instance, the texinfo.style is as follows (we avoid colors for Texinfo outputs):

     keyword b ;
     type b ;
     variable f, i ;
     string f ;
     comment nf, i, noref ;
     preproc b ;
     
     // line numbers
     linenum f;
     
     // Internet related
     url f;
     
     // for diffs
     oldfile i;
     newfile i;
     difflines b;

You may also specify more than on of these options separated by commas, e.g.

     keyword blue u, b ;

Please keep in mind that in this case the order of these specified options is kept during the generation of the output; for instance, depending on the specific output format, the sequences u, b and b, u may lead to different results. In particular, the style that comes first is used after the ones that follow. For instance, in the case of HTML, the sequence u, b will lead to the following formatting: <u><b>...</b></u>.

The noref option specifies that for this element reference information are not generated (see Generating References). For instance, this is used for the comment element, since we do not want that elements in a comment are searched for cross-references.

These are all possible color logical names handled by source-highlight4:

     black
     red
     darkred
     brown
     yellow
     cyan
     blue
     pink
     purple
     orange
     brightorange
     green
     brightgreen
     darkgreen
     teal
     gray
     darkblue

You can also use the direct color scheme for the specific output format, by using double quotes, such as, e.g., "#00FF00" in HTML5. Of course, the double quotes will be discarded during the generation.


Next: , Previous: Output format style, Up: Configuration files

5.2 Language map

This configuration file associates a file extension to a specific language definition file. You can also use such file extension to specify the --src-lang option (see Simple Usage). Source-highlight comes with such a file, called lang.map.

Of course, you can override the settings of this file by writing your own language map file and specify such file with the command line option --lang-map). Moreover, as explained above, if a file lang.map is present in the current directory, such version will be used. The format of such file is quite simple:

     extension = language definition file

The default language definition file is shown in Introduction.


Next: , Previous: Language map, Up: Configuration files

5.3 Language definition files

These files are crucial for source-highlight since they specify the source elements that have to be highlighted. These files also allow to specify your own language definitions in order to deal with a language that is not handled by source-highlight6. The syntax for these files is explained in Language Definitions.


Next: , Previous: Language definition files, Up: Configuration files

5.4 Output Language map

This configuration file associates an output format to a specific output language definition file. You can use the name of that output format to specify the --out-format option (see Simple Usage). Source-highlight comes with such a file, called outlang.map.

Of course, you can override the settings of this file by writing your own output language map file and specify such file with the command line option --outlang-map). Moreover, as explained above, if a file outlang.map is present in the current directory, such version will be used. The format of such file is quite simple:

     output format name = language definition file

The default language definition file is shown in Introduction.

In particular, there is a convention for the output format name in the output language map, according to the suffix of the name with a dash -:

-doc
The one used when --doc command line option is given
-css-doc
The one used when --css command line option is given
-css
The one used when --css and --no-doc command line options are given

If a combination of the above mentioned command line options is given for a specific output format, and a corresponding definition file is not specified in the map file, then an error is raised.

For instance, if you specified the definition file for your language mylang and also one for dealing with --doc option, i.e., a definition file for mylang-doc, and you run source-highlight as follows:

source-highlight -f mylang --css mycss.css

You will get the following error:

source-highlight: output language mylang-css-doc not handled


Next: , Previous: Output Language map, Up: Configuration files

5.5 Output Language definition files

These files are crucial for source-highlight since they specify how the source elements are highlighted. These files also allow to specify your own output format definitions in order to deal with an output format that is not handled by source-highlight7. The syntax for these files is explained in Output Language Definitions.


Previous: Output Language definition files, Up: Configuration files

5.6 Developing your own definition files

I encourage those who write new language definitions or correct/modify existing language definitions to send them to me so that they can be added to the source-highlight distribution!

Since these files require more explanations (that, however, are not necessary to the standard usage of source-highlight), they are carefully explained in separate parts: Language Definitions and Output Language Definitions.


Next: , Previous: Configuration files, Up: Top

6 Invoking source-highlight

The format for running the source-highlight program is:

     source-highlight option ...

source-highlight supports the following options, shown by the output of source-highlight --help:

     source-highlight
     
     Highlight the syntax of a source file (e.g. Java) into a specific format (e.g.
     HTML)
     
     Usage: source-highlight [OPTIONS]...
     
       -h, --help                    Print help and exit
       -V, --version                 Print version and exit
       -i, --input=filename          input file. default std input
       -o, --output=filename         output file. default std output. If STDOUT is
                                       specified, the output is directed to standard
                                       output
       -s, --src-lang=STRING         source language (use --lang-list to get the
                                       complete list).  If not specified, the source
                                       language will be guessed from the file
                                       extension.
           --lang-list               list all the supported language and associated
                                       language definition file
           --outlang-list            list all the supported output language and
                                       associated language definition file
       -f, --out-format=STRING       output format (use --outlang-list to get the
                                       complete list)  (default=`html')
       -v, --verbose                 verbose mode on
       -d, --doc                     create an output file that can be used as a
                                       stand alone document (e.g., not to be
                                       included in another one)
           --no-doc                  cancel the --doc option even if it is implied
                                       (e.g., when css is given)
       -c, --css=filename            the external style sheet filename.  Implies
                                       --doc
       -T, --title=STRING            give a title to the output document.  Implies
                                       --doc
       -t, --tab=INT                 specify tab length.  (default=`8')
       -H, --header=filename         file to insert as header
       -F, --footer=filename         file to insert as footer
           --style-file=filename     specify the file containing format options
                                       (default=`default.style')
           --outlang-def=filename    output language definition file
           --outlang-map=filename    output language map file
                                       (default=`outlang.map')
           --data-dir=path           directory where language definition files and
                                       language maps are searched for.  If not
                                       specified these files are searched for in the
                                       current directory and in the data dir
                                       installation directory
           --output-dir=path         output directory
           --lang-def=filename       language definition file
           --lang-map=filename       language map file  (default=`lang.map')
     
     reference generation:
       -n, --line-number             number all output lines
           --line-number-ref[=prefix]
                                     number all output lines and generate an anchor,
                                       made of the specified prefix + the line
                                       number  (default=`line')
           --gen-references=STRING   generate references  (possible
                                       values=\"inline\", \"postline\", \"postdoc\"
                                       default=`inline')
           --ctags-file=filename     specify the file generated by ctags that will
                                       be used to generate references
                                       (default=`tags')
           --ctags=cmd               how to run the ctags command.  If this option
                                       is not specified, ctags will be executed with
                                       the default value.  If it is specified with
                                       an empty string, ctags will not be executed
                                       at all  (default=`ctags --excmd=n
                                       --tag-relative=yes')
     
     testing:
           --gen-version             put source-highlight version in the generated
                                       file  (default=on)
           --check-lang=filename     only check the correctness of a language
                                       definition file
           --check-outlang=filename  only check the correctness of an output
                                       language definition file
           --failsafe                if no language definition is found for the
                                       input, it is simply copied to the output
           --debug-langdef           debug a language definition
           --show-regex=filename     show the regular expression automaton
                                       corresponding to a language definition file

Let us explain some options in details (apart from those that should be clear from the --help output itself, and those already explained in Simple Usage).

--doc
-d
If you want a stand alone output document (i.e., an output file that is not thought to be included in another document), specify this option (otherwise you just get some text that you can paste into another document). If you choose this option and do not provide a --title, the your source file name will be used as the title.
--no-doc
The --doc option above is actually implied by other command line options (e.g., --css). If you do not want this (e.g., you want to include the output in an existing document containing the global style sheet), you can disable this by using --no-doc.
--css
-c
Specify the style sheet file (e.g., a .css for HTML8) for the output document.
--tab
-t
With this options, tab characters will be converted into specified number of space characters (tabulation points will be preserved). This option is automatically selected when generating line numbers.
--output-dir
You can pass to source-highlight more than one input file (see Simple Usage). In this case you cannot specify the output file name. In such cases the output files will be automatically generated into the directory where you invoked the command from; if you want the output files to be generated into a different directory you can use this option.
--line-number-ref
As --line-number, this option numbers all the output lines, and, additionally, generates an anchor for each line. The anchor consists of the specified prefix (default is line) and the line number (e.g., line25). For instance, as prefix, if you deal with many files, you can use the file name. Notice that some output languages might not support this feature (e.g., esc, since it makes no sense in such case). See Anchors and References for defining how to generate an anchor in a specific output language.
--failsafe
If no language specification is found, an error will be printed and the program exits. With this option, instead, in such situations, the input is simply copied as it is to the output. This is useful when source-highlight is used with many input files, and it is also used in the src-hilite-lesspipe.sh script, Using source-highlight with less.
--debug-lang
--show-regex
Allows to debug a language definition file, Debugging.

The other command line options dealing with references are explained in more details in Generating References.


Next: , Previous: Invoking source-highlight, Up: Top

7 Language Definitions

Since version 2.0 source-highlight uses a specific syntax to specify source language elements (e.g., keywords, strings, comments, etc.). Before version 2.0, language elements were scanned through Flex. This had the drawback of writing a new flex file to deal with a new language; even worse, a new language could not be added “dynamically”: you had to recompile the whole source-highlight program.

Instead, now, language elements are specified in a file, loaded dynamically, through a (hopefully) simple syntax. Then, these definitions are used internally to create, on-the-fly, regular expressions that are used to highlight the elements. In particular, we use the regular expressions provided by the Boost library (see Installation). Thus, when writing a language definition file you will surely have to deal with regular expressions. Of course, we use the Boost regex library regular expression syntax. We refer to Boost documentation for such syntax, http://www.boost.org/libs/regex/doc/syntax.html.

Here, we see such syntax in details, by relying on many examples. This allows a user to easily modify an existing language definition and create a new one. These files have, typically, extension .lang.

Each definition basically associates a regular expression to a language element and defines a name for the language element. Such name will be used to associate a particular style (e.g., bold face, color, etc.) to the highlighting of such elements. You cannot use names that are the same of keywords used in the language definition syntax (e.g., start, as shown later, is a reserved word).

Comments can be given by using #; the rest of the line is considered as a comment.

Source-highlight will scan each line of the input file separately. So a regular expression that tries to match new line characters is destined to fail. However, the language definition syntax provides means to deal with multiple lines (see Delimited definitions and State/Environment Definitions).


Next: , Previous: Language Definitions, Up: Language Definitions

7.1 Simple definitions

The simpler way of specify language elements is to list the possible alternatives. This is the case, for instance, for keywords. For instance, in java.lang you have:

     keyword = "abstract|assert|break|case|catch|class|const",
               "continue|default|do|else|extends|false|final",
               "finally|for|goto|if|implements|instanceof|interface"
     keyword = "native|new|null|private|protected|public|return",
               "static|strictfp|super|switch|synchronized|throw",
               "throws|true|this|transient|try|volatile|while"

The elements must be specified in double quotes. You can separate quoted definitions with commas. Alternatively, within a quoted definition, alternatives can be separated with the pipe symbol |. The above definition defines the language element keyword. Each time an element is found in the source file, it is highlighted with the style for the element with the same name in the output format style file (notice that all elements shown in the example are take from the language definition files that come with source-highlight and there is a style for each of such elements, see Configuration files). If such an element is not specified in the output format style file, it is simply not highlighted (so pay attention to typos :-).

From the above example you may have noticed that language element definitions are cumulative, so the second keyword definition does not replace the first one. (Indeed, in some case you may want to actually redefine a language element; this is possible as explained in the following sections.)

Notice that words specified in double quotes have to match exactly in a source file, and they must be isolated (not surrounded by anything but spaces). Thus for instance class is matched as a keyword, but in my_class the substring class is not matched as keyword. From the point of view of regular expressions a string such as class in a double quote simple definition is intended as \<(class)\>.

Special characters have to be escaped with the character \. So for instance if you want to specify the character |, which is normally used to separate alternatives in double quoted strings, you have to specify \|.

Definitions in double quotes are interpreted literally (thus, e.g., a dot . is interpreted as the character . not as the regular expression wild card). If you want to enjoy the full power of regular expressions to specify a language alternative, you have to use single quoted strings ('), instead of double quoted strings.

For instance, the following is the definition for a preprocessor directive in C/C++:

     preproc = '^[[:blank:]]*#([[:blank:]]*[[:word:]]*)'

Notice that the definition 'class' is different from "class", as explained above. Thus, for instance 'class' matches also the sub-expression class inside my_class.

Finally, at the end of a list of definitions, one can specify the keyword nonsensitive; in that case, the specified strings will be interpreted in a non case sensitive way. For instance, we use this feature in Pascal language definition, pascal.lang where keywords are parsed in a non sensitive way:

     keyword = "alfa|and|array|begin|case|const|div",
           "do|downto|else|end|false|file|for|function|get|goto|if|in",
           "label|mod|new|not|of|or|pack|packed|page|program",
           "put|procedure|read|readln|record|repeat|reset|rewrite|set",
           "text|then|to|true|type|unpack|until|var|while|with|writeln|write"
       nonsensitive


Next: , Previous: Simple definitions, Up: Language Definitions

7.2 Line wide definitions

It is often useful to define a language element that affects all the remaining characters up to the end of the line. For such definitions, instead of the = you must use the keyword start. For instance, the following is the definition of a single line comment in C++:

     comment start "//"

This says that when the two characters // are encountered in the source file, everything from these characters, include, up to the end of the line, will be highlighted according to the style comment.


Next: , Previous: Line wide definitions, Up: Language Definitions

7.3 Order of definitions

It is important to observe that the order of language definitions is important since it will be used during regular expression matching. You then have to make sure that, if there are definitions that start with same characters, the longest expression is specified first in the file. For instance if you write

     symbol = "/"
     comment start "//"

The first expression will always be matched first, and the second expression will never be matched. The right order is

     comment start "//"
     symbol = "/"


Next: , Previous: Order of definitions, Up: Language Definitions

7.4 Delimited definitions

Many elements are delimited by specific character sequences. For instance, strings and multiline comments. The syntax for such an element definition is

     <name> delim <left delimited> <right delimiter> \
             {escape <escape character>} \
             {multiline} {nested}

The escape specification allows to specify the escape character that may precede one of the delimiters inside the element. This is optional.

For instance, this is the definition of C-like strings:

     string delim "\"" "\"" escape "\\"

Notice that \ is a special characters in definitions so it has to be escaped. If the escape specification was omitted, the C string "write \"hello\" string" would have been highlight incorrectly (it would have been highlighted as the string "write \", the normal character sequence hello\ and the string " string").

The option multiline specifies that the element can spawn multiple lines. For instance, PHP strings are defined as follows:

     string delim "\"" "\"" escape "\\" multiline

The option nested instructs to count possible multiple occurrences of delimited characters and to match relative multiple occurrences. For instance, C-like multiline comments are specified as follows:

     comment delim "/*" "*/" multiline nested

If nested was not used the following nested comment would have not been highlighted correctly:

     /*
        This is a /* nested comment */
     */

As said above, definitions are cumulative, and they are also cumulative even when using different syntactic forms. Thus, for instance, the complete definition for C++-style comments are the following:

     comment start "//"
     comment delim "/*" "*/" multiline nested


Next: , Previous: Delimited definitions, Up: Language Definitions

7.5 Variable definitions

It is possible to define variables to be re-used in many parts in a language definition file. A variable is defined by using

vardef <name of the variable> = <list of definitions>

Once defined, a variable can be used by prepending the symbol $ to its name. For instance,

     vardef FUNCTION = '(?:[[:alpha:]]|_)[[:word:]]*[[:blank:]]*(?=\()'
     function = $FUNCTION

The capital letters are used only for readability.

It is also possible to concatenate variables and expressions, and reuse variables inside further variable definitions:

     vardef basic_time = '[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}'
     vardef time = '\<' + $basic_time + '\>'


Next: , Previous: Variable definitions, Up: Language Definitions

7.6 File inclusion

It is possible to include other language definition files into another file. This is inclusion actually physically includes the contents of the included file into the current file during parsing, at the exact point of inclusion (just like the #include in C/C++). This is useful for re-using definitions in many files. For instance, C++ comment definitions are given in a file c_comment.lang, and this file is included in the Java and C++ definition files. The same happens for number and functions. For instance, the file java.lang contains the following include instructions:

     include "c_comment.lang"
     
     include "number.lang"
     
     keywords ...
     
     include "function.lang"

Notice that the order of inclusion is crucial since the order of definition is crucial. If function definition was included before keyword definitions, then the sentence if (exp) would be highlighted as a function invocation.


Next: , Previous: File inclusion, Up: Language Definitions

7.7 State/Environment Definitions

Sometimes you want some source element to be highlighted only if they are surrounded by other elements. Source-highlight language definitions provides also this feature.

     state|environment <standard definition> begin
       <other definitions>
     end

This structure is recursive (so other state/environment definitions can be given within a state/environment). The meaning of a state/environment is that the definitions within the begin ... end are matched only if the definitions that define the state/environment have been matched. When entering a state/environment, however, the definitions given outside the state/environment are not matched. The difference between state and environment is that in the latter, normal parts of the source language (i.e., those that do not match any definition) are highlighted according to the style of the definition that defines the environment.

As an example, the following defines the multiline nested C comment, and highlights URL and e-mail addresses only when they appear inside a comment (notice that this uses file inclusion):

     environment comment delim "/*" "*/" multiline nested begin
           include "url.lang"
     end

Notice that we used environment because everything else inside a comment has to be formatted according to the comment style.

While for programming language definitions states/environments can be avoided (although they allow to highlight some parts only if inside a specific environment, e.g., URLs inside comments, or documentation tags in Javadoc comments), they are pretty important for highlighting files such as logs and ChangeLog files, since elements have to be highlighted when they appear in a specific position. For instance, for ChangeLog (see changelog.lang), we use a state for highlighting the date, name, e-mail:

     state date start '[[:digit:]]{2,4}-?[[:digit:]]{2}-?[[:digit:]]{2}' begin
       string = '<(?:[[:word:]]*|\.)+@(?:[[:word:]]*|\.)+>'
       url = '(?:[[:word:]]|[[:punct:]])+'
     end

Notice that definitions that appear inside a state/environment have the same scope of the expressions that define the environment. While this makes sense for start and delim definitions, it may makes less sense for simple definitions (i.e., those that simply lists all possible expressions): in fact, in this case, such expressions do not define a scope. For such definitions, the semantics of state/environment is that the state/environment starts after matching one of the alternatives. And where will it end? In this case you must explicitly exit the environment. For instance, you can say that, when inside a state/environment, a specific language definition, when encountered also exits the environment (with the keyword exit). You can even exit all the environments with exitall. For instance, the following definition, highlights a non empty string following a web method:

     vardef non_empty = '[^[:blank:]]+'
     
     state webmethod = "OPTIONS|GET|HEAD|POST|PUT|DELETE",
               "TRACE|CONNECT|PROPFIND|MKCOL|COPY|MOVE|LOCK|UNLOCK" begin
       string = $non_empty exit
     end

If you ever need such advanced features, you may want to take a look at the log.lang definition file that defines highlighting for several log files (access logs, Apache logs, etc.).


Next: , Previous: State/Environment Definitions, Up: Language Definitions

7.8 Redefinitions and Substitutions

These two features are useful when you want to define a language by re-using an existing language definition with some changes. Typically you include another language definition file and you redefine/substitute some elements.

When you use redef you erase all the previous definitions of that language elements with the new one. The new language element definition will be placed exactly in the point of the new definition. We use this feature, for instance, when we define the sml language by re-using the caml one: they differ only for the keywords9. In fact, the contents of sml.lang is summarized as follows:

     include "caml.lang"
     
     redef keyword = "abstraction|abstype|and|andalso..."
     
     redef type = "int|byte|boolean|char|long|float|double|short|void"

Since the new language element definition appears in the exact point of the redefinition, this means that such a regular expression will be matched only if all the previous ones (the ones of the included file) cannot be matched. This may lead to unwanted results in some cases (not in the sml case though). In other words the following code

     keyword = "foo"
     keyword = "bar"
     type = "int"
     redef keyword = "myfoo"

is equivalent to the following one

     type = "int"
     keyword = "myfoo"

If this is not what you want, you can use subst, which is similar to redef apart from that it replaces the previous first definition of that language element in the exact point of that first definition (all other possible definitions are simply erased). That is to say that the following code

     keyword = "foo"
     keyword = "bar"
     type = "int"
     subst keyword = "myfoo"

is equivalent to the following one

     keyword = "myfoo"
     type = "int"

It is up to you to decide which one fits best your needs. We use this feature to define javascript in terms of java:

     include "java.lang"
     
     subst keyword = "abstract|break|case|catch|class|if..."

Here using redef would have led to the unwanted behavior that if (exp) would have been highlighted as a function call, since the function element definition would have come first (and then matched first) than the redefinition of if as a keyword.


Next: , Previous: Redefinitions and Substitutions, Up: Language Definitions

7.9 Notes on regular expressions

Although we refer to Boost documentation for such syntax10, we want to provide here some explanations of some forms of regular expressions that might be unknown but that are pretty useful in language definitions.

Typically, when you need to group sub-expressions with parenthesis, but you don't want the parenthesis to spit out another marked sub-expression, you can use a non-marking parenthesis (?:expression). This is not necessary in the language definition syntax: even though you use standard parenthesis, source-highlight will transform it into a non-marking parenthesis.

A useful regular expression form is the Forward Lookahead Asserts that come in two forms, one for positive forward lookahead asserts, and one for negative lookahead asserts:

(?=abc)
matches zero characters only if they are followed by the expression “abc”.
(?!abc)
matches zero characters only if they are not followed by the expression “abc”.

For instance, in the definition of a function we use the following regular expression:

     ([[:alpha:]]|_)[[:word:]]*[[:blank:]]*(?=\()

Thus after the name of a function we test, with the regular expression (?=\() whether an open parenthesis ( can be matched. If it can be matched, however, we leave that part in the input (so that the parenthesis will not be formatted the same way of a function name).

Please, be careful when using such regular expression forms: since part of the input is not actually removed you may end up always scanning the same input part (thus looping) if you do not write the regular expressions well. For instance, consider this language definition

     state foo = '(?=foo)' begin
       foo = '(?=foo)'
     end

and the following input file:

     hello
     foo
     bar

As soon as we match the word foo we leave it in the input and we enter a state where we try to match the word foo still leaving it in the input. As you might have guess this will make source-highlight loop forever. Probably one might have wanted to write this language definition:

     state foo = '(?=foo)' begin
       foo = 'foo'
     end

but a cut-and-paste error had its way ;-)


Next: , Previous: Notes on regular expressions, Up: Language Definitions

7.10 Concluding Remarks

By mixing all these features you can unleash your imagination and define highlighting for complex source languages such as Flex and Bison by writing few lines of code and re-use existing ones. For instance, Flex and Bison have their own syntax and lets you write C/C++ code in specific parts of the source language, e.g., the code between the outmost brackets, in the following example, is C++ code, and should be highlighted following C++ language definitions (apart from variables that are prefixed with $):

     globaltags : options { if (...) { setTags( $1 ); } }

This is easy to do (taken from flex.lang):

     state cbracket delim "{" "}" multiline nested begin
       variable = '\$.'
       include "cpp.lang"
     end

Notice that, since we used nested we can be sure that the C++ language definitions are not considered anymore when we matched the last closing }.


Next: , Previous: Concluding Remarks, Up: Language Definitions

7.11 Debugging

When writing a language definition file, it is quite useful to be able to debug it (by using complex regular expressions one may experience unwanted behaviors). Since version 2.1 the command line option --debug-lang is available. When using this option, some additional information are printed to the standard output.

When using this command line option the additional information produced has the following format:

     <.lang filename>:<line number>: <matched subexpression>
     formatting: <source file string to be formatted>
     entering: <next state's regular expression>
     exiting:
     exitingall:

The lines starting with entering, exiting and exitingall are related to entering a new state/environment and exiting one and all states/environments. The first line shows a link to the .lang definition file and the line number, i.e., and the sub-expression that matched and the line starting with formatting shows the source file string that matched with that expression. If a line starting with formatting is not preceded by a line with the link to the sub-expression, it means that no particular regular expression has matched, and thus the style normal will be used to format that string.

Consider the following (simplified) Java source file:

     01: /*
     02:   This is to demonstrate --debug-lang
     03:   http://www.lorenzobettini.it
     04: */
     05:
     06: package hello;
     07:
     08: public class Hello {
     09:         // just some greetings ;-)  /*
     10:     int i = 10;
     11:     System.out.println("Hello World!");
     12: }
     

Now you can debug the java.lang file by using the --debug-lang command line option. And the output is as follows:

     c_comment.lang:15: (/\*)
     formatting: "/*" as comment
     entering: (\*/)|(/\*)|...
     formatting: "" as comment
     formatting: "  This is to demonstrate --debug-lang" as comment
     formatting: "  " as comment
     url.lang:2: ((?:(?:[[:word:]]+://(?:[[:word:]]+[\./\-_]?)+)))
     formatting: "http://www.lorenzobettini.it" as url
     formatting: "" as comment
     c_comment.lang:15: (\*/)
     formatting: "*/" as comment
     exiting 1 level(s): (\<(?:import|package)\>)|(//)|...
     formatting: "" as normal
     formatting: "" as normal
     java.lang:1: (\<(?:import|package)\>)
     formatting: "package" as preproc
     formatting: " hello" as normal
     symbols.lang:1: ((?:~|!|%|\^|\*|\(|\)...
     formatting: ";" as symbol
     ... omissis ...
     c_comment.lang:2: (//)
     formatting: "//" as comment
     entering: (\z)
     formatting: " just some greetings ;-)  /*" as comment
     c_comment.lang:2: (\z)
     formatting: "" as comment
     exiting 1 level(s): (\<(?:import|package)\>)|(//)|...

This should provide enough information to understand how the regular expressions are used and how the states/environments are entered and exited. Please notice that the sub-expressions that are shown may differ from the original ones specified in the .lang file. This is due to the preprocessing that is performed by Source-highlight. Moreover, some sub-expressions are not defined at all in the .lang file: for instance, this is the case for line wide definitions, i.e., those that are defined with the keyword start, Line wide definitions. The last lines above, showing entering: (\z), mean that we wait to reach the end of a line.

Another useful feature in debugging is the option --show-regex that shows the regular expression automaton that source-highlight creates.

For instance, consider this language definition (comment-show.lang):

     vardef TODO = '(TODO|FIXME)([:]?)'
     
     environment comment delim "/**" "*/" multiline nested begin
       type = '@[[:alpha:]]+'
       todo = $TODO
     end
     
     string delim "<" ">"
     
     string2 delim "<<" ">>" multiline

If you now execute the following command:

     source-highlight --show-regex=comment-show.lang

you will get, on the standard error, the following output:

      STATE 1
       0: normal  (exit level: 0, exit_all: 0, next: none)
       1: comment (/\*\*) (exit level: 0, exit_all: 0, next: 2)
          STATE 2
           0: comment  (exit level: 0, exit_all: 0, next: none)
           1: comment (\*/) (exit level: 1, exit_all: 0, next: none)
           2: comment (/\*\*) (exit level: 0, exit_all: 0, next: 2)
           3: type ((?:@[[:alpha:]]+)) (exit level: 0, exit_all: 0, next: none)
           4: todo ((?:(?:TODO|FIXME)(?:[:]?))) (exit level: 0, exit_all: 0, next: none)
       2: string (<(?:[^<>])*>) (exit level: 0, exit_all: 0, next: none)
       3: string2 (<<) (exit level: 0, exit_all: 0, next: 3)
          STATE 3
           0: string2  (exit level: 0, exit_all: 0, next: none)
           1: string2 (>>) (exit level: 1, exit_all: 0, next: none)

This shows the states of the regular expression automaton that source-highlight creates and will use to format an input source.

Each state is associated a unique number in order to identify them. Then for each state it shows the regular expressions associated to each element. The first element (the one numbered with 0) of each state is always the default style for that state, i.e., the style applied if no regular expression is matched (in fact it does not have an associated regular expression). For instance, in the initial state the default style is normal. Then, we can see that if we match a /** (it is shown as a string with escaped special characters, /\*\*) we enter a new state, in this case the state 2 (next: 2). This corresponds to the delimited element defining a new environment. The fact that it is actually and environment and not a state11 can be seen by the fact that the default style is the same of the environment itself. If we match a */, i.e., the end of the delimited element, we exit one level (exit level: 1) meaning that we go back to state 1. Since the delimited element is defined as nested, we can notice that in the state 2 we have that if we match /** we simply enter a new instance of state 2 itself.

The string and string2 show the difference implied by the multiline option: since source-highlight handles a line of input separately, the first delimited definition can be handled with a single regular expression while the multiline version cannot.


Previous: Debugging, Up: Language Definitions

7.12 Tutorials on Language Definitions

Now we provide some examples of language definitions. In the previous sections we have already provided some code snippets, while here we provide complete examples of language definitions that are included in the source-highlight distribution itself.

In particular we will first show the language definition for the language definition syntax itself (file langdef.lang). This will be used to highlight the examples of language definitions that we will show in this section (the highlighting will not be visible if you are viewing this manual with the info command). Of course, this example is highlighted itself.

     # this is the language definition for the
     # language definition syntax itself
     comment start "#"
     
     preproc = "include"
     
     string delim "\"" "\"" escape "\\" multiline
     string delim "'" "'" escape "\\" multiline
     
     keyword = "state|environment|begin|end|delim|escape|start",
               "multiline|nested|vardef|exitall|exit",
               "redef|subst|nonsensitive"
     
     symbol = "=|+|,"
     
     vardef ID = '[[:word:]]+'
     
     variable = '\$' + $ID
     
     variable = $ID
     

The style that is used to highlight these examples in Texinfo is texinfo.style that is shown in Output format style. The language definition for the style syntax (file style.lang) is even simpler:

     # this is the language definition for the
     # style definition syntax
     comment start "//"
     
     string delim "\"" "\"" escape "\\"
     
     keyword = "purple|orange|brightorange|brightgreen|darkgreen",
               "green|darkred|red|brown|pink|yellow|cyan",
               "black|teal|gray|darkblue|blue",
               "normal|linenum",
               "noref|nf|f|u|i|b"
     
     symbol = ",|;"
     
     variable = '[[:word:]]+'
     

Notice that this definition is pretty simple since the language definition syntax is simple. In the next examples we will see how to use more complex features to highlight more complex language syntaxes.


Next: , Previous: Tutorials on Language Definitions, Up: Tutorials on Language Definitions

7.12.1 Highlighting C/C++

This is the language definition for C/C++, included in the file cpp.lang:

     # definitions for C/C++
     include "c_comment.lang"
     
     state preproc start '^[[:blank:]]*#(?:[[:blank:]]*include)' begin
             string delim "<" ">"
             string delim "\"" "\"" escape "\\"
             include "c_comment.lang"
     end
     
     preproc = '^[[:blank:]]*#([[:blank:]]*[[:word:]]*)'
     
     include "number.lang"
     
     include "c_string.lang"
     
     keyword = "__asm|__cdecl|__declspec|__export|__far16",
       "__fastcall|__fortran|__import",
       "__pascal|__rtti|__stdcall|_asm|_cdecl",
       "__except|_export|_far16|_fastcall",
       "__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto",
       "break|case|catch|cdecl|class|const|const_cast|continue|default|delete",
       "do|dynamic_cast|else|enum|explicit|extern|false|for|friend|goto",
       "if|inline|mutable|namespace|new|operator|pascal|private|protected",
       "public|register|reinterpret_cast|return|sizeof|static|static_cast",
       "struct|switch|template|this|throw|true",
       "try|typedef|typeid|typename|union",
       "using|virtual|volatile|while"
     
     type = "bool|char|double|float|int|long",
       "short|signed|unsigned|void|wchar_t"
     
     include "symbols.lang"
     
     cbracket = "{|}"
     
     include "function.lang"
     

Notice that this makes use of lots of includes since these parts are reused in other language definitions (e.g., Java has lots of parts that are in common with C/C++ so we wrote these parts in separate files). In particular the comments definitions:

     # c_comment.lang
     comment start "//"
     
     vardef TODO = '(TODO|FIXME)([:]?)'
     
     # comments with documentation tags
     environment comment delim "/**" "*/" multiline nested begin
       include "url.lang"
       include "html.lang"
       type = '@[[:alpha:]]+'
       todo = $TODO
     end
     
     # standard comments
     environment comment delim "/*" "*/" multiline nested begin
       include "url.lang"
       todo = $TODO
     end

Here we have the definitions for line-wide comments (//) and for multi line comments where we highlight also URL addresses and e-mail addresses (defined in the file url.lang not shown here). Moreover, for comments that are used in automatic documentation generation tools (such as Doxygen or Javadoc), i.e., those that start with /**) we also highlight the complete HTML syntax (defined in the file html.lang not shown here).

Going back to cpp.lang we see that for preprocessor directives #include we use a state definition since in this case the file included with the <file> syntax must be formatted as strings (and only in this context the <> must be considered as strings, anywhere else they are operators). Since a state erases definitions defined outside the state we must include c_comment.lang again in order to highlight comments also in this context12. Then we have a definition of preproc that catches all the other preprocessor directives.

The included file number.lang defines the regular expression that catches number constants (not shown here), then we include the file c_string.lang that define strings (again shared by Java):

     vardef SPECIALCHAR = '\\.'
     
     environment string delim "\"" "\"" begin
       specialchar = $SPECIALCHAR
     end
     
     environment string delim "'" "'" begin
       specialchar = $SPECIALCHAR
     end
     

inside a string we want to highlight in a different way the special characters (such as, e.g., \n, \t, etc.) and in general escaped characters, matched by the regular expression `\\.'.

The included file symbols.lang defines all the symbols (shared also by other languages):

     symbol = "~","!","%","^","*","(",")","-","+","=","[",
             "]","\\",":",";",",",".","/","?","&","<",">","\|"

This has nothing interesting but the fact that it shows that the character \ and | have to be escaped.

The included file function.lang defines the regular expression to match a function definition or invocation:

     vardef FUNCTION = '([[:alpha:]]|_)[[:word:]]*[[:blank:]]*(?=\()'
     function = $FUNCTION

that shows an example of forward lookahead assert for the opening parenthesis (see Notes on regular expressions). As noted in File inclusion, it is crucial that this file is included after the keyword definition.


Next: , Previous: Highlighting C/C++, Up: Tutorials on Language Definitions

7.12.2 Highlighting Diff files

Now we want to highlight files that are generated by diff (typically used to create patches). This program can generate outputs in three different formats (at least at best of my knowledge).

With the option -u|--unified the differences among files are shown in the same context, for instance (the examples of the diff files shown here are manually modified so that they can fit in the page width):

     diff -ruP source-highlight-2.1.1/source-highlight.spec ...
     --- source-highlight-2.1.1/source-highlight.spec ...
     +++ source-highlight-2.1.2/source-highlight.spec ...
     @@ -6,8 +6,8 @@
     
      Summary:   syntax highlighting for source documents
      Name:      source-highlight
     -Version:   2.1.1
     -Release:   2.1.1
     +Version:   2.1.2
     +Release:   2.1.2
      License:   GPL
      Group:     Utilities/Console
      Source:    ftp://ftp.gnu.org/gnu/source-highlight/%{name}-%{version}.tar.gz
     

With the option -c--context the differences are shown into two different parts:

     diff -rc2P source-highlight-2.1.1/source-highlight.spec ...
     *** source-highlight-2.1.1/source-highlight.spec ...
     --- source-highlight-2.1.2/source-highlight.spec ...
     ***************
     *** 7,12 ****
       Summary:   syntax highlighting for source documents
       Name:      source-highlight
     ! Version:   2.1.1
     ! Release:   2.1.1
       License:   GPL
       Group:     Utilities/Console
     --- 7,12 ----
       Summary:   syntax highlighting for source documents
       Name:      source-highlight
     ! Version:   2.1.2
     ! Release:   2.1.2
       License:   GPL
       Group:     Utilities/Console
     diff -rc2P source-highlight-2.1.1/src/latex.outlang ...
     *** source-highlight-2.1.1/src/latex.outlang ...
     --- source-highlight-2.1.2/src/latex.outlang ...
     ***************
     *** 35,37 ****
     --- 35,38 ----
       "--" "-\\/-"
       "---" "-\\/-\\/-"
     + "\"" "\"{}" # avoids problems with some inputenc
       end
     

Without options it generates only the essential difference information without any addition context lines:

     diff -rP source-highlight-2.1.1/source-highlight.spec ...
     9,10c9,10
     < Version:   2.1.1
     < Release:   2.1.1
     ---
     > Version:   2.1.2
     > Release:   2.1.2

Summarizing, we would like to be able to handle all these three different syntaxes; notice that the first format and the second format have something conflicting: the first one uses the --- to indicate the new version of a file while the second format uses it to indicate the old version of a file. Since we want to highlight differently the old parts and the new parts (this is not visible in the Texinfo highlighting due to the lack of enhanced formatting features, but it is visible for instance in HTML output where we use two different colors), this behavior adds some difficulties. Of course, we could define three different language definitions, one for each diff output format. However, we prefer to handle them all in the same file!

This is the language definition for diff files:

     # language definition for files created with 'diff'
     
     # diff created with -u option
     state oldfile = '(?=^[-]{3})' begin
       oldfile start '^[-]{3}'
       oldfile start '^[-]'
       newfile start '^[+]'
       difflines start '^@@'
     end
     
     # diff created with -c option
     state oldfile = '(?=^[*]{3})' begin
       environment oldfile = '^[*]{3}[[:blank:]]+[[:digit:]]' begin
         normal start '^[[:space:]]'
         newfile = '(?=^[-]{3})' exit
       end
       oldfile start '^[*]{3}'
     
       environment newfile = '^[-]{3}[[:blank:]]+[[:digit:]]' begin
         normal start '^[[:space:]]'
         newfile = '(?=^[*]{3})' exit
         normal start '^diff' exit
       end
       newfile start '^[-]{3}'
     end
     
     # otherwise, created without options
     state difflines = '(?=^[[:digit:]])' begin
       difflines start '^[[:digit:]]'
       oldfile start '^[<]'
       newfile start '^[>]'
     end
     

Since we can safely assume that when we process a diff file it contains only information created with the same diff command line switch, we define three different states that correspond to the three diff output formats. Notice that these states are entered with a simple definition; as noted in State/Environment Definitions, this means that no automatic exit means are provided, and since no explicit exit condition is specified, this means that once one of this state is entered it will never be exited. This is consistent with our goal. Of course, the expression that makes us enter a state must be defined correctly, and in particular we first search for an initial --- sequence since this is used as the first difference specification by the -u|--unified option, so this is a distinguishing feature to be used to infer which diff format file we are processing.

Another interesting thing, is that we use the forward lookahead assert for the opening parenthesis (see Notes on regular expressions), since we only want to see which file format we are processing. Once we entered the right state we can define the regular expressions for the elements of the specific diff file format.

For the files created with the option -c|--context we define two inner environments, one for the new file part and one for the old file part (these are delimited by a --- or *** and line number information). Notice that these are environments, so anything that is not matched by any expression is formatted according to the style of the element that defines the environment. Thus, we provide an expression for text that must be formatted as normal. For diff files this corresponds to a line that start with a space or with diff (take a look at the examples above). In particular the latter case can take place only during the new file part. In both environments we must define the exit conditions. In both cases these correspond to the beginning of the complementary part; also in this case we use forward lookahead assertions, since we use it only to exit the environment. The outer definitions for oldfile and newfile are used to match the lines with source file information information.

The third state, corresponding to the normal diff output format, should be straightforward by now.


Previous: Highlighting Diff files, Up: Tutorials on Language Definitions

7.12.3 Pseudo semantic analysis

Source-hihglight, by means of regular expressions can only perform lexical analysis of the input source. In particular, it is based on the assumption that the input source is syntactically correct with respect to the input language. However, by using the language definition syntax and by writing the right regular expression it is possible to simulate some sort of semantic analysis of the input source.

For instance, consider the following C (or C++) source file:

     // test special #if 0 treatment
     
     int main() {
     #if 0 // equivalent to a comment
       int i = 10;
       printf("this should never be executed\n");
       return 1;
     #else
       printf("Hello world!\n");
       return 0;
     #endif
     
       printf("never reach here!\n");
     }
     

It is easy to verify that the code between #if 0 and #else will be never executed (indeed it will not even be compiled). Thus, we might want to format it as a comment.

We then write another language definition file, based on the file cpp.lang:

     environment comment start '^[[:blank:]]*#if[[:blank:]]+0' begin
       comment start '^[[:blank:]]*#(else|endif)' exit
     end
     
     include "cpp.lang"
     

We intentionally included an error in this first version: we used the start element to start the environment, but such element has the scope of a single line, thus, it does not have the desidered behavior:

     // test special #if 0 treatment
     
     int main() {
     #if 0 // equivalent to a comment
       int i = 10;
       printf("this should never be executed\n");
       return 1;
     #else
       printf("Hello world!\n");
       return 0;
     #endif
     
       printf("never reach here!\n");
     }
     

A better solution is the following one:

     environment comment = '^[[:blank:]]*#[[:blank:]]*if[[:blank:]]+0' begin
       comment start '^[[:blank:]]*#[[:blank:]]*(else|endif)' exit
     end
     
     include "cpp.lang"
     

here we enter the comment environment by not using a delimited element, but simply the regular expression to match #ifdef 0. Then we exit the environment either when we match an #else or a #endif. This seems to work:

     // test special #if 0 treatment
     
     int main() {
     #if 0 // equivalent to a comment
       int i = 10;
       printf("this should never be executed\n");
       return 1;
     #else
       printf("Hello world!\n");
       return 0;
     #endif
     
       printf("never reach here!\n");
     }
     

However, it does not work if we consider nested #if...#else; for instance consider the following code, formatted with the previous language definition:

     // test special #if 0 treatment
     
     int main() {
     #if 0 // equivalent to a comment
       int i = 10;
       printf("this should never be executed\n");
     #  ifdef FOO
       printf("foo\n");
     #     ifndef BAR
       printf("no bar\n");
     #     else
     #     endif
     #  else
       printf("no foo\n");
     #  endif // FOO
       return 1;
     #else
       printf("Hello world!\n");
       return 0;
     #endif
     
       printf("never reach here!\n");
     }
     

The problem is that the previous language definition does not consider nested #if and thus, the first time it matches a #else or an #endif it exits the comment environment.

We must then take into account possible nested occurrences. This can be done by using a delimited element with the nested option (Delimited definitions):

     # treat the preprocess statement
     #  #if 0
     #    ...
     #  #else
     # as a comment
     
     environment comment = '^[[:blank:]]*#[[:blank:]]*if[[:blank:]]+0' begin
       comment start '^[[:blank:]]*#[[:blank:]]*else' exit
       comment delim '^[[:blank:]]*#[[:blank:]]*if'
                     '^[[:blank:]]*#[[:blank:]]*endif' multiline nested
     
     end
     
     include "cpp.lang"
     
     

This time the right block of code is correctly formatted as a comment:

     // test special #if 0 treatment
     
     int main() {
     #if 0 // equivalent to a comment
       int i = 10;
       printf("this should never be executed\n");
     #  ifdef FOO
       printf("foo\n");
     #     ifndef BAR
       printf("no bar\n");
     #     else
     #     endif
     #  else
       printf("no foo\n");
     #  endif // FOO
       return 1;
     #else
       printf("Hello world!\n");
       return 0;
     #endif
     
       printf("never reach here!\n");
     }
     

Notice that it is crucial to exit the environment even when we match an #else (not only an #endif, since, this way, we can match again another #ifdef 0; consider, for instance, the following code:

     // test special #if 0 treatment
     
     int main() {
     #if 0 // equivalent to a comment
       int i = 10;
       printf("this should never be executed\n");
       return 1;
     #else
       printf("Hello world!\n");
     #   if 0 // another one
       return 1;
     #   else
       return 0;
     #   endif
     #endif
     
       printf("never reach here!\n");
     }
     


Next: , Previous: Language Definitions, Up: Top

8 Output Language Definitions

Since version 2.1 source-highlight uses a specific syntax to specify output formats (e.g., how to format in HTML, LaTeX, etc.). Before version 2.1, in order to add a new output format, many C++ classes had to be written. This had the drawback that a new output format could not be added “dynamically”: you had to recompile the whole source-highlight program.

Instead, now, an output format is specified in a file, loaded dynamically, through a (hopefully) simple syntax. Then, these definitions are used internally to create, on-the-fly, text formatters.

Here, we see such syntax in details, by relying on many examples. This allows a user to easily modify an existing output format definition and create a new one. These files have, typically, extension .outlang.

Each definition basically associates a text style (such as, e.g., bold, italics, colors, etc.) to the representation of that style into the output format (such as, e.g., <b>$text</b> in HTML). The representation is given in " and you can use the classic escape character \ to use the " inside the definition. If you want to specify the ASCII code for a character you can do so by specifying the numeric code in hexadecimal notation preceded by \x, for an example, see Style template.

If no definition is given for a specific style, e.g., bold, then when that style is requested during formatting, the text will be formatted as it is, i.e., the style without the definition is simply ignored.

Comments can be given by using #; the rest of the line is considered as a comment.

Files can be included in the same way as for language definitions, File inclusion.

In any case, if a definition for a style is given more than once, the last definition replaces all the others.


Next: , Previous: Output Language Definitions, Up: Output Language Definitions

8.1 File extension

With the line:

     extension "<file extension>"

you define the default file extension (without the .) used to generate files formatted according to this output format. This is used when no output file name is specified; if the file extension is not included in the .outlang is not defined, and no output file name is specified, an error will occur.

For instance, this is used in html_common.outlang:

     extension "html"


Next: , Previous: File extension, Up: Output Language Definitions

8.2 Text styles

These are the text styles that one can define:

     bold
     italics
     underline
     notfixed
     fixed

These, of course, correspond to the ones used to specify the output format style, Output format style.

These definitions, for instance, are from the HTML format definition:

     bold "<b>$text</b>"
     italics "<i>$text</i>"
     underline "<u>$text</u>"

Inside a definition you use the special variable $text to specify where the actual text to be formatted has to be inserted. For instance, the definition of bold above says that if you need to format the keyword class in bold in HTML, the following text will be generated: <b>class</b>. This variable is used also when mixing more than one styles recursively, in particular if you want to format in bold and italics (i.e, first bold and then italics, or, in other words, the sequence i, b is used in the the output format style file, see Output format style), then first the text class is substituted for $text into <b>$text</b> and then the text <b>class</b> will be substituted for $text into <i>$text</i>, thus obtaining <i><b>class</b></i>.


Next: , Previous: Text styles, Up: Output Language Definitions

8.3 Colors

The definition for using colors during formatting requires the definition for the color style:

     color "..."

For instance, for HTML we have:

     color "<font color=\"$style\">$text</font>"

Apart from the variable $text that we already saw, we have also the variable $style, that will be replaced with the actual color.

Source-highlight recognizes a number of color constants, see Output format style.

You then must associate a color constant to the color definition in the output format, through the colormap definition:

     colormap
     "color constant" "color representation"
     "color constant" "color representation"
     ...
     default "default color representation"
     end

The default row (notice the absence of ") defines the color to be used in case a color constant is used during formatting, but it is not defined in the output format.

For instance, for HTML we have:

     colormap
     "green" "#33CC00"
     "red" "#FF0000"
     "darkred" "#990000"
     "blue" "#0000FF"
     "brown" "#9A1900"
     "pink" "#CC33CC"
     "yellow" "#FFCC00"
     "cyan" "#66FFFF"
     "purple" "#993399"
     "orange" "#FF6600"
     "brightorange" "#FF9900"
     "brightgreen" "#33FF33"
     "darkgreen" "#009900"
     "black" "#000000"
     "teal" "#008080"
     "gray" "#808080"
     "darkblue" "#000080"
     default "#000000"
     end

If your output format does not handle colors you can simply avoid the definitions of color and colormap and Source-highlight will simply ignore colors.

The color is applied after applying the other styles, e.g., bold, italics, etc.

Thus, by continuing the example of the previous section, suppose you defined the following output style for keywords:

     keyword blue i, b;

then the class text will be replaced to $text variable and the value #0000FF to $style inside the color definition <font color="$style">$text</font> obtaining <font color="#0000FF">class</font> which will then be replaced to $text in <b>$text</b> and so on for italics, finally obtaining

<i><b><font color="#0000FF">class</font></b></i>.


Next: , Previous: Colors, Up: Output Language Definitions

8.4 Anchors and References

When using the command line option --line-number-ref (Invoking source-highlight) an anchor is generated in the output file for each line numbering. The style of the anchor is defined by the definition anchor. If this is not defined, the option --line-number-ref has no effect. The $linenum variable will be replaced with the line number, and the $text variable with the actual text.

For instance, for HTML we have

     anchor "<a name=\"$linenum\">$text</a>"

Since version 2.2 source-highlight can also generate references to several elements (e.g., variables, class definitions, etc.), Generating References. Also in this case the definition anchor is used; furthermore, the definition of reference is required. In the definition of anchor and reference, apart from the variable $linenum, we also have the variables $infile (the name of the original input file) and $infilename (the name of the original input file without the path) and in the definition of reference we also have the variable $outfile (the name of the file where the anchor is). One can decide how to define an anchor and a reference by using these two variables. For instance, for HTML we have

     reference "<a href=\"$outfile#$linenum\">$text</a>"

Notice, that in this case we use the $outfile since we actually generate a link to another (or possibly the same) output file.

On the contrary, for LaTeX, since we do not generate a “clickable” reference, we refer to the original input file (we use both $infilename and $linenum in both definitions of anchor and reference):

     anchor "\label{$infilename:$linenum}$text"
     reference "{\hfill $text $\rightarrow$ $infile:$linenum, \
                page~\pageref{$infilename:$linenum}}"

In particular, we use $infilename for generating the \label and not $infile because the path symbol would “disturb” LaTeX (while we use the complete file path in the textual information of the reference).

This will generate a right aligned reference. Notice that it is assumed that when generating references in LaTeX one uses --gen-references=postline or --gen-references=postdoc and not --gen-references=inline (Generating References), since it makes no sense to generate an inline reference (or at least I would not know how to generate a nice looking one :-).

Furthermore, for Texinfo:

     anchor "@anchor{$infilename:$linenum}$text"
     reference "@flushright
     @xref{$infilename:$linenum,$text,$text $infile:$linenum}.
     @end flushright"

Notice that using both $infilename (and not $infile for the same reasons) and $linenum also in the definition of anchor somehow ensures that there are no duplicate anchors; this is done for LaTeX and Texinfo but not for HTML because it is assumed that the generated .tex and .texinfo file is included directly in a master file, as it is done in this manual (while, for instance, it is assumed that a separate HTML file is generated for each source and kept separate). If this is not your case you can change the definitions of anchor and reference as you see fit. Some examples of outputs with references in Texinfo are shown in Examples.

Indeed, one can use three more definitions for reference that corresponds to the three arguments that can be passed to --gen-references command line option (Generating References): inline_reference, postline_reference and postdoc_reference. If one of this not defined, then the same definition of reference is used. Having the possibility of specifying different definitions is useful for instance in the case of HTML: the same style for an inline reference is pretty ugly when used also for a postline or postdoc reference:

     postline_reference "<a href=\"$outfile#$linenum\">$text -> $infile:$linenum</a>"
     postdoc_reference "<a href=\"$outfile#$linenum\">$text -> $infile:$linenum</a>"
     reference "<a href=\"$outfile#$linenum\">$text</a>"


Next: , Previous: Anchors and References, Up: Output Language Definitions

8.5 One style

If the output format you are defining does not have a specific style for bold, italics, ... and for colors you can simply use the definition onestyle, where you can use both $style and $text. This will be used for any style (indeed any other definition such as bold, italics, color will be ignored). Indeed, in this case, it is assumed that the style of each source element is defined in a file with its own syntax, i.e., not with a syntax defined by Source-highlight. (This is the case, for instance, of HTML using CSS style sheets.) Moreover, since the output format style is not used, during formatting the variable $style will be replaced with the name of the element to highlight (e.g., keyword, comment, etc.).

For instance, for HTML CSS, we simply have:

     onestyle "<span class=\"$style\">$text</span>"

In fact, HTML CSS relies on style definitions provided in a separate file (the .css file indeed). Thus, when formatting a keyword, e.g., abstract, we will obtain:

     <span class="keyword">abstract</span>

Of course, the style for keyword must be defined in the .css file.


Next: , Previous: One style, Up: Output Language Definitions

8.6 Style template

Some output formats are based on a unique template that where the other styles are composed; during composition the styles can be separated with a specific separator:

     styletemplate "..."
     styleseparator "..."

This is used, for instance, for the ANSI color escape sequence output format (esc.outlang):

     styletemplate "\x1b[$stylem$text\x1b[m"
     styleseparator ";"
     
     bold "01$style"
     underline "04$style"
     italics "$style"
     color "$style"

Notice that, since more than one style can be mixed into the style template, bold, underline, ... explicitly use the variable $style.


Next: , Previous: Style template, Up: Output Language Definitions

8.7 Line prefix

This feature allows you to generate a string as the prefix of each generated line that corresponds to an input line (i.e., this prefix is not generated for other generated output elements, e.g., the lines in the header, footer, etc.).

We use this feature in the LaTeX output (LaTeX output):

     lineprefix "\mbox{}"

This way each line in the LaTeX output is prefixed with \mbox{}13.

Another interesting example that uses lineprefix is the javadoc output, see Generating HTML output.


Next: , Previous: Line prefix, Up: Output Language Definitions

8.8 Character translation

Some characters that are in the source file may have a special meaning in an output format, so they need some preprocessing (e.g., escaping them). You can specify the translation table with:

     translations
     "original sequence" "transformed sequence"
     "original sequence" "transformed sequence"
     ...
     end

For instance, for HTML, we have the following translation table:

     translations
     "&" "&amp;"
     "<" "&lt;"
     ">" "&gt;"
     end


Next: , Previous: Character translation, Up: Output Language Definitions

8.9 Document template

You can define the document template, i.e., the beginning and the end of an output file, with

     doctemplate
     "...beginning..."
     "...end..."
     end

For instance, for HTML we have

     doctemplate
     "<pre><tt>"
     "</tt></pre>
     "
     end

Notice that in the end part there is an explicit new line.

In the definition of the doctemplate the following variables can be used and will be replaced during the output generation:

$title
the value of the title for the output file (e.g., the one passed with the --title command line option;
$header
the contents of the file specified with the command line option --header;
$footer
the contents of the file specified with the command line option --footer;
$css
the value passed with the command line option --css;
$additional
other additional information. Source-highlight replaces this with its name and its version.

For instance, for an HTML document with css, (file cssdoc.outlang) we have:

     doctemplate
     "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\"
         \"http://www.w3.org/TR/REC-html40/strict.dtd\">
     <html>
     <head>
     <meta http-equiv=\"Content-Type\"
     content=\"text/html; charset=iso-8859-1\">
     <meta name=\"GENERATOR\" content=\"$additional\">
     <title>$title</title>
     <link rel=\"stylesheet\" href=\"$css\" type=\"text/css\">
     </head>
     <body>
     $header<pre><tt>"
     "</tt></pre>
     $footer</body>
     </html>
     "
     end


Previous: Document template, Up: Output Language Definitions

8.10 Generating HTML output

As a complete example we show the file html_common.outlang which contains the common definitions for the various HTML output formats (html.outlang, htmldoc.outlang, etc.):

     extension "html"
     
     bold "<b>$text</b>"
     italics "<i>$text</i>"
     underline "<u>$text</u>"
     color "<font color=\"$style\">$text</font>"
     anchor "<a name=\"$linenum\">$text</a>"
     postline_reference "<a href=\"$outfile#$linenum\">$text -> $infile:$linenum</a>"
     postdoc_reference "<a href=\"$outfile#$linenum\">$text -> $infile:$linenum</a>"
     reference "<a href=\"$outfile#$linenum\">$text</a>"
     
     colormap
     "green" "#33CC00"
     "red" "#FF0000"
     "darkred" "#990000"
     "blue" "#0000FF"
     "brown" "#9A1900"
     "pink" "#CC33CC"
     "yellow" "#FFCC00"
     "cyan" "#66FFFF"
     "purple" "#993399"
     "orange" "#FF6600"
     "brightorange" "#FF9900"
     "brightgreen" "#33FF33"
     "darkgreen" "#009900"
     "black" "#000000"
     "teal" "#008080"
     "gray" "#808080"
     "darkblue" "#000080"
     default "#000000"
     end
     
     translations
     "&" "&amp;"
     "<" "&lt;"
     ">" "&gt;"
     end
     

Moreover, this file is also used for generating javadoc output:

     include "html_common.outlang"
     
     doctemplate
     " * <!-- Generated by Source-highlight -->
      * <pre><tt>
     "
     " * </tt></pre>
     "
     end
     
     lineprefix " * "
     
     translations
     "*/" "&#42;/" # this avoids the */ to be interpreted as
     # the end of a comment inside a javadoc comment
     end
     

The javadoc output format is useful to format code snippets that have to be included inside a javadoc comment of another Java file14. Apart from being formatted nicely in the generated HTML documentation, this also releaves the programmer from escaping specific characters in the code snippet (i.e., &, < and >). Notice also that it also avoids the sequence */ to be interpreted as the closing of the (javadoc) comment. For instance, if you write this code:

     /**
      * This is an example of usage
      *
      * <pre><tt>
      * System.out.println("*/");
      * </tt></pre>
      */

The resulting Java code contains a syntax error. If you use source-highlight to format the code to insert in a javadoc comment you will avoid these problems.

An example of a javadoc generated HTML page containing a code snippet formatted with source-highlight can be found in the file SimpleClass-doc.html in the documentation directory.


Next: , Previous: Output Language Definitions, Up: Top

9 Generating References

Since version 2.2 Source-highlight also produces references to fields, variables, etc. In order to do this it relies on the program Exuberant Ctags, by Darren Hiebert, available at http://ctags.sourceforge.net. Thus, you must install this program if you want Source-highlight to provide this feature.

The ctags program generates an index (or “tag”) file for a variety of language objects found in file(s). This allows these items to be quickly and easily located by a text editor or other utility (as in this case for Source-highlight). A “tag” signifies a language object for which an index entry is available (or, alternatively, the index entry created for that object)15.

This means that Source-highlight is able to generate references for a specific source language if and only if ctags handles such language. We refer to the command line options of ctags: --list-maps and --list-languages to find out the associations of file extensions and supported languages.

Reference generation is enable by using the command line option --gen-references (Invoking source-highlight). This option takes an argument that rules how references will be generated:

inline
a reference pointer will be generated exactly in the same place of the specific element. This is useful in output formats that naturally supports links, such as HTML, while it is useless for output formats that do not support inline links, such as LaTeX.
postline
if a line of the input source contains elements for which we found references, the list of references will be generated right after the line (see the examples, Examples).
postdoc
All the references will be generated after the whole input file has been generated.

There is an exception: when an element has more than one reference (because a variable is defined in many sources or because a method is overloaded) then if inline is specified, the generation switches to postline for that occurrence.

When --gen-references is specified, Source-highlight first invokes ctags. The use can customize this call by using the command line option --ctags (Invoking source-highlight). In particular, if one does not want ctags to be invoked by Source-highlight (e.g., because the tags file has already been generated) then --ctags must be passed an empty string, "". In this case or when the specified ctags command line generates an alternative output tag file (the default generated file is tags), one can specify the exact tag file with the command line option --ctags-file.

Once the tag file is generated, Source-highlight relies on the library readtags provided by the ctags distribution, and included in the Source-highlight sources.

Notice that if a program element is formatted according to a style that has the option noref (see Output format style) then this element is not considered a tag, and no reference is generated. This is the case, for instance, for a comment element: each string that is generated with the comment style, since this is declared with the option noref, it is not considered a tag (see Examples).


Next: , Previous: Generating References, Up: Top

10 Examples

Here we provide some examples of sources formatted with Source-highlight using the -f texinfo command line option. Please keep in mind that the highlighting will not be visible in the Info file, but only in the printed manual and in the HTML output (well, at least line numbers are visible everywhere :-).

The first example is produced by using the command:

     source-highlight -f texinfo -i test.java -o test.java.texinfo -n

and here's the result

     01: /*
     02:   This is a classical Hello program
     03:   to test source-highlight with Java programs.
     04:   
     05:   to have an html translation type
     06:
     07:         source-highlight -s java -f html --input Hello.java --output Hello.html
     08:         source-highlight -s java -f html < Hello.java > Hello.html
     09:
     10:   or type source-highlight --help for the list of options
     11:
     12:   written by
     13:   Lorenzo Bettini
     14:   http://www.lorenzobettini.it
     15:   http://www.gnu.org/software/src-highlite
     16: */
     17:
     18: package hello;
     19:
     20: import java.io.* ;
     21:
     22: /**
     23:  * <p>
     24:  * A simple Hello World class, used to demonstrate some
     25:  * features of Java source highlighting.
     26:  * </p>
     27:  * TODO: nothing, just to show an highlighted TODO or FIXME
     28:  *
     29:  * @author Lorenzo Bettini
     30:  * @version 2.0
     31:  */
     32: public class Hello {
     33:     int foo = 1998 ;
     34:     int hex_foo = 0xCAFEBABE;
     35:     boolean b = false;
     36:     Integer i = null ;
     37:     char c = '\'', d = 'n', e = '\\' ;
     38:     String xml = "<tag attr=\"value\">&auml;</tag>", foo2 = "\\" ;
     39:
     40:     public static void main( String args[] ) {
     41:         // just some greetings ;-)  /*
     42:         System.out.println( "Hello from java2html :-)" ) ;
     43:         System.out.println( "\tby Lorenzo Bettini" ) ;
     44:         System.out.println( "\thttp://www.lorenzobettini.it" ) ;
     45:         if (argc > 0)
     46:             String param = argc[0];
     47:         //System.out.println( "bye bye... :-D" ) ; // see you soon
     48:     }
     49: }
     

The second example shows the use of --gen-references functionality. In particular, the following output is generated with the command:

     source-highlight -f texinfo -i test.h -o test_ref.h.texinfo -n \
          --gen-references=postline

and here's the result (notice how the comment line containing the string mysum does not contain references, since it is a comment element, and this element has the option noref in the texinfo.style, see Output format style. The same holds for the _TEXTGEN_H comment in the last comment line).

     01: /*
     02: ** Copyright (C) 1999, 2000, 2001 Lorenzo Bettini
     03: **  
     04: ** This program is free software; you can redistribute it and/or modify
     05: ** it under the terms of the GNU General Public License as published by
     06: ** the Free Software Foundation; either version 2 of the License, or
     07: ** (at your option) any later version.
     08: **  
     09: ** This program is distributed in the hope that it will be useful,
     10: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
     11: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12: ** GNU General Public License for more details.
     13: **  
     14: ** You should have received a copy of the GNU General Public License
     15: ** along with this program; if not, write to the Free Software
     16: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     17: **  
     18: */
     19:
     20: // this file also contains the definition of mysum as a #define
     21:
     22: // textgenerator.h : Text Generator class &&
     23:
     24: #ifndef _TEXTGEN_H
     
See _TEXTGEN_H.
25: #define _TEXTGEN_H 26: 27: #define foo(x) (x + 1) 28: 29: #define mysum myfunbody 30: 31: #include <iostream.h> // for cerr 32: 33: #include "genfun.h" /* for generating functions */ 34: 35: class TextGenerator { 36: public : 37: virtual void generate( const char *s ) const { (*sout) << s ; } 38: virtual void generate( const char *s, int start, int end ) const 39: { 40: for ( int i = start ; i <= end ; ++i ) 41: (*sout) << s[i] ; 42: return a<p->b ? a : 3; 43: } 44: virtual void generateln( const char *s ) const 45: { 46: generate( s ) ;
See generate.
See generate.
47: (*sout) << endl ; 48: } 49: virtual void generateEntire( const char *s ) const 50: { 51: startTextGeneration() ;
See startTextGeneration.
See startTextGeneration.
52: generate(s) ;
See generate.
See generate.
53: endTextGeneration() ;
See endTextGeneration.
See endTextGeneration.
54: } 55: virtual void startTextGeneration() const {} 56: virtual void endTextGeneration() const {} 57: virtual void beginText( const char *s ) const 58: { 59: startTextGeneration() ;
See startTextGeneration.
See startTextGeneration.
60: if ( s ) 61: generate( s ) ;
See generate.
See generate.
62: } 63: virtual void endText( const char *s ) const 64: { 65: if ( s ) 66: generate( s ) ;
See generate.
See generate.
67: endTextGeneration() ;
See endTextGeneration.
See endTextGeneration.
68: } 69: } ; 70: 71: // Decorator 72: class TextDecorator : public TextGenerator {
See TextGenerator.
73: protected : 74: TextGenerator *decorated ;
See TextGenerator.
75: 76: public : 77: TextDecorator( TextGenerator *t ) : decorated( t ) {}
See TextGenerator.
See decorated.
78: 79: virtual void startTextGeneration() const 80: { 81: startDecorate() ; 82: if ( decorated )
See decorated.
83: decorated->startTextGeneration() ;
See startTextGeneration.
See decorated.
See startTextGeneration.
84: } 85: virtual void endTextGeneration() const 86: { 87: if ( decorated )
See decorated.
88: decorated->endTextGeneration() ;
See endTextGeneration.
See decorated.
See endTextGeneration.
89: endDecorate() ; 90: mysum;
See mysum.
91: } 92: 93: // pure virtual functions 94: virtual void startDecorate() const = 0 ; 95: virtual void endDecorate() const = 0 ; 96: } ; 97: 98: #endif // _TEXTGEN_H


Next: , Previous: Examples, Up: Top

11 Reporting Bugs

If you find a bug in source-highlight, please send electronic mail to

bug-source-highlight at gnu dot org

Include the version number, which you can find by running source-highlight --version. Also include in your message the output that the program produced and the output you expected.

If you have other questions, comments or suggestions about source-highlight, contact the author via electronic mail (find the address at http://www.lorenzobettini.it). The author will try to help you out, although he may not have time to fix your problems.


Next: , Previous: Problems, Up: Top

12 Mailing Lists

The following mailing lists are available:

help-source-highlight at gnu dot org

for generic discussions about the program and for asking for help about it (open mailing list), http://mail.gnu.org/mailman/listinfo/help-source-highlight

info-source-highlight at gnu dot org

for receiving information about new releases and features (read-only mailing list), http://mail.gnu.org/mailman/listinfo/info-source-highlight.

If you want to subscribe to a mailing list just go to the URL and follow the instructions, or send me an e-mail and I'll subscribe you.


Previous: Mailing Lists, Up: Top

Concept Index

Short Contents

Table of Contents


Footnotes

[1] Command lines that are too long are split into multiple indented lines separated by a \. Of course these commands are to be given in one line only, anyway.

[2] Before version 2.1, this file was called tags.j2h which used to be a very obscure name. I hope this name convention is a better one :-).

[3] Before version 2.1, this command line option was called --tags-file which used to be a very obscure name. I hope this name convention is a better one :-).

[4] You can see these colors in HTML in the file colors.html.

[5] Notice that, since version 2.2, you must use double quotes.

[6] This is the main difference introduced in version 2.0 with respect the the previous version.

[7] This is the main difference introduced in version 2.1 with respect the the previous version.

[8] As explained before, originally Source-highlight was thought mainly for generating HTML output, this is why the term css is used for style sheets.

[9] At least, to the best of my knowledge :-)

[10] http://www.boost.org/libs/regex/doc/syntax.html

[11] Please notice that this concept of state is different from the concept of “state” of an automaton.

[12] As a future extension we might think of providing a way, in the language definition syntax, to define a state/environment that extends the outer contexts instead of overriding them.

[13] This is a sort of trick to insert spaces at the beginning of a line without using a tabular environment; without the leading \mbox{} these spaces would be ignored. This is the only way I found to achieve this, if you have suggestions, please let me know!

[14] Although I haven't tested it, I think this will work also for Doxygen comments.

[15] This description is taken from the ctags man page