stage2 提供近乎完整的功能但出现严重性能问题 添加stage0的原生性能记录 stage1添加更多内置函数 nob重构代码使其更加健壮
This commit is contained in:
1 parent
b5a7a811b3
commit
e02b0c633a
35 files changed
+14018
-216
No files matched your search
+44
-2
@@ -136,6 +136,48 @@ fn parse_error(self: *Parser, tok: *Token, what: []u8) {
|
||||
Diag.err(self.diag, span_of(self, tok), .ParseExpected, what);
|
||||
}
|
||||
|
||||
// 转义解码(镜像 stage1 spl_decode_escape):去掉引号,处理 \n \t \r \0 \\ \' \" \xHH。
|
||||
fn hex_digit(c: u8): u8 {
|
||||
if c >= '0' && c <= '9' { ret c - '0'; }
|
||||
if c >= 'a' && c <= 'f' { ret (c - 'a') + 10; }
|
||||
if c >= 'A' && c <= 'F' { ret (c - 'A') + 10; }
|
||||
ret 0;
|
||||
}
|
||||
|
||||
fn decode_str(t: ByteView): ByteBuffer {
|
||||
@assert(t.len >= 2 && t.ptr != null, "string literal token must include quotes");
|
||||
var out = ByteBuffer.new();
|
||||
if t.len < 2 { ret out; }
|
||||
var i: usize = 1;
|
||||
var end = t.len - 1;
|
||||
while i < end {
|
||||
var c = t.ptr[i];
|
||||
if c == '\\' && i + 1 < end {
|
||||
var e = t.ptr[i + 1];
|
||||
if e == 'x' && i + 3 < end {
|
||||
var hi = hex_digit(t.ptr[i + 2]);
|
||||
var lo = hex_digit(t.ptr[i + 3]);
|
||||
match ByteBuffer.append_byte(&out, (hi * 16 + lo) as u8) {
|
||||
.Ok => {}, .Err(error) => {},
|
||||
}
|
||||
i = i + 4;
|
||||
} else {
|
||||
var d: u8 = e;
|
||||
if e == 'n' { d = '\n'; }
|
||||
else if e == 't' { d = '\t'; }
|
||||
else if e == 'r' { d = '\r'; }
|
||||
else if e == '0' { d = 0; }
|
||||
match ByteBuffer.append_byte(&out, d) { .Ok => {}, .Err(error) => {} }
|
||||
i = i + 2;
|
||||
}
|
||||
} else {
|
||||
match ByteBuffer.append_byte(&out, c) { .Ok => {}, .Err(error) => {} }
|
||||
i = i + 1;
|
||||
}
|
||||
}
|
||||
ret out;
|
||||
}
|
||||
|
||||
fn expect_punct(self: *Parser, punct: Punct, what: []u8): bool {
|
||||
var t = peek(self);
|
||||
if t == null || !kind_is_punct(t.kind, punct) {
|
||||
@@ -355,7 +397,7 @@ fn parse_pattern_atom(self: *Parser): usize {
|
||||
},
|
||||
.Str => {
|
||||
advance(self);
|
||||
ret new_node(self, .ExprStringLit(.{ .data = t.text }), t);
|
||||
ret new_node(self, .ExprStringLit(.{ .data = Ast.push_string(self.ast, decode_str(t.text)) }), t);
|
||||
},
|
||||
.Keyword(k) => {
|
||||
match k {
|
||||
@@ -1293,7 +1335,7 @@ fn parse_primary(self: *Parser): usize {
|
||||
},
|
||||
.Str => {
|
||||
advance(self);
|
||||
ret new_node(self, .ExprStringLit(.{ .data = t.text }), t);
|
||||
ret new_node(self, .ExprStringLit(.{ .data = Ast.push_string(self.ast, decode_str(t.text)) }), t);
|
||||
},
|
||||
.Keyword(k) => {
|
||||
if @enum2int(k) == @enum2int(Keyword.True) {
|
||||
|
||||
Reference in new issue
Block a user