stage2 迁移ast解析器 stage1 防止错误死循环
This commit is contained in:
1 parent
bb9d0a9f18
commit
b5a7a811b3
7 files changed
+3000
-71
No files matched your search
+11
-20
@@ -32,16 +32,11 @@ type Radix = enum {
|
||||
Hex,
|
||||
}
|
||||
|
||||
type IntValue = struct {
|
||||
value: u64,
|
||||
radix: Radix,
|
||||
}
|
||||
|
||||
type TokenKind = enum {
|
||||
Keyword: Keyword,
|
||||
Punct: Punct,
|
||||
Ident,
|
||||
Int: IntValue,
|
||||
Int: Radix, // 仅记进制;数值由 parser 按任意宽度(mp)解析
|
||||
Float,
|
||||
Char: u8,
|
||||
Str,
|
||||
@@ -57,13 +52,10 @@ kind: TokenKind,
|
||||
text: ByteView,
|
||||
line: usize,
|
||||
col: usize,
|
||||
offset: usize, // 源文件字节偏移(供 AST Span 使用)
|
||||
|
||||
fn make(kind: TokenKind, text: ByteView, line: usize, col: usize): Token {
|
||||
ret Token { .kind = kind, .text = text, .line = line, .col = col };
|
||||
}
|
||||
|
||||
fn int_value(value: u64, radix: Radix): IntValue {
|
||||
ret IntValue { .value = value, .radix = radix };
|
||||
fn make(kind: TokenKind, text: ByteView, line: usize, col: usize, offset: usize): Token {
|
||||
ret Token { .kind = kind, .text = text, .line = line, .col = col, .offset = offset };
|
||||
}
|
||||
|
||||
fn is_trivia(kind: TokenKind): bool {
|
||||
@@ -254,24 +246,23 @@ fn test_keywords() {
|
||||
}
|
||||
|
||||
fn test_adt_payloads() {
|
||||
var t = make(.Int(int_value(255, .Hex)), view("0xFF"), 3, 5);
|
||||
var t = make(.Int(.Hex), view("0xFF"), 3, 5, 0);
|
||||
@assert(t.line == 3 && t.col == 5, "position");
|
||||
match t.kind {
|
||||
.Int(iv) => {
|
||||
@assert(iv.value == 255, "int value");
|
||||
.Int(radix) => {
|
||||
var hex: Radix = .Hex;
|
||||
@assert(@enum2int(iv.radix) == @enum2int(hex), "int radix");
|
||||
@assert(@enum2int(radix) == @enum2int(hex), "int radix");
|
||||
},
|
||||
_ => { @assert(false, "expected int"); },
|
||||
}
|
||||
|
||||
var c = make(.Char('a'), view("'a'"), 1, 1);
|
||||
var c = make(.Char('a'), view("'a'"), 1, 1, 0);
|
||||
match c.kind {
|
||||
.Char(value) => { @assert(value == 'a', "char value"); },
|
||||
_ => { @assert(false, "expected char"); },
|
||||
}
|
||||
|
||||
var p = make(.Punct(.ArrowBoth), view("<->"), 1, 1);
|
||||
var p = make(.Punct(.ArrowBoth), view("<->"), 1, 1, 0);
|
||||
match p.kind {
|
||||
.Punct(punct) => {
|
||||
var both: Punct = .ArrowBoth;
|
||||
@@ -280,7 +271,7 @@ fn test_adt_payloads() {
|
||||
_ => { @assert(false, "expected punct"); },
|
||||
}
|
||||
|
||||
var k = make(.Keyword(.Ret), view("ret"), 1, 1);
|
||||
var k = make(.Keyword(.Ret), view("ret"), 1, 1, 0);
|
||||
match k.kind {
|
||||
.Keyword(kw) => {
|
||||
var ret_kw: Keyword = .Ret;
|
||||
@@ -390,7 +381,7 @@ fn test_kind_names() {
|
||||
expect_kind_name(.Punct(.Dollar), "TOK_DOLLAR");
|
||||
|
||||
expect_kind_name(.Ident, "TOK_IDENT");
|
||||
expect_kind_name(.Int(int_value(0, .Dec)), "TOK_INT_LITERAL");
|
||||
expect_kind_name(.Int(.Dec), "TOK_INT_LITERAL");
|
||||
expect_kind_name(.Float, "TOK_FLOAT_LITERAL");
|
||||
expect_kind_name(.Char('a'), "TOK_CHAR_LITERAL");
|
||||
expect_kind_name(.Str, "TOK_STRING_LITERAL");
|
||||
|
||||
Reference in new issue
Block a user