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
|
package main
import (
"strings"
"github.com/alecthomas/kong"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/repr"
)
type (
ExprString struct {
Value string `@String`
}
ExprNumber struct {
Value float64 `@Int | @Float`
}
ExprIdent struct {
Name string `@Ident`
}
ExprParens struct {
Inner ExprPrecAll `"(" @@ ")"`
}
ExprUnary struct {
Op string `@("-" | "!")`
Expr ExprOperand `@@`
}
ExprAddSub struct {
Head ExprPrec2 `@@`
Tail []ExprAddSubExt `@@+`
}
ExprAddSubExt struct {
Op string `@("+" | "-")`
Expr ExprPrec2 `@@`
}
ExprMulDiv struct {
Head ExprPrec3 `@@`
Tail []ExprMulDivExt `@@+`
}
ExprMulDivExt struct {
Op string `@("*" | "/")`
Expr ExprPrec3 `@@`
}
ExprRem struct {
Head ExprOperand `@@`
Tail []ExprRemExt `@@+`
}
ExprRemExt struct {
Op string `@"%"`
Expr ExprOperand `@@`
}
ExprPrecAll interface{ exprPrecAll() }
ExprPrec2 interface{ exprPrec2() }
ExprPrec3 interface{ exprPrec3() }
ExprOperand interface{ exprOperand() }
)
// These expression types can be matches as individual operands
func (ExprIdent) exprOperand() {}
func (ExprNumber) exprOperand() {}
func (ExprString) exprOperand() {}
func (ExprParens) exprOperand() {}
func (ExprUnary) exprOperand() {}
// These expression types can be matched at precedence level 3
func (ExprIdent) exprPrec3() {}
func (ExprNumber) exprPrec3() {}
func (ExprString) exprPrec3() {}
func (ExprParens) exprPrec3() {}
func (ExprUnary) exprPrec3() {}
func (ExprRem) exprPrec3() {}
// These expression types can be matched at precedence level 2
func (ExprIdent) exprPrec2() {}
func (ExprNumber) exprPrec2() {}
func (ExprString) exprPrec2() {}
func (ExprParens) exprPrec2() {}
func (ExprUnary) exprPrec2() {}
func (ExprRem) exprPrec2() {}
func (ExprMulDiv) exprPrec2() {}
// These expression types can be matched at the minimum precedence level
func (ExprIdent) exprPrecAll() {}
func (ExprNumber) exprPrecAll() {}
func (ExprString) exprPrecAll() {}
func (ExprParens) exprPrecAll() {}
func (ExprUnary) exprPrecAll() {}
func (ExprRem) exprPrecAll() {}
func (ExprMulDiv) exprPrecAll() {}
func (ExprAddSub) exprPrecAll() {}
type Expression struct {
X ExprPrecAll `@@`
}
var parser = participle.MustBuild[Expression](
// This grammar requires enough lookahead to see the entire expression before
// it can select the proper binary expression type - in other words, we only
// know that `1 * 2 * 3 * 4` isn't the left-hand side of an addition or subtraction
// expression until we know for sure that no `+` or `-` operator follows it
participle.UseLookahead(99999),
// Register the ExprOperand union so we can parse individual operands
participle.Union[ExprOperand](ExprUnary{}, ExprIdent{}, ExprNumber{}, ExprString{}, ExprParens{}),
// Register the ExprPrec3 union so we can parse expressions at precedence level 3
participle.Union[ExprPrec3](ExprRem{}, ExprUnary{}, ExprIdent{}, ExprNumber{}, ExprString{}, ExprParens{}),
// Register the ExprPrec2 union so we can parse expressions at precedence level 2
participle.Union[ExprPrec2](ExprMulDiv{}, ExprRem{}, ExprUnary{}, ExprIdent{}, ExprNumber{}, ExprString{}, ExprParens{}),
// Register the ExprPrecAll union so we can parse expressions at the minimum precedence level
participle.Union[ExprPrecAll](ExprAddSub{}, ExprMulDiv{}, ExprRem{}, ExprUnary{}, ExprIdent{}, ExprNumber{}, ExprString{}, ExprParens{}),
)
func main() {
var cli struct {
Expr []string `arg required help:"Expression to parse."`
}
ctx := kong.Parse(&cli)
expr, err := parser.ParseString("", strings.Join(cli.Expr, " "))
ctx.FatalIfErrorf(err)
repr.Println(expr)
}
|