Skip to content

Commit a9ba44d

Browse files
committed
Add support for comments
1 parent f311f78 commit a9ba44d

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

checker/checker_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ MapOfAny[0]
477477
cannot 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

482487
func TestCheck_error(t *testing.T) {

expr_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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 {

parser/lexer/lexer_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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

168185
func compareTokens(i1, i2 []Token) bool {

parser/lexer/state.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)