-
Notifications
You must be signed in to change notification settings - Fork 1
/
parserc.go
306 lines (290 loc) · 9.82 KB
/
parserc.go
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package ini
// The parser implements the following grammar:
//
// document ::= DOCUMENT-START section* DOCUMENT-END
// section ::= SECTION-START (node | comment)* SECTION-END
// node ::= KEY VALUE SCALAR
// comment ::= COMMENT SCALAR
// Peek the next token in the token queue.
func peek_token(parser *ini_parser_t) *ini_token_t {
if parser.token_available || ini_parser_fetch_more_tokens(parser) {
return &parser.tokens[parser.tokens_head]
}
return nil
}
// Remove the next token from the queue (must be called after peek_token).
func skip_token(parser *ini_parser_t) {
parser.token_available = false
parser.tokens_parsed++
parser.document_end_produced = parser.tokens[parser.tokens_head].typ == ini_DOCUMENT_END_TOKEN
parser.tokens_head++
}
// Get the next event.
func ini_parser_parse(parser *ini_parser_t, event *ini_event_t) bool {
// Erase the event object.
*event = ini_event_t{}
// No events after the end of the stream or error.
if parser.document_end_produced || parser.error != ini_NO_ERROR || parser.state == ini_PARSE_DOCUMENT_END_STATE {
return true
}
// Generate the next event.
return ini_parser_state_machine(parser, event)
}
// Set parser error.
func ini_parser_set_parser_error(parser *ini_parser_t, problem string, problem_mark ini_mark_t) bool {
parser.error = ini_PARSER_ERROR
parser.problem = problem
parser.problem_mark = problem_mark
return false
}
func ini_parser_set_parser_error_context(parser *ini_parser_t, context string, context_mark ini_mark_t, problem string, problem_mark ini_mark_t) bool {
parser.error = ini_PARSER_ERROR
parser.context = context
parser.context_mark = context_mark
parser.problem = problem
parser.problem_mark = problem_mark
return false
}
// State dispatcher.
func ini_parser_state_machine(parser *ini_parser_t, event *ini_event_t) bool {
//trace("ini_parser_state_machine", "state:", parser.state.String())
switch parser.state {
case ini_PARSE_DOCUMENT_START_STATE:
return ini_parser_parse_document_start(parser, event)
case ini_PARSE_SECTION_FIRST_START_STATE:
return ini_parser_parse_section_start(parser, event, true)
case ini_PARSE_SECTION_START_STATE:
return ini_parser_parse_section_start(parser, event, false)
case ini_PARSE_SECTION_INHERIT_STATE:
return ini_parser_parse_section_inherit(parser, event)
case ini_PARSE_SECTION_ENTRY_STATE:
return ini_parser_parse_section_entry(parser, event)
case ini_PARSE_SECTION_KEY_STATE:
return ini_parser_parse_key(parser, event)
case ini_PARSE_SECTION_VALUE_STATE:
return ini_parser_parse_value(parser, event)
default:
panic("invalid parser state")
}
return false
}
func ini_parser_parse_document_start(parser *ini_parser_t, event *ini_event_t) bool {
token := peek_token(parser)
if token != nil {
if token.typ == ini_DOCUMENT_START_TOKEN {
skip_token(parser)
parser.states = append(parser.states, ini_PARSE_DOCUMENT_END_STATE)
parser.state = ini_PARSE_SECTION_FIRST_START_STATE
*event = ini_event_t{
typ: ini_DOCUMENT_START_EVENT,
start_mark: token.start_mark,
end_mark: token.end_mark,
}
return true
} else {
return ini_parser_set_parser_error(parser, "did not find expected <document-start>", token.start_mark)
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <document-start>", parser.mark)
}
}
// Parse the section:
func ini_parser_parse_section_start(parser *ini_parser_t, event *ini_event_t, first bool) bool {
//defer trace("ini_parser_parse_section_start")
token := peek_token(parser)
if token != nil {
if token.typ == ini_DOCUMENT_END_TOKEN {
parser.state = parser.states[len(parser.states)-1]
parser.states = parser.states[:len(parser.states)-1]
*event = ini_event_t{
typ: ini_DOCUMENT_END_EVENT,
start_mark: token.start_mark,
end_mark: token.end_mark,
}
} else {
if first && token.typ == ini_KEY_TOKEN {
parser.state = ini_PARSE_SECTION_ENTRY_STATE
*event = ini_event_t{
typ: ini_SCALAR_EVENT,
start_mark: token.start_mark,
end_mark: token.start_mark,
value: []byte(DEFAULT_SECTION),
tag: []byte(ini_STR_TAG),
}
} else if token.typ == ini_SECTION_START_TOKEN {
skip_token(parser)
token := peek_token(parser)
if token != nil {
if token.typ == ini_SCALAR_TOKEN {
skip_token(parser)
parser.state = ini_PARSE_SECTION_INHERIT_STATE
*event = ini_event_t{
typ: ini_SCALAR_EVENT,
start_mark: token.start_mark,
end_mark: token.end_mark,
value: []byte(token.value),
tag: []byte(ini_STR_TAG),
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <scalar>", token.start_mark)
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <scalar>", parser.mark)
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <section-start> or <key>", token.start_mark)
}
}
return true
} else {
return ini_parser_set_parser_error(parser, "did not find expected <document-end> or <section-start> or <key>", parser.mark)
}
}
// Parse the section:
func ini_parser_parse_section_inherit(parser *ini_parser_t, event *ini_event_t) bool {
//defer trace("ini_parser_parse_section_inherit")
// SECTION-INHERIT Token (:)
section_key := []byte(DEFAULT_SECTION)
start_mark := parser.mark
end_mark := parser.mark
token := peek_token(parser)
if token != nil {
if token.typ == ini_SECTION_INHERIT_TOKEN {
skip_token(parser)
token = peek_token(parser)
if token != nil {
if token.typ == ini_SCALAR_TOKEN {
start_mark = token.start_mark
end_mark = token.end_mark
section_key = token.value
skip_token(parser)
} else {
return ini_parser_set_parser_error(parser, "did not find expected <scalar>", token.start_mark)
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <scalar>", parser.mark)
}
}
parser.state = ini_PARSE_SECTION_ENTRY_STATE
*event = ini_event_t{
typ: ini_SECTION_INHERIT_EVENT,
start_mark: start_mark,
end_mark: end_mark,
value: section_key,
tag: []byte(ini_STR_TAG),
}
return true
} else {
return false
}
}
func ini_parser_parse_section_entry(parser *ini_parser_t, event *ini_event_t) bool {
token := peek_token(parser)
parser.state = ini_PARSE_SECTION_KEY_STATE
if token != nil && token.typ == ini_SECTION_ENTRY_TOKEN {
skip_token(parser)
*event = ini_event_t{
typ: ini_SECTION_ENTRY_EVENT,
start_mark: token.start_mark,
end_mark: token.end_mark,
tag: []byte(ini_SECTION_TAG),
}
} else {
*event = ini_event_t{
typ: ini_SECTION_ENTRY_EVENT,
start_mark: parser.mark,
end_mark: parser.mark,
tag: []byte(ini_SECTION_TAG),
}
}
return true
}
// Parse the productions:
// properties ::= (KEY1.KEY2 = VALUE)
//
func ini_parser_parse_key(parser *ini_parser_t, event *ini_event_t) bool {
token := peek_token(parser)
if token != nil {
if token.typ == ini_KEY_TOKEN {
skip_token(parser)
token := peek_token(parser)
if token != nil {
if token.typ == ini_SCALAR_TOKEN {
skip_token(parser)
parser.state = ini_PARSE_SECTION_VALUE_STATE
*event = ini_event_t{
typ: ini_SCALAR_EVENT,
start_mark: token.start_mark,
end_mark: token.end_mark,
value: token.value,
style: ini_style_t(token.style),
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <scalar>", token.start_mark)
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <scalar>", parser.mark)
}
} else {
if token.typ != ini_SECTION_START_TOKEN && token.typ != ini_DOCUMENT_END_TOKEN {
return ini_parser_set_parser_error(parser, "did not find expected <key> or <section-start>", token.start_mark)
} else {
parser.state = ini_PARSE_SECTION_START_STATE
*event = ini_event_t{
typ: ini_SECTION_ENTRY_EVENT,
start_mark: token.start_mark,
end_mark: token.end_mark,
}
}
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <key> or <section-start>", parser.mark)
}
return true
}
func ini_parser_parse_value(parser *ini_parser_t, event *ini_event_t) bool {
token := peek_token(parser)
if token != nil {
if token.typ == ini_MAP_TOKEN {
skip_token(parser)
parser.state = ini_PARSE_SECTION_KEY_STATE
*event = ini_event_t{
typ: ini_MAPPING_EVENT,
start_mark: token.start_mark,
end_mark: token.end_mark,
}
} else if token.typ == ini_VALUE_TOKEN {
skip_token(parser)
token := peek_token(parser)
if token != nil && token.typ == ini_SCALAR_TOKEN {
skip_token(parser)
parser.state = ini_PARSE_SECTION_KEY_STATE
*event = ini_event_t{
typ: ini_SCALAR_EVENT,
start_mark: token.start_mark,
end_mark: token.end_mark,
value: token.value,
style: ini_style_t(token.style),
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <scalar>", token.start_mark)
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <value> or <map>", token.start_mark)
}
} else {
return ini_parser_set_parser_error(parser, "did not find expected <value> or <map>", parser.mark)
}
return true
}
func ini_parser_process_empty_scalar(parser *ini_parser_t, event *ini_event_t, mark ini_mark_t) bool {
*event = ini_event_t{
typ: ini_SCALAR_EVENT,
start_mark: mark,
end_mark: mark,
value: nil, // Empty
tag: []byte(ini_NULL_TAG),
style: ini_style_t(ini_PLAIN_SCALAR_STYLE),
}
return true
}