Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
First of all, you should look in the listings package manual. There are all the setup explained. Next you can look in the source code, specifically in the files called: lstlang1.sty,lstlang2.sty and lstlang3.sty. For references of how it is setup. It is actually quite easy to create new languages. –  zeroth Jan 23 '12 at 17:02
    
@zeroth Thank you, I will look at the files. Meamwhile I added some information to the question about what exactly I need, maybe you can suggest something more concrete? –  Alexander Galkin Jan 23 '12 at 17:34

1 Answer 1

up vote 2 down vote accepted

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}
share|improve this answer
    
Thank you! I am aware of the way how I can add my own keywords (and I did it also for some custom languages). I added some information into my question to clarify what I need. –  Alexander Galkin Jan 23 '12 at 17:27
1  
@AlexanderGalkin: Maybe you could elaborate a bit more on the specifics you want the solution to address? From my understanding, Peters answer provides all that you need. What else do you need exactly? –  Daniel Jan 23 '12 at 17:34
    
@Daniel I ammended my original question with more information. In the Peter's solution all single strings (like "versioin", "name") are predefined -- this is the wrong way for me. Instead of defining new keywords, I need to be able to match strings(text between quotes), literals (text all in uppercase) with something like regexp and then colorize those. This is exactly where I need help with listings. –  Alexander Galkin Jan 23 '12 at 17:36
    
@AlexanderGalkin: Now that you have added a list of requirements, you should take this as far as you can an produce a MWE with a specific problem. There should be several solutions on this site that will help you with most of the requirements listed in your updated question. Be glad to help when you get stuck on a specific item. –  Peter Grill Jan 23 '12 at 17:42
    
@PeterGrill Can you extend your sample code with at least one example for pattern matching in the source code (or point out any similar example code) using regexp or something alike? I don't see how I can move towards MWE from your code, except for adding every possible string in my code to the keywords list that seems to be counter-productive. –  Alexander Galkin Jan 23 '12 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.