Added support for multiline comments using '/*' and '*/'

This commit is contained in:
cpunch 2021-02-10 15:39:36 -06:00
parent 78e21a0f28
commit 7db0782991

View File

@ -147,6 +147,8 @@ static char peekNext(CLexState *state) {
} }
char next(CLexState *state) { char next(CLexState *state) {
if (isEnd(state))
return '\0'; // return a null terminator
state->currentChar++; state->currentChar++;
return state->currentChar[-1]; return state->currentChar[-1];
} }
@ -182,11 +184,21 @@ void skipWhitespace(CLexState *state) {
break; break;
case '/': // consume comments case '/': // consume comments
if (peekNext(state) == '/') { if (peekNext(state) == '/') {
// skip to next line (also let \n be consumed on the next iteration to properly handle that) // 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); 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; break;
} }
return; // it's a TOKEN_SLASH, let the main body handle that return; // it's a TOKEN_SLASH, let the main body handle that