fix ability to parse character literals

master
Andrew Kelley 2016-04-24 16:33:32 -07:00
parent 3886fdc19b
commit d1b65c6f46
2 changed files with 28 additions and 1 deletions

View File

@ -136,6 +136,8 @@ enum TokenizeState {
TokenizeStateRawStringContents,
TokenizeStateRawStringMaybeEnd,
TokenizeStateCharLiteral,
TokenizeStateCharLiteralEscape,
TokenizeStateCharLiteralEnd,
TokenizeStateSawStar,
TokenizeStateSawSlash,
TokenizeStateSawPercent,
@ -955,8 +957,25 @@ void tokenize(Buf *buf, Tokenization *out) {
end_token(&t);
t.state = TokenizeStateStart;
break;
default:
case '\\':
t.state = TokenizeStateCharLiteralEscape;
break;
default:
t.state = TokenizeStateCharLiteralEnd;
break;
}
break;
case TokenizeStateCharLiteralEscape:
t.state = TokenizeStateCharLiteralEnd;
break;
case TokenizeStateCharLiteralEnd:
switch (c) {
case '\'':
end_token(&t);
t.state = TokenizeStateStart;
break;
default:
tokenize_error(&t, "invalid character: '%c'", c);
}
break;
case TokenizeStateZero:
@ -1118,6 +1137,8 @@ void tokenize(Buf *buf, Tokenization *out) {
tokenize_error(&t, "unterminated raw string");
break;
case TokenizeStateCharLiteral:
case TokenizeStateCharLiteralEscape:
case TokenizeStateCharLiteralEnd:
tokenize_error(&t, "unterminated character literal");
break;
case TokenizeStateSymbol:

View File

@ -1333,3 +1333,9 @@ fn pointer_comparison() {
fn ptr_eql(a: &[]u8, b: &[]u8) -> bool {
a == b
}
#attribute("test")
fn character_literals() {
assert('\'' == single_quote);
}
const single_quote = '\'';