-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
159 lines (141 loc) · 3.83 KB
/
string.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
package dLola
import (
"errors"
"fmt"
// "strconv"
)
type StrExpr interface {
AcceptStr(StrExprVisitor)
Sprint() string
GetPos() Position
InstantiateStrExpr(int, int) InstStrExpr
ConstantSubsStrExpr(*Spec) StrExpr
}
type StrExprVisitor interface {
VisitStringLiteralExpr(StringLiteralExpr) //
VisitStreamOffsetExpr(StreamOffsetExpr) //same method with same arguments as in ExprVisitor
VisitConstExpr(ConstExpr) //same method with same arguments as in ExprVisitor
VisitIfThenElseExpr(IfThenElseExpr) //same method with same arguments as in ExprVisitor
VisitStrConcatExpr(StrConcatExpr) //
}
type StringLiteralExpr struct {
S string
Pos Position
}
type StrConcatExpr struct {
Left StrExpr
Right StrExpr
}
// Accept
func (this StringLiteralExpr) AcceptStr(visitor StrExprVisitor) {
visitor.VisitStringLiteralExpr(this)
}
func (this StrConcatExpr) AcceptStr(visitor StrExprVisitor) {
visitor.VisitStrConcatExpr(this)
}
func (this StreamOffsetExpr) AcceptStr(visitor StrExprVisitor) {
visitor.VisitStreamOffsetExpr(this)
}
func (this ConstExpr) AcceptStr(visitor StrExprVisitor) {
visitor.VisitConstExpr(this)
}
// Sprint()
func (this StringLiteralExpr) Sprint() string {
return this.S
}
func (this StrConcatExpr) Sprint() string {
return fmt.Sprintf("%s strConcat %s", this.Left.Sprint(), this.Right.Sprint())
}
func NewStringLiteralExpr(a, p interface{}) StringLiteralExpr {
return StringLiteralExpr{a.(string), NewPosition(p)}
}
func NewStrConcatExpr(l, r interface{}) StrConcatExpr {
return StrConcatExpr{l.(StrExpr), r.(StrExpr)}
}
func (this StringLiteralExpr) GetPos() Position {
return this.Pos
}
func (this StrConcatExpr) GetPos() Position {
return this.Left.GetPos()
}
func getStrExpr(e interface{}) (StrExpr, error) {
switch v := e.(type) {
case StringExpr:
return v.StExpr, nil
case StreamOffsetExpr:
return v, nil
case ConstExpr:
return v, nil
case StrExpr:
return v, nil
default:
str := fmt.Sprintf("cannot convert to num \"%s\"\n", e.(Expr).Sprint())
return nil, errors.New(str)
}
}
func StrExprToExpr(expr StrExpr) Expr {
switch v := expr.(type) {
case StreamOffsetExpr:
return v
case ConstExpr:
return v
default:
return NewStringExpr(expr) //note: here we use expr, not its type
}
}
// FLATTEN
type RightSubexprStr interface {
buildBinaryExpr(left StrExpr, right StrExpr) StrExpr
getInner() StrExpr
}
type RightStrConcatExpr struct{ E StrExpr }
func (r RightStrConcatExpr) buildBinaryExpr(left StrExpr, right StrExpr) StrExpr {
return StrConcatExpr{left, right}
}
func (r RightStrConcatExpr) getInner() StrExpr { return r.E }
func NewRightStrConcatExpr(a interface{}) RightStrConcatExpr {
n, _ := getStrExpr(a)
return RightStrConcatExpr{n}
}
func FlattenStr(a, b interface{}) Expr {
exprs := ToSlice(b)
first, _ := getStrExpr(a)
if len(exprs) == 0 {
return StrExprToExpr(first)
}
right := exprs[len(exprs)-1].(RightSubexprStr)
curr := right.getInner()
for i := len(exprs) - 2; i >= 0; i-- {
left := exprs[i].(RightSubexprStr)
curr = right.buildBinaryExpr(left.getInner(), curr)
right = left
}
ret := StrExprToExpr(right.buildBinaryExpr(first, curr))
return ret
}
type StrComparison interface {
Sprint() string
AcceptStrComp(StrComparisonVisitor)
GetPos() Position
InstantiateStrCompExpr(int, int) InstStrComparison
ConstantSubsStrCompExpr(*Spec) StrComparison
}
type StrComparisonVisitor interface {
VisitStrEqExpr(StrEqExpr)
}
type StrEqExpr struct {
Left StrExpr
Right StrExpr
}
func (this StrEqExpr) AcceptStrComp(visitor StrComparisonVisitor) {
visitor.VisitStrEqExpr(this)
}
func (this StrEqExpr) Sprint() string {
return fmt.Sprintf("%s strEq %s", this.Left.Sprint(), this.Right.Sprint())
}
func (this StrEqExpr) GetPos() Position {
return this.Left.GetPos()
}
func NewStrEqExpr(l, r interface{}) StrEqExpr {
return StrEqExpr{l.(StrExpr), r.(StrExpr)}
}