From 7db0782991de81e5cab51e9fd7d3117f8fc7fef2 Mon Sep 17 00:00:00 2001 From: cpunch Date: Wed, 10 Feb 2021 15:39:36 -0600 Subject: [PATCH] Added support for multiline comments using '/*' and '*/' --- src/clex.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/clex.c b/src/clex.c index 81b33d4..e27b645 100644 --- a/src/clex.c +++ b/src/clex.c @@ -147,6 +147,8 @@ static char peekNext(CLexState *state) { } char next(CLexState *state) { + if (isEnd(state)) + return '\0'; // return a null terminator state->currentChar++; return state->currentChar[-1]; } @@ -182,11 +184,21 @@ void skipWhitespace(CLexState *state) { break; case '/': // consume comments if (peekNext(state) == '/') { - // skip to next line (also let \n be consumed on the next iteration to properly handle that) - while (!isEnd(state) && peek(state) != '\n' && peek(state) != '\0') // if it's not a newline or null terminator + while (!isEnd(state) && peek(state) != '\n') // if it's not a newline or the end of the source next(state); - + + // keep consuming whitespace + break; + } else if (peekNext(state) == '*') { // multiline comments + while (!isEnd(state) && !(peek(state) == '*' && peekNext(state) == '/')) // if it's the end of the comment or the end of the source + next(state); + + // consume the '*/' + next(state); + next(state); + + // keep consuming whitespace break; } return; // it's a TOKEN_SLASH, let the main body handle that