I am trying to create listing for a home-brew language (using \lstdefinelanguage). The problem is that the comments in the language are somewhat ambiguous. For example, the following code is valid:
* This is a comment
a = 4 /* This is a comment as well */
********** This is a comment ********
a = a + 1 /* So is this */
* And Surprisingly This is a comment
* And This
* AND THIS!
a = a * 100500 /* Star here is not a comment! */
So I need to make sure that the '*' is treated as a comment iff in the beginning of the line (might have spaces before), but not in the middle, where it is a multiplication.
What I have so far is the following:
\lstdefinelanguage{HOMEBREW}{
alsoletter = {\$},
comment = [l]{*},
keywords = {
*,
command,
eval,
if, elseif, else, endif,
include,
open, close,
print, printf,
set, reset,
while, endwhile,
},
sensitive = false,
string = [b]",
% MORE:
% morecomment = [l]{**},
% moredelim = [l]{(}{)},
}
Is it even possible?
Best Answer
I had to do something similar in one of my packages. Instead of a detailed explanation here, I've left a few comments in my code, for you to follow how I did it.
Note: Because
\lst@AddToHook
modifies things globally, you will need additional precautions if you intend to have listings using a language other thanHOMEBREW
in the same document. Let me know.Edit: Unfortunately, this solution is not perfect. Everything works fine with the sample code you posted, but if you start a block comment on a line that already contains a
*
and close that block comment on one of the following lines, that block comment won't be highlighted properly.