mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-05 08:10:05 +00:00
added special character support to strings
This commit is contained in:
parent
0745fd10a9
commit
9ccb258a93
20
src/clex.c
20
src/clex.c
@ -191,11 +191,29 @@ void skipWhitespace(CLexState *state) {
|
|||||||
CToken parseString(CLexState *state) {
|
CToken parseString(CLexState *state) {
|
||||||
makeBuffer(state); // buffer mode
|
makeBuffer(state); // buffer mode
|
||||||
while (peek(state) != '"' && !isEnd(state)) {
|
while (peek(state) != '"' && !isEnd(state)) {
|
||||||
if (peek(state) == '\n') // strings can't stretch across lines
|
switch (peek(state)) {
|
||||||
|
case '\n': // strings can't stretch across lines
|
||||||
return makeError(state, "Unterminated string!");
|
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!
|
saveBuffer(state); // save the character!
|
||||||
next(state); // consume
|
next(state); // consume
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isEnd(state))
|
if (isEnd(state))
|
||||||
return makeError(state, "Unterminated string!");
|
return makeError(state, "Unterminated string!");
|
||||||
|
Loading…
Reference in New Issue
Block a user