I use listings
package in my diploma thesis for source code. Besides the mainstream languages like Java and C# I also need to include the sources of the language grammars (.g) which are pure mark-up languages in the following format:
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
;
What I want to reach is the following (or similar) colorizing of my code (see picture):
How can I do it? Is there any preset language template I could use?
EDIT:
I am aware of how to add own languages by adding new keywords and color rules. What I am missing are the following:
- How to set the lines beginning with double slash to be commentaries (probably take C template?)
- All strings should be green. String are character sequences beginning and ending with single quotes, like in the example above. The content doesn't matter and should be always colored with one color.
- All literals should be dark blue. Literals are non-strings written completely in upper case (see "STRING" in my code)
- Options should be black. Those follow the following pattern: text followed by curly brackets, in curly brackets the parameters are assigned as "param = value" (see options)
- Everything that doesn't fit the above rules should remain magenta (= rules, many examples above).
I have an idea how to accomplish the first point, but I need help with the rest of customization.
Best Answer
Updated Version:
Here is an updated version which addresses your requirements:
options{}
is in black until a trailing}
is encountered.Code:
Initial Version
Adapting the solution from Extend a language with additional keywords? should get you started: