parser: make internal tokens use ArrayList(Token)
This commit is contained in:
parent
8f7535e14e
commit
90543d1d56
1 changed files with 15 additions and 12 deletions
|
@ -15,16 +15,25 @@ const Node = ast.Node;
|
|||
const Expr = ast.Expr;
|
||||
const Stmt = ast.Stmt;
|
||||
|
||||
const TokenList = std.ArrayList(Token);
|
||||
|
||||
pub const Parser = struct {
|
||||
allocator: *Allocator,
|
||||
scanner: *Scanner,
|
||||
|
||||
tokens: []Token = undefined,
|
||||
tokens: TokenList,
|
||||
|
||||
hadError: bool = false,
|
||||
|
||||
pub fn init(allocator: *Allocator, scanner: *Scanner) Parser {
|
||||
return Parser{ .allocator = allocator, .scanner = scanner };
|
||||
return Parser{
|
||||
.allocator = allocator,
|
||||
.scanner = scanner,
|
||||
.tokens = TokenList.init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *@This()) void {
|
||||
self.tokens.deinit();
|
||||
}
|
||||
|
||||
fn doError(self: *Parser, comptime fmt: []const u8, args: ...) void {
|
||||
|
@ -36,11 +45,11 @@ pub const Parser = struct {
|
|||
}
|
||||
|
||||
fn peek(self: *Parser) Token {
|
||||
return self.tokens[self.tokens.len - 1];
|
||||
return self.tokens.at(self.tokens.len - 1);
|
||||
}
|
||||
|
||||
fn previous(self: *Parser) Token {
|
||||
return self.tokens[self.tokens.len - 2];
|
||||
return self.tokens.at(self.tokens.len - 2);
|
||||
}
|
||||
|
||||
fn tokenError(self: *Parser, token: Token, msg: []const u8) Result!void {
|
||||
|
@ -73,12 +82,7 @@ pub const Parser = struct {
|
|||
}
|
||||
}
|
||||
|
||||
self.tokens = try self.allocator.realloc(
|
||||
self.tokens,
|
||||
self.tokens.len + 1,
|
||||
);
|
||||
|
||||
self.tokens[self.tokens.len - 1] = token;
|
||||
try self.tokens.append(token);
|
||||
std.debug.warn("skip to {}\n", token);
|
||||
return token;
|
||||
}
|
||||
|
@ -306,7 +310,6 @@ pub const Parser = struct {
|
|||
}
|
||||
|
||||
pub fn parse(self: *Parser) !*ast.Node {
|
||||
self.tokens = try self.allocator.alloc(Token, 0);
|
||||
var root = try ast.mkRoot(self.allocator);
|
||||
|
||||
var token_opt: ?Token = null;
|
||||
|
|
Loading…
Reference in a new issue