[Tex/LaTex] Using “listings” package to colorize the source code of ANTLR grammar file

colorhighlightinglistingssyntax

I use listings package in my diploma thesis for source code. Besides the mainstream languages like Java and C# I also need to include the sources of the language grammars (.g) which are pure mark-up languages in the following format:

options {
    language=Java;
    //backtrack=true;
    // please comment it in for correct lexer generation!
    // the definitions of many operators require backtracking!
} 


// program blocks

program
    : (declaration)+
    ;

declaration 
    :  metaDeclaration
    |  actionDeclaration
    |  globalDeclaration
    |  eventDeclaration 
    ;   

metaDeclaration
    : 'meta' metaParam STRING? ';'
    ;

metaParam 
    : 'version'
    | 'name'
    | 'icon'
    | 'color'
    | 'private'
    ;

globalDeclaration
    : 'var' varIdentifier ':' typeIdentifier 
       codeblock
    ;

What I want to reach is the following (or similar) colorizing of my code (see picture):

ANTLR highlighing

How can I do it? Is there any preset language template I could use?

EDIT:
I am aware of how to add own languages by adding new keywords and color rules. What I am missing are the following:

  1. How to set the lines beginning with double slash to be commentaries (probably take C template?)
  2. All strings should be green. String are character sequences beginning and ending with single quotes, like in the example above. The content doesn't matter and should be always colored with one color.
  3. All literals should be dark blue. Literals are non-strings written completely in upper case (see "STRING" in my code)
  4. Options should be black. Those follow the following pattern: text followed by curly brackets, in curly brackets the parameters are assigned as "param = value" (see options)
  5. Everything that doesn't fit the above rules should remain magenta (= rules, many examples above).

I have an idea how to accomplish the first point, but I need help with the rest of customization.

Best Answer

Updated Version:

Here is an updated version which addresses your requirements:

  1. Double slash are comments are in gray and italics
  2. Strings in single quote are green
  3. Literals (written in all caps) are dark blue (but need to be specified)
  4. options{} is in black until a trailing } is encountered.
  5. Everything else is magenta
  6. Line breaking is enabled (see lines 5-7)

enter image description here

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstset{% This applies to ALL lstlisting
    backgroundcolor=\color{yellow!10},%
    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
    }%

% Applies only when you use it
\lstdefinestyle{MyLang}{
    basicstyle=\small\ttfamily\color{magenta},%
    breaklines=true,%                                      allow line breaks
    moredelim=[s][\color{green!50!black}\ttfamily]{'}{'},% single quotes in green
    moredelim=*[s][\color{black}\ttfamily]{options}{\}},%  options in black (until trailing })
    commentstyle={\color{gray}\itshape},%                  gray italics for comments
    morecomment=[l]{//},%                                  define // comment
    emph={%
        STRING%                                            literal strings listed here
        },emphstyle={\color{blue}\ttfamily},%              and formatted in blue
    alsoletter={:,|,;},%
    morekeywords={:,|,;},%                                 define the special characters
    keywordstyle={\color{black}},%                         and format them in black
}

\begin{document}
\begin{lstlisting}[style=MyLang]
options {
    language=Java;
    //backtrack=true;
    // please comment it in for correct lexer generation!
    // the definitions of many operators require backtracking!
} 


// program blocks

program
    : (declaration)+
    ;

declaration 
    :  metaDeclaration
    |  actionDeclaration
    |  globalDeclaration
    |  eventDeclaration 
    ;   

metaDeclaration
    : 'meta' metaParam STRING? ';'
    ;

metaParam 
    : 'version'
    | 'name'
    | 'icon'
    | 'color'
    | 'private'
    ;

globalDeclaration
    : 'var' varIdentifier ':' typeIdentifier 
       codeblock
    ;\end{lstlisting}
\end{document}

Initial Version

Adapting the solution from Extend a language with additional keywords? should get you started:

enter image description here

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\lstset{%
    backgroundcolor=\color{yellow!20},%
    basicstyle=\small\ttfamily\color{blue},%
    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
    }%

% Add your keywords here, and have this in a separate file
% and include it in your preamble
\lstset{emph={%  
    color, icon, meta, name, private, var%
    },emphstyle={\color{green}}%
}%


\begin{document}
\begin{lstlisting}
metaParam 
    : 'version'
    | 'name'
    | 'icon'
    | 'color'
    | 'private'
    ;

globalDeclaration
    : 'var' varIdentifier ':' typeIdentifier 
       codeblock
    ;
\end{lstlisting}
\end{document}