Skip to content

Commit

Permalink
std.zig.tokenizer: support hex escape in char literals
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed May 25, 2018
1 parent 4308541 commit b74dda3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions std/zig/parser_test.zig
@@ -1,3 +1,16 @@
test "zig fmt: float literal with exponent" {
try testCanonical(
\\test "aoeu" {
\\ switch (state) {
\\ TermState.Start => switch (c) {
\\ '\x1b' => state = TermState.Escape,
\\ else => try out.writeByte(c),
\\ },
\\ }
\\}
\\
);
}
test "zig fmt: float literal with exponent" {
try testCanonical(
\\pub const f64_true_min = 4.94065645841246544177e-324;
Expand Down
34 changes: 34 additions & 0 deletions std/zig/tokenizer.zig
Expand Up @@ -220,6 +220,8 @@ pub const Tokenizer = struct {
MultilineStringLiteralLineBackslash,
CharLiteral,
CharLiteralBackslash,
CharLiteralEscape1,
CharLiteralEscape2,
CharLiteralEnd,
Backslash,
Equal,
Expand Down Expand Up @@ -612,9 +614,32 @@ pub const Tokenizer = struct {
result.id = Token.Id.Invalid;
break;
},
'x' => {
state = State.CharLiteralEscape1;
},
else => {
state = State.CharLiteralEnd;
},
},

State.CharLiteralEscape1 => switch (c) {
'0'...'9', 'a'...'z', 'A'...'F' => {
state = State.CharLiteralEscape2;
},
else => {
result.id = Token.Id.Invalid;
break;
},
},

State.CharLiteralEscape2 => switch (c) {
'0'...'9', 'a'...'z', 'A'...'F' => {
state = State.CharLiteralEnd;
},
else => {
result.id = Token.Id.Invalid;
break;
},
},

State.CharLiteralEnd => switch (c) {
Expand Down Expand Up @@ -988,6 +1013,8 @@ pub const Tokenizer = struct {
State.MultilineStringLiteralLineBackslash,
State.CharLiteral,
State.CharLiteralBackslash,
State.CharLiteralEscape1,
State.CharLiteralEscape2,
State.CharLiteralEnd,
State.StringLiteralBackslash => {
result.id = Token.Id.Invalid;
Expand Down Expand Up @@ -1127,6 +1154,13 @@ test "tokenizer" {
});
}

test "tokenizer - char literal with hex escape" {
testTokenize( \\'\x1b'
, []Token.Id {
Token.Id.CharLiteral,
});
}

test "tokenizer - float literal e exponent" {
testTokenize("a = 4.94065645841246544177e-324;\n", []Token.Id {
Token.Id.Identifier,
Expand Down

0 comments on commit b74dda3

Please sign in to comment.