forked from derlinkshaender/cmdparser
/
cmdparser.go
612 lines (568 loc) · 14.7 KB
/
cmdparser.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package cmdparser
import (
"errors"
"fmt"
"golang.org/x/exp/utf8string"
"regexp"
"strconv"
"strings"
"text/scanner"
"unicode/utf8"
)
// NewParser creates a new parser instance
func NewParser() *CommandParser {
return &CommandParser{
options: 0,
inputLine: "",
grammar: map[string]string{},
rules: map[string]*RuleStruct{},
ParseResult: map[string]CmdToken{},
}
}
// SetOptions allows to set parsing options
func (theParser *CommandParser) SetOptions(options uint64) {
theParser.options = options
}
// SetCommandGrammar load the map with the grammar into the parser
func (theParser *CommandParser) SetCommandGrammar(cg map[string]string) {
for k, v := range cg {
theParser.grammar[k] = v
theParser.rules[k] = theParser.prepareRule(k, v)
}
if theParser.options&OptionDebug != 0 {
for i, v := range theParser.rules["START"].Items {
fmt.Println(i, ":", v)
}
}
}
// SetInputString feeds the command line input into the parser for procesing
func (theParser *CommandParser) SetInputString(inputLine string) {
theParser.inputLine = inputLine
theParser.TokenizeCommandLine()
}
func (theParser *CommandParser) golangTokenizer(line string) []*PreToken {
var theScanner scanner.Scanner
result := []*PreToken{}
theScanner.Init(strings.NewReader(line))
theScanner.Mode = scanner.ScanFloats | scanner.ScanIdents | scanner.ScanInts | scanner.ScanStrings
tok := theScanner.Scan()
for tok != scanner.EOF && tok != COMMENTCHAR {
s := theScanner.TokenText()
theToken := &PreToken{
Type: tok,
Text: s,
Position: theScanner.Position,
}
result = append(result, theToken)
tok = theScanner.Scan()
}
return result
}
func tokenFromIdentifier(preToken *PreToken) (*CmdToken, error) {
low := strings.ToLower(preToken.Text)
token := &CmdToken{
Position: preToken.Position,
}
var err error
// process booleans
if low == "true" || low == "yes" || low == "false" || low == "no" {
token.Text = low
token.Type = TokenBool
token.Value = (low == "yes" || low == "true")
} else {
token.Text = preToken.Text
token.Type = TokenIdent
token.Value = preToken.Text
}
return token, err
}
func tokenFromInt(preToken *PreToken) (*CmdToken, error) {
var err error
token := &CmdToken{
Text: preToken.Text,
Type: TokenInt,
Position: preToken.Position,
}
val, err := strconv.Atoi(preToken.Text)
if err == nil {
token.Value = val
} else {
token.Value = nil
token.Type = TokenERR
err = errors.New("NOT_AN_INT")
}
return token, err
}
func tokenFromFloat(preToken *PreToken) (*CmdToken, error) {
var err error
token := &CmdToken{
Text: preToken.Text,
Type: TokenFloat,
Position: preToken.Position,
}
val, err := strconv.ParseFloat(preToken.Text, 64)
if err == nil {
token.Value = val
} else {
token.Value = nil
token.Type = TokenERR
err = errors.New("NOT_A_FLOAT")
}
return token, err
}
func tokenFromString(preToken *PreToken) (*CmdToken, error) {
var err error
token := &CmdToken{
Text: preToken.Text,
Type: TokenString,
Position: preToken.Position,
}
val, err := strconv.Unquote(preToken.Text)
if err == nil {
token.Value = val
} else {
token.Value = nil
token.Type = TokenERR
err = errors.New("COULD_NOT_UNQUOTE")
}
return token, err
}
func tokenFromChar(preToken *PreToken) (*CmdToken, error) {
var err error
token := &CmdToken{
Text: preToken.Text,
Type: TokenChar,
Position: preToken.Position,
}
r, _ := utf8.DecodeRuneInString(preToken.Text)
token.Value = r
return token, err
}
// watch out, different signature from the rest, because we must combine more than
// one pretoken to form the expression string
func tokenFromExpression(preTokens []*PreToken, startIndex int) (*CmdToken, int, error) {
var err error
currIndex := startIndex
token := &CmdToken{
Text: "",
Type: TokenExpr,
Position: preTokens[currIndex].Position,
}
if preTokens[currIndex].Text == "'" {
tmpStr := ""
for {
currIndex++
// are we done with the expression?
if currIndex >= len(preTokens) || preTokens[currIndex].Type == '\'' {
break
}
tmpStr += preTokens[currIndex].Text
}
token.Text = tmpStr
token.Type = TokenExpr
token.Value = preTokens[startIndex:currIndex]
} else {
token.Value = nil
token.Type = TokenERR
err = errors.New("MISSING_SINGLE_QUOTE")
}
return token, currIndex, err
}
// TokenizeCommandLine creates the list of CmdParser tokens. to make things easier
// we first use the internal scanner from GO, then post-process the tokens from the scanner.
func (theParser *CommandParser) TokenizeCommandLine() {
preTokens := theParser.golangTokenizer(theParser.inputLine)
postTokens := []*CmdToken{}
var err error
var postTok *CmdToken
index := 0
for {
tok := preTokens[index]
switch tok.Type {
case scanner.Ident:
postTok, err = tokenFromIdentifier(tok)
if err == nil {
postTokens = append(postTokens, postTok)
}
case scanner.Int:
postTok, err = tokenFromInt(tok)
if err == nil {
postTokens = append(postTokens, postTok)
}
case scanner.Float:
postTok, err = tokenFromFloat(tok)
if err == nil {
postTokens = append(postTokens, postTok)
}
case scanner.String:
postTok, err = tokenFromString(tok)
if err == nil {
postTokens = append(postTokens, postTok)
}
case '\'':
postTok, index, err = tokenFromExpression(preTokens, index)
if err == nil {
postTokens = append(postTokens, postTok)
}
default:
postTok, err = tokenFromChar(tok)
if err == nil {
postTokens = append(postTokens, postTok)
}
}
if err != nil {
postTokens = nil
theParser.TokenizerError = true
} else {
theParser.TokenizerError = false
}
index++
if index >= len(preTokens) {
break
}
}
theParser.tokenList = postTokens
}
// convenience function to dump the token list of the parser
func (theParser *CommandParser) dump() {
for i, v := range theParser.tokenList {
fmt.Printf("%2d: ", i)
fmt.Print(v.Type, "> ")
fmt.Println(v.String())
}
}
// copy a token struct
func copyToken(tok *CmdToken) *CmdToken {
result := *tok
return &result
}
// peek a token without advancing the input token list
func (theParser *CommandParser) peek() *CmdToken {
var result *CmdToken
if len(theParser.tokenList) > 0 {
result = copyToken(theParser.tokenList[0])
}
return result
}
// consume a token from the input token list
func (theParser *CommandParser) read() *CmdToken {
var result *CmdToken
if len(theParser.tokenList) > 0 {
result = theParser.tokenList[0]
theParser.tokenList = append([]*CmdToken{}, theParser.tokenList[1:]...)
}
return result
}
// un-read a token back into the token list
func (theParser *CommandParser) unread(tok *CmdToken) {
if tok != nil {
theParser.tokenList = append([]*CmdToken{tok}, theParser.tokenList...)
}
}
func (theParser *CommandParser) splitRule(ruleString string) ([]string, GrammarItemType) {
var temp []string
result := []string{}
var resultType GrammarItemType
if strings.Index(ruleString, CHOICESTRING) > 0 {
resultType = Choice
temp = strings.Split(ruleString, CHOICESTRING)
} else {
resultType = Sequence
temp = strings.Split(ruleString, " ")
}
for i := range temp {
if strings.TrimSpace(temp[i]) != "" {
result = append(result, strings.TrimSpace(temp[i]))
}
}
return result, resultType
}
func (theParser *CommandParser) expressionType(expr string) GrammarItemType {
var result GrammarItemType
switch expr[0] {
case '"':
result = IdentifierExpr
case '\'':
result = CharExpr
case '[':
result = ClassExpr
case '!':
result = DataTypeExpr
default:
result = SymbolExpr
}
return result
}
func (theParser *CommandParser) expressionCardinality(expr string) GrammarItemCardinality {
var result GrammarItemCardinality
switch expr[len(expr)-1] {
case '*':
result = CardinalityZeroOrMore
case '+':
result = CardinalityOneOrMore
case '?':
result = CardinalityZeroOrOne
default:
result = CardinalityOne
}
return result
}
func (theParser *CommandParser) prepareRule(name, expression string) *RuleStruct {
items := []*RuleItem{}
ruleItems, ruleType := theParser.splitRule(expression)
rs := &RuleStruct{
Name: name,
Items: items,
Type: ruleType,
}
for _, ruleItem := range ruleItems {
item := &RuleItem{
Cardinality: theParser.expressionCardinality(ruleItem),
ExprType: theParser.expressionType(ruleItem),
ExprString: ruleItem,
ParentRule: rs,
Seen: false,
}
if item.Cardinality != CardinalityOne {
// strip out cardinality char
item.ExprString = item.ExprString[0 : len(item.ExprString)-1]
}
switch item.ExprType {
case IdentifierExpr, CharExpr:
// remove quotes
item.ExprString = item.ExprString[1 : len(item.ExprString)-1]
case DataTypeExpr:
// remove exclamation mark
item.ExprString = item.ExprString[1:]
}
// TODO: find a better way to extract runes from a string
if item.ExprType == CharExpr {
s := utf8string.NewString(item.ExprString)
r := s.At(1)
if r == '\'' {
item.ExprString = s.String()
} else {
panic("CharExpr does not contain rune literal!")
}
}
rs.Items = append(rs.Items, item)
}
return rs
}
func matchClassExpr(theClass string, tokptr *CmdToken) bool {
itemMatch, err := regexp.MatchString(theClass, tokptr.Text)
if err != nil {
itemMatch = false
} else {
itemMatch = tokptr.Type == TokenString
}
return itemMatch
}
func matchDataTypeExpr(theDataType TokenType, tokptr *CmdToken) bool {
return tokptr.Type == theDataType
}
func getCardinality(ruleItemPtr *RuleItem) (minxOccur, maxOccur int) {
min := 0
max := 0
switch ruleItemPtr.Cardinality {
case CardinalityOne:
min = 1
max = 1
case CardinalityOneOrMore:
min = 1
max = 1 << 32
case CardinalityZeroOrOne:
min = 0
max = 1
case CardinalityZeroOrMore:
min = 0
max = 1 << 32
}
return min, max
}
func (theParser *CommandParser) matchRuleItem(ruleItemPtr *RuleItem, tokptr *CmdToken) bool {
isMatch := false
ruleItemPtr.Seen = true
if theParser.options&OptionDebug != 0 {
fmt.Println("ruleItem:", ruleItemPtr.String())
if tokptr != nil {
fmt.Println("tokenPtr:", tokptr.String())
} else {
fmt.Println("tokenPtr is NIL!")
}
}
if tokptr == nil {
return false
}
switch ruleItemPtr.ExprType {
case CharExpr:
isMatch = tokptr.Type == TokenChar && ruleItemPtr.ExprString == tokptr.Text
case IdentifierExpr:
isMatch = tokptr.Type == TokenIdent && ruleItemPtr.ExprString == tokptr.Value
case ClassExpr:
isMatch = matchClassExpr(ruleItemPtr.ExprString, tokptr)
case SymbolExpr:
if tokptr != nil {
theParser.unread(tokptr)
}
isMatch = theParser.matchRule(theParser.rules[ruleItemPtr.ExprString])
case DataTypeExpr:
dtStr := strings.ToLower(ruleItemPtr.ExprString)
var reqType TokenType
switch dtStr {
case "expression":
reqType = TokenExpr
case "string":
reqType = TokenString
case "int":
reqType = TokenInt
case "bool":
reqType = TokenBool
case "float":
reqType = TokenFloat
case "char":
reqType = TokenChar
}
isMatch = matchDataTypeExpr(reqType, tokptr)
}
if isMatch {
ruleItemPtr.TokenPtr = tokptr
}
if !isMatch {
if !ruleItemPtr.Seen {
theParser.errorList = append(theParser.errorList, &ParseError{Column: theParser.tokptr.Position.Column, Message: "Rule " + ruleItemPtr.ParentRule.Name + ", Expected " + ruleItemPtr.String()})
}
}
return isMatch
}
func (theParser *CommandParser) matchItemWithToken(ruleItemPtr *RuleItem) bool {
result := false
var matchCount int
minOccur, maxOccur := getCardinality(ruleItemPtr)
for {
match := theParser.matchRuleItem(ruleItemPtr, theParser.tokptr)
// no annotation means ONE
if minOccur == 1 && maxOccur == 1 {
result = match
break
}
if match {
matchCount++
result = match
// match a matching Expr+ or a Expr*
if maxOccur >= 1<<32 || (minOccur == 0 && maxOccur == 1) {
theParser.tokptr = theParser.read()
// break out, if token is nil => matching read until end of input
if theParser.tokptr == nil {
result = match
break
}
}
// too many matches?
if maxOccur == 1 && matchCount > 1 {
result = false
break
}
} else {
// match a Expr?
if minOccur == 0 || matchCount >= minOccur {
result = true
if theParser.tokptr != nil {
theParser.unread(theParser.tokptr)
}
break
}
if matchCount < minOccur {
result = false
break
}
}
}
return result
}
func (theParser *CommandParser) matchRule(rule *RuleStruct) bool {
if theParser.options&OptionDebug != 0 {
fmt.Println("Trying to match ", rule.Name)
}
match := false
if rule.Type == Sequence {
// all must match
for _, item := range rule.Items {
theParser.tokptr = theParser.read()
if theParser.options&OptionDebug != 0 {
fmt.Println("Using Sequence Item:", item.String())
}
match = theParser.matchItemWithToken(item)
if theParser.options&OptionDebug != 0 {
fmt.Println(">> Result:", match)
}
if !match {
break
}
}
} else if rule.Type == Choice {
// check if any of them matched
match = false
theParser.tokptr = theParser.read()
for _, item := range rule.Items {
if theParser.options&OptionDebug != 0 {
fmt.Println("Using Choice Item:", item.String())
}
im := theParser.matchItemWithToken(item)
if theParser.options&OptionDebug != 0 {
fmt.Println(" Result:", im)
}
if im {
match = true
break
}
}
} else {
fmt.Println("You should not be here ...")
panic(fmt.Errorf("Invalid rule type %v", rule.Type))
}
theParser.rules[rule.Name].seen = true
if theParser.options&OptionDebug != 0 {
fmt.Println("Done rule ", rule.Name, " match=", match)
}
return match
}
// AtEnd detects if the parser has processed the input stream to the end
func (theParser *CommandParser) AtEnd() bool {
return len(theParser.tokenList) == 0
}
func (theParser *CommandParser) buildParseResults() {
for _, rule := range theParser.rules {
for _, v := range rule.Items {
key := strings.ToLower(rule.Name + "_" + v.ExprString)
if v.TokenPtr != nil {
theParser.ParseResult[key] = *v.TokenPtr
}
}
}
}
// Parse is the function you call to start the parsing process.
func (theParser *CommandParser) Parse() bool {
rule := theParser.rules["START"]
match := theParser.matchRule(rule)
if !theParser.AtEnd() {
// if there still is stuff to parse, it's not a match ...
match = false
if theParser.options&OptionDebug != 0 {
fmt.Println("Not at end => no match")
fmt.Println("Token list =>", theParser.tokenList)
}
}
theParser.buildParseResults()
theParser.IsMatch = match
return match
}
// DumpRules is a convenience function to dump a rule set
func (theParser *CommandParser) DumpRules() {
for _, rule := range theParser.rules {
fmt.Println(rule.Name)
for i, ri := range rule.Items {
fmt.Println(" ", i, ri.Seen, " >", ri.TokenPtr.String())
}
}
}