-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.go
86 lines (77 loc) · 1.78 KB
/
grammar.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
package flare
import (
"github.com/zyedidia/gpeg/charset"
"github.com/zyedidia/gpeg/isa"
p "github.com/zyedidia/gpeg/pattern"
)
var (
alpha = p.Set(charset.Range('A', 'Z').Add(charset.Range('a', 'z')))
alnum = p.Set(charset.Range('A', 'Z').Add(charset.Range('a', 'z')).Add(charset.Range('0', '9')))
digit = p.Set(charset.Range('0', '9'))
xdigit = p.Set(charset.Range('0', '9').Add(charset.Range('A', 'F')).Add(charset.Range('a', 'f')))
space = p.Set(charset.New([]byte{9, 10, 11, 12, 13, ' '}))
dec_num = p.Plus(digit)
hex_num = p.Concat(
p.Literal("0"),
p.Set(charset.New([]byte{'x', 'X'})),
p.Plus(xdigit),
)
oct_num = p.Concat(
p.Literal("0"),
p.Plus(p.Set(charset.New([]byte{'0', '7'}))),
)
integer = p.Concat(
p.Or(
hex_num,
oct_num,
dec_num,
),
)
float = p.Concat(
p.Optional(p.Set(charset.New([]byte{'+', '-'}))),
p.Or(
p.Concat(
p.Concat(
p.Or(
p.Concat(
p.Star(digit),
p.Literal("."),
p.Plus(digit),
),
p.Concat(
p.Plus(digit),
p.Literal("."),
p.Star(digit),
),
),
),
p.Optional(p.Concat(
p.Set(charset.New([]byte{'e', 'E'})),
p.Optional(p.Set(charset.New([]byte{'+', '-'}))),
p.Plus(digit),
)),
),
p.Concat(
p.Plus(digit),
p.Set(charset.New([]byte{'e', 'E'})),
p.Optional(p.Set(charset.New([]byte{'+', '-'}))),
p.Plus(digit),
),
),
)
word = p.Concat(
p.Or(alpha, p.Literal("_")),
p.Star(p.Or(alnum, p.Literal("_"))),
)
)
func wordMatch(words ...string) p.Pattern {
m := make(map[string]struct{})
for _, w := range words {
m[w] = struct{}{}
}
word = p.Concat(
p.Or(alpha, p.Literal("_")),
p.Star(p.Or(alnum, p.Literal("_"), p.Literal("."), p.Literal("-"))),
)
return p.Check(word, isa.MapChecker(m))
}