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 am a PhD student in A.I. that is using the listings package to render its code snippets.

I am trying to define a language environment for Drools - a quite famous Production Rules System - that is meant to display the snippets much like the Eclipse perspective used to edit rules.

Despite all my efforts, I still have a few issues/questions that I cannot solve by myself. Could you please give me some help? First of all, an MWE with a Drools code snippet:

\documentclass{article}

\usepackage{listings}
\lstset{morekeywords={},
    showstringspaces=false}
\usepackage[dvipsnames,svgnames,x11names]{xcolor}

\definecolor{commentGreen}{RGB}{128,127,41}
\definecolor{stringGreen}{RGB}{0,127,38}
\definecolor{keywordRed}{RGB}{151,0,11}
\definecolor{typePurple}{RGB}{128,3,82}
\definecolor{variableBlue}{RGB}{0,0,255}
\definecolor{annotationRed}{RGB}{198,4,38}
\definecolor{annotationBlue}{RGB}{32,21,223}

\lstdefinelanguage[drl]{Drools}{
    sensitive=true,
    keywordsprefix=\$,
    alsoletter={:, },
    morekeywords=[2]{long,int,boolean,double,float,enum},
    morekeywords=[3]{declare,end,extends,from,insert,modify,not,new,no loop,query,retract,rule,salience,then,this,when, eval},
    morekeywords=[4]{after,before,during,over window:length,over window:time,overlaps},
    morekeywords=[5]{{no loop},over window:length,over window:time},
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    moredelim=[s][\color{annotationRed}\itshape]{@}{)},
    moredelim=[s][\color{annotationBlue}\itshape]{@[}{]},
    morestring=[b]",
    morestring=[d]',
%%% default style
    basicstyle=\normalfont\ttfamily,
    % identifierstyle=\normalfont,
    commentstyle=\color{commentGreen}\itshape,
    keywordstyle=\color{keywordRed}\bfseries,
    keywordstyle=[1]\color{variableBlue}\itshape,
    keywordstyle=[2]\color{typePurple}\bfseries,
    keywordstyle=[3]\color{keywordRed}\bfseries,
    keywordstyle=[4]\color{keywordRed}\bfseries,
    keywordstyle=[5]\color{stringGreen}\bfseries,
    stringstyle=\color{stringGreen}
}[comments,keywords,strings]



\begin{document}

\begin{lstlisting}[language={[drl]Drools}]
declare MyEvent
  @role(event)
  @timestamp(time)
  @duration(length)
  length  : long = 0 @[2]
  time    : long     @[1]
end 

rule "Transformation rule for 'Initiates' objects"
no loop
when
  Initiates(
    $e: event, $ep: eventPtrn, 
    $f: fluent, $fp: fluentPtrn, 
    $c: context )
then
  Helper h = Helper.getInstance(); 
  // 'h' is a wrapper to ease conversion with $*
  String r = h.transform($e, $ep, $f, $fp, $c);
  h.insertRule(r);
end

/*
rule "Resulting rule"
when
  $e: TurnOn( $t: time )
  $f: lightOn() // a query returning this fluent instance
  holdsAt( powerAvail(), $t )
then
  insert(new Declip($e, $f, $t));
end
*/
\end{lstlisting}

\end{document}

And finally the questions:

  1. Why have I to add a \lstset{morekeywords={}} statement just after \usepackage{listings} to avoid warnings on compilation on every $ symbol within my listing?
  2. How do I introduce keywords composed of two or more words? I am trying to accomplish that by using alsoletter={:, } but it does not work with the whitespace. I also tried with \ but with no luck. Just in case you are about to suggest me to define length,loop,no,over,time,window as single-word keywords, consider that it is not possible because otherwise valid identifiers as length (see the declare piece of code) would get the format of keywords.
  3. How do I format integer and real numbers? Is there any more elegant (or simple) way to do that other than the two methods already present on this site? Such workarounds are quite cumbersome and difficult to maintain when I do change something...
  4. How do I properly highlight variables (they are introduced by a $ symbol in Drools)? In the MWE I am trying to exploit keywordsprefix=\$, but I get those special keywords highlighted in comments too: is there any way to tell listings not to look in comments as it is possible to do with moredelim? Is this a bug? Shall I report it to the maintainer of the package? Just to add more details to the discussion: I also tried to use moredelim=[l][\color{variableBlue}\itshape]\$, but all the rest of the line get highlighted too. Finally I also tried with moredelim=[s][\color{variableBlue}\itshape]{\$}{:},moredelim=[s][\color{variableBlue}\itshape]{\$}{\ },etc. but it seems I cannot have more delimiters with the same starting symbol; in addition, termination symbol gets highlighted too while it should not (and if I introduce an asterisk in front of the pattern, both the starting and terminating symbols are hidden which is, again, not what I expect).
  5. I have found no mention of how to specify default formats for a language (remember, I am planning to obtain an environment that looks the same to the Drools editor): usually it is totally decoupled, so a user may introduce its own style with a \lstset{...} block. However, am I doing something completely fool by adding formats to the \lstdefinelanguage{...} block? Is there any best practice to follow?

Thanks in advance for your time.

share|improve this question

closed as not a real question by Joseph Wright♦ Dec 2 '12 at 10:58

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Welcome to TeX.SE. –  Peter Grill Nov 2 '12 at 2:29
1  
I think you should split this into separate questions so that then those questions and answers will be more likely to help future visitors as they will (in theory) be more easily to locate should someone else have a similar question. If the questions are related, then perhaps they should be combined into one. Upon a cursory read, it appears that 1 and 4 might be related issues, so if you agree then making those into one question would make sense. –  Peter Grill Nov 2 '12 at 2:31
1  
As it stands, this does not fit the site format: we really do need one issue per question. –  Joseph Wright Dec 2 '12 at 10:59

Browse other questions tagged or ask your own question.