-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnumber.go
58 lines (53 loc) · 942 Bytes
/
number.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
package matcher
import (
"github.com/viant/parsly"
)
type number struct{}
//TokenMatch matches a number
func (n *number) Match(cursor *parsly.Cursor) (matched int) {
input := cursor.Input
pos := cursor.Pos
if isSing := input[pos] == '-'; isSing {
pos++
}
size := len(input)
hasDecPoint := false
hasExponent := false
valid := false
var i int
outer:
for i = pos; i < size; i++ {
switch input[i] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
valid = true
case 'e', 'E':
if !valid || hasExponent {
return 0
}
hasExponent = true
if i+1 < size {
switch input[i+1] {
case '+', '-':
i++
}
}
valid = false
case '.':
if !valid || hasDecPoint {
return 0
}
valid = false
hasDecPoint = true
default:
break outer
}
}
if !valid {
return 0
}
return i - cursor.Pos
}
//NewNumber creates a number matcher
func NewNumber() *number {
return &number{}
}