forked from zdebeer99/goexpression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
199 lines (181 loc) · 4.5 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
package goexpression
import (
"fmt"
"github.com/zdebeer99/goexpression/scanner"
)
type stateFn func(*parser) stateFn
type parser struct {
scan *scanner.Scanner
root *TreeNode
curr *TreeNode
err error
state stateFn
}
func Parse(input string) (*TreeNode, error) {
root := NewTreeNode(NewEmptyToken())
parse := &parser{scanner.NewScanner(input), root, root, nil, nil}
parse.parse()
return root, parse.err
}
func (this *parser) getCurr() Token {
if this.curr != nil {
return this.curr.Value
}
return nil
}
func (this *parser) parse() {
this.pumpExpression()
}
func (this *parser) add(token Token) *TreeNode {
return this.curr.Add(token)
}
func (this *parser) push(token Token) *TreeNode {
return this.curr.Push(token)
}
func (this *parser) lastNode() *TreeNode {
return this.curr.LastElement()
}
func (this *parser) parentNode() *TreeNode {
return this.curr.Parent()
}
func (this *parser) error(err interface{}, a ...interface{}) {
var errortxt string
if val, ok := err.(error); ok {
errortxt = val.Error()
} else {
if len(a) > 0 {
errortxt = fmt.Sprintf(err.(string), a)
} else {
errortxt = err.(string)
}
}
lasttoken := this.commit()
if len(lasttoken) < 10 {
for i := len(lasttoken); i < 10 && !this.scan.IsEOF(); i++ {
this.scan.Next()
}
lasttoken = lasttoken + this.commit()
}
debug := fmt.Errorf("Line: %v, near %q, Error: %s", this.scan.LineNumber(), lasttoken, errortxt)
this.add(NewErrorToken(debug.Error()))
this.err = debug
}
func (this *parser) commit() string {
return this.scan.Commit()
}
//parseOpenBracket
func (this *parser) parseOpenBracket() bool {
this.curr = this.add(NewGroupToken("()"))
this.commit()
return true
}
//parseCloseBracket
func (this *parser) parseCloseBracket() stateFn {
for {
v1, ok := this.curr.Value.(*GroupToken)
if ok && v1.GroupType == "()" {
this.commit()
this.curr = this.curr.Parent()
return branchExpressionOperatorPart
}
if ok && v1.GroupType == "" {
//must be a bracket part of a parent loop, exit this sub loop.
this.scan.Backup()
return nil
}
if this.curr.Parent() == nil {
this.error("Brackets not closed.")
return nil
}
this.curr = this.curr.Parent()
}
panic("Should be impossible to reach this point.")
}
func (this *parser) AcceptOperator() bool {
scan:=this.scan
for _,op:= range operatorList{
if scan.Prefix(op) {
return true
}
}
return false
}
//parseOperator
func (this *parser) parseOperator() bool {
operator := this.commit()
lastnode := this.lastNode()
onode, ok := this.getCurr().(*OperatorToken)
//push excisting operator up in tree structure
if ok {
//operator is the same current operator ignore
if onode.Operator == operator {
return true
}
//change order for */ presedence
//fmt.Println(onode, operator, onode.Precedence(operator))
if onode.Precedence(operator) > 0 {
if lastnode != nil {
this.curr = lastnode.Push(NewOperatorToken(operator))
return true
}
}
//after */ presedence fallback and continue pushing +- operators from the bottom.
if onode.Precedence(operator) < 0 {
for {
v1, ok := this.curr.Parent().Value.(*OperatorToken)
//if ok && strings.Index("+-", v1.Name) >= 0 {
if ok && operators.Level(v1.Operator)>=0{
this.curr = this.curr.Parent()
} else {
break
}
}
}
//standard operator push
this.curr = this.push(NewOperatorToken(operator))
return true
}
//set previous found value as argument of the operator
if lastnode != nil {
this.curr = lastnode.Push(NewOperatorToken(operator))
} else {
this.error(fmt.Sprintf("Expecting a value before operator %q", operator))
this.state = nil
}
return true
}
//parseLRFunc
func (this *parser) parseLRFunc() bool {
lrfunc := this.commit()
lastnode := this.lastNode()
if lastnode != nil {
this.curr = lastnode.Push(NewLRFuncToken(lrfunc))
} else {
this.error(fmt.Sprintf("Expecting a value before operator %q", lrfunc))
this.state = nil
}
return false
}
func (this *parser) ParseText() string {
scan := this.scan
r := scan.Next()
if r == '"' || r == '\'' {
scan.Ignore()
endqoute := r
for {
r = scan.Next()
if r == endqoute {
scan.Backup()
txt := scan.Commit()
scan.Next()
scan.Ignore()
return txt
}
if scan.IsEOF() {
this.error("Missing Qoute and end of text.")
return "Error"
}
}
}
return ""
}