3

I want to add a very simple macro language to listings. It uses single-letter keywords/operators, with no whitespace between. I can't see how to make keywords that don't require some kind of separator between them, i.e. I can make keywords for G, H, I, and they will colorise in G H I but not GHI.

Macro language is:

  • 09, AF are numbers
  • GZ and ,.<>;: are keywords/operators
  • "…" are quoted strings
  • all other chars are ignored (and I would like to colorise them grey)

So a valid macro might be

G2R67S4F, X 5A. ODC>4ET"A quoted string"P

in the above, I want G R S X , . > T P to be colorised as keywords and 67 DC 4E colorised as numbers.

  • Can you post what you have tried so far? – Andrew Swann May 25 '16 at 7:23
3

A straight-forward way of doing this would be to use listings' capability to perform literate replacements:

enter image description here

\documentclass{article}

\usepackage{listings,xcolor}

\colorlet{numbercol}{blue}% Colour of numbers: 0-9 and A-F
\colorlet{keywordcol}{red}% Colour of keywords: G-Z and ,.<>;:
\colorlet{stringcol}{green}% Colour of strings: "..."

\lstset{
  basicstyle = \ttfamily,
  morestring=[b][\color{stringcol}]",
  showstringspaces=false,
  literate = *{0}{{\textcolor{numbercol}{\ttfamily 0}}}1 % numbers
              {1}{{\textcolor{numbercol}{\ttfamily 1}}}1
              {2}{{\textcolor{numbercol}{\ttfamily 2}}}1
              {3}{{\textcolor{numbercol}{\ttfamily 3}}}1
              {4}{{\textcolor{numbercol}{\ttfamily 4}}}1
              {5}{{\textcolor{numbercol}{\ttfamily 5}}}1
              {6}{{\textcolor{numbercol}{\ttfamily 6}}}1
              {7}{{\textcolor{numbercol}{\ttfamily 7}}}1
              {8}{{\textcolor{numbercol}{\ttfamily 8}}}1
              {9}{{\textcolor{numbercol}{\ttfamily 9}}}1
              {A}{{\textcolor{numbercol}{\ttfamily A}}}1
              {B}{{\textcolor{numbercol}{\ttfamily B}}}1
              {C}{{\textcolor{numbercol}{\ttfamily C}}}1
              {D}{{\textcolor{numbercol}{\ttfamily D}}}1
              {E}{{\textcolor{numbercol}{\ttfamily E}}}1
              {F}{{\textcolor{numbercol}{\ttfamily F}}}1
              {G}{{\textcolor{keywordcol}{\ttfamily G}}}1 % keywords
              {H}{{\textcolor{keywordcol}{\ttfamily H}}}1
              {I}{{\textcolor{keywordcol}{\ttfamily I}}}1
              {J}{{\textcolor{keywordcol}{\ttfamily J}}}1
              {K}{{\textcolor{keywordcol}{\ttfamily K}}}1
              {L}{{\textcolor{keywordcol}{\ttfamily L}}}1
              {M}{{\textcolor{keywordcol}{\ttfamily M}}}1
              {N}{{\textcolor{keywordcol}{\ttfamily N}}}1
              {O}{{\textcolor{keywordcol}{\ttfamily O}}}1
              {P}{{\textcolor{keywordcol}{\ttfamily P}}}1
              {Q}{{\textcolor{keywordcol}{\ttfamily Q}}}1
              {R}{{\textcolor{keywordcol}{\ttfamily R}}}1
              {S}{{\textcolor{keywordcol}{\ttfamily S}}}1
              {T}{{\textcolor{keywordcol}{\ttfamily T}}}1
              {U}{{\textcolor{keywordcol}{\ttfamily U}}}1
              {V}{{\textcolor{keywordcol}{\ttfamily V}}}1
              {W}{{\textcolor{keywordcol}{\ttfamily W}}}1
              {X}{{\textcolor{keywordcol}{\ttfamily X}}}1
              {Y}{{\textcolor{keywordcol}{\ttfamily Y}}}1
              {Z}{{\textcolor{keywordcol}{\ttfamily Z}}}1
              {,}{{\textcolor{keywordcol}{\ttfamily ,}}}1
              {.}{{\textcolor{keywordcol}{\ttfamily .}}}1
              {<}{{\textcolor{keywordcol}{\ttfamily <}}}1
              {>}{{\textcolor{keywordcol}{\ttfamily >}}}1
              {;}{{\textcolor{keywordcol}{\ttfamily ;}}}1
              {:}{{\textcolor{keywordcol}{\ttfamily :}}}1
}

\begin{document}

\begin{lstlisting}
G2R67S4F, X 5A. ODC>4ET"A quoted string"P
\end{lstlisting}

\end{document}

literate=[*]<replacement item> ... <replacement item> works in the way that each <replacement item> takes the form {<search>}{<replace>}{<width>}, where <search> is replaced with <replace> and takes up the width of <width> characters. The optional * applies the <replacement item>s to only non-string entries.

You can adjust the colours to suit your needs.

0

Thanks that works perfectly!

I put it in a language define:

\lstdefinelanguage{BL233} {   
   basicstyle = \ttfamily,
   morestring=[b][\color{stringcol}]",
   showstringspaces=false,
   literate = *{0}{{\textcolor{numbercol}{\ttfamily 0}}}1 % numbers
 }

Also lets me define a second language expansion that explains the single letter macro commands: eg this expand S to Start

{S}{{\textcolor{keywordcol}{\ttfamily S}}{\textcolor{meaningcol}{\ttfamily tart}}}5

  • It looks like you've got two separate accounts, which means you cannot edit your original post or leave comments or accept answer. The Stack Exchange staff can merge them together for you. – Werner Jun 6 '16 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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