This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
249 lines (212 loc) · 6.06 KB
/
misc.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package antlraci
import (
"errors"
"fmt"
"strings"
"github.com/JesseCoretta/go-stackage"
"github.com/antlr4-go/antlr/v4"
)
var (
printf func(string, ...any) (int, error) = fmt.Printf
sprintf func(string, ...any) string = fmt.Sprintf
split func(string, string) []string = strings.Split
join func([]string, string) string = strings.Join
lc func(string) string = strings.ToLower
eq func(string, string) bool = strings.EqualFold
rplc func(string, string, string) string = strings.ReplaceAll
hasPfx func(string, string) bool = strings.HasPrefix
hasSfx func(string, string) bool = strings.HasSuffix
strct func(string, string) int = strings.Count
trimS func(string) string = strings.TrimSpace
trimSfx func(string, string) string = strings.TrimSuffix
trimPfx func(string, string) string = strings.TrimPrefix
)
var AntlrUseStderr bool = false
func strInSlice(str string, slices []string, strict ...bool) bool {
var matchCase bool
if len(strict) > 0 {
matchCase = strict[0]
}
for i := 0; i < len(slices); i++ {
if matchCase {
if str == slices[i] {
return true
}
} else {
if eq(str, slices[i]) {
return true
}
}
}
return false
}
func initAntlr(raw string) (p *ACIParser, err error) {
// don't waste time on a zero string ...
if len(raw) == 0 {
err = errorf("Zero-length input string; impossible parsing request")
return
}
// create a new input stream using the raw input.
is := antlr.NewInputStream(raw)
// prepare lexer
lx := NewACILexer(is)
if !AntlrUseStderr {
lx.RemoveErrorListeners()
}
// Prepare the parser using a new CommonTokenStream
// as input, define as p, and verified for nilness.
if p = NewACIParser(antlr.NewCommonTokenStream(lx, 0)); p == nil {
err = errorf("Unknown ANTLR error; received a nil %T instance", p)
return
}
if !AntlrUseStderr {
p.RemoveErrorListeners()
}
// Declare our intent build a parse tree
p.BuildParseTrees = true
return
}
/*
errorf wraps errors.New and returns a non-nil instance of error
based upon a non-nil/non-zero msg input value with optional args.
*/
func errorf(msg any, x ...any) error {
switch tv := msg.(type) {
case string:
if len(tv) > 0 {
return errors.New(sprintf(tv, x...))
}
case error:
if tv != nil {
return errors.New(sprintf(tv.Error(), x...))
}
}
return nil
}
func andBindRules() stackage.Stack { return stackage.And() }
func orBindRules() stackage.Stack { return stackage.Or() }
func notBindRules() stackage.Stack { return stackage.Not() }
func targetRules() stackage.Stack { return stackage.List(9) }
func stackageList() stackage.Stack { return stackage.List() }
func stackageBasic() stackage.Stack { return stackage.Basic() }
func newCondition() (r stackage.Condition) {
return
}
func newStack() (r stackage.Stack) {
return
}
/*
stackByBoolOp is used exclusively in Bind Rules parsing, and
will return a (potentially populated) logical Boolean WORD
based stack (e.g.: AND) within the st variable, alongside a
Boolean true/false value (identified).
The Boolean return value indicates whether the input string
value (op) successfully mapped to a known Boolean WORD-based
stackage.Stack configuration. In such a case, true is returned.
A return value of false indicates no WORD operator was matched,
and that a fallback to a List-style stackage.Stack has occurred.
This should not necessarily be perceived as an error condition.
The contents variadic input argument, which is wholly optional,
will facilitate the addition (by stackage.Stack.Push) of one (1)
or more values -- of any type -- into the return st instance once
it has been initialized. Any added contents arguments are honored
regardless of fallback state.
*/
func stackByBoolOp(op string, contents ...any) (st stackage.Stack, identified bool) {
switch oper := lc(op); oper {
// match the Boolean AND logical
// WORD operator ...
case `and`:
identified = true
st = stackage.And()
for i := 0; i < len(contents); i++ {
st.Push(contents[i])
}
return
// match the Boolean OR logical
// WORD operator ...
case `or`:
identified = true
st = stackage.Or()
for i := 0; i < len(contents); i++ {
st.Push(contents[i])
}
return
// match the Boolean NOT (AND NOT)
// logical WORD operator ...
case `and not`:
identified = true
st = stackage.Not()
for i := 0; i < len(contents); i++ {
st.Push(contents[i])
}
return
}
st = andBindRules()
for i := 0; i < len(contents); i++ {
st.Push(contents[i])
}
return
}
func printStack(x stackage.Stack, parent int) {
for i := 0; i < x.Len(); i++ {
slice, _ := x.Index(i)
switch tv := slice.(type) {
case stackage.Stack:
k := trimS(tv.Kind())
printf("%T[%d:%d] :: [%d;%s]: %s\n", tv, parent, i, tv.Len(), k, tv)
printStack(tv, i)
case stackage.Condition:
printf("%T[%d:%d] :: %s\n", tv, parent, i, tv)
}
}
}
func disenvelopSingleBindRule(tree antlr.Tree) (antlr.Tree, bool) {
if tree.GetChildCount() == 1 {
switch tv := tree.GetChild(0).(type) {
case *BindRuleContext:
return tv, true
}
}
return tree, false
}
func disenvelopStack(outer stackage.Stack) (inner stackage.Stack, ok bool) {
if outer.Len() != 1 {
inner = outer
return
}
slice, _ := outer.Index(0)
switch tv := slice.(type) {
case stackage.Stack:
//if eq(outer.Kind(), tv.Kind()) {
ok = true
inner = tv
//}
default:
inner = outer
}
return
}
func disenvelopCondition(outer stackage.Stack) (inner stackage.Condition, ok bool) {
if outer.Len() != 1 {
return
}
slice, _ := outer.Index(0)
switch tv := slice.(type) {
case stackage.Condition:
inner = tv
ok = true
}
return
}
/*
trimParen strips the leading and trailing parenthetical characters
from the input value (raw). The resulting instance is returned. No
changes occur if not parentheticals were present at the beginning
and/or end of the string.
This is used only for BindRule instances with parentheticals, we
wish to remove for simplified and more reliable parsing ...
*/
func trimParen(raw string) string {
return trimSfx(trimPfx(raw, `(`), `)`)
}