File tree Expand file tree Collapse file tree 4 files changed +65
-1
lines changed Expand file tree Collapse file tree 4 files changed +65
-1
lines changed Original file line number Diff line number Diff line change @@ -477,6 +477,11 @@ MapOfAny[0]
477
477
cannot use int to get an element from map[string]interface {} (1:10)
478
478
| MapOfAny[0]
479
479
| .........^
480
+
481
+ 1 /* one */ + "2"
482
+ invalid operation: + (mismatched types int and string) (1:13)
483
+ | 1 /* one */ + "2"
484
+ | ............^
480
485
`
481
486
482
487
func TestCheck_error (t * testing.T ) {
Original file line number Diff line number Diff line change @@ -1032,6 +1032,10 @@ func TestExpr(t *testing.T) {
1032
1032
`lowercase` ,
1033
1033
"lowercase" ,
1034
1034
},
1035
+ {
1036
+ `1 /* one*/ + 2 /* two */` ,
1037
+ 3 ,
1038
+ },
1035
1039
}
1036
1040
1037
1041
for _ , tt := range tests {
Original file line number Diff line number Diff line change @@ -163,6 +163,23 @@ var lexTests = []lexTest{
163
163
{Kind : EOF },
164
164
},
165
165
},
166
+ {
167
+ `foo // comment
168
+ bar // comment` ,
169
+ []Token {
170
+ {Kind : Identifier , Value : "foo" },
171
+ {Kind : Identifier , Value : "bar" },
172
+ {Kind : EOF },
173
+ },
174
+ },
175
+ {
176
+ `foo /* comment */ bar` ,
177
+ []Token {
178
+ {Kind : Identifier , Value : "foo" },
179
+ {Kind : Identifier , Value : "bar" },
180
+ {Kind : EOF },
181
+ },
182
+ },
166
183
}
167
184
168
185
func compareTokens (i1 , i2 []Token ) bool {
Original file line number Diff line number Diff line change @@ -26,11 +26,13 @@ func root(l *lexer) stateFn {
26
26
return number
27
27
case r == '?' :
28
28
return questionMark
29
+ case r == '/' :
30
+ return slash
29
31
case strings .ContainsRune ("([{" , r ):
30
32
l .emit (Bracket )
31
33
case strings .ContainsRune (")]}" , r ):
32
34
l .emit (Bracket )
33
- case strings .ContainsRune ("#,?:%+-/ ^" , r ): // single rune operator
35
+ case strings .ContainsRune ("#,?:%+-^" , r ): // single rune operator
34
36
l .emit (Operator )
35
37
case strings .ContainsRune ("&|!=*<>" , r ): // possible double rune operator
36
38
l .accept ("&|=*" )
@@ -158,3 +160,39 @@ func questionMark(l *lexer) stateFn {
158
160
l .emit (Operator )
159
161
return root
160
162
}
163
+
164
+ func slash (l * lexer ) stateFn {
165
+ if l .accept ("/" ) {
166
+ return singleLineComment
167
+ }
168
+ if l .accept ("*" ) {
169
+ return multiLineComment
170
+ }
171
+ l .emit (Operator )
172
+ return root
173
+ }
174
+
175
+ func singleLineComment (l * lexer ) stateFn {
176
+ for {
177
+ r := l .next ()
178
+ if r == eof || r == '\n' {
179
+ break
180
+ }
181
+ }
182
+ l .ignore ()
183
+ return root
184
+ }
185
+
186
+ func multiLineComment (l * lexer ) stateFn {
187
+ for {
188
+ r := l .next ()
189
+ if r == eof {
190
+ return l .error ("unclosed comment" )
191
+ }
192
+ if r == '*' && l .accept ("/" ) {
193
+ break
194
+ }
195
+ }
196
+ l .ignore ()
197
+ return root
198
+ }
You can’t perform that action at this time.
0 commit comments