Thursday, October 6, 2011

Latex listings definitions for modeling languages

Seems as if the Latex listings package does not let me go...

After blogging about how to nicely format listing with the Latex listings package, using a Eclipse syntax coloring style, and solving some tiny yet annoying problems with hyphenations of inline listings and labels within listings, I have compiled a couple of language definitions for (Eclipse) modeling languages.

Actually, these definitions are rather small, as they basically contain a list of keywords of a language. Besides the language definition, I usually define a command for quickly writing some inline listing code. I have explained this command in an earlier blog posting.

So, here are the definitions for QVT, ATL, ETL, Xtend, and Xtext.

QVT -- Query, View, Transformations

\lstdefinelanguage{QVT}{
  morekeywords={
% relations:  
   checkonly, domain, enforce, extends, implementedby, import, 
  key, overrides, primitive, query, relation, top, 
  transformation, when, where
% operational: 
 Bag, Collection, Dict, OrderedSet, Sequence, Set, Tuple, abstract, 
access, and, any, assert, blackbox, break, case, class, collect, 
collectNested, collectOne, collectselect, collectselectOne, 
composes, compute, configuration, constructor, continue, datatype, 
default, derived, disjuncts, do, elif, else, end, endif, 
enum, except, exists, extends, exception, false, forAll, forEach , 
forOne, from, helper, if, implies, import , in, inherits, init, 
inout, intermediate, invresolve, invresolveIn, invresolveone, 
invresolveoneIn , isUnique, iterate, late, let, library, literal, 
log, main, map, mapping, merges, metamodel, modeltype, new, not, 
null, object, one, or, ordered, out, package, population, primitive, property, 
query, raise, readonly, references, refines, reject, resolve, resolveIn, resolveone, 
resolveoneIn, return, select, selectOne, sortedBy, static, 
switch, tag, then, transformation, true, try, typedef, unlimited, 
uses, var, when, where, while, with, xcollect , xmap, xor, xselect 
 },
 keywordstyle=[2]{\textbf},
 morecomment=[l]{--},
 morestring=[b]{'},
 tabsize=4}
 
\newcommand{\lstQVT}[1]{\lstinline[language=QVT,breaklines=true,basicstyle=\listingsfontinline,mathescape,literate={\-}{}{0\discretionary{-}{}{}}]§#1§}

ATL -- Atlas Transformation Language

\lstdefinelanguage{ATL}{
  morekeywords={true,false,
   Bag,Set,OrderedSet,Sequence,Tuple,Integer,Real,Boolean,String,TupleType,
  not,and,or,xor,implies,module,create,from,uses,helper,def,context,
  rule,using,derived,to,mapsTo,distinct,
  foreach,in,do,if,then,else,endif,let,
  library,query,for,div,refining,entrypoint},
 keywordstyle=[2]{\textbf},
 morecomment=[l]{--},
 morestring=[b]{'},
 tabsize=4}
 
\newcommand{\lstATL}[1]{\lstinline[language=ATL,breaklines=true,basicstyle=\listingsfontinline,mathescape,literate={\-}{}{0\discretionary{-}{}{}}]§#1§}

ETL -- Epsilon Transformation Language

\lstdefinelanguage{ETL}{
  morekeywords={true,false,
   pre, rule, operation, 
  var, transform,to,
  select, selectOne, all,
  @greedy,
  for,if,else,
  not,and,or,
  Any,String,Boolean,
  true,false,return},
 keywordstyle=[2]{\textbf},
 morecomment=[l]{//}, 
 morecomment=[s]{/*}{*/}, 
 morestring=[b]",
 tabsize=4}
 
\newcommand{\lstETL}[1]{\lstinline[language=ETL,breaklines=true,basicstyle=\listingsfontinline,mathescape,literate={\-}{}{0\discretionary{-}{}{}}]§#1§}


Xtend 1.0.1

\lstdefinelanguage{Xtend}{
 morekeywords={cached,case,default,extension,false,import,JAVA,WORKFLOWSLOT,let,new,null,private,create,switch,this,true,reexport,around,if,then,else,context},
 keywordstyle=[2]{\textbf},
 morecomment=[l]{//}, 
 morecomment=[s]{/*}{*/}, 
 morestring=[b]",
 tabsize=4}
 
\newcommand{\lstXtend}[1]{\lstinline[breaklines=true,language=Xtend,basicstyle=\listingsfontinline,mathescape,literate={\-}{}{0\discretionary{-}{}{}}]\S#1\S}

Xtext 1.0.1

\lstdefinelanguage{Xtext}{
 morekeywords={grammar, with, hidden, generate, as, import, returns, current, terminal, enum},
 keywordstyle=[2]{\textbf},
 morecomment=[l]{//}, 
 morecomment=[s]{/*}{*/}, 
 morestring=[b]",
 tabsize=4}
 
\newcommand{\lstXtext}[1]{\lstinline[breaklines=true,language=Xtext,basicstyle=\listingsfontinline,mathescape,literate={\-}{}{0\discretionary{-}{}{}}]\S#1\S}

If you need a new language definition for your own Xtext based modeling or programming or domain specific or whatever language, it is very easy to retrieve the tokens of the language. Within the project of your Xtext grammar file, a special ANTLR file containing all the tokens will be generated. You will find that file in
src-gen//parser/antlr/internal/Internal.tokens

Xpand

[Updated February 2012] A friend of mine (thank you, Jens!) just sent me a new definition for Xpand:
\lstdefinelanguage{Xpand}{
 morekeywords={IMPORT, DEFINE, ENDDEFINE, LET, ENDLET, FOR, FILE, ENDFILE, ITERATOR, FOREACH, AS, IF, ENDFOREACH, ENDIF, EXPAND, INSTANCEOF, USING, SEPARATOR, CSTART, CEND, PROTECT, ENDPROTECT, ID, EXTENSION, ERROR, WARNING, INFO},
 inputencoding=latin1,
 extendedchars=true,
 % note: first and last guillemot of comments will not appear in comment style, guillemots are not possible in delimiters
 morecomment=[s]{REM}{ENDREM},
 morestring=[s]{"}{"},
 % for use with UTF-8
 literate={«}{\guillemotleft}{1}
          {»}{\guillemotright}{1}
}
This definition is a slight modification of the definitions posted in the Eclipse M2T forum (and also at the Rtsys Group Wiki, Uni Kiel) a while ago.

1 comment:

Javier said...

Great! That's just what I needed. As a contribution, I should say that Xpand has also AROUND and ENDAROUND as keywords.