-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcfgrammar_fmr.go
179 lines (170 loc) · 3.11 KB
/
cfgrammar_fmr.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
package fmr
import (
"fmt"
"math/big"
"unicode"
)
func (p *parser) semanticFn() (f *FMR, err error) {
p.ws()
f = &FMR{}
if f.Fn, err = p.funcName(); err != nil {
return
}
if f.Args, err = p.funcArgs(); err != nil {
return
}
p.ws()
return
}
func (p *parser) funcName() (string, error) {
var ret []rune
var prev rune = eof
var r rune
first := true
Loop:
for {
switch r = p.next(); {
case unicode.IsLetter(r) || r == '_':
ret = append(ret, r)
case unicode.IsDigit(r) && !first:
ret = append(ret, r)
case r == '.' && prev != '.' && !first:
ret = append(ret, r)
default:
p.backup()
break Loop
}
first = false
prev = r
}
if len(ret) == 0 {
return "", fmt.Errorf("%s : no funcName", p.posInfo())
}
p.ws()
return string(ret), nil
}
func (p *parser) funcArgs() (args []*Arg, err error) {
if err = p.eat('('); err != nil {
return
}
var r rune
var arg *Arg
for {
p.ws()
switch r = p.peek(); {
case r == '@':
if arg, err = p.contextArg(); err != nil {
return
}
case r == '$':
if arg, err = p.idxArg(); err != nil {
return
}
case r == '"':
if arg, err = p.strArg(); err != nil {
return
}
case unicode.IsDigit(r):
if arg, err = p.numArg(false); err != nil {
return
}
case r == '-':
if err = p.eat('-'); err != nil {
return
}
if arg, err = p.numArg(true); err != nil {
return
}
default:
if arg, err = p.fArg(); err != nil {
return
}
}
args = append(args, arg)
if r == ',' {
continue
} else {
p.ws()
r = p.next()
if r == ',' {
continue
} else if r == ')' {
break
} else {
err = fmt.Errorf("%s : unexpected semantic args", p.posInfo())
return
}
}
}
return
}
func (p *parser) contextArg() (arg *Arg, err error) {
if err = p.eat('@'); err != nil {
return
}
arg = &Arg{"context", "@"}
return
}
func (p *parser) idxArg() (arg *Arg, err error) {
if err = p.eat('$'); err != nil {
return
}
var idx int
if idx, err = p.getInt(); err != nil {
return
}
arg = &Arg{"index", idx}
return
}
func (p *parser) strArg() (*Arg, error) {
var text string
var err error
if _, text, err = p.terminal(); err != nil {
return nil, err
}
return &Arg{"string", text}, nil
}
func (p *parser) numArg(neg bool) (*Arg, error) {
var ret []rune
hasDot := false
for r := p.next(); ; r = p.next() {
if unicode.IsDigit(r) {
ret = append(ret, r)
} else if r == '.' {
if hasDot {
return nil, fmt.Errorf("%s : unexpected dot", p.posInfo())
}
hasDot = true
ret = append(ret, r)
} else {
break
}
}
if len(ret) == 0 {
return nil, fmt.Errorf("%s : number expected", p.posInfo())
}
p.backup()
if neg {
ret = append([]rune{'-'}, ret...)
}
if hasDot {
n := new(big.Float)
if _, err := fmt.Sscan(string(ret), n); err != nil {
return nil, err
}
return &Arg{"float", n}, nil
}
n := new(big.Int)
if _, err := fmt.Sscan(string(ret), n); err != nil {
return nil, err
}
return &Arg{"int", n}, nil
}
func (p *parser) fArg() (*Arg, error) {
var f *FMR
var err error
if f, err = p.semanticFn(); err != nil {
return nil, err
}
return &Arg{"func", f}, nil
}