-
Notifications
You must be signed in to change notification settings - Fork 25
/
parse_form.go
180 lines (172 loc) · 4.13 KB
/
parse_form.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
package matcher
import (
"github.com/corywalker/expreduce/expreduce/atoms"
"github.com/corywalker/expreduce/pkg/expreduceapi"
)
type parsedForm struct {
startI int
endI int
form expreduceapi.Ex
origForm expreduceapi.Ex
isBlank bool
isImpliedBs bool
isOptional bool
defaultExpr expreduceapi.Ex
hasPat bool
patSym *atoms.Symbol
}
func parseRepeated(
e expreduceapi.ExpressionInterface,
) (expreduceapi.Ex, int, int, bool) {
min, max := -1, -1
if len(e.GetParts()) < 2 {
return nil, min, max, false
}
if len(e.GetParts()) >= 3 {
list, isList := atoms.HeadAssertion(e.GetParts()[2], "System`List")
if !isList {
return nil, min, max, false
}
if len(list.GetParts()) != 2 {
return nil, min, max, false
}
i, isInt := list.GetParts()[1].(*atoms.Integer)
if !isInt {
return nil, min, max, false
}
ival := i.Val.Int64()
min = int(ival)
max = min
}
return e.GetParts()[1], min, max, true
}
func parseForm(
lhsComponent expreduceapi.Ex,
isFlat bool,
sequenceHead string,
headDefault expreduceapi.Ex,
cl expreduceapi.LoggingInterface,
) (res parsedForm) {
// Calculate the min and max elements this component can match.
toParse := lhsComponent
optional, isOptional := atoms.HeadAssertion(toParse, "System`Optional")
if isOptional {
toParse = optional.GetParts()[1]
}
patTest, isPatTest := atoms.HeadAssertion(toParse, "System`PatternTest")
if isPatTest {
toParse = patTest.GetParts()[1]
}
pat, isPat := atoms.HeadAssertion(toParse, "System`Pattern")
var patSym *atoms.Symbol
if isPat {
var patIsSym bool
patSym, patIsSym = pat.GetParts()[1].(*atoms.Symbol)
if patIsSym {
toParse = pat.GetParts()[2]
} else {
// Valid patterns must have symbols to define.
isPat = false
}
}
bns, isBns := atoms.HeadAssertion(toParse, "System`BlankNullSequence")
bs, isBs := atoms.HeadAssertion(toParse, "System`BlankSequence")
blank, isBlank := atoms.HeadAssertion(toParse, "System`Blank")
repeated, isRepeated := atoms.HeadAssertion(toParse, "System`Repeated")
repeatedNull, isRepeatedNull := atoms.HeadAssertion(
toParse,
"System`RepeatedNull",
)
isImpliedBs := isBlank && isFlat
// Ensure isBlank is exclusive from isImpliedBs
isBlank = isBlank && !isImpliedBs
form := toParse
startI := 1 // also includes implied blanksequence
endI := 1
var defaultExpr expreduceapi.Ex
if isOptional {
defaultToUse := headDefault
if len(optional.GetParts()) >= 3 {
defaultToUse = optional.GetParts()[2]
}
if len(optional.GetParts()) >= 2 {
startI = 0
if defaultToUse == nil {
startI = 1
}
endI = 1
// I think the !isPatTest part might be a hack.
if isImpliedBs && !isPatTest {
endI = maxInt
}
//form = optional.Parts[1]
defaultExpr = defaultToUse
}
} else if isBns {
form = blankNullSequenceToBlank(bns)
startI = 0
endI = maxInt
} else if isImpliedBs {
form = blank
endI = maxInt
if len(blank.GetParts()) >= 2 {
sym, isSym := blank.GetParts()[1].(*atoms.Symbol)
if isSym {
// If we have a pattern like k__Plus
if sym.Name == sequenceHead {
form = atoms.NewExpression([]expreduceapi.Ex{atoms.NewSymbol("System`Blank")})
startI = 2
} else {
endI = 1
}
}
}
} else if isBlank {
form = blank
} else if isRepeated {
repPat, repMin, repMax, repOk := parseRepeated(repeated)
if repOk {
if repMin != -1 {
startI = repMin
}
if repMax != -1 {
endI = repMax
} else {
// an undefined end can match to the end of the sequence.
endI = maxInt
}
form = repPat
}
} else if isRepeatedNull {
if len(repeatedNull.GetParts()) == 2 {
startI = 0
endI = maxInt
form = repeatedNull.GetParts()[1]
}
} else if isBs {
form = blankSequenceToBlank(bs)
endI = maxInt
}
if isPatTest {
form = atoms.NewExpression(
[]expreduceapi.Ex{
patTest.GetParts()[0],
form,
patTest.GetParts()[2],
},
)
}
res.startI = startI
res.endI = endI
res.form = form
res.defaultExpr = defaultExpr
res.origForm = lhsComponent
res.isImpliedBs = isImpliedBs
res.isOptional = isOptional
res.isBlank = isBlank
if isPat {
res.hasPat = true
res.patSym = patSym
}
return res
}