authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-23 17:35:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-23 17:35:21-04:00
log89953ec83d8afe4fed0fc9e3cdded09c7522bf86
tree42f76e6da37b08e0022af060545c5e5d3f6bd476
parent55cb9ef138c7cf0a23e7f852a82884612a3ca663
signaturelock-open Commit is signed but in an unrecognized format.

character literals: allow unicode escapes

also make the documentation for character literals more clear. closes #2089 see #2097

7 files changed, 56 insertions(+), 30 deletions(-)

doc/langref.html.in+13-3
......@@ -501,7 +501,16 @@ pub fn main() void {
501501 </div>
502502 {#see_also|Optionals|undefined#}
503503 {#header_close#}
504 {#header_open|String Literals#}
504 {#header_open|String Literals and Character Literals#}
505 <p>
506 String literals are UTF-8 encoded byte arrays.
507 </p>
508 <p>
509 Character literals have type {#syntax#}comptime_int{#endsyntax#}, the same as
510 {#link|Integer Literals#}. All {#link|Escape Sequences#} are valid in both string literals
511 and character literals. Once https://github.com/ziglang/zig/issues/2097 is implemented,
512 character literals will be allowed to have a single UTF-8 encoded codepoint.
513 </p>
505514 {#code_begin|test#}
506515const assert = @import("std").debug.assert;
507516const mem = @import("std").mem;
......@@ -513,6 +522,7 @@ test "string literals" {
513522 assert(normal_bytes.len == 5);
514523 assert(normal_bytes[1] == 'e');
515524 assert('e' == '\x65');
525 assert('\U01f4a9' == 128169);
516526 assert(mem.eql(u8, "hello", "h\x65llo"));
517527
518528 // A C string literal is a null terminated pointer.
......@@ -521,7 +531,7 @@ test "string literals" {
521531 assert(null_terminated_bytes[5] == 0);
522532}
523533 {#code_end#}
524 {#see_also|Arrays|Zig Test#}
534 {#see_also|Arrays|Zig Test|Source Encoding#}
525535 {#header_open|Escape Sequences#}
526536 <div class="table-wrapper">
527537 <table>
......@@ -8530,7 +8540,7 @@ pub fn main() void {
85308540 );
85318541}
85328542 {#code_end#}
8533 {#see_also|String Literals#}
8543 {#see_also|String Literals and Character Literals#}
85348544 {#header_close#}
85358545
85368546 {#header_open|Import from C Header File#}
src/all_types.hpp+1-1
......@@ -845,7 +845,7 @@ struct AstNodeStringLiteral {
845845};
846846
847847struct AstNodeCharLiteral {
848 uint8_t value;
848 uint32_t value;
849849};
850850
851851struct AstNodeFloatLiteral {
src/tokenizer.cpp+8-9
......@@ -1103,11 +1103,15 @@ void tokenize(Buf *buf, Tokenization *out) {
11031103
11041104 if (t.char_code_index >= t.char_code_end) {
11051105 if (t.unicode) {
1106 if (t.char_code <= 0x7f) {
1106 if (t.char_code > 0x10ffff) {
1107 tokenize_error(&t, "unicode value out of range: %x", t.char_code);
1108 }
1109 if (t.cur_tok->id == TokenIdCharLiteral) {
1110 t.cur_tok->data.char_lit.c = t.char_code;
1111 t.state = TokenizeStateCharLiteralEnd;
1112 } else if (t.char_code <= 0x7f) {
11071113 // 00000000 00000000 00000000 0xxxxxxx
11081114 handle_string_escape(&t, (uint8_t)t.char_code);
1109 } else if (t.cur_tok->id == TokenIdCharLiteral) {
1110 tokenize_error(&t, "unicode value too large for character literal: %x", t.char_code);
11111115 } else if (t.char_code <= 0x7ff) {
11121116 // 00000000 00000000 00000xxx xx000000
11131117 handle_string_escape(&t, (uint8_t)(0xc0 | (t.char_code >> 6)));
......@@ -1129,14 +1133,9 @@ void tokenize(Buf *buf, Tokenization *out) {
11291133 handle_string_escape(&t, (uint8_t)(0x80 | ((t.char_code >> 6) & 0x3f)));
11301134 // 00000000 00000000 00000000 00xxxxxx
11311135 handle_string_escape(&t, (uint8_t)(0x80 | (t.char_code & 0x3f)));
1132 } else {
1133 tokenize_error(&t, "unicode value out of range: %x", t.char_code);
11341136 }
11351137 } else {
1136 if (t.cur_tok->id == TokenIdCharLiteral && t.char_code > UINT8_MAX) {
1137 tokenize_error(&t, "value too large for character literal: '%x'",
1138 t.char_code);
1139 }
1138 assert(t.char_code <= 255);
11401139 handle_string_escape(&t, (uint8_t)t.char_code);
11411140 }
11421141 }
src/tokenizer.hpp+1-1
......@@ -148,7 +148,7 @@ struct TokenStrLit {
148148};
149149
150150struct TokenCharLit {
151 uint8_t c;
151 uint32_t c;
152152};
153153
154154struct Token {
std/zig/parser_test.zig+7
......@@ -1,3 +1,10 @@
1test "zig fmt: character literal larger than u8" {
2 try testCanonical(
3 \\const x = '\U01f4a9';
4 \\
5 );
6}
7
18test "zig fmt: infix operator and then multiline string literal" {
29 try testCanonical(
310 \\const x = "" ++
std/zig/tokenizer.zig+21-16
......@@ -236,8 +236,7 @@ pub const Tokenizer = struct {
236236 MultilineStringLiteralLine,
237237 CharLiteral,
238238 CharLiteralBackslash,
239 CharLiteralEscape1,
240 CharLiteralEscape2,
239 CharLiteralHexEscape,
241240 CharLiteralEnd,
242241 Backslash,
243242 Equal,
......@@ -293,6 +292,8 @@ pub const Tokenizer = struct {
293292 .start = self.index,
294293 .end = undefined,
295294 };
295 var seen_escape_digits: usize = undefined;
296 var expected_escape_digits: usize = undefined;
296297 while (self.index < self.buffer.len) : (self.index += 1) {
297298 const c = self.buffer[self.index];
298299 switch (state) {
......@@ -658,26 +659,31 @@ pub const Tokenizer = struct {
658659 break;
659660 },
660661 'x' => {
661 state = State.CharLiteralEscape1;
662 state = State.CharLiteralHexEscape;
663 seen_escape_digits = 0;
664 expected_escape_digits = 2;
662665 },
663 else => {
664 state = State.CharLiteralEnd;
666 'u' => {
667 state = State.CharLiteralHexEscape;
668 seen_escape_digits = 0;
669 expected_escape_digits = 4;
665670 },
666 },
667
668 State.CharLiteralEscape1 => switch (c) {
669 '0'...'9', 'a'...'z', 'A'...'F' => {
670 state = State.CharLiteralEscape2;
671 'U' => {
672 state = State.CharLiteralHexEscape;
673 seen_escape_digits = 0;
674 expected_escape_digits = 6;
671675 },
672676 else => {
673 result.id = Token.Id.Invalid;
674 break;
677 state = State.CharLiteralEnd;
675678 },
676679 },
677680
678 State.CharLiteralEscape2 => switch (c) {
681 State.CharLiteralHexEscape => switch (c) {
679682 '0'...'9', 'a'...'z', 'A'...'F' => {
680 state = State.CharLiteralEnd;
683 seen_escape_digits += 1;
684 if (seen_escape_digits == expected_escape_digits) {
685 state = State.CharLiteralEnd;
686 }
681687 },
682688 else => {
683689 result.id = Token.Id.Invalid;
......@@ -1045,8 +1051,7 @@ pub const Tokenizer = struct {
10451051 State.Backslash,
10461052 State.CharLiteral,
10471053 State.CharLiteralBackslash,
1048 State.CharLiteralEscape1,
1049 State.CharLiteralEscape2,
1054 State.CharLiteralHexEscape,
10501055 State.CharLiteralEnd,
10511056 State.StringLiteralBackslash,
10521057 State.LBracketStar,
test/stage1/behavior/misc.zig+5
......@@ -699,3 +699,8 @@ test "thread local variable" {
699699 S.t += 1;
700700 expect(S.t == 1235);
701701}
702
703test "unicode escape in character literal" {
704 var a: u24 = '\U01f4a9';
705 expect(a == 128169);
706}