aboutsummaryrefslogtreecommitdiff
path: root/core/text/template/parse/ast.odin
blob: 9447e995c5ec70de9f19cc27fbdf40774ed1c730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package text_template_parse

import "../scan"

Pos :: scan.Pos
Token :: scan.Token
Token_Kind :: scan.Token_Kind

new_node :: proc($T: typeid, pos: Pos = 0, allocator := context.allocator) -> ^T {
	n := new(T, allocator)
	n.pos = pos
	n.variant = n
	return n
}

Node :: struct {
	pos: Pos,
	variant: union{
		^Node_Text,
		^Node_Comment,
		^Node_Action,
		^Node_Pipeline,
		^Node_Chain,
		^Node_Command,
		^Node_Import,
		^Node_Dot,
		^Node_Field,
		^Node_Identifier,
		^Node_Operator,
		^Node_If,
		^Node_For,
		^Node_List,
		^Node_Nil,
		^Node_Bool,
		^Node_Number,
		^Node_String,
		^Node_Variable,
		^Node_With,
		^Node_Break,
		^Node_Continue,

		// Dummy nodes
		^Node_Else,
		^Node_End,
	},
}

Node_Branch :: struct{
	using base: Node,
	pipe: ^Node_Pipeline,
	list: ^Node_List,
	else_list: ^Node_List,
}

Node_Text :: struct{
	using base: Node,
	text: string,
}
Node_Action :: struct{
	using base: Node,
	pipe: ^Node_Pipeline,
}
Node_Bool :: struct{
	using base: Node,
	ok: bool,
}
Node_Chain :: struct{
	using base: Node,
	node: ^Node,
	fields: [dynamic]string,
}
Node_Command :: struct{
	using base: Node,
	args: [dynamic]^Node,
}
Node_Dot :: struct{
	using base: Node,
}
Node_Field :: struct{
	using base: Node,
	idents: []string,
}
Node_Identifier :: struct{
	using base: Node,
	ident: string,
}
Node_Operator :: struct{
	using base: Node,
	value: string,
}


Node_If   :: distinct Node_Branch
Node_For  :: distinct Node_Branch
Node_With :: distinct Node_Branch

Node_List :: struct{
	using base: Node,
	nodes: [dynamic]^Node,
}
Node_Nil :: struct{
	using base: Node,
}
Node_Number :: struct{
	using base: Node,
	text: string,
	i: Maybe(i64),
	u: Maybe(u64),
	f: Maybe(f64),
}
Node_Pipeline :: struct{
	using base: Node,
	is_assign: bool,
	decl: [dynamic]^Node_Variable,
	cmds: [dynamic]^Node_Command,
}
Node_String :: struct{
	using base: Node,
	quoted: string,
	text: string, // after processing
}
Node_Import :: struct{
	using base: Node,
	name: string, // unquoted
	pipe: ^Node_Pipeline,
}
Node_Variable :: struct{
	using base: Node,
	name: string,
}
Node_Comment :: struct{
	using base: Node,
	text: string,
}
Node_Break :: struct{
	using base: Node,
}
Node_Continue :: struct{
	using base: Node,
}

Node_Else :: struct {
	using base: Node,
}

Node_End :: struct {
	using base: Node,
}

chain_add :: proc(c: ^Node_Chain, field: string) {
	field := field
	if len(field) == 0 || field[0] != '.' {
		panic("not a .field")
	}
	field = field[1:]
	if field == "" {
		panic("empty field")
	}
	append(&c.fields, field)
}