# How do I tell the listings package that comment characters only take effect at the start of a line?

I'm trying to use the listings package to format a primitive, line-oriented, DSL. The language has three types of line: comment, command, and data.

Here's an MWE that 'works but not quite':

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{%
basicstyle=\small\ttfamily,
frame=single,
morecomment=[l][\color{green}]{*},
morecomment=[l][\color{red}]{\#},
}
\begin{document}
\begin{lstlisting}
* A comment
* A # comment
# A command
# A * command
Data
Some # data
Some * data
\end{lstlisting}
\end{document}


resulting in this:

Now, I need to stop the interpretation of # and * in data as starting comments, so I tried to use the morecomment=[f] option to replace my [l] options:

  morecomment=[f][0][\color{green}]{*},
morecomment=[f][0][\color{red}]{\#},


but this now stops on the first line of the listing, with

! Missing number, treated as zero. <to be read again>  \protect  l.13
*
A comment ?


Can anyone tell me what I'm doing wrong?

You're almost there, you just need

  morecomment=[f][\color{green}][0]{*},
morecomment=[f][\color{red}][0]{\#},


\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{%
basicstyle=\small\ttfamily,
frame=single,
morecomment=[f][\color{green}][0]{*},
morecomment=[f][\color{red}][0]{\#},
}
\begin{document}
\begin{lstlisting}
* A comment
* A # comment
# A command
# A * command
Data
Some # data
Some * data
\end{lstlisting}
\end{document}

• "You're almost there" -- that's too kind, thank you! – Brent.Longborough Oct 11 '17 at 19:08