Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple Expression Parsing with Antlr and Simple String Splitting #1703

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions RuleLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// DELETE THIS CONTENT IF YOU PUT COMBINED GRAMMAR IN Parser TAB
lexer grammar RuleLexer;

EQ : '=' ;
COMMA : ',' ;
LPAREN : '(' ;
RPAREN : ')' ;
ARROW : '->' ;
ASTERIX : '*' ;

WS: [ \t\n\r\f]+ -> skip ;

// TODO: fill in missing plans
PLAN: 'azure'
| 'azure_lite'
| 'aws'
| 'gcp'
| 'trial'
| 'free'
| 'sap-converged-cloud'
;

PR: 'PR';
HR: 'HR';

S: 'S';
EU: 'EU';

VAL: [a-zA-Z_-]*[a-zA-Z_0-9]+;
48 changes: 48 additions & 0 deletions RuleParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
grammar RuleParser;
options { tokenVocab=RuleLexer; }

ruleEntry
: entry EOF
;

entry: PLAN
| PLAN ARROW outputAttrList
| PLAN inputAttrInParen
| PLAN inputAttrInParen ARROW outputAttrList
;

inputAttrInParen: LPAREN RPAREN
| LPAREN inputAttrList RPAREN
;

inputAttrList: inputAttrVal
| inputAttrVal COMMA inputAttrList
;

outputAttrList: outputAttrVal
| outputAttrVal COMMA outputAttrList
;

inputAttrVal: prVal
| hrVal
;

outputAttrVal: s
| eu
;

prVal: pr EQ val;

hrVal: hr EQ val;

pr: PR;

hr: HR;

s: S;

eu: EU;

val: VAL
| ASTERIX
;
39 changes: 39 additions & 0 deletions common/hyperscaler/rules/evaluator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package rules

type Evaluator struct {
rules []*Rule
}

func NewEvaluator(parser Parser) *Evaluator {
return &Evaluator{

Check failure on line 8 in common/hyperscaler/rules/evaluator.go

View workflow job for this annotation

GitHub Actions / run-go-linter

File is not `gofmt`-ed with `-s` (gofmt)

Check failure on line 8 in common/hyperscaler/rules/evaluator.go

View workflow job for this annotation

GitHub Actions / run-go-linter

File is not `goimports`-ed (goimports)
// rules: parser.Parse(),
}
}

/**
* Validate rules.
*/
func (e *Evaluator) Validate() bool {
return true
}

/**
* Evaluate rules and output search labels.
*/
func (e *Evaluator) Evaluate( /*srk*/ ) string {
matchedRules := e.findMatchedRules( /*srk*/ )

// sort rules by priority
matchedRules = e.sortRules(matchedRules)

// apply one found rule
return matchedRules[0].Labels()
}

func (e *Evaluator) sortRules(matchedRules []*Rule) []*Rule {
panic("unimplemented")
}

func (e *Evaluator) findMatchedRules() []*Rule {
panic("unimplemented")
}
56 changes: 56 additions & 0 deletions common/hyperscaler/rules/grammar/antlr/RuleLexer.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
token literal names:
null
'='
','
'('
')'
'->'
'*'
null
null
'PR'
'HR'
'S'
'EU'
null

token symbolic names:
null
EQ
COMMA
LPAREN
RPAREN
ARROW
ASTERIX
WS
PLAN
PR
HR
S
EU
VAL

rule names:
EQ
COMMA
LPAREN
RPAREN
ARROW
ASTERIX
WS
PLAN
PR
HR
S
EU
VAL

channel names:
DEFAULT_TOKEN_CHANNEL
HIDDEN

mode names:
DEFAULT_MODE

atn:
[4, 0, 13, 120, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 1, 0, 1, 0, 1, 1, 1, 1, 1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 7, 33, 1, 0, 0, 0, 9, 35, 1, 0, 0, 0, 11, 38, 1, 0, 0, 0, 13, 41, 1, 0, 0, 0, 15, 96, 1, 0, 0, 0, 17, 98, 1, 0, 0, 0, 19, 101, 1, 0, 0, 0, 21, 104, 1, 0, 0, 0, 23, 106, 1, 0, 0, 0, 25, 112, 1, 0, 0, 0, 27, 28, 5, 61, 0, 0, 28, 2, 1, 0, 0, 0, 29, 30, 5, 44, 0, 0, 30, 4, 1, 0, 0, 0, 31, 32, 5, 40, 0, 0, 32, 6, 1, 0, 0, 0, 33, 34, 5, 41, 0, 0, 34, 8, 1, 0, 0, 0, 35, 36, 5, 45, 0, 0, 36, 37, 5, 62, 0, 0, 37, 10, 1, 0, 0, 0, 38, 39, 5, 42, 0, 0, 39, 12, 1, 0, 0, 0, 40, 42, 7, 0, 0, 0, 41, 40, 1, 0, 0, 0, 42, 43, 1, 0, 0, 0, 43, 41, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 45, 1, 0, 0, 0, 45, 46, 6, 6, 0, 0, 46, 14, 1, 0, 0, 0, 47, 48, 5, 97, 0, 0, 48, 49, 5, 122, 0, 0, 49, 50, 5, 117, 0, 0, 50, 51, 5, 114, 0, 0, 51, 97, 5, 101, 0, 0, 52, 53, 5, 97, 0, 0, 53, 54, 5, 122, 0, 0, 54, 55, 5, 117, 0, 0, 55, 56, 5, 114, 0, 0, 56, 57, 5, 101, 0, 0, 57, 58, 5, 95, 0, 0, 58, 59, 5, 108, 0, 0, 59, 60, 5, 105, 0, 0, 60, 61, 5, 116, 0, 0, 61, 97, 5, 101, 0, 0, 62, 63, 5, 97, 0, 0, 63, 64, 5, 119, 0, 0, 64, 97, 5, 115, 0, 0, 65, 66, 5, 103, 0, 0, 66, 67, 5, 99, 0, 0, 67, 97, 5, 112, 0, 0, 68, 69, 5, 116, 0, 0, 69, 70, 5, 114, 0, 0, 70, 71, 5, 105, 0, 0, 71, 72, 5, 97, 0, 0, 72, 97, 5, 108, 0, 0, 73, 74, 5, 102, 0, 0, 74, 75, 5, 114, 0, 0, 75, 76, 5, 101, 0, 0, 76, 97, 5, 101, 0, 0, 77, 78, 5, 115, 0, 0, 78, 79, 5, 97, 0, 0, 79, 80, 5, 112, 0, 0, 80, 81, 5, 45, 0, 0, 81, 82, 5, 99, 0, 0, 82, 83, 5, 111, 0, 0, 83, 84, 5, 110, 0, 0, 84, 85, 5, 118, 0, 0, 85, 86, 5, 101, 0, 0, 86, 87, 5, 114, 0, 0, 87, 88, 5, 103, 0, 0, 88, 89, 5, 101, 0, 0, 89, 90, 5, 100, 0, 0, 90, 91, 5, 45, 0, 0, 91, 92, 5, 99, 0, 0, 92, 93, 5, 108, 0, 0, 93, 94, 5, 111, 0, 0, 94, 95, 5, 117, 0, 0, 95, 97, 5, 100, 0, 0, 96, 47, 1, 0, 0, 0, 96, 52, 1, 0, 0, 0, 96, 62, 1, 0, 0, 0, 96, 65, 1, 0, 0, 0, 96, 68, 1, 0, 0, 0, 96, 73, 1, 0, 0, 0, 96, 77, 1, 0, 0, 0, 97, 16, 1, 0, 0, 0, 98, 99, 5, 80, 0, 0, 99, 100, 5, 82, 0, 0, 100, 18, 1, 0, 0, 0, 101, 102, 5, 72, 0, 0, 102, 103, 5, 82, 0, 0, 103, 20, 1, 0, 0, 0, 104, 105, 5, 83, 0, 0, 105, 22, 1, 0, 0, 0, 106, 107, 5, 69, 0, 0, 107, 108, 5, 85, 0, 0, 108, 24, 1, 0, 0, 0, 109, 111, 7, 1, 0, 0, 110, 109, 1, 0, 0, 0, 111, 114, 1, 0, 0, 0, 112, 110, 1, 0, 0, 0, 112, 113, 1, 0, 0, 0, 113, 116, 1, 0, 0, 0, 114, 112, 1, 0, 0, 0, 115, 117, 7, 2, 0, 0, 116, 115, 1, 0, 0, 0, 117, 118, 1, 0, 0, 0, 118, 116, 1, 0, 0, 0, 118, 119, 1, 0, 0, 0, 119, 26, 1, 0, 0, 0, 5, 0, 43, 96, 112, 118, 1, 6, 0, 0]
23 changes: 23 additions & 0 deletions common/hyperscaler/rules/grammar/antlr/RuleLexer.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
EQ=1
COMMA=2
LPAREN=3
RPAREN=4
ARROW=5
ASTERIX=6
WS=7
PLAN=8
PR=9
HR=10
S=11
EU=12
VAL=13
'='=1
','=2
'('=3
')'=4
'->'=5
'*'=6
'PR'=9
'HR'=10
'S'=11
'EU'=12
51 changes: 51 additions & 0 deletions common/hyperscaler/rules/grammar/antlr/RuleParser.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
token literal names:
null
'='
','
'('
')'
'->'
'*'
null
null
'PR'
'HR'
'S'
'EU'
null

token symbolic names:
null
EQ
COMMA
LPAREN
RPAREN
ARROW
ASTERIX
WS
PLAN
PR
HR
S
EU
VAL

rule names:
ruleEntry
entry
inputAttrInParen
inputAttrList
outputAttrList
inputAttrVal
outputAttrVal
prVal
hrVal
pr
hr
s
eu
val


atn:
[4, 1, 13, 93, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 43, 8, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 2, 51, 8, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 3, 3, 58, 8, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 3, 4, 65, 8, 4, 1, 5, 1, 5, 3, 5, 69, 8, 5, 1, 6, 1, 6, 3, 6, 73, 8, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 10, 1, 10, 1, 11, 1, 11, 1, 12, 1, 12, 1, 13, 1, 13, 1, 13, 0, 0, 14, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 0, 1, 2, 0, 6, 6, 13, 13, 86, 0, 28, 1, 0, 0, 0, 2, 42, 1, 0, 0, 0, 4, 50, 1, 0, 0, 0, 6, 57, 1, 0, 0, 0, 8, 64, 1, 0, 0, 0, 10, 68, 1, 0, 0, 0, 12, 72, 1, 0, 0, 0, 14, 74, 1, 0, 0, 0, 16, 78, 1, 0, 0, 0, 18, 82, 1, 0, 0, 0, 20, 84, 1, 0, 0, 0, 22, 86, 1, 0, 0, 0, 24, 88, 1, 0, 0, 0, 26, 90, 1, 0, 0, 0, 28, 29, 3, 2, 1, 0, 29, 30, 5, 0, 0, 1, 30, 1, 1, 0, 0, 0, 31, 43, 5, 8, 0, 0, 32, 33, 5, 8, 0, 0, 33, 34, 5, 5, 0, 0, 34, 43, 3, 8, 4, 0, 35, 36, 5, 8, 0, 0, 36, 43, 3, 4, 2, 0, 37, 38, 5, 8, 0, 0, 38, 39, 3, 4, 2, 0, 39, 40, 5, 5, 0, 0, 40, 41, 3, 8, 4, 0, 41, 43, 1, 0, 0, 0, 42, 31, 1, 0, 0, 0, 42, 32, 1, 0, 0, 0, 42, 35, 1, 0, 0, 0, 42, 37, 1, 0, 0, 0, 43, 3, 1, 0, 0, 0, 44, 45, 5, 3, 0, 0, 45, 51, 5, 4, 0, 0, 46, 47, 5, 3, 0, 0, 47, 48, 3, 6, 3, 0, 48, 49, 5, 4, 0, 0, 49, 51, 1, 0, 0, 0, 50, 44, 1, 0, 0, 0, 50, 46, 1, 0, 0, 0, 51, 5, 1, 0, 0, 0, 52, 58, 3, 10, 5, 0, 53, 54, 3, 10, 5, 0, 54, 55, 5, 2, 0, 0, 55, 56, 3, 6, 3, 0, 56, 58, 1, 0, 0, 0, 57, 52, 1, 0, 0, 0, 57, 53, 1, 0, 0, 0, 58, 7, 1, 0, 0, 0, 59, 65, 3, 12, 6, 0, 60, 61, 3, 12, 6, 0, 61, 62, 5, 2, 0, 0, 62, 63, 3, 8, 4, 0, 63, 65, 1, 0, 0, 0, 64, 59, 1, 0, 0, 0, 64, 60, 1, 0, 0, 0, 65, 9, 1, 0, 0, 0, 66, 69, 3, 14, 7, 0, 67, 69, 3, 16, 8, 0, 68, 66, 1, 0, 0, 0, 68, 67, 1, 0, 0, 0, 69, 11, 1, 0, 0, 0, 70, 73, 3, 22, 11, 0, 71, 73, 3, 24, 12, 0, 72, 70, 1, 0, 0, 0, 72, 71, 1, 0, 0, 0, 73, 13, 1, 0, 0, 0, 74, 75, 3, 18, 9, 0, 75, 76, 5, 1, 0, 0, 76, 77, 3, 26, 13, 0, 77, 15, 1, 0, 0, 0, 78, 79, 3, 20, 10, 0, 79, 80, 5, 1, 0, 0, 80, 81, 3, 26, 13, 0, 81, 17, 1, 0, 0, 0, 82, 83, 5, 9, 0, 0, 83, 19, 1, 0, 0, 0, 84, 85, 5, 10, 0, 0, 85, 21, 1, 0, 0, 0, 86, 87, 5, 11, 0, 0, 87, 23, 1, 0, 0, 0, 88, 89, 5, 12, 0, 0, 89, 25, 1, 0, 0, 0, 90, 91, 7, 0, 0, 0, 91, 27, 1, 0, 0, 0, 6, 42, 50, 57, 64, 68, 72]
23 changes: 23 additions & 0 deletions common/hyperscaler/rules/grammar/antlr/RuleParser.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
EQ=1
COMMA=2
LPAREN=3
RPAREN=4
ARROW=5
ASTERIX=6
WS=7
PLAN=8
PR=9
HR=10
S=11
EU=12
VAL=13
'='=1
','=2
'('=3
')'=4
'->'=5
'*'=6
'PR'=9
'HR'=10
'S'=11
'EU'=12
Loading
Loading