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]
477477cannot use int to get an element from map[string]interface {} (1:10)
478478 | MapOfAny[0]
479479 | .........^
480+
481+ 1 /* one */ + "2"
482+ invalid operation: + (mismatched types int and string) (1:13)
483+ | 1 /* one */ + "2"
484+ | ............^
480485`
481486
482487func TestCheck_error (t * testing.T ) {
Original file line number Diff line number Diff line change @@ -1032,6 +1032,10 @@ func TestExpr(t *testing.T) {
10321032 `lowercase` ,
10331033 "lowercase" ,
10341034 },
1035+ {
1036+ `1 /* one*/ + 2 /* two */` ,
1037+ 3 ,
1038+ },
10351039 }
10361040
10371041 for _ , tt := range tests {
Original file line number Diff line number Diff line change @@ -163,6 +163,23 @@ var lexTests = []lexTest{
163163 {Kind : EOF },
164164 },
165165 },
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+ },
166183}
167184
168185func compareTokens (i1 , i2 []Token ) bool {
Original file line number Diff line number Diff line change @@ -26,11 +26,13 @@ func root(l *lexer) stateFn {
2626 return number
2727 case r == '?' :
2828 return questionMark
29+ case r == '/' :
30+ return slash
2931 case strings .ContainsRune ("([{" , r ):
3032 l .emit (Bracket )
3133 case strings .ContainsRune (")]}" , r ):
3234 l .emit (Bracket )
33- case strings .ContainsRune ("#,?:%+-/ ^" , r ): // single rune operator
35+ case strings .ContainsRune ("#,?:%+-^" , r ): // single rune operator
3436 l .emit (Operator )
3537 case strings .ContainsRune ("&|!=*<>" , r ): // possible double rune operator
3638 l .accept ("&|=*" )
@@ -158,3 +160,39 @@ func questionMark(l *lexer) stateFn {
158160 l .emit (Operator )
159161 return root
160162}
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