forked from Vexu/arocc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preprocessor: add support for UCN identifiers
Closes Vexu#823
- Loading branch information
Showing
4 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const std = @import("std"); | ||
|
||
const DecodedUniversalChar = struct { | ||
codepoint: u32, | ||
consumed: usize, | ||
}; | ||
|
||
/// Decodes a C99-style universal character name (e.g., \uXXXX or \UXXXXXXXX) | ||
/// into a unicode codepoint. Returns the decoded character and the number of | ||
/// bytes consumed from the input string. | ||
fn decodeUniversalChar(input: []const u8) ?DecodedUniversalChar { | ||
const is_long = input[1] == 'U'; | ||
const required: usize = if (is_long) 10 else 6; | ||
|
||
if (input.len < required) | ||
return null; | ||
|
||
const hex_part = input[2..required]; | ||
var codepoint: u32 = 0; | ||
for (hex_part) |c| { | ||
codepoint *= 16; | ||
const value = switch (c) { | ||
'0'...'9' => c - '0', | ||
'a'...'f' => 10 + (c - 'a'), | ||
'A'...'F' => 10 + (c - 'A'), | ||
else => return null, | ||
}; | ||
codepoint += value; | ||
} | ||
|
||
return .{ .codepoint = codepoint, .consumed = required }; | ||
} | ||
|
||
pub const CharIterator = struct { | ||
str: []const u8, | ||
i: usize = 0, | ||
|
||
pub fn next(self: *@This()) ?u32 { | ||
if (self.i >= self.str.len) return null; | ||
if (self.str[self.i] == '\\' and self.i + 1 < self.str.len and (self.str[self.i + 1] == 'u' or self.str[self.i + 1] == 'U')) { | ||
const decoded = decodeUniversalChar(self.str[self.i..]) orelse { | ||
self.i += 1; | ||
return '\\'; | ||
}; | ||
self.i += decoded.consumed; | ||
return decoded.codepoint; | ||
} else { | ||
const len = std.unicode.utf8ByteSequenceLength(self.str[self.i]) catch 1; | ||
const cp = switch (len) { | ||
1 => self.str[self.i], | ||
2 => std.unicode.utf8Decode2(self.str[self.i..][0..2].*), | ||
3 => std.unicode.utf8Decode3(self.str[self.i..][0..3].*), | ||
4 => std.unicode.utf8Decode4(self.str[self.i..][0..4].*), | ||
else => unreachable, | ||
} catch { | ||
defer self.i += 1; | ||
return self.str[self.i]; | ||
}; | ||
self.i += len; | ||
return cp; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
int foo(void) { | ||
int \u4F60\u597D = 5; | ||
int \u0061 = 5; // TODO: error: character 'a' cannot be specified by a universal character name | ||
return 你好; | ||
} | ||
|
||
struct S { | ||
int 你好; | ||
}; | ||
|
||
int bar(int x) { | ||
struct S s; | ||
s.\u4F60\u597D = x; | ||
return s.你好; | ||
} | ||
|
||
#define TESTS_SKIPPED 1 |