-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
223 lines (200 loc) · 5.89 KB
/
parse.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
package formula_engine
import (
"fmt"
"github.com/pkg/errors"
)
// parser 语法解析器
type parser struct {
Tokens []*token
CurrentToken *token
LastIdx int
Idx int
}
func newParser(t []*token) *parser {
p := &parser{
Tokens: t,
LastIdx: -1,
Idx: -1,
}
p.advance()
return p
}
// Parse 解析
// BNF 范式
// <expr> ::= <and_term> { OR <expr> }
// <and_term> ::= <not_expr> { AND <and_expr>}
// <not_term> ::= { NOT } <com_expr>
// <com_term> ::= <pri_pre> { GT|LT|EQ|NEQ|GTE|LTE <pri_pre> } // compare term
// <pri_ope> ::= <pri_ope> { +|- <sec_ope> } // Primary operation
// <sec_ope> ::= <sec_ope> { *|/ <ter_ope> } // Secondary operation
// <ter_ope> ::= <factor> { ^ <ter_ope> } // Tertiary operation
// <factor> ::= NUM| FUNCTION LPAREN [ expr { COMMA expr }] RPAREN| IDENTIFIER| { PLUS | MINUS } <factor>| LPAREN <expr> RPAREN
func (p *parser) Parse() (AstNode, error) {
res, err := p.expr()
if err != nil {
return nil, err
} else if p.CurrentToken.Type != TTEof {
return nil, p.makeErr(illegalSyntaxErrMsg, fmt.Sprintf("Unable to parse completely.Idx: %d", p.Idx))
}
return res, nil
}
// expr <expr> ::= <and_term> { OR <expr> }
func (p *parser) expr() (AstNode, error) {
return p.binOpLeft(p.andTerm, p.expr, []TT{TTOr})
}
func (p *parser) andTerm() (AstNode, error) {
return p.binOpLeft(p.notTerm, p.andTerm, []TT{TTAnd})
}
func (p *parser) notTerm() (AstNode, error) {
if p.CurrentToken.Type == TTNot {
tok := p.CurrentToken
p.advance()
node, err := p.notTerm()
if err != nil {
return nil, err
}
return newAstUnNode(tok, node), nil
} else {
return p.comTerm()
}
}
func (p *parser) comTerm() (AstNode, error) {
return p.binOpLeft(p.priOpe, p.priOpe, []TT{TTGt, TTGte, TTEq, TTNeq, TTLt, TTLte})
}
func (p *parser) priOpe() (AstNode, error) {
return p.binOpRight(p.secOpe, []TT{TTPlus, TTMinus})
}
func (p *parser) secOpe() (AstNode, error) {
return p.binOpRight(p.terOpe, []TT{TTMul, TTDiv})
}
func (p *parser) terOpe() (AstNode, error) {
return p.binOpLeft(p.factor, p.terOpe, []TT{TTPow})
}
// factor <factor> ::= NUM | FUNCTION LPAREN [ expr { COMMA IDENTIFIER }] RPAREN | IDENTIFIER | { PLUS | MINUS } factor | LPAREN expr RPAREN
func (p *parser) factor() (AstNode, error) {
tok := p.CurrentToken
switch {
case InSlice([]TT{TTNum, TTIdentifier}, tok.Type):
// NUM | IDENTIFIER
p.advance()
return newAstSinNode(tok), nil
case InSlice([]TT{TTPlus, TTMinus}, tok.Type):
// { PLUS | MINUS } factor
p.advance()
fac, err := p.factor()
if err != nil {
return nil, err
}
return newAstUnNode(tok, fac), nil
case tok.Type == TTFunction:
// FUNCTION LPAREN [ expr { COMMA IDENTIFIER }] RPAREN
p.advance()
if p.CurrentToken.Type != TTLparen {
return nil, p.makeErr(illegalSyntaxErrMsg, fmt.Sprintf("UnExpected tokType:'%s', expected '(' after function name", p.CurrentToken.Type))
}
params := make([]AstNode, 0)
p.advance()
if p.CurrentToken.Type != TTRparen {
node, err := p.expr()
if err != nil {
return nil, err
}
params = append(params, node)
for p.CurrentToken.Type == TTComma {
p.advance()
node, err := p.expr()
if err != nil {
return nil, err
}
params = append(params, node)
}
}
if p.CurrentToken.Type != TTRparen {
return nil, p.makeErr(illegalSyntaxErrMsg, fmt.Sprintf("UnExpected tokType:'%s', expected ')' when there is '(' before", p.CurrentToken.Type))
}
num, ok := FuncParNumMap[tok.Value]
if !ok {
return nil, p.makeErr(systemErrMsg, fmt.Sprintf("Can not found function name %s in FuncParNumMap, please plus it", tok.Value))
}
err := checkParNum(num, len(params))
if err != nil {
return nil, errors.Wrapf(err, getTokPos(tok))
}
p.advance()
return newAstGeneralNode(tok, params...), nil
case tok.Type == TTLparen:
// LPAREN expr RPAREN
p.advance()
expr, err := p.expr()
if err != nil {
return nil, err
}
if p.CurrentToken.Type == TTRparen {
p.advance()
return expr, nil
} else {
return nil, p.makeErr(illegalSyntaxErrMsg, fmt.Sprintf("UnExpected tokType:'%s', expected ')' when there is '(' before", tok.Type))
}
default:
return nil, p.makeErr(illegalSyntaxErrMsg, fmt.Sprintf("UnExpected tokType:'%s'", tok.Type))
}
}
// binOpRight 二元操作生成默认右枝存在二叉树(如:+,-,*,/)
func (p *parser) binOpRight(f func() (AstNode, error), ops []TT) (AstNode, error) {
p.LastIdx = p.Idx
left, err := f()
if err != nil {
return nil, err
}
for InSlice(ops, p.CurrentToken.Type) {
tok := p.CurrentToken
p.advance()
right, err := f()
if err != nil {
return nil, err
}
// 最后构建的时候,如果只有left,则返回left。如果有tok和right,组装构建后返回
left = newAstBinNode(tok, left, right)
}
return left, nil
}
// binOpLeft 二元操作生成默认左枝存在二叉树(如:^)
func (p *parser) binOpLeft(f1 func() (AstNode, error), f2 func() (AstNode, error), ops []TT) (AstNode, error) {
left, err := f1()
if err != nil {
return nil, err
}
if InSlice(ops, p.CurrentToken.Type) {
tok := p.CurrentToken
p.advance()
right, err := f2()
if err != nil {
return nil, err
}
// 最后构建的时候,如果只有left,则返回left。如果有tok和right,组装构建后返回
left = newAstBinNode(tok, left, right)
}
return left, nil
}
// advance 下一个
func (p *parser) advance() {
p.Idx += 1
if p.Idx < len(p.Tokens) {
p.CurrentToken = p.Tokens[p.Idx]
} else {
p.CurrentToken = nil
}
}
// resetLast 重设记录点
func (p *parser) resetLast() {
p.LastIdx = -1
}
// rollBackToLast 回滚到记录点
func (p *parser) rollBackToLast() {
p.Idx = p.LastIdx
p.CurrentToken = p.Tokens[p.Idx]
}
// makeErrWithIdx 组装错误
func (p *parser) makeErr(errName, details string) error {
return makeErrWithIdx(p.Idx, errName, details)
}