One way to do this is to split the listings for each line range, and place an ornament in between them. There are many options as to what to use as a separator. To illustrate, I have used an ornament from this question in between the listings and another from this question on how to insert a border at the end of the listings.
Here is the code:
\documentclass{article}
\usepackage{filecontents}
\usepackage{listings}
%------------------------ Used for ornament between listings
% Ornament from https://tex.stackexchange.com/questions/11320/end-of-paragraph-with-ornament
\usepackage{pifont,fourier-orns}% These are needed only for the ornament
\newcommand\crulefill[1][1ex]{\leavevmode\leaders\hrule depth \dimexpr-#1+0.4pt height #1\hfill\kern0pt}
\newcommand\ornline[2][1ex]{\trivlist\item\crulefill[#1]#2\crulefill[#1]\endtrivlist}
%------------------------ Used for ornament at end of listings
%https://tex.stackexchange.com/questions/30973/how-do-i-insert-a-border-below-text/30979#30979
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\myrule}{O{1pt} O{3pt} O{black}}{%
\par\nobreak % don't break a page here
\kern\the\prevdepth % don't take into account the depth of the preceding line
\kern#2 % space before the rule
{\color{#3}\hrule height #1 width\hsize} % the rule
\kern#2 % space after the rule
\nointerlineskip % no additional space after the rule
}
%------------------------
\begin{filecontents*}{foo.java}
public int nextInt(int n) {
if (n<=0)
throw new IllegalArgumentException("n must be positive");
if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)next(31)) >> 31);
int bits, val;
do {
bits = next(31);
val = bits % n;
} while(bits - val + (n-1) < 0);
return val;
}
\end{filecontents*}
\lstdefinestyle{MyListStyle} {numbers=left, language=Java}
\begin{document}
\lstinputlisting[style=MyListStyle,linerange={2-6},firstnumber=2]{foo.java}
\ornline[0.6ex]{\decoone}
\lstinputlisting[style=MyListStyle,linerange={9-11},firstnumber=9]{foo.java}
\myrule[5pt][5pt][orange]
\end{document}
Updated Version:
Here is an updated version which addresses your requirements:
- Double slash are comments are in gray and italics
- Strings in single quote are green
- Literals (written in all caps) are dark blue (but need to be specified)
options{}
is in black until a trailing }
is encountered.
- Everything else is magenta
- Line breaking is enabled (see lines 5-7)
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:
\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}
Best Answer
After trying to do this by changing the catcode of
.
to letter and a short look through thelistings
code I decided to RTFM and found thealsoletter
option quite quickly. You also may want to use thealsodigit
option instead.