-
Notifications
You must be signed in to change notification settings - Fork 17
/
grammarparser.go
311 lines (287 loc) · 7.15 KB
/
grammarparser.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
307
308
309
310
311
package lrparser
import (
"fmt"
"strconv"
"github.com/jmigpin/editor/util/parseutil/pscan"
)
type grammarParser struct {
ri *RuleIndex
sc *pscan.Scanner
}
func newGrammarParser(ri *RuleIndex) *grammarParser {
gp := &grammarParser{ri: ri}
gp.sc = pscan.NewScanner()
return gp
}
func (gp *grammarParser) parse(fset *FileSet) error {
gp.sc.SetSrc(fset.Src)
// rules are parsed into the ruleindex (parse result)
if p2, err := gp.parseRules(0); err != nil {
return gp.sc.SrcError(p2, err)
}
return nil
}
func (gp *grammarParser) parseRules(pos int) (int, error) {
return gp.sc.M.And(pos,
gp.sc.W.Loop(gp.sc.W.Or(
gp.parseSpacesOrComments,
gp.parseDefRule,
)),
gp.sc.W.Eof(),
)
}
func (gp *grammarParser) parseDefRule(pos int) (int, error) {
pos0 := pos
isStart := false
isNoPrint := false
vkName := gp.sc.NewValueKeeper()
vkItem := gp.sc.NewValueKeeper()
p2, err := gp.sc.M.And(pos,
gp.sc.W.Optional(gp.sc.W.And(
gp.sc.W.Sequence(defRuleStartSym),
func(pos int) (int, error) { isStart = true; return pos, nil },
)),
gp.sc.W.Optional(gp.sc.W.And(
gp.sc.W.Sequence(defRuleNoPrintSym),
func(pos int) (int, error) { isNoPrint = true; return pos, nil },
)),
vkName.WKeepValue(gp.parseName),
gp.parseOptSpacesOrComments,
gp.sc.W.FatalOnError("expecting '='",
gp.sc.W.Rune('='),
),
gp.parseOptSpacesOrComments,
vkItem.WKeepValue(gp.parseItemRule),
gp.parseOptSpacesOrComments,
gp.sc.W.FatalOnError("expecting close rule \";\"?",
gp.sc.W.Rune(';'),
),
)
if err != nil {
return p2, err
}
name := vkName.V.(string)
if gp.ri.has(name) {
return p2, fmt.Errorf("rule already defined: %v", name)
}
// setup
dr := &DefRule{name: name, isStart: isStart, isNoPrint: isNoPrint}
dr.setOnlyChild(vkItem.V.(Rule))
dr.SetPos(pos0, p2)
gp.ri.set(dr.name, dr)
return p2, nil
}
func (gp *grammarParser) parseName(pos int) (any, int, error) {
u := "[_a-zA-Z][_a-zA-Z0-9$]*"
return gp.sc.M.StringValue(pos, gp.sc.W.RegexpFromStartCached(u, 100))
}
//----------
func (gp *grammarParser) parseItemRule(pos int) (any, int, error) {
return gp.sc.M.OrValue(pos,
gp.parseIfRule,
gp.parseOrTreeRule, // precedence tree
)
}
//----------
func (gp *grammarParser) parseIfRule(pos int) (any, int, error) {
pos0 := pos
vk := gp.sc.NewValueKeepers(3)
p2, err := gp.sc.M.And(pos,
gp.sc.W.Sequence("if"),
gp.parseSpacesOrComments,
gp.sc.W.FatalOnError("expecting name",
vk[0].WKeepValue(gp.parseRefRule),
),
// then
gp.parseOptSpacesOrComments,
gp.sc.W.FatalOnError("expecting '?'",
gp.sc.W.Rune('?'),
),
gp.parseOptSpacesOrComments,
vk[1].WKeepValue(gp.parseItemRule),
// else
gp.parseOptSpacesOrComments,
gp.sc.W.FatalOnError("expecting ':'",
gp.sc.W.Rune(':'),
),
gp.parseOptSpacesOrComments,
vk[2].WKeepValue(gp.parseItemRule),
)
if err != nil {
return nil, p2, err
}
// setup
res := &IfRule{}
res.addChilds(vk[0].V.(Rule))
res.addChilds(vk[1].V.(Rule))
res.addChilds(vk[2].V.(Rule))
res.SetPos(pos0, p2)
return res, p2, nil
}
//----------
func (gp *grammarParser) parseOrTreeRule(pos int) (any, int, error) {
w := []Rule{}
if p2, err := gp.sc.M.LoopSep(pos,
gp.sc.W.OnValue(
// precedence tree construction ("and" is higher precedence than "or")
gp.parseAndTreeRule,
func(v any) { w = append(w, v.(Rule)) },
),
// separator
gp.sc.W.And(
gp.parseOptSpacesOrComments,
gp.sc.W.Rune('|'),
gp.parseOptSpacesOrComments,
),
); err != nil {
return nil, p2, err
} else {
if len(w) == 1 {
return w[0], p2, nil // just the "and" rule
}
res := &OrRule{}
res.childs2 = w
res.SetPos(pos, p2)
return res, p2, nil
}
}
func (gp *grammarParser) parseAndTreeRule(pos int) (any, int, error) {
w := []Rule{}
// NOTE: better than using a loopsep because it doesn't include the end spaces
if p2, err := gp.sc.M.Loop(pos, gp.sc.W.And(
gp.parseOptSpacesOrComments,
gp.sc.W.OnValue(
gp.parseBasicItemRule,
func(v any) { w = append(w, v.(Rule)) },
),
)); err != nil {
return nil, p2, err
} else {
if len(w) == 1 {
return w[0], p2, nil // just the "basic" rule
}
res := &AndRule{}
res.childs2 = w
res.SetPos(pos, p2)
return res, p2, nil
}
}
//----------
func (gp *grammarParser) parseBasicItemRule(pos int) (any, int, error) {
return gp.sc.M.OrValue(pos,
gp.parseProcRule,
gp.parseRefRule,
gp.parseStringRule,
gp.parseParenRule,
)
}
func (gp *grammarParser) parseProcRule(pos int) (any, int, error) {
vkName := gp.sc.NewValueKeeper()
args := []ProcRuleArg{}
if p2, err := gp.sc.M.And(pos,
gp.sc.W.Sequence("@"),
vkName.WKeepValue(gp.parseName),
gp.sc.W.Rune('('),
gp.parseOptSpacesOrComments,
gp.sc.W.LoopSep(
gp.sc.W.OnValue(
gp.sc.W.OrValue(
gp.parseItemRule,
gp.sc.M.IntValue,
),
func(v any) { args = append(args, v.(ProcRuleArg)) },
),
// separator
gp.sc.W.And(
gp.parseOptSpacesOrComments,
gp.sc.W.Rune(','),
gp.parseOptSpacesOrComments,
),
),
gp.parseOptSpacesOrComments,
gp.sc.W.Rune(')'),
); err != nil {
return nil, p2, err
} else {
res := &ProcRule{}
res.name = vkName.V.(string)
res.args = args
res.SetPos(pos, p2)
return res, p2, nil
}
}
func (gp *grammarParser) parseRefRule(pos int) (any, int, error) {
if v, p2, err := gp.parseName(pos); err != nil {
return nil, p2, err
} else {
res := &RefRule{name: v.(string)}
res.SetPos(pos, p2)
return res, p2, nil
}
}
func (gp *grammarParser) parseStringRule(pos int) (any, int, error) {
if v, p2, err := gp.sc.M.StringValue(pos,
gp.sc.W.StringSection("\"", '\\', true, 1000, false),
); err != nil {
return nil, p2, err
} else {
// needed: ex: transforms "\n" (2 runes) into a single '\n'
str := v.(string)
u, err := strconv.Unquote(str)
if err != nil {
return nil, pos, gp.sc.EnsureFatalError(err)
}
sr := &StringRule{}
sr.runes = []rune(u)
sr.SetPos(pos, p2)
return sr, p2, nil
}
}
func (gp *grammarParser) parseParenRule(pos int) (any, int, error) {
vk := gp.sc.NewValueKeepers(2)
if p2, err := gp.sc.M.And(pos,
gp.sc.W.Rune('('),
gp.parseOptSpacesOrComments,
vk[0].WKeepValue(gp.parseItemRule),
gp.parseOptSpacesOrComments,
gp.sc.W.Rune(')'),
gp.sc.W.Optional(vk[1].WKeepValue(gp.sc.W.RuneValue(gp.sc.W.Or(
gp.sc.W.Rune(rune(parenRTOptional)),
gp.sc.W.Rune(rune(parenRTZeroOrMore)),
gp.sc.W.Rune(rune(parenRTOneOrMore)),
gp.sc.W.Rune(rune(parenRTStrMid)),
gp.sc.W.Rune(rune(parenRTStrOr)),
gp.sc.W.Rune(rune(parenRTStrOrRange)),
gp.sc.W.Rune(rune(parenRTStrOrNeg)),
)))),
); err != nil {
return nil, p2, err
} else {
res := &ParenRule{}
res.setOnlyChild(vk[0].V.(Rule))
if vk[1].V != nil {
res.typ = parenRType(vk[1].V.(rune))
}
res.SetPos(pos, p2)
return res, p2, nil
}
}
//----------
func (gp *grammarParser) parseOptSpacesOrComments(pos int) (int, error) {
return gp.sc.M.Optional(pos, gp.parseSpacesOrComments)
}
func (gp *grammarParser) parseSpacesOrComments(pos int) (int, error) {
return gp.sc.M.Loop(pos, gp.sc.W.Or(
gp.sc.W.Spaces(true, 0),
gp.parseComments,
))
}
func (gp *grammarParser) parseComments(pos int) (int, error) {
return gp.sc.M.And(pos,
gp.sc.W.Or(
gp.sc.W.Rune('#'),
gp.sc.W.Sequence("//"),
),
gp.sc.W.ToNLOrErr(true, 0),
)
}