feat: ast structure and utilities
This commit is contained in:
parent
662c38b360
commit
5268a53148
5 changed files with 970 additions and 5 deletions
67
src/Parse.zig
Normal file
67
src/Parse.zig
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
const std = @import("std");
|
||||
const assert = std.debug.assert;
|
||||
const tok = @import("tokenizer.zig");
|
||||
const Token = tok.Token;
|
||||
const Tokenizer = tok.Tokenizer;
|
||||
const Ast = @import("Ast.zig");
|
||||
|
||||
const Parse = @This();
|
||||
|
||||
gpa: std.mem.Allocator,
|
||||
arena: std.mem.Allocator,
|
||||
tokenizer: Tokenizer,
|
||||
token: Token,
|
||||
scratch: std.ArrayListUnmanaged(*Ast.Node),
|
||||
grammar_stack: std.ArrayListUnmanaged(ScanContext),
|
||||
block_stack: std.ArrayListUnmanaged(State),
|
||||
choice_stack: std.ArrayListUnmanaged(State),
|
||||
errors: std.ArrayListUnmanaged(Ast.Error),
|
||||
panic_mode: bool,
|
||||
flags: u32,
|
||||
max_parse_depth: usize,
|
||||
max_argument_count: usize,
|
||||
knot_offset: usize = 0,
|
||||
|
||||
pub const Error = error{
|
||||
ParseError,
|
||||
OutOfMemory,
|
||||
};
|
||||
|
||||
pub const State = struct {
|
||||
level: usize,
|
||||
scratch_offset: usize,
|
||||
source_offset: usize,
|
||||
};
|
||||
|
||||
pub const StmtContext = struct {
|
||||
tag: Tag,
|
||||
is_block_created: bool = false,
|
||||
expression_node: ?*Ast.Node = null,
|
||||
level: usize = 0,
|
||||
blocks_top: usize,
|
||||
choices_top: usize,
|
||||
scratch_top: usize,
|
||||
|
||||
pub const Tag = enum {
|
||||
block,
|
||||
conditional,
|
||||
};
|
||||
};
|
||||
|
||||
pub const ScanContext = struct {
|
||||
grammar: Tokenizer.Grammar,
|
||||
source_offset: usize,
|
||||
};
|
||||
|
||||
pub fn deinit(p: *Parse) void {
|
||||
p.scratch.deinit(p.gpa);
|
||||
p.grammar_stack.deinit(p.gpa);
|
||||
p.block_stack.deinit(p.gpa);
|
||||
p.choice_stack.deinit(p.gpa);
|
||||
p.errors.deinit(p.gpa);
|
||||
p.* = undefined;
|
||||
}
|
||||
|
||||
pub fn parseFile(_: *Parse) Error!*Ast.Node {
|
||||
return error.OutOfMemory;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue