-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.go
157 lines (136 loc) · 2.73 KB
/
lexer.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
package calcu
import (
"errors"
"fmt"
"regexp"
"github.com/shopspring/decimal"
)
const (
invalid = iota - 1
eof = 0
)
var (
rules = []rule{
{regexp.MustCompile("^[_a-zA-Z][_a-zA-Z0-9]*"), IDENT},
{regexp.MustCompile("^[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?"), NUM},
{regexp.MustCompile(`^"[^"]*"`), LITERALSTR},
}
)
type rule struct {
re *regexp.Regexp
token int
}
type lexer struct {
in string
rules []rule
um UnitManager
root Node
lastError error
}
func newLexer(expr string) *lexer {
return &lexer{
in: expr,
rules: rules,
um: StdUm,
}
}
func (l *lexer) Lex(lval *exprSymType) int {
// Skip spaces.
for len(l.in) > 0 && isSpace(l.in[0]) {
l.in = l.in[1:]
}
// Check if the input has ended.
if len(l.in) == 0 {
lval.token = eof
return eof
}
// Try math on unit first
if n, ok := l.um.Peek(l.in); ok {
str := l.in[:n]
l.in = l.in[n:]
if str[0] == '(' {
// remove brackets
str = str[1 : len(str)-1]
}
lval.str = str
lval.token = UNIT
return UNIT
}
for _, r := range rules {
str := r.re.FindString(l.in)
if str != "" {
l.in = l.in[len(str):]
switch r.token {
case IDENT:
lval.str = str
case NUM:
lval.str = str
case LITERALSTR:
// remove quote
str = str[1 : len(str)-1]
r.token = l.lexLiteralStr(str, lval)
}
lval.token = r.token
return r.token
}
}
// Otherwise return the next letter.
lval.token = invalid
ret := int(l.in[0])
l.in = l.in[1:]
return ret
}
func (l *lexer) lexLiteralStr(s string, lval *exprSymType) int {
if ok := l.um.IsUnit(s); ok {
lval.str = s
return UNIT
}
if _, err := NewMeasureValueFromString(s); err == nil {
lval.str = s
return LITERALMV
}
lval.str = s
return LITERALSTR
}
func (l *lexer) Error(e string) {
l.lastError = errors.New(e)
}
func (l *lexer) setErr(err error) {
l.lastError = err
}
func (l *lexer) setRoot(node Node) {
l.root = node
}
func isSpace(c byte) bool {
return c == ' ' || c == '\t' || c == '\n'
}
func startWithSeparator(s string) bool {
for len(s) > 0 && isSpace(s[0]) {
s = s[1:]
}
if len(s) == 0 {
return true
}
c := s[0]
return c == ',' || c == ';' || c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/'
}
func NewMeasureValueFromString(s string) (*MeasureValue, error) {
l := newLexer(s)
var lvals []exprSymType
for {
var lval exprSymType
ret := l.Lex(&lval)
if ret == eof {
break
}
if lval.token != invalid {
lvals = append(lvals, lval)
}
}
if len(lvals) != 2 || (lvals[0].token != NUM && lvals[1].token != UNIT) {
return nil, fmt.Errorf("invalid measure value: %s", s)
}
num, unit := lvals[0].str, lvals[1].str
d, _ := decimal.NewFromString(num)
return &MeasureValue{um: StdUm, unit: unit, value: d}, nil
}