Created
November 11, 2017 20:20
-
-
Save yaneury/4aa08e1aaad3ceb0ae6215d6865f790a to your computer and use it in GitHub Desktop.
Participle/Valid Proof Of Concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Ruleset : OrTerm; | |
| OrTerm : OrTerm "|" XorTerm | XorTerm; | |
| XorTerm : XorTerm "^" AndTerm | AndTerm; | |
| AndTerm : AndTerm "&" Primary | Primary; | |
| Primary : validator | "(" OrTerm ")" | "!" Primary; | |
| */ | |
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| "github.com/alecthomas/participle" | |
| "github.com/alecthomas/participle/lexer" | |
| ) | |
| const ( | |
| pattern string = `(\s+)` + | |
| `|(?P<ID>[a-zA-Z]+\([a-zA-Z0-9,]*\))` + | |
| `|(?P<OPERATORS>!|\|\^|&)` | |
| ) | |
| type PrimaryTerm struct { | |
| ID string `@ID` | |
| Subruleset *Ruleset `| "(" @@ ")"` | |
| Not *OrTerm `| "!" @@` | |
| } | |
| type AndTerm struct { | |
| Left *PrimaryTerm `@@` | |
| Right []*PrimaryTerm `{ @@ }` | |
| } | |
| type AndOpTerm struct { | |
| Operator string `@ "&"` | |
| Term *AndTerm `@@` | |
| } | |
| type XorTerm struct { | |
| Left *AndTerm `@@` | |
| Right []*AndOpTerm `{ @@ }` | |
| } | |
| type XorOpTerm struct { | |
| Operator string `@ "^"` | |
| Term *XorTerm `@@` | |
| } | |
| type OrTerm struct { | |
| Left *XorTerm `@@` | |
| Right []*XorOpTerm `{ @@ }` | |
| } | |
| type OrOpTerm struct { | |
| Operator string `@ "|"` | |
| Term *OrTerm `@@` | |
| } | |
| type Ruleset struct { | |
| Left *OrTerm `@@` | |
| Right []*OrOpTerm `{ @@ }` | |
| } | |
| func main() { | |
| def, err := lexer.Regexp(pattern) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| parser, err := participle.Build(&Ruleset{}, def) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| rs := &Ruleset{} | |
| err = parser.ParseString("alpha()&beta()", rs) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| fmt.Println(rs.Left.Left.Left.Left) | |
| fmt.Println(rs.Right) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment