-
Notifications
You must be signed in to change notification settings - Fork 5
/
tree.go
507 lines (456 loc) · 14 KB
/
tree.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package abnf
import (
"fmt"
"reflect"
"strconv"
"github.com/elimity-com/abnf/definition"
"github.com/elimity-com/abnf/operators"
)
// NewRuleSet converts given raw data to a set of ABNF rules.
func NewRuleSet(rawABNF []byte) RuleSet {
rawRuleList := definition.Rulelist(rawABNF).Best()
ruleSet := make(RuleSet)
for _, line := range rawRuleList.Children {
if line.Contains("rule") {
rule := parseRule(line)
ruleSet[rule.name] = rule
}
}
return ruleSet
}
// RuleList is a list of ABNF rules.
type RuleList []Rule
// RuleSet converts a rule list to a set.
func (list RuleList) RuleSet() RuleSet {
ruleSet := make(RuleSet)
for _, rule := range list {
ruleSet[rule.Name()] = rule
}
return ruleSet
}
// RuleSet is a set of ABNF rules.
type RuleSet map[string]Rule
// RuleList converts a rule set to a list.
func (set RuleSet) RuleList() RuleList {
var ruleList RuleList
for _, rule := range set {
ruleList = append(ruleList, rule)
}
return ruleList
}
// Rule represents an ABNF rule.
type Rule struct {
name string
operator Operator
}
// Equals checks whether both rule trees are equal to each other.
func (r Rule) Equals(other Rule) error {
if r.name != other.name {
return fmt.Errorf("names do not match: expected %s, got %s", r.name, other.name)
}
return r.operator.equals(other.operator)
}
// Name returns the name of the rule.
func (r Rule) Name() string {
return r.name
}
// parseRule converts a raw rule node to a (more) readable one.
// ABNF: rule = rulename defined-as elements c-nl
func parseRule(rawNode *operators.Node) Rule {
return Rule{
name: rawNode.GetSubNode("rulename").String(),
operator: parseAlternation(rawNode.GetSubNode("alternation")),
}
}
// Operator represents a node of a rule.
type Operator interface {
// Key returns that key (name) of the operator.s
Key() string
// equals checks whether this operator equals given other.
equals(other Operator) error
codeGeneratorNode // code generator
parserGeneratorNode // parser generator
}
// AlternationOperator represents an alternation node of a rule.
type AlternationOperator struct {
key string
subOperators []Operator
}
func (alt AlternationOperator) Key() string {
return alt.key
}
func (alt AlternationOperator) equals(other Operator) error {
otherAlt, ok := other.(AlternationOperator)
if !ok {
return fmt.Errorf("other is not of the same type: %s", reflect.TypeOf(other))
}
if alt.key != otherAlt.key {
return fmt.Errorf("keys do not match: expected %s, got %s", alt.key, otherAlt.key)
}
if len(alt.subOperators) != len(otherAlt.subOperators) {
return fmt.Errorf("lenght of sub operators do not match: expected %d, got %d", len(alt.subOperators), len(otherAlt.subOperators))
}
for i, subOperator := range alt.subOperators {
if err := subOperator.equals(otherAlt.subOperators[i]); err != nil {
return err
}
}
return nil
}
// parseAlternation converts a raw (nested) alternation node to a (more) readable one.
// ABNF: alternation = concatenation *(*c-wsp "/" *c-wsp concatenation)
func parseAlternation(rawNode *operators.Node) Operator {
// an alternation has at least one concatenation node
subOperators := []Operator{
parseConcatenation(rawNode.GetSubNode("concatenation")),
}
// get all other concatenation nodes
for _, other := range rawNode.GetSubNodesBefore(`*c-wsp "/" *c-wsp concatenation`, "(", "[") {
if rawConcat := other.GetNode("concatenation"); rawConcat != nil {
subOperators = append(subOperators, parseConcatenation(rawConcat))
}
}
// not need to return an alternation of one element
if len(subOperators) == 1 {
return subOperators[0]
}
return AlternationOperator{
key: rawNode.String(),
subOperators: subOperators,
}
}
// ConcatenationOperator represents a concatenation node of a rule.
type ConcatenationOperator struct {
key string
subOperators []Operator
}
func (concat ConcatenationOperator) Key() string {
return concat.key
}
func (concat ConcatenationOperator) equals(other Operator) error {
otherConcat, ok := other.(ConcatenationOperator)
if !ok {
return fmt.Errorf("other is not of the same type: %s", reflect.TypeOf(other))
}
if concat.key != otherConcat.key {
return fmt.Errorf("keys do not match: expected %s, got %s", concat.key, otherConcat.key)
}
if len(concat.subOperators) != len(otherConcat.subOperators) {
return fmt.Errorf("lenght of sub operators do not match: expected %d, got %d", len(concat.subOperators), len(otherConcat.subOperators))
}
for i, subOperator := range concat.subOperators {
if err := subOperator.equals(otherConcat.subOperators[i]); err != nil {
return err
}
}
return nil
}
// parseConcatenation converts a raw (nested) concatenation node to a (more) readable one.
// ABNF: concatenation = repetition *(1*c-wsp repetition)
func parseConcatenation(rawNode *operators.Node) Operator {
// a concatenation has at least one repetition node
subOperators := []Operator{
parseRepetition(rawNode.GetSubNode("repetition")),
}
// get all other repetition nodes
for _, other := range rawNode.GetSubNodesBefore(`1*c-wsp repetition`, "(", "[") {
if rawConcat := other.GetNode("repetition"); rawConcat != nil {
subOperators = append(subOperators, parseRepetition(rawConcat))
}
}
// not need to return a concatenation of one element
if len(subOperators) == 1 {
return subOperators[0]
}
return ConcatenationOperator{
key: rawNode.String(),
subOperators: subOperators,
}
}
// RepetitionOperator represents a repetition node of a rule.
type RepetitionOperator struct {
key string
min, max int
subOperator Operator
}
func (rep RepetitionOperator) Key() string {
return rep.key
}
func (rep RepetitionOperator) equals(other Operator) error {
otherRep, ok := other.(RepetitionOperator)
if !ok {
return fmt.Errorf("other is not of the same type: %s", reflect.TypeOf(other))
}
if rep.key != otherRep.key {
return fmt.Errorf("keys do not match: expected %s, got %s", rep.key, otherRep.key)
}
if rep.min != otherRep.min {
return fmt.Errorf("min subValues do not match: expected %d, got %d", rep.min, otherRep.min)
}
if rep.max != otherRep.max {
return fmt.Errorf("max subValues do not match: expected %d, got %d", rep.max, otherRep.max)
}
return rep.subOperator.equals(otherRep.subOperator)
}
// parseRepetition converts a raw (nested) repetition node to a (more) readable one.
// ABNF: repetition = [repeat] element
func parseRepetition(rawNode *operators.Node) Operator {
if rawNode.Children[0].IsEmpty() {
// no repeat
return parseElement(rawNode.GetSubNode("element"))
}
min, max := parseRepeat(rawNode.GetSubNode("repeat"))
return RepetitionOperator{
key: rawNode.String(),
min: min, max: max,
subOperator: parseElement(rawNode.GetSubNode("element")),
}
}
// parseRepetition converts a raw (nested) repetition node to a two their respective min and max values.
// ABNF: repeat = 1*DIGIT / (*DIGIT "*" *DIGIT)
func parseRepeat(rawNode *operators.Node) (int, int) {
if rawNode.Children[0].Key == "1*DIGIT" {
i, _ := strconv.Atoi(rawNode.String())
return i, i
}
min, max, asterisk := 0, -1, false
for _, child := range rawNode.Children[0].Children {
if child.Key == "*DIGIT" {
if child.IsEmpty() {
continue
}
if !asterisk {
min, _ = strconv.Atoi(child.String())
} else {
max, _ = strconv.Atoi(child.String())
}
} else {
asterisk = true
}
}
return min, max
}
// parseRepetition converts a raw (nested) element node to a (more) readable one.
// ABNF: element = rulename / group / option / char-val / num-val / prose-val
func parseElement(rawNode *operators.Node) Operator {
switch rawNode := rawNode.Children[0]; rawNode.Key {
case "rulename":
return parseRuleName(rawNode)
case "group":
return parseGroup(rawNode)
case "option":
return parseOption(rawNode)
case "char-val":
return parseCharacterValue(rawNode)
case "num-val":
return parseNumericValue(rawNode)
case "prose-val":
panic("not implemented")
default:
return nil
}
}
// RuleNameOperator represents a rule name node of a rule.
type RuleNameOperator struct {
key string
}
func (name RuleNameOperator) Key() string {
return name.key
}
func (name RuleNameOperator) equals(other Operator) error {
otherName, ok := other.(RuleNameOperator)
if !ok {
return fmt.Errorf("other is not of the same type: %s", reflect.TypeOf(other))
}
if name.key != otherName.key {
return fmt.Errorf("keys do not match: expected %s, got %s", name.key, otherName.key)
}
return nil
}
// parseRuleName converts a raw (nested) rulename node to a (more) readable one.
// ABNF: rulename = ALPHA *(ALPHA / DIGIT / "-")
func parseRuleName(rawNode *operators.Node) Operator {
return RuleNameOperator{
key: rawNode.String(),
}
}
// parseGroup converts a raw (nested) group node to a (more) readable one.
// ABNF: group = "(" *c-wsp alternation *c-wsp ")"
func parseGroup(rawNode *operators.Node) Operator {
return parseAlternation(rawNode.GetSubNode("alternation"))
}
// OptionOperator represents an option node of a rule.
type OptionOperator struct {
key string
subOperator Operator
}
func (opt OptionOperator) Key() string {
return opt.key
}
func (opt OptionOperator) equals(other Operator) error {
otherOpt, ok := other.(OptionOperator)
if !ok {
return fmt.Errorf("other is not of the same type: %s", reflect.TypeOf(other))
}
if opt.key != otherOpt.key {
return fmt.Errorf("keys do not match: expected %s, got %s", opt.key, otherOpt.key)
}
return opt.subOperator.equals(otherOpt.subOperator)
}
// parseOption converts a raw (nested) option node to a (more) readable one.
// ABNF: option = "[" *c-wsp alternation *c-wsp "]"
func parseOption(rawNode *operators.Node) Operator {
rawAlternation := rawNode.GetSubNode("alternation")
return OptionOperator{
key: rawNode.String(),
subOperator: parseAlternation(rawAlternation),
}
}
// CharacterValueOperator represents a character value node of a rule.
type CharacterValueOperator struct {
value string
}
func (value CharacterValueOperator) Key() string {
return value.value
}
func (value CharacterValueOperator) equals(other Operator) error {
otherValue, ok := other.(CharacterValueOperator)
if !ok {
return fmt.Errorf("other is not of the same type: %s", reflect.TypeOf(other))
}
if value.value != otherValue.value {
return fmt.Errorf("subValues do not match: expected %s, got %s", value.value, otherValue.value)
}
return nil
}
// parseCharacterValue converts a raw (nested) character value node to a (more) readable one.
// ABNF: char-val = DQUOTE *(%x20-21 / %x23-7E) DQUOTE
func parseCharacterValue(rawNode *operators.Node) Operator {
rawValue := rawNode.GetSubNode("*(%x20-21 / %x23-7E)")
return CharacterValueOperator{
value: rawValue.String(),
}
}
// NumericValueOperator represents a numeric value node of a rule.
type NumericValueOperator struct {
key string
hyphen, points bool
numericType numericType
value []string
}
func (value NumericValueOperator) Key() string {
return value.key
}
func (value NumericValueOperator) equals(other Operator) error {
otherValue, ok := other.(NumericValueOperator)
if !ok {
return fmt.Errorf("other is not of the same type: %s", reflect.TypeOf(other))
}
if value.key != otherValue.key {
return fmt.Errorf("keys do not match: expected %s, got %s", value.key, otherValue.key)
}
if value.numericType != otherValue.numericType {
return fmt.Errorf(
"numeric types do not match: expected %s, got %s",
value.numericType, otherValue.numericType,
)
}
if len(value.value) != len(otherValue.value) {
return fmt.Errorf(
"lenght of values do not match: expected %d, got %d",
len(value.value), len(otherValue.value),
)
}
if value.hyphen != otherValue.hyphen || value.points != otherValue.points {
return fmt.Errorf(
"value types do not match: expected -%t .%t, got -%t .%t",
value.hyphen, value.points, otherValue.hyphen, otherValue.points,
)
}
for i, part := range value.value {
if part != otherValue.value[i] {
return fmt.Errorf(
"value parts %d do not match: expected %s, got %s", i, part, otherValue.value[i])
}
}
return nil
}
// parseCharacterValue converts a raw (nested) numeric value node to a (more) readable one.
// ABNF: num-val = "%" (bin-val / dec-val / hex-val)
func parseNumericValue(rawNode *operators.Node) Operator {
rawValue := rawNode.Children[1].Children[0]
var numericType numericType
switch rawValue.Key {
case "bin-val":
numericType = binary
case "dec-val":
numericType = decimal
case "hex-val":
numericType = hexadecimal
}
values, hasHyphen, hasPoints := make([]string, 0), false, false
for _, child := range rawValue.Children {
if rawNumericValue := child.GetNode(string(numericType)); rawNumericValue != nil {
// encountered hyphen
if child.Contains("-") {
hasHyphen = true
}
// encountered point(s)
if child.Contains(".") {
hasPoints = true
}
if hasHyphen {
values = append(values, rawNumericValue.String())
} else if hasPoints {
for _, part := range child.GetSubNodes(string(numericType)) {
values = append(values, part.String())
}
} else {
values = []string{rawNumericValue.String()}
}
}
}
return NumericValueOperator{
key: rawNode.String(),
hyphen: hasHyphen,
points: hasPoints,
numericType: numericType,
value: values,
}
}
type numericType string
const (
binary numericType = "1*BIT"
decimal numericType = "1*DIGIT"
hexadecimal numericType = "1*HEXDIG"
)
func (value NumericValueOperator) toIntegers() [][]int {
bytes := make([][]int, len(value.value))
switch value.numericType {
case binary:
for i, part := range value.value {
raw, _ := strconv.ParseInt(part, 2, 64)
bytes[i] = []int{int(raw)}
}
case decimal:
for i, part := range value.value {
raw, _ := strconv.Atoi(part)
bytes[i] = []int{raw}
}
case hexadecimal:
for i, part := range value.value {
bytes[i] = hexStringToBytes(part)
}
default:
panic("invalid numeric type")
}
return bytes
}
func hexStringToBytes(hexStr string) []int {
n, _ := strconv.ParseInt(hexStr, 16, 64)
b := make([]int, (len(hexStr)+1)/2)
for i := range b {
b[i] = int(byte(n >> uint64(8*(len(b)-i-1))))
}
return b
}