Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 29 additions & 34 deletions lexers/embedded/lox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@
<ensure_nl>true</ensure_nl>
</config>
<rules>
<state name="classname">
<rule pattern="[a-zA-Z_]\w*">
<token type="NameClass"/>
<pop depth="1"/>
</rule>
</state>
<state name="funname">
<rule pattern="[a-zA-Z_]\w*">
<token type="NameFunction"/>
<pop depth="1"/>
</rule>
</state>
<state name="varname">
<rule pattern="[a-zA-Z_]\w*">
<token type="NameVariable"/>
<pop depth="1"/>
</rule>
</state>
<state name="whitespace">
<rule pattern="\n">
<token type="Text"/>
Expand All @@ -39,23 +21,39 @@
<rule pattern="//.*?\n">
<token type="CommentSingle"/>
</rule>
<rule pattern="(and|else|for|if|or|print|return|super|this|while)\b">
<rule pattern="(and|break|case|continue|default|else|for|if|or|print|return|super|switch|this|while)\b">
<token type="Keyword"/>
</rule>
<rule pattern="(false|nil|true)\b">
<token type="KeywordConstant"/>
</rule>
<rule pattern="(class)(\s*)">
<token type="KeywordDeclaration"/>
<push state="classname"/>
</rule>
<rule pattern="(fun)(\s*)">
<token type="KeywordDeclaration"/>
<push state="funname"/>
</rule>
<rule pattern="(var)(\s*)">
<token type="KeywordDeclaration"/>
<push state="varname"/>
<rule pattern="(class)(\s+)([a-zA-Z_]\w*)">
<bygroups>
<token type="KeywordDeclaration"/>
<token type="Text"/>
<token type="NameClass"/>
</bygroups>
</rule>
<rule pattern="(fun)(\s+)([a-zA-Z_]\w*)">
<bygroups>
<token type="KeywordDeclaration"/>
<token type="Text"/>
<token type="NameFunction"/>
</bygroups>
</rule>
<rule pattern="(const)(\s+)([a-zA-Z_]\w*)">
<bygroups>
<token type="KeywordDeclaration"/>
<token type="Text"/>
<token type="NameConstant"/>
</bygroups>
</rule>
<rule pattern="(var)(\s+)([a-zA-Z_]\w*)">
<bygroups>
<token type="KeywordDeclaration"/>
<token type="Text"/>
<token type="NameVariable"/>
</bygroups>
</rule>
<rule pattern="\d+(\.\d*|[eE][+\-]?\d+)">
<token type="LiteralNumberFloat"/>
Expand All @@ -69,10 +67,7 @@
<rule pattern="!|\+|-|\*|/|&lt;|&gt;|=">
<token type="Operator"/>
</rule>
<rule pattern="[{(;,]">
<token type="Punctuation"/>
</rule>
<rule pattern="[}).]">
<rule pattern="[{}():;.,]">
<token type="Punctuation"/>
</rule>
<rule pattern="[a-zA-Z_]\w*">
Expand Down
13 changes: 13 additions & 0 deletions lexers/testdata/lox.actual
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ while (a < 10) {

for (var a = 1; a < 10; a = a + 1) {
print a;
continue;
}

makeBreakfast(bacon, eggs, toast);
Expand Down Expand Up @@ -130,3 +131,15 @@ class Brunch < Breakfast {
print "How about a Bloody Mary?";
}
}

switch (a) {
case 1:
print("1");
break;
case 2:
print("1");
break;
default:
print("default");
break;
}
98 changes: 82 additions & 16 deletions lexers/testdata/lox.expected
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,22 @@
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"imAVariable"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"here is my value\""},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"iAmNil"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"breakfast"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
Expand Down Expand Up @@ -258,7 +261,8 @@
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"a"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
Expand Down Expand Up @@ -299,7 +303,8 @@
{"type":"Keyword","value":"for"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"("},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"a"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
Expand Down Expand Up @@ -331,6 +336,9 @@
{"type":"Text","value":" "},
{"type":"NameVariable","value":"a"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"continue"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
Expand All @@ -348,7 +356,8 @@
{"type":"NameVariable","value":"makeBreakfast"},
{"type":"Punctuation","value":"();"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"fun "},
{"type":"KeywordDeclaration","value":"fun"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"addPair"},
{"type":"Punctuation","value":"("},
{"type":"NameVariable","value":"a"},
Expand All @@ -370,7 +379,8 @@
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"KeywordDeclaration","value":"fun "},
{"type":"KeywordDeclaration","value":"fun"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"identity"},
{"type":"Punctuation","value":"("},
{"type":"NameVariable","value":"a"},
Expand Down Expand Up @@ -399,21 +409,24 @@
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"// Prints \"3\".\n"},
{"type":"Text","value":"\n"},
{"type":"KeywordDeclaration","value":"fun "},
{"type":"KeywordDeclaration","value":"fun"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"returnFunction"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"outside"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"outside\""},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"fun "},
{"type":"KeywordDeclaration","value":"fun"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"inner"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
Expand All @@ -433,7 +446,8 @@
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"fn"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
Expand All @@ -444,7 +458,8 @@
{"type":"NameVariable","value":"fn"},
{"type":"Punctuation","value":"();"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"class "},
{"type":"KeywordDeclaration","value":"class"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Breakfast"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
Expand Down Expand Up @@ -486,7 +501,8 @@
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"CommentSingle","value":"// Store it in variables.\n"},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"someVariable"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
Expand Down Expand Up @@ -518,7 +534,8 @@
{"type":"LiteralStringDouble","value":"\"sourdough\""},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"class "},
{"type":"KeywordDeclaration","value":"class"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Breakfast"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
Expand Down Expand Up @@ -556,7 +573,8 @@
{"type":"CommentSingle","value":"// ...\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"KeywordDeclaration","value":"var "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"NameVariable","value":"baconAndToast"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
Expand All @@ -578,7 +596,8 @@
{"type":"Text","value":"\n"},
{"type":"CommentSingle","value":"// \"Enjoy your bacon and toast, Dear Reader.\"\n"},
{"type":"Text","value":"\n"},
{"type":"KeywordDeclaration","value":"class "},
{"type":"KeywordDeclaration","value":"class"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Brunch"},
{"type":"Text","value":" "},
{"type":"Operator","value":"\u003c"},
Expand Down Expand Up @@ -634,5 +653,52 @@
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"Keyword","value":"switch"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"("},
{"type":"NameVariable","value":"a"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"case"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"1\""},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"break"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"case"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"1\""},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"break"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"default"},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"default\""},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"break"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"}
]