| package main | |
| import ( | |
| "os" | |
| "github.com/alecthomas/participle" | |
| "github.com/alecthomas/participle/lexer" | |
| "github.com/alecthomas/repr" | |
| ) | |
| var iniLexer = lexer.Must(lexer.Regexp( | |
| `(?m)` + | |
| `(\s+)` + | |
| `|(^[#;].*$)` + | |
| `|(?P<Unaryop>[!])` + | |
| `|(?P<Ident>[a-zA-Z][a-zA-Z_\d]*)` + | |
| `|(?P<String>"(?:\\.|[^"])*")` + | |
| `|(?P<Lparen>[\(])` + | |
| `|(?P<Rparen>[\)])` + | |
| `|(?P<Comma>[,])`, | |
| )) | |
| type FullExpression struct { | |
| UnaryOp *string `{ @Unaryop }` | |
| Expression []*Expression `| @@` | |
| } | |
| type Expression struct { | |
| Function *string `@Ident` | |
| Lparen *string `@Lparen` | |
| Arg []*Arg `{ @@ }` | |
| Rparen *string `@Rparen` | |
| } | |
| type Arg struct { | |
| Arg *string `@Ident | @String` | |
| Comma *string `| Comma` | |
| } | |
| func main() { | |
| parser, err := participle.Build(&FullExpression{}, participle.Lexer(iniLexer), participle.Unquote(iniLexer, "String")) | |
| if err != nil { | |
| panic(err) | |
| } | |
| myAST := &FullExpression{} | |
| err = parser.Parse(os.Stdin, myAST) | |
| if err != nil { | |
| panic(err) | |
| } | |
| repr.Println(myAST, repr.Indent(" "), repr.OmitEmpty(true)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment