Skip to content

Commit b527a28

Browse files
Update lexer lox (#1175)
Update lexer lox #1100
1 parent 26a0cc1 commit b527a28

File tree

3 files changed

+124
-50
lines changed

3 files changed

+124
-50
lines changed

lexers/embedded/lox.xml

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,6 @@
66
<ensure_nl>true</ensure_nl>
77
</config>
88
<rules>
9-
<state name="classname">
10-
<rule pattern="[a-zA-Z_]\w*">
11-
<token type="NameClass"/>
12-
<pop depth="1"/>
13-
</rule>
14-
</state>
15-
<state name="funname">
16-
<rule pattern="[a-zA-Z_]\w*">
17-
<token type="NameFunction"/>
18-
<pop depth="1"/>
19-
</rule>
20-
</state>
21-
<state name="varname">
22-
<rule pattern="[a-zA-Z_]\w*">
23-
<token type="NameVariable"/>
24-
<pop depth="1"/>
25-
</rule>
26-
</state>
279
<state name="whitespace">
2810
<rule pattern="\n">
2911
<token type="Text"/>
@@ -39,23 +21,39 @@
3921
<rule pattern="//.*?\n">
4022
<token type="CommentSingle"/>
4123
</rule>
42-
<rule pattern="(and|else|for|if|or|print|return|super|this|while)\b">
24+
<rule pattern="(and|break|case|continue|default|else|for|if|or|print|return|super|switch|this|while)\b">
4325
<token type="Keyword"/>
4426
</rule>
4527
<rule pattern="(false|nil|true)\b">
4628
<token type="KeywordConstant"/>
4729
</rule>
48-
<rule pattern="(class)(\s*)">
49-
<token type="KeywordDeclaration"/>
50-
<push state="classname"/>
51-
</rule>
52-
<rule pattern="(fun)(\s*)">
53-
<token type="KeywordDeclaration"/>
54-
<push state="funname"/>
55-
</rule>
56-
<rule pattern="(var)(\s*)">
57-
<token type="KeywordDeclaration"/>
58-
<push state="varname"/>
30+
<rule pattern="(class)(\s+)([a-zA-Z_]\w*)">
31+
<bygroups>
32+
<token type="KeywordDeclaration"/>
33+
<token type="Text"/>
34+
<token type="NameClass"/>
35+
</bygroups>
36+
</rule>
37+
<rule pattern="(fun)(\s+)([a-zA-Z_]\w*)">
38+
<bygroups>
39+
<token type="KeywordDeclaration"/>
40+
<token type="Text"/>
41+
<token type="NameFunction"/>
42+
</bygroups>
43+
</rule>
44+
<rule pattern="(const)(\s+)([a-zA-Z_]\w*)">
45+
<bygroups>
46+
<token type="KeywordDeclaration"/>
47+
<token type="Text"/>
48+
<token type="NameConstant"/>
49+
</bygroups>
50+
</rule>
51+
<rule pattern="(var)(\s+)([a-zA-Z_]\w*)">
52+
<bygroups>
53+
<token type="KeywordDeclaration"/>
54+
<token type="Text"/>
55+
<token type="NameVariable"/>
56+
</bygroups>
5957
</rule>
6058
<rule pattern="\d+(\.\d*|[eE][+\-]?\d+)">
6159
<token type="LiteralNumberFloat"/>
@@ -69,10 +67,7 @@
6967
<rule pattern="!|\+|-|\*|/|&lt;|&gt;|=">
7068
<token type="Operator"/>
7169
</rule>
72-
<rule pattern="[{(;,]">
73-
<token type="Punctuation"/>
74-
</rule>
75-
<rule pattern="[}).]">
70+
<rule pattern="[{}():;.,]">
7671
<token type="Punctuation"/>
7772
</rule>
7873
<rule pattern="[a-zA-Z_]\w*">

lexers/testdata/lox.actual

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ while (a < 10) {
6868

6969
for (var a = 1; a < 10; a = a + 1) {
7070
print a;
71+
continue;
7172
}
7273

7374
makeBreakfast(bacon, eggs, toast);
@@ -130,3 +131,15 @@ class Brunch < Breakfast {
130131
print "How about a Bloody Mary?";
131132
}
132133
}
134+
135+
switch (a) {
136+
case 1:
137+
print("1");
138+
break;
139+
case 2:
140+
print("1");
141+
break;
142+
default:
143+
print("default");
144+
break;
145+
}

lexers/testdata/lox.expected

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,22 @@
192192
{"type":"Text","value":"\n"},
193193
{"type":"Punctuation","value":"}"},
194194
{"type":"Text","value":"\n\n"},
195-
{"type":"KeywordDeclaration","value":"var "},
195+
{"type":"KeywordDeclaration","value":"var"},
196+
{"type":"Text","value":" "},
196197
{"type":"NameVariable","value":"imAVariable"},
197198
{"type":"Text","value":" "},
198199
{"type":"Operator","value":"="},
199200
{"type":"Text","value":" "},
200201
{"type":"LiteralStringDouble","value":"\"here is my value\""},
201202
{"type":"Punctuation","value":";"},
202203
{"type":"Text","value":"\n"},
203-
{"type":"KeywordDeclaration","value":"var "},
204+
{"type":"KeywordDeclaration","value":"var"},
205+
{"type":"Text","value":" "},
204206
{"type":"NameVariable","value":"iAmNil"},
205207
{"type":"Punctuation","value":";"},
206208
{"type":"Text","value":"\n\n"},
207-
{"type":"KeywordDeclaration","value":"var "},
209+
{"type":"KeywordDeclaration","value":"var"},
210+
{"type":"Text","value":" "},
208211
{"type":"NameVariable","value":"breakfast"},
209212
{"type":"Text","value":" "},
210213
{"type":"Operator","value":"="},
@@ -258,7 +261,8 @@
258261
{"type":"Text","value":"\n"},
259262
{"type":"Punctuation","value":"}"},
260263
{"type":"Text","value":"\n\n"},
261-
{"type":"KeywordDeclaration","value":"var "},
264+
{"type":"KeywordDeclaration","value":"var"},
265+
{"type":"Text","value":" "},
262266
{"type":"NameVariable","value":"a"},
263267
{"type":"Text","value":" "},
264268
{"type":"Operator","value":"="},
@@ -299,7 +303,8 @@
299303
{"type":"Keyword","value":"for"},
300304
{"type":"Text","value":" "},
301305
{"type":"Punctuation","value":"("},
302-
{"type":"KeywordDeclaration","value":"var "},
306+
{"type":"KeywordDeclaration","value":"var"},
307+
{"type":"Text","value":" "},
303308
{"type":"NameVariable","value":"a"},
304309
{"type":"Text","value":" "},
305310
{"type":"Operator","value":"="},
@@ -331,6 +336,9 @@
331336
{"type":"Text","value":" "},
332337
{"type":"NameVariable","value":"a"},
333338
{"type":"Punctuation","value":";"},
339+
{"type":"Text","value":"\n "},
340+
{"type":"Keyword","value":"continue"},
341+
{"type":"Punctuation","value":";"},
334342
{"type":"Text","value":"\n"},
335343
{"type":"Punctuation","value":"}"},
336344
{"type":"Text","value":"\n\n"},
@@ -348,7 +356,8 @@
348356
{"type":"NameVariable","value":"makeBreakfast"},
349357
{"type":"Punctuation","value":"();"},
350358
{"type":"Text","value":"\n\n"},
351-
{"type":"KeywordDeclaration","value":"fun "},
359+
{"type":"KeywordDeclaration","value":"fun"},
360+
{"type":"Text","value":" "},
352361
{"type":"NameFunction","value":"addPair"},
353362
{"type":"Punctuation","value":"("},
354363
{"type":"NameVariable","value":"a"},
@@ -370,7 +379,8 @@
370379
{"type":"Text","value":"\n"},
371380
{"type":"Punctuation","value":"}"},
372381
{"type":"Text","value":"\n"},
373-
{"type":"KeywordDeclaration","value":"fun "},
382+
{"type":"KeywordDeclaration","value":"fun"},
383+
{"type":"Text","value":" "},
374384
{"type":"NameFunction","value":"identity"},
375385
{"type":"Punctuation","value":"("},
376386
{"type":"NameVariable","value":"a"},
@@ -399,21 +409,24 @@
399409
{"type":"Text","value":" "},
400410
{"type":"CommentSingle","value":"// Prints \"3\".\n"},
401411
{"type":"Text","value":"\n"},
402-
{"type":"KeywordDeclaration","value":"fun "},
412+
{"type":"KeywordDeclaration","value":"fun"},
413+
{"type":"Text","value":" "},
403414
{"type":"NameFunction","value":"returnFunction"},
404415
{"type":"Punctuation","value":"()"},
405416
{"type":"Text","value":" "},
406417
{"type":"Punctuation","value":"{"},
407418
{"type":"Text","value":"\n "},
408-
{"type":"KeywordDeclaration","value":"var "},
419+
{"type":"KeywordDeclaration","value":"var"},
420+
{"type":"Text","value":" "},
409421
{"type":"NameVariable","value":"outside"},
410422
{"type":"Text","value":" "},
411423
{"type":"Operator","value":"="},
412424
{"type":"Text","value":" "},
413425
{"type":"LiteralStringDouble","value":"\"outside\""},
414426
{"type":"Punctuation","value":";"},
415427
{"type":"Text","value":"\n "},
416-
{"type":"KeywordDeclaration","value":"fun "},
428+
{"type":"KeywordDeclaration","value":"fun"},
429+
{"type":"Text","value":" "},
417430
{"type":"NameFunction","value":"inner"},
418431
{"type":"Punctuation","value":"()"},
419432
{"type":"Text","value":" "},
@@ -433,7 +446,8 @@
433446
{"type":"Text","value":"\n"},
434447
{"type":"Punctuation","value":"}"},
435448
{"type":"Text","value":"\n"},
436-
{"type":"KeywordDeclaration","value":"var "},
449+
{"type":"KeywordDeclaration","value":"var"},
450+
{"type":"Text","value":" "},
437451
{"type":"NameVariable","value":"fn"},
438452
{"type":"Text","value":" "},
439453
{"type":"Operator","value":"="},
@@ -444,7 +458,8 @@
444458
{"type":"NameVariable","value":"fn"},
445459
{"type":"Punctuation","value":"();"},
446460
{"type":"Text","value":"\n\n"},
447-
{"type":"KeywordDeclaration","value":"class "},
461+
{"type":"KeywordDeclaration","value":"class"},
462+
{"type":"Text","value":" "},
448463
{"type":"NameClass","value":"Breakfast"},
449464
{"type":"Text","value":" "},
450465
{"type":"Punctuation","value":"{"},
@@ -486,7 +501,8 @@
486501
{"type":"Punctuation","value":"}"},
487502
{"type":"Text","value":"\n\n"},
488503
{"type":"CommentSingle","value":"// Store it in variables.\n"},
489-
{"type":"KeywordDeclaration","value":"var "},
504+
{"type":"KeywordDeclaration","value":"var"},
505+
{"type":"Text","value":" "},
490506
{"type":"NameVariable","value":"someVariable"},
491507
{"type":"Text","value":" "},
492508
{"type":"Operator","value":"="},
@@ -518,7 +534,8 @@
518534
{"type":"LiteralStringDouble","value":"\"sourdough\""},
519535
{"type":"Punctuation","value":";"},
520536
{"type":"Text","value":"\n\n"},
521-
{"type":"KeywordDeclaration","value":"class "},
537+
{"type":"KeywordDeclaration","value":"class"},
538+
{"type":"Text","value":" "},
522539
{"type":"NameClass","value":"Breakfast"},
523540
{"type":"Text","value":" "},
524541
{"type":"Punctuation","value":"{"},
@@ -556,7 +573,8 @@
556573
{"type":"CommentSingle","value":"// ...\n"},
557574
{"type":"Punctuation","value":"}"},
558575
{"type":"Text","value":"\n"},
559-
{"type":"KeywordDeclaration","value":"var "},
576+
{"type":"KeywordDeclaration","value":"var"},
577+
{"type":"Text","value":" "},
560578
{"type":"NameVariable","value":"baconAndToast"},
561579
{"type":"Text","value":" "},
562580
{"type":"Operator","value":"="},
@@ -578,7 +596,8 @@
578596
{"type":"Text","value":"\n"},
579597
{"type":"CommentSingle","value":"// \"Enjoy your bacon and toast, Dear Reader.\"\n"},
580598
{"type":"Text","value":"\n"},
581-
{"type":"KeywordDeclaration","value":"class "},
599+
{"type":"KeywordDeclaration","value":"class"},
600+
{"type":"Text","value":" "},
582601
{"type":"NameClass","value":"Brunch"},
583602
{"type":"Text","value":" "},
584603
{"type":"Operator","value":"\u003c"},
@@ -634,5 +653,52 @@
634653
{"type":"Punctuation","value":"}"},
635654
{"type":"Text","value":"\n"},
636655
{"type":"Punctuation","value":"}"},
656+
{"type":"Text","value":"\n\n"},
657+
{"type":"Keyword","value":"switch"},
658+
{"type":"Text","value":" "},
659+
{"type":"Punctuation","value":"("},
660+
{"type":"NameVariable","value":"a"},
661+
{"type":"Punctuation","value":")"},
662+
{"type":"Text","value":" "},
663+
{"type":"Punctuation","value":"{"},
664+
{"type":"Text","value":"\n "},
665+
{"type":"Keyword","value":"case"},
666+
{"type":"Text","value":" "},
667+
{"type":"LiteralNumberInteger","value":"1"},
668+
{"type":"Punctuation","value":":"},
669+
{"type":"Text","value":"\n "},
670+
{"type":"Keyword","value":"print"},
671+
{"type":"Punctuation","value":"("},
672+
{"type":"LiteralStringDouble","value":"\"1\""},
673+
{"type":"Punctuation","value":");"},
674+
{"type":"Text","value":"\n "},
675+
{"type":"Keyword","value":"break"},
676+
{"type":"Punctuation","value":";"},
677+
{"type":"Text","value":"\n "},
678+
{"type":"Keyword","value":"case"},
679+
{"type":"Text","value":" "},
680+
{"type":"LiteralNumberInteger","value":"2"},
681+
{"type":"Punctuation","value":":"},
682+
{"type":"Text","value":"\n "},
683+
{"type":"Keyword","value":"print"},
684+
{"type":"Punctuation","value":"("},
685+
{"type":"LiteralStringDouble","value":"\"1\""},
686+
{"type":"Punctuation","value":");"},
687+
{"type":"Text","value":"\n "},
688+
{"type":"Keyword","value":"break"},
689+
{"type":"Punctuation","value":";"},
690+
{"type":"Text","value":"\n "},
691+
{"type":"Keyword","value":"default"},
692+
{"type":"Punctuation","value":":"},
693+
{"type":"Text","value":"\n "},
694+
{"type":"Keyword","value":"print"},
695+
{"type":"Punctuation","value":"("},
696+
{"type":"LiteralStringDouble","value":"\"default\""},
697+
{"type":"Punctuation","value":");"},
698+
{"type":"Text","value":"\n "},
699+
{"type":"Keyword","value":"break"},
700+
{"type":"Punctuation","value":";"},
701+
{"type":"Text","value":"\n"},
702+
{"type":"Punctuation","value":"}"},
637703
{"type":"Text","value":"\n"}
638704
]

0 commit comments

Comments
 (0)