From 236172c9c5c50e115c67112560dfcad0df917843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Kapri=C5=A1?= Date: Mon, 25 Nov 2024 22:53:29 +0100 Subject: [PATCH] Add support for nested C-style comments --- pj1-go/lexer.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pj1-go/lexer.go b/pj1-go/lexer.go index 89ba5b7..97cd3df 100644 --- a/pj1-go/lexer.go +++ b/pj1-go/lexer.go @@ -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('_') {