From d1b65c6f46ab0891db3c80db2eedbb434168aabe Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 24 Apr 2016 16:33:32 -0700 Subject: [PATCH] fix ability to parse character literals --- src/tokenizer.cpp | 23 ++++++++++++++++++++++- test/self_hosted.zig | 6 ++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 62a43fc40..79b87c453 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -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: diff --git a/test/self_hosted.zig b/test/self_hosted.zig index b136279bd..610e9c755 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -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 = '\'';