Add odin/parser/parse_files.odin

This commit is contained in:
gingerBill
2020-12-13 17:09:41 +00:00
parent f64584b92a
commit cffbd2d276
6 changed files with 200 additions and 47 deletions
+48 -5
View File
@@ -35,11 +35,6 @@ Node_State_Flag :: enum {
}
Node_State_Flags :: distinct bit_set[Node_State_Flag];
Comment_Group :: struct {
list: []tokenizer.Token,
}
Node :: struct {
pos: tokenizer.Pos,
end: tokenizer.Pos,
@@ -47,6 +42,54 @@ Node :: struct {
derived: any,
}
Comment_Group :: struct {
using node: Node,
list: []tokenizer.Token,
}
Package_Kind :: enum {
Normal,
Runtime,
Init,
}
Package :: struct {
using node: Node,
kind: Package_Kind,
id: int,
name: string,
fullpath: string,
files: map[string]^File,
user_data: rawptr,
}
File :: struct {
using node: Node,
id: int,
pkg: ^Package,
fullpath: string,
src: []byte,
docs: ^Comment_Group,
pkg_decl: ^Package_Decl,
pkg_token: tokenizer.Token,
pkg_name: string,
decls: [dynamic]^Stmt,
imports: [dynamic]^Import_Decl,
directive_count: int,
comments: [dynamic]^Comment_Group,
syntax_warning_count: int,
syntax_error_count: int,
}
// Base Types
Expr :: struct {
using expr_base: Node,
+5
View File
@@ -67,6 +67,11 @@ clone_node :: proc(node: ^Node) -> ^Node {
align = ti.align;
}
switch in node.derived {
case Package, File:
panic("Cannot clone this node type");
}
res := cast(^Node)mem.alloc(size, align);
src: rawptr = node;
if node.derived != nil {
-40
View File
@@ -1,40 +0,0 @@
package odin_ast
import "core:odin/tokenizer"
Package_Kind :: enum {
Normal,
Runtime,
Init,
}
Package :: struct {
kind: Package_Kind,
id: int,
name: string,
fullpath: string,
files: []^File,
user_data: rawptr,
}
File :: struct {
id: int,
pkg: ^Package,
fullpath: string,
src: []byte,
pkg_decl: ^Package_Decl,
pkg_token: tokenizer.Token,
pkg_name: string,
decls: [dynamic]^Stmt,
imports: [dynamic]^Import_Decl,
directive_count: int,
comments: [dynamic]^Comment_Group,
syntax_warning_count: int,
syntax_error_count: int,
}
+45
View File
@@ -59,6 +59,18 @@ walk :: proc(v: ^Visitor, node: ^Node) {
}
switch n in &node.derived {
case File:
if n.docs != nil {
walk(v, n.docs);
}
walk_stmt_list(v, n.decls[:]);
case Package:
for _, f in n.files {
walk(v, f);
}
case Comment_Group:
// empty
case Bad_Expr:
case Ident:
case Implicit:
@@ -252,29 +264,59 @@ walk :: proc(v: ^Visitor, node: ^Node) {
case Bad_Decl:
case Value_Decl:
if n.docs != nil {
walk(v, n.docs);
}
walk_attribute_list(v, n.attributes[:]);
walk_expr_list(v, n.names);
if n.type != nil {
walk(v, n.type);
}
walk_expr_list(v, n.values);
if n.comment != nil {
walk(v, n.comment);
}
case Package_Decl:
if n.docs != nil {
walk(v, n.docs);
}
if n.comment != nil {
walk(v, n.comment);
}
case Import_Decl:
if n.docs != nil {
walk(v, n.docs);
}
if n.comment != nil {
walk(v, n.comment);
}
case Foreign_Block_Decl:
if n.docs != nil {
walk(v, n.docs);
}
walk_attribute_list(v, n.attributes[:]);
if n.foreign_library != nil {
walk(v, n.foreign_library);
}
walk(v, n.body);
case Foreign_Import_Decl:
if n.docs != nil {
walk(v, n.docs);
}
walk_attribute_list(v, n.attributes[:]);
walk(v, n.name);
if n.comment != nil {
walk(v, n.comment);
}
case Proc_Group:
walk_expr_list(v, n.args);
case Attribute:
walk_expr_list(v, n.elems);
case Field:
if n.docs != nil {
walk(v, n.docs);
}
walk_expr_list(v, n.names);
if n.type != nil {
walk(v, n.type);
@@ -282,6 +324,9 @@ walk :: proc(v: ^Visitor, node: ^Node) {
if n.default_value != nil {
walk(v, n.default_value);
}
if n.comment != nil {
walk(v, n.comment);
}
case Field_List:
for x in n.list {
walk(v, x);