-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
134 lines (115 loc) · 2.72 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
package rule_engine
import (
"fmt"
"regexp"
"strconv"
"unicode"
"github.com/shopspring/decimal"
)
type RuleEngineLex struct {
str string
pos int
err *EngineErr
resNode *TokenNode
oper *TokenOperator
}
func NewRuleEngineLex(str string, oper *TokenOperator) *RuleEngineLex {
return &RuleEngineLex{
str: str,
oper: oper,
}
}
func (lex *RuleEngineLex) setErr(err error) int {
if err == nil {
return Success
}
lex.err = err.(*EngineErr)
return int(lex.err.ErrCode)
}
func (lex *RuleEngineLex) matchRule(str string) (int, string) {
token, resStr := 0, ""
for _, tokenRule := range TOKEN_RULE_LIST {
r, _ := regexp.Compile("^" + tokenRule.reStr)
matchStr := r.FindString(str)
if len(matchStr) != 0 {
token, resStr = tokenRule.token, matchStr
break
}
}
// there is some key words match the identifier
if token == IDENTIFIER {
for _, tokenRule := range KEY_WORD_LIST {
r, _ := regexp.Compile("^" + tokenRule.reStr)
matchStr := r.FindString(resStr)
if len(matchStr) != 0 && matchStr == resStr {
token, resStr = tokenRule.token, matchStr
break
}
}
}
return token, resStr
}
func (lex *RuleEngineLex) Lex(lval *ruleEngineSymType) int {
for ; lex.pos < len(lex.str); lex.pos++ {
c := rune(lex.str[lex.pos])
if !unicode.IsSpace(c) {
break
}
}
if lex.pos >= len(lex.str) {
return END
}
token, matchStr := lex.matchRule(lex.str[lex.pos:])
var err error
if len(matchStr) > 0 {
lex.pos += len(matchStr)
lval.node = &TokenNode{
ValueType: valueTokenToValueType[token],
}
switch token {
case STRING:
lval.node.Value = matchStr[1 : len(matchStr)-1]
case INTEGER:
if lval.node.Value, err = strconv.ParseInt(matchStr, 0, 64); err != nil {
return ERROR
}
case FLOAT:
if lex.oper.decimalMode {
if lval.node.Value, err = decimal.NewFromString(matchStr); err != nil {
return ERROR
}
lval.node.ValueType = ValueTypeDecimal
} else {
if lval.node.Value, err = strconv.ParseFloat(matchStr, 64); err != nil {
return ERROR
}
}
case TRUE:
lval.node.Value, token = true, BOOL
case FALSE:
lval.node.Value, token = false, BOOL
case IDENTIFIER:
lval.node.Value = matchStr
}
return token
}
c := rune(lex.str[lex.pos])
if _, ok := VALID_CHAR_SET[c]; ok {
lex.pos += 1
return int(c) // 直接使用这个char
}
return ERROR // some thing wrong
}
func (lex *RuleEngineLex) Error(s string) {
prefix := make([]byte, lex.pos)
for i := 0; i < lex.pos; i++ {
prefix[i] = ' '
}
lex.err = GetError(ErrRuleEngineSyntaxError, fmt.Sprintf("%v, pos: %v\n%v\n%v\n", s, lex.pos, lex.str, string(prefix)+"^"))
}
func (lex *RuleEngineLex) getErrCode() int {
if lex.err != nil {
return int(lex.err.ErrCode)
}
return 0
}