-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate.go
96 lines (83 loc) · 2.77 KB
/
calculate.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
package alteryx_formulas
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/tlarsen7572/alteryx_formulas/parser"
"regexp"
"time"
)
type Calculator interface {
Calculate() (interface{}, []error)
}
type RecordInfo interface {
GetCurrentBool(fieldName string) (bool, bool, error)
GetCurrentInt(fieldName string) (int, bool, error)
GetCurrentFloat(fieldName string) (float64, bool, error)
GetCurrentString(fieldName string) (string, bool, error)
GetCurrentDate(fieldName string) (time.Time, bool, error)
GetFieldTypeByName(fieldName string) (string, error)
}
func NewCalculator(formula string, info RecordInfo) (Calculator, []error) {
inputStream := antlr.NewInputStream(formula)
lexer := parser.NewAlteryxFormulasLexer(inputStream)
tokens := antlr.NewCommonTokenStream(lexer, antlr.LexerDefaultTokenChannel)
p := parser.NewAlteryxFormulasParser(tokens)
p.RemoveErrorListeners()
errors := &errorListener{}
p.AddErrorListener(errors)
tree := p.Expr()
walker := antlr.ParseTreeWalker{}
firstListener := &firstPassListener{recordInfo: info, symbols: make(map[antlr.ParserRuleContext]int)}
walker.Walk(firstListener, tree)
if len(errors.errs) > 0 {
return nil, errors.errs
}
wordExp, _ := regexp.Compile(`([^\s]+)(?:\s+|$)`)
secondListener := &secondPassListener{
symbols: firstListener.symbols,
calc: &calculator{
recordInfo: info,
wordExp: wordExp,
},
}
antlr.ParseTreeWalkerDefault.Walk(secondListener, tree)
return secondListener.calc, errors.errs
}
func Calculate(formula string, info RecordInfo) (interface{}, []error) {
calc, errs := NewCalculator(formula, info)
if len(errs) > 0 {
return nil, errs
}
return calc.Calculate()
}
type errorListener struct {
errs []error
}
func (l *errorListener) SyntaxError(_ antlr.Recognizer, _ interface{}, line, column int, msg string, _ antlr.RecognitionException) {
l.errs = append(l.errs, fmt.Errorf(`line %v:%v: %v`, line, column, msg))
}
func (l *errorListener) ReportAmbiguity(_ antlr.Parser, _ *antlr.DFA, _, _ int, _ bool, _ *antlr.BitSet, _ antlr.ATNConfigSet) {
panic("implement me")
}
func (l *errorListener) ReportAttemptingFullContext(_ antlr.Parser, _ *antlr.DFA, _, _ int, _ *antlr.BitSet, _ antlr.ATNConfigSet) {
panic("implement me")
}
func (l *errorListener) ReportContextSensitivity(_ antlr.Parser, _ *antlr.DFA, _, _, _ int, _ antlr.ATNConfigSet) {
panic("implement me")
}
const (
ByteType = `Byte`
BoolType = `Bool`
Int16Type = `Int16`
Int32Type = `Int32`
Int64Type = `Int64`
FixedDecimalType = `FixedDecimal`
FloatType = `Float`
DoubleType = `Double`
StringType = `String`
WStringType = `WString`
V_StringType = `V_String`
V_WStringType = `V_WString`
DateType = `Date`
DateTimeType = `DateTime`
)