-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
167 lines (153 loc) · 3.61 KB
/
parser.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
package xpression
import (
"fmt"
)
func Parse(path []byte) ([]*Token, error) {
tokens, err := lexer(path)
if err != nil {
return nil, err
}
return parser(tokens)
}
func lexer(path []byte) ([]*Token, error) {
path = path[:trimSpaces(path)]
l := len(path)
i := 0
tokens := make([]*Token, 0)
var tok *Token
var err error
prevOperator := opPlus
for i < l {
i, tok, err = readNextToken(path, i, prevOperator)
if err != nil {
return nil, fmt.Errorf("%w at %d: %s", err, i, getLastWord(path[i:]))
}
if tok != nil {
prevOperator = tok.Operator
tokens = append(tokens, tok)
}
}
return tokens, nil
}
func parser(tokens []*Token) ([]*Token, error) {
opStack := new(tokenStack)
result := new(tokenStack)
for _, token := range reverse(tokens) {
switch token.Category {
case tcLiteral:
result.push(token)
case tcVariable:
result.pushDouble(&Token{}, token)
case tcOperator:
for {
top := opStack.peek()
if top == nil {
break
}
if top.Category == tcRightParenthesis {
break
}
tokenPrecedence := operatorDetails[token.Operator].Precedence
topPrecedence := operatorDetails[top.Operator].Precedence
tokenAssociativity := operatorDetails[token.Operator].Associativity
if tokenPrecedence < topPrecedence || (tokenPrecedence == topPrecedence && tokenAssociativity == aRight) {
result.pushDouble(opStack.popDouble())
continue
}
break
}
opStack.push(token)
case tcRightParenthesis:
opStack.push(token)
case tcLeftParenthesis:
for {
top := opStack.peek()
if top == nil {
return nil, errMismatchedParentheses
}
if top.Category == tcRightParenthesis {
opStack.pop()
break
}
result.pushDouble(opStack.popDouble())
}
}
}
// pop the rest of the operators
for result.pushDouble(opStack.popDouble()) != nil {
}
return reverse(result.get()), nil
}
func readNextToken(path []byte, i int, prevOperator Operator) (int, *Token, error) {
var err error
i, err = skipSpaces(path, i)
if err != nil {
return i, nil, err
}
// parentheses
if path[i] == '(' {
return i + 1, &Token{Category: tcLeftParenthesis, Operator: opLeftParenthesis}, nil
}
if path[i] == ')' {
return i + 1, &Token{Category: tcRightParenthesis, Operator: opRightParenthesis}, nil
}
// operator
for _, op := range operatorSpelling {
if matchSubslice(path[i:], op.Spelling) {
if op.Code == opDivide && prevOperator != opNone {
return readRegexp(path, i)
}
if op.Code == opMinus && prevOperator != opNone {
op.Code = opUnaryMinus
}
return i + len(op.Spelling), &Token{Category: tcOperator, Operator: op.Code}, nil
}
}
// literal:
// number
if path[i] >= '0' && path[i] <= '9' {
return readNumber(path, i)
}
// string
if path[i] == '"' || path[i] == '\'' {
return readString(path, i)
}
// bool or null
i, tok, err := readBoolNull(path, i)
if err == nil {
return i, tok, nil
}
// variable
if (path[i] >= 'a' && path[i] <= 'z') || (path[i] >= 'A' && path[i] <= 'Z') || path[i] == '$' || path[i] == '@' {
return readVar(path, i)
}
return i, nil, errUnknownToken
}
func skipSpaces(input []byte, i int) (int, error) {
l := len(input)
for ; i < l; i++ {
if !bytein(input[i], []byte{' ', ',', '\t', '\r', '\n'}) {
break
}
}
return i, nil
}
func trimSpaces(input []byte) int {
l := len(input)
i := l - 1
for ; i >= 0; i-- {
if !bytein(input[i], []byte{' ', ',', '\t', '\r', '\n'}) {
break
}
}
return i + 1
}
// returns true if b matches one of the elements of seq
func bytein(b byte, seq []byte) bool {
for i := 0; i < len(seq); i++ {
if b == seq[i] {
return true
}
}
return false
}