-
Notifications
You must be signed in to change notification settings - Fork 6
/
numbers.go
266 lines (235 loc) · 5.59 KB
/
numbers.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
package parser
import (
"encoding/json"
"strings"
"github.com/HexmosTech/gabs/v2"
"github.com/HexmosTech/lama2/utils"
)
// A Number consists of a mandatory integer part,
// and optional Fraction and Exponent parts. The Number
// method "collects" these three elements, converts them
// into a json.Number() type, and finally returns the
// Number wrapped within a gabs Container
func (p *Lama2Parser) Number() (*gabs.Container, error) {
intPart, e1 := p.Match([]string{"Integer"})
fracPart, e2 := p.Match([]string{"Fraction"})
expPart, e3 := p.Match([]string{"Exponent"})
if e1 != nil {
return nil, e1
}
f := ""
e := ""
if e2 == nil {
f = fracPart.Data().(string)
}
if e3 == nil {
e = expPart.Data().(string)
}
temp := gabs.New()
if f == "" && e == "" {
num := json.Number(intPart.Data().(string))
temp.Set(num)
} else if e == "" {
iStr := intPart.Data().(string)
fStr := fracPart.Data().(string)
temp.Set(json.Number(iStr + fStr))
} else {
iStr := intPart.Data().(string)
temp.Set(json.Number(iStr + f + e))
}
return temp, nil
}
// An Exponent consists of mandatory 'e' or 'E',
// optional Sign, followed by Digits
func (p *Lama2Parser) Exponent() (*gabs.Container, error) {
pows, e := p.CharClass("eE")
if e != nil {
return nil, e
}
sign, e2 := p.Match([]string{"Sign"})
if e2 != nil {
sign = gabs.New()
sign.Set("", "value")
}
d, e3 := p.Match([]string{"Digits"})
if e3 != nil {
return nil, e3
}
dVal := d.S("value").Data().(string)
temp := gabs.New()
temp.Set(string(pows) + sign.S("value").Data().(string) + dVal)
return temp, nil
}
func (p *Lama2Parser) Fraction() (*gabs.Container, error) {
s, e := p.Match([]string{"FractionRule1"})
if e != nil {
return nil, e
}
temp := gabs.New()
temp.Set(s.Data().(string))
return temp, nil
}
// A Fraction consists of mandatory "." (dot),
// followed by Digits.
func (p *Lama2Parser) FractionRule1() (*gabs.Container, error) {
s := make([]string, 0)
r, e := p.CharClass(".")
if e != nil {
return nil, e
}
s = append(s, string(r))
r2, e2 := p.Match([]string{"Digits"})
if e2 != nil {
return nil, e2
}
digs := r2.Search("value").Data().(string)
s = append(s, digs)
temp := gabs.New()
temp.Set(strings.Join(s, ""))
return temp, nil
}
func (p *Lama2Parser) Integer() (*gabs.Container, error) {
s, e := p.Match([]string{
"IntegerRule4",
"IntegerRule3", "IntegerRule2", "IntegerRule1",
})
temp := gabs.New()
if e == nil {
temp.Set(s.Data().(string))
return temp, nil
}
return s, e
}
// InterRule1 matches a Digit
func (p *Lama2Parser) IntegerRule1() (*gabs.Container, error) {
s := make([]string, 0)
r, e := p.Match([]string{"Digit"})
if e == nil {
s = append(s, r.Search("value").Data().(string))
} else {
return nil, e
}
temp := gabs.New()
temp.Set(strings.Join(s, ""))
return temp, e
}
// IntegerRule2 matches 1-9 mandatorily, and then
// tries to follow it with Digits
func (p *Lama2Parser) IntegerRule2() (*gabs.Container, error) {
s := make([]string, 0)
iOneNine, e := p.Match([]string{"OneNine"})
if e == nil {
s = append(s, iOneNine.Search("value").Data().(string))
} else {
return nil, e
}
iDigits, e := p.Match([]string{"Digits"})
if e == nil {
s = append(s, iDigits.Search("value").Data().(string))
}
temp := gabs.New()
temp.Set(strings.Join(s, ""))
return temp, nil
}
// IntegerRule3 starts with a mandatory Sign, and
// follows with IntegerRule1 (Digit)
func (p *Lama2Parser) IntegerRule3() (*gabs.Container, error) {
s := make([]string, 0)
r, e := p.Match([]string{"Sign"})
if e == nil {
rVal := r.Search("value").Data().(string)
if rVal == "-" {
s = append(s, rVal)
} else {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Expected negative sign",
[]string{})
}
} else {
return nil, e
}
r, e = p.Match([]string{"IntegerRule1"})
if e == nil {
rVal := r.Data().(string)
s = append(s, rVal)
} else {
return nil, e
}
temp := gabs.New()
temp.Set(strings.Join(s, ""))
return temp, nil
}
// IntegerRule4 starts with a mandatory Sign, and
// follows with IntegerRule2
func (p *Lama2Parser) IntegerRule4() (*gabs.Container, error) {
s := make([]string, 0)
r, e := p.Match([]string{"Sign"})
if e == nil {
rVal := r.Search("value").Data().(string)
if rVal == "-" {
s = append(s, rVal)
} else {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Expected negative sign",
[]string{})
}
} else {
return nil, e
}
r, e = p.Match([]string{"IntegerRule2"})
if e == nil {
rVal := r.Data().(string)
s = append(s, rVal)
} else {
return nil, e
}
temp := gabs.New()
temp.Set(strings.Join(s, ""))
return temp, nil
}
func (p *Lama2Parser) Digits() (*gabs.Container, error) {
s := make([]string, 0)
r, e := p.Match([]string{"Digit"})
if e == nil {
s = append(s, r.Search("value").Data().(string))
} else {
return nil, e
}
for {
r, e = p.Match([]string{"Digit"})
if e == nil {
s = append(s, r.Search("value").Data().(string))
} else {
break
}
}
temp := gabs.New()
temp.Set(strings.Join(s, ""), "value")
return temp, nil
}
func (p *Lama2Parser) Digit() (*gabs.Container, error) {
s, e := p.CharClass("0")
temp := gabs.New()
if e == nil {
temp.Set(string(s), "value")
return temp, nil
}
d, e2 := p.Match([]string{"OneNine"})
return d, e2
}
func (p *Lama2Parser) OneNine() (*gabs.Container, error) {
s, e := p.CharClass("1-9")
temp := gabs.New()
if e == nil {
temp.Set(string(s), "value")
return temp, nil
}
return nil, e
}
func (p *Lama2Parser) Sign() (*gabs.Container, error) {
s, e := p.CharClass("-+")
temp := gabs.New()
if e == nil {
temp.Set(string(s), "value")
return temp, e
}
return nil, e
}