Skip to content

Commit

Permalink
Add Vala Lexer (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
glerroo committed Nov 5, 2022
1 parent d6ea504 commit be4aebd
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 0 deletions.
72 changes: 72 additions & 0 deletions lexers/embedded/vala.xml
@@ -0,0 +1,72 @@

<lexer>
<config>
<name>Vala</name>
<alias>vala</alias>
<alias>vapi</alias>
<filename>*.vala</filename>
<filename>*.vapi</filename>
<mime_type>text/x-vala</mime_type>
</config>
<rules>
<state name="whitespace">
<rule pattern="^\s*#if\s+0"><token type="CommentPreproc"/><push state="if0"/></rule>
<rule pattern="\n"><token type="TextWhitespace"/></rule>
<rule pattern="\s+"><token type="TextWhitespace"/></rule>
<rule pattern="\\\n"><token type="Text"/></rule>
<rule pattern="//(\n|(.|\n)*?[^\\]\n)"><token type="CommentSingle"/></rule>
<rule pattern="/(\\\n)?[*](.|\n)*?[*](\\\n)?/"><token type="CommentMultiline"/></rule>
</state>
<state name="statements">
<rule pattern="[L@]?&quot;"><token type="LiteralString"/><push state="string"/></rule>
<rule pattern="L?&#x27;(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\&#x27;\n])&#x27;"><token type="LiteralStringChar"/></rule>
<rule pattern="(?s)&quot;&quot;&quot;.*?&quot;&quot;&quot;"><token type="LiteralString"/></rule>
<rule pattern="(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?"><token type="LiteralNumberFloat"/></rule>
<rule pattern="(\d+\.\d*|\.\d+|\d+[fF])[fF]?"><token type="LiteralNumberFloat"/></rule>
<rule pattern="0x[0-9a-fA-F]+[Ll]?"><token type="LiteralNumberHex"/></rule>
<rule pattern="0[0-7]+[Ll]?"><token type="LiteralNumberOct"/></rule>
<rule pattern="\d+[Ll]?"><token type="LiteralNumberInteger"/></rule>
<rule pattern="[~!%^&amp;*+=|?:&lt;&gt;/-]"><token type="Operator"/></rule>
<rule pattern="(\[)(Compact|Immutable|(?:Boolean|Simple)Type)(\])"><bygroups><token type="Punctuation"/><token type="NameDecorator"/><token type="Punctuation"/></bygroups></rule>
<rule pattern="(\[)(CCode|(?:Integer|Floating)Type)"><bygroups><token type="Punctuation"/><token type="NameDecorator"/></bygroups></rule>
<rule pattern="[()\[\],.]"><token type="Punctuation"/></rule>
<rule pattern="(as|base|break|case|catch|construct|continue|default|delete|do|else|enum|finally|for|foreach|get|if|in|is|lock|new|out|params|return|set|sizeof|switch|this|throw|try|typeof|while|yield)\b"><token type="Keyword"/></rule>
<rule pattern="(abstract|const|delegate|dynamic|ensures|extern|inline|internal|override|owned|private|protected|public|ref|requires|signal|static|throws|unowned|var|virtual|volatile|weak|yields)\b"><token type="KeywordDeclaration"/></rule>
<rule pattern="(namespace|using)(\s+)"><bygroups><token type="KeywordNamespace"/><token type="TextWhitespace"/></bygroups><push state="namespace"/></rule>
<rule pattern="(class|errordomain|interface|struct)(\s+)"><bygroups><token type="KeywordDeclaration"/><token type="TextWhitespace"/></bygroups><push state="class"/></rule>
<rule pattern="(\.)([a-zA-Z_]\w*)"><bygroups><token type="Operator"/><token type="NameAttribute"/></bygroups></rule>
<rule pattern="(void|bool|char|double|float|int|int8|int16|int32|int64|long|short|size_t|ssize_t|string|time_t|uchar|uint|uint8|uint16|uint32|uint64|ulong|unichar|ushort)\b"><token type="KeywordType"/></rule>
<rule pattern="(true|false|null)\b"><token type="NameBuiltin"/></rule>
<rule pattern="[a-zA-Z_]\w*"><token type="Name"/></rule>
</state>
<state name="root">
<rule><include state="whitespace"/></rule>
<rule><push state="statement"/></rule>
</state>
<state name="statement">
<rule><include state="whitespace"/></rule>
<rule><include state="statements"/></rule>
<rule pattern="[{}]"><token type="Punctuation"/></rule>
<rule pattern=";"><token type="Punctuation"/><pop depth="1"/></rule>
</state>
<state name="string">
<rule pattern="&quot;"><token type="LiteralString"/><pop depth="1"/></rule>
<rule pattern="\\([\\abfnrtv&quot;\&#x27;]|x[a-fA-F0-9]{2,4}|[0-7]{1,3})"><token type="LiteralStringEscape"/></rule>
<rule pattern="[^\\&quot;\n]+"><token type="LiteralString"/></rule>
<rule pattern="\\\n"><token type="LiteralString"/></rule>
<rule pattern="\\"><token type="LiteralString"/></rule>
</state>
<state name="if0">
<rule pattern="^\s*#if.*?(?&lt;!\\)\n"><token type="CommentPreproc"/><push/></rule>
<rule pattern="^\s*#el(?:se|if).*\n"><token type="CommentPreproc"/><pop depth="1"/></rule>
<rule pattern="^\s*#endif.*?(?&lt;!\\)\n"><token type="CommentPreproc"/><pop depth="1"/></rule>
<rule pattern=".*?\n"><token type="Comment"/></rule>
</state>
<state name="class">
<rule pattern="[a-zA-Z_]\w*"><token type="NameClass"/><pop depth="1"/></rule>
</state>
<state name="namespace">
<rule pattern="[a-zA-Z_][\w.]*"><token type="NameNamespace"/><pop depth="1"/></rule>
</state>
</rules>
</lexer>
42 changes: 42 additions & 0 deletions lexers/testdata/vala.actual
@@ -0,0 +1,42 @@
class HelloWorld: Object {

public signal void some_event ();

public void method () {
some_event ();
}

struct Vector {
public double x;
public double y;
public double z;
}

enum WindowType {
TOPLEVEL,
POPUP
}

private uint year = 0;

public HelloWorld () {
}

public HelloWorld.with_year (int year) {
if (year>0)
this.year = year;
}

public void greeting () {
if (year == 0)
print ("Hello World\n");
else
/* Strings prefixed with '@' are string templates. */
print (@"Hello World, $(this.year)\n");
}
}

void main (string[] args) {
var helloworld = new HelloWorld.with_year (2021);
helloworld.greeting ();
}
214 changes: 214 additions & 0 deletions lexers/testdata/vala.expected
@@ -0,0 +1,214 @@
[
{"type":"KeywordDeclaration","value":"class"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"HelloWorld"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"Object"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"KeywordDeclaration","value":"public"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordDeclaration","value":"signal"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"void"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"some_event"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"();"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"KeywordDeclaration","value":"public"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"void"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"method"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"()"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"some_event"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"();"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n \n "},
{"type":"KeywordDeclaration","value":"struct"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Vector"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"public"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"double"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"x"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"public"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"double"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"y"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"public"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"double"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"z"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"Keyword","value":"enum"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"WindowType"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"TOPLEVEL"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"POPUP"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"KeywordDeclaration","value":"private"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"uint"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"year"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"KeywordDeclaration","value":"public"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"HelloWorld"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"()"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"KeywordDeclaration","value":"public"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"HelloWorld"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"with_year"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"("},
{"type":"KeywordType","value":"int"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"year"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Keyword","value":"if"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"year"},
{"type":"Operator","value":"\u003e"},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Keyword","value":"this"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"year"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"year"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"KeywordDeclaration","value":"public"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"void"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"greeting"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"()"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Keyword","value":"if"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"year"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"=="},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"print"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\"Hello World"},
{"type":"LiteralStringEscape","value":"\\n"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Keyword","value":"else"},
{"type":"TextWhitespace","value":"\n "},
{"type":"CommentMultiline","value":"/* Strings prefixed with '@' are string templates. */"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"print"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"@\"Hello World, $(this.year)"},
{"type":"LiteralStringEscape","value":"\\n"},
{"type":"LiteralString","value":"\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":" \n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"KeywordType","value":"void"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"main"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"("},
{"type":"KeywordType","value":"string"},
{"type":"Punctuation","value":"[]"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"args"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"helloworld"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"new"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"HelloWorld"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"with_year"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"("},
{"type":"LiteralNumberInteger","value":"2021"},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"helloworld"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"greeting"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"();"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n"}
]

0 comments on commit be4aebd

Please sign in to comment.