forked from laytan/odin-ini-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.odin
196 lines (167 loc) · 4.42 KB
/
parser.odin
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package ini
import "core:strings"
INIValue :: struct {
key: string,
value: string,
}
INISection :: struct {
name: string,
values: [dynamic]INIValue,
}
// INI is a map from section to a map from key to values.
// Pairs defined before the first section are put into the "" key: INI[""].
INI :: [dynamic]INISection
ini_section :: proc(
i: ^INI,
section_name: string,
) -> (
^INISection,
bool,
) #optional_ok {
for &sec in i {
if sec.name == section_name {
return &sec, true
}
}
return nil, false
}
ini_delete_section :: proc(i: ^INI, section_name: string) {
for sec, idx in i {
if sec.name == section_name {
delete(sec.name)
for v in sec.values {
delete(v.key)
delete(v.value)
}
delete(sec.values)
ordered_remove(i, idx)
return
}
}
}
ini_value :: proc(
sec: ^INISection,
key: string,
) -> (
string,
bool,
) #optional_ok {
for v in sec.values {
if v.key == key {
return v.value, true
}
}
return "", false
}
ini_delete :: proc(i: ^INI) {
for sec in i {
delete(sec.name)
for value in sec.values {
delete(value.key)
delete(value.value)
}
delete(sec.values)
}
delete(i^)
}
ParseErr :: enum {
EOF, // Probably not an error (returned when ok).
IllegalToken,
KeyWithoutEquals,
ValueWithoutKey,
UnexpectedEquals,
}
ParseResult :: struct {
err: ParseErr,
pos: Pos,
}
// Parser parses the tokens from the lexer into the ini map.
Parser :: struct {
lexer: ^Lexer,
ini: ^INI,
curr_section: ^INISection,
}
make_parser :: proc(l: ^Lexer, ini: ^INI) -> Parser {
p: Parser
p.lexer = l
p.ini = ini
if len(p.ini) == 0 || p.ini[0].name != "" {
append(p.ini, INISection{name = ""})
}
p.curr_section = &p.ini[0]
return p
}
parse_into :: proc(data: []byte, ini: ^INI) -> ParseResult {
l := make_lexer(data)
p := make_parser(&l, ini)
res := parser_parse(&p)
return res
}
parse :: proc(data: []byte) -> (INI, ParseResult) {
ini: INI
res := parse_into(data, &ini)
if res.err != .EOF {
ini_delete(&ini)
}
return ini, res
}
parser_parse :: proc(using p: ^Parser) -> ParseResult {
for t := lexer_next(lexer);; t = lexer_next(lexer) {
if res, ok := parser_parse_token(p, t).?; ok {
return res
}
}
}
@(private = "file")
parser_parse_token :: proc(using p: ^Parser, t: Token) -> Maybe(ParseResult) {
token := t
for {
switch token.type {
case .Illegal:
return ParseResult{.IllegalToken, token.pos}
case .Key:
assignment := lexer_next(lexer)
if assignment.type != .Assign {
return ParseResult{.KeyWithoutEquals, token.pos}
}
key := strings.clone(string(token.value))
value_buf: strings.Builder
// Parse all values and add it to the current key
value: Token = ---
for value = lexer_next(lexer);
value.type == .Value;
value = lexer_next(lexer) {
strings.write_string(&value_buf, string(value.value))
strings.write_rune(&value_buf, ' ')
}
// Trim the trailing whitepsace character
value_str := strings.to_string(value_buf)
value_str = value_str[:len(value_str) - 1]
append(
&curr_section.values,
INIValue{key = key, value = value_str},
)
token = value
continue
case .Section:
#no_bounds_check no_brackets := token.value[1:len(token.value) - 1]
key := string(no_brackets)
if sec, ok := ini_section(ini, key); ok {
curr_section = sec
} else {
append(ini, INISection{name = strings.clone(key)})
curr_section = &ini[len(ini) - 1]
}
return nil
case .Value:
return ParseResult{.ValueWithoutKey, token.pos}
case .Assign:
return ParseResult{.UnexpectedEquals, token.pos}
// Ignoring comments.
case .Comment:
case .EOF:
return ParseResult{.EOF, token.pos}
}
}
return nil
}