Ensure the parser can parse expressions into an AST.
(t *testing.T)
| 30 | |
| 31 | // Ensure the parser can parse expressions into an AST. |
| 32 | func TestParser_ParseExpr(t *testing.T) { |
| 33 | var tests = []struct { |
| 34 | s string |
| 35 | expr expr.Expr |
| 36 | err string |
| 37 | }{ |
| 38 | // Primitives |
| 39 | {s: `100`, expr: &expr.NumberLiteral{Val: 100, Int: 100, Expr: "100", ExprType: expr.Unsigned}}, |
| 40 | {s: `'foo bar'`, expr: &expr.StringLiteral{Val: "foo bar"}}, |
| 41 | {s: `true`, expr: &expr.BooleanLiteral{Val: true}}, |
| 42 | {s: `false`, expr: &expr.BooleanLiteral{Val: false}}, |
| 43 | {s: `my_ident`, expr: &expr.VarRef{Val: "my_ident"}}, |
| 44 | {s: `*`, expr: &expr.Wildcard{}}, |
| 45 | |
| 46 | // Simple binary expression |
| 47 | { |
| 48 | s: `1 + 2`, |
| 49 | expr: &expr.BinaryExpr{ |
| 50 | Op: expr.ADD, |
| 51 | LHS: &expr.NumberLiteral{Val: 1, Int: 1, Expr: "1", ExprType: expr.Unsigned}, |
| 52 | RHS: &expr.NumberLiteral{Val: 2, Int: 2, Expr: "2", ExprType: expr.Unsigned}, |
| 53 | }, |
| 54 | }, |
| 55 | |
| 56 | // Binary expression with LHS precedence |
| 57 | { |
| 58 | s: `1 * 2 + 3`, |
| 59 | expr: &expr.BinaryExpr{ |
| 60 | Op: expr.ADD, |
| 61 | LHS: &expr.BinaryExpr{ |
| 62 | Op: expr.MUL, |
| 63 | LHS: &expr.NumberLiteral{Val: 1, Int: 1, Expr: "1", ExprType: expr.Unsigned}, |
| 64 | RHS: &expr.NumberLiteral{Val: 2, Int: 2, Expr: "2", ExprType: expr.Unsigned}, |
| 65 | }, |
| 66 | RHS: &expr.NumberLiteral{Val: 3, Int: 3, Expr: "3", ExprType: expr.Unsigned}, |
| 67 | }, |
| 68 | }, |
| 69 | |
| 70 | // Binary expression with RHS precedence |
| 71 | { |
| 72 | s: `1 + 2 * 3`, |
| 73 | expr: &expr.BinaryExpr{ |
| 74 | Op: expr.ADD, |
| 75 | LHS: &expr.NumberLiteral{Val: 1, Int: 1, Expr: "1", ExprType: expr.Unsigned}, |
| 76 | RHS: &expr.BinaryExpr{ |
| 77 | Op: expr.MUL, |
| 78 | LHS: &expr.NumberLiteral{Val: 2, Int: 2, Expr: "2", ExprType: expr.Unsigned}, |
| 79 | RHS: &expr.NumberLiteral{Val: 3, Int: 3, Expr: "3", ExprType: expr.Unsigned}, |
| 80 | }, |
| 81 | }, |
| 82 | }, |
| 83 | |
| 84 | // Binary expression with LHS paren group. |
| 85 | { |
| 86 | s: `(1 + 2) * 3`, |
| 87 | expr: &expr.BinaryExpr{ |
| 88 | Op: expr.MUL, |
| 89 | LHS: &expr.ParenExpr{ |