From 421489b1cae33a054a7eb8e852b01dabbeafc3c1 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Fri, 28 Jan 2022 18:51:23 -0500 Subject: Lexer: Support inline comments Inline comments are now supported with `// Comment here`. No block comments (for now) --- cup/lexer.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cup/lexer.c b/cup/lexer.c index d03ea78..b665353 100644 --- a/cup/lexer.c +++ b/cup/lexer.c @@ -129,9 +129,18 @@ Token Lexer_next(Lexer *lexer) return Lexer_make_token(lexer, TOKEN_MINUSEQUALS, 2); return Lexer_make_token(lexer, TOKEN_MINUS, 1); } + + case '/': { + if (peek(lexer, 1) == '/') { + lexer->pos += 2; // skip the '//' + while (lexer->pos < lexer->len && lexer->src[lexer->pos] != '\n') + lexer->pos++; + continue; + } + return Lexer_make_token(lexer, TOKEN_SLASH, 1); + } case '*': return Lexer_make_token(lexer, TOKEN_STAR, 1); - case '/': return Lexer_make_token(lexer, TOKEN_SLASH, 1); case '%': return Lexer_make_token(lexer, TOKEN_PERCENT, 1); -- cgit v1.2.3