Documentation
¶
Overview ¶
Example (Parse) ¶
package main import ( "fmt" ) func lexAndParse(condition string) { tokens, err := tokenize(condition) if err != nil { panic(err) } node, err := parse(tokens) if err != nil { fmt.Println(err.Error()) } else { fmt.Println(node.String()) } } func main() { lexAndParse("X = 5") lexAndParse("X = 5 AND Y = 6") lexAndParse("(X = 5 AND Y = 6)") lexAndParse("(X = 5 AND Y = 6) OR (X = 10 AND Y = 11)") lexAndParse("X < 5") lexAndParse("X <= 5") lexAndParse("NOT X = 5") lexAndParse("NOT (X = 10 AND Y = 11)") lexAndParse("5 OR 6") }
Output: EQ{ID{"X"},VAL{"5"}} AND{EQ{ID{"X"},VAL{"5"}},EQ{ID{"Y"},VAL{"6"}}} AND{EQ{ID{"X"},VAL{"5"}},EQ{ID{"Y"},VAL{"6"}}} OR{AND{EQ{ID{"X"},VAL{"5"}},EQ{ID{"Y"},VAL{"6"}}},AND{EQ{ID{"X"},VAL{"10"}},EQ{ID{"Y"},VAL{"11"}}}} LT{ID{"X"},VAL{"5"}} LTE{ID{"X"},VAL{"5"}} NOT{EQ{ID{"X"},VAL{"5"}}} NOT{AND{EQ{ID{"X"},VAL{"10"}},EQ{ID{"Y"},VAL{"11"}}}} OR{VAL{"5"},VAL{"6"}}
Example (Tokenize) ¶
tokens, err := tokenize(`foo < 10 AND (bar = "x" OR NOT baz = "y")`) if err != nil { panic(err) } for _, token := range tokens { fmt.Println(token) }
Output: ID["foo"]{1} LT["<"]{5} VAL["10"]{7} AND["AND"]{10} LPAR["("]{14} ID["bar"]{15} EQ["="]{19} VAL["\"x\""]{21} OR["OR"]{25} NOT["NOT"]{28} ID["baz"]{32} EQ["="]{36} VAL["\"y\""]{38} RPAR[")"]{41}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
func Parse ¶
Example ¶
package main import ( "fmt" "strings" ) func main() { root, err := Parse(`foo < 10 AND (bar = "x" OR NOT baz = "y")`) if err != nil { panic(err) } prettyPrint(root) } func prettyPrint(root Node) { str := root.String() var indent = 0 for _, char := range str { if char == '{' { fmt.Println(" {") indent++ fmt.Print(strings.Repeat(" ", indent)) } else if char == '}' { fmt.Println(",") indent-- fmt.Print(strings.Repeat(" ", indent), "}") } else if char == ',' { fmt.Println(",") fmt.Print(strings.Repeat(" ", indent)) } else { fmt.Print(string(char)) } } }
Output: AND { LT { ID { "foo", }, VAL { "10", }, }, OR { EQ { ID { "bar", }, VAL { "\"x\"", }, }, NOT { EQ { ID { "baz", }, VAL { "\"y\"", }, }, }, }, }
Click to show internal directories.
Click to hide internal directories.