added special character support to strings

This commit is contained in:
CPunch 2020-11-26 12:48:36 -06:00
parent 0745fd10a9
commit 9ccb258a93
1 changed files with 22 additions and 4 deletions

View File

@ -191,10 +191,28 @@ void skipWhitespace(CLexState *state) {
CToken parseString(CLexState *state) {
makeBuffer(state); // buffer mode
while (peek(state) != '"' && !isEnd(state)) {
if (peek(state) == '\n') // strings can't stretch across lines
return makeError(state, "Unterminated string!");
saveBuffer(state); // save the character!
next(state); // consume
switch (peek(state)) {
case '\n': // strings can't stretch across lines
return makeError(state, "Unterminated string!");
case '\\': { // special character
next(state); // consume the '\' character
switch (peek(state)) {
case 'r': case 'n': appendBuffer(state, '\n'); break;
case 't': appendBuffer(state, '\t'); break;
case '\\': appendBuffer(state, '\\'); break;
default:
return makeError(state, "Unknown special character!"); // TODO: maybe a more descriptive error?
}
next(state); // consume special character
break;
}
default: {
saveBuffer(state); // save the character!
next(state); // consume
}
}
}
if (isEnd(state))