Add support for nested C-style comments

This commit is contained in:
Petar Kapriš 2024-11-25 22:53:29 +01:00
parent 9f3ab157eb
commit 236172c9c5

View file

@ -183,6 +183,28 @@ func (l *Lexer) scanToken() {
for l.peek() != '\n' && !l.atEnd() {
l.advance()
}
} else if l.match('*') {
l.advance()
l.advance() // swallow the "/*"
nesting := 1
// A comment goes until we reach */, however,
// the comments can also nest
for {
if l.peek() == '/' && l.peekNext() == '*' {
nesting++
l.advance()
l.advance()
} else if l.peek() == '*' && l.peekNext() == '/' {
nesting--
l.advance()
l.advance()
} else {
if l.peek() == '\n' {
l.line++
}
l.advance()
}
}
} else if l.match('.') {
l.addSimpleToken(SLASH_DOT)
} else if l.match('_') {