cloudspannerecosystem / memefish

memefish is the foundation to analyze Spanner SQL
https://cloudspannerecosystem.dev/memefish/
MIT License
76 stars 19 forks source link

Introduce `BadNode` and `ErrorList` #189

Open makenowjust opened 2 weeks ago

makenowjust commented 2 weeks ago

This PR introduces BadNode and ErrorList.

Example:

$ go run ./tools/parse/main.go 'select (1 +) * (% 2)'
--- Error
syntax error: :1:12: unexpected token: )

  1:  select (1 +) * (% 2)
                 ^

syntax error: :1:17: unexpected token: %

  1:  select (1 +) * (% 2)
                      ^

--- AST
&ast.QueryStatement{
  Hint:  (*ast.Hint)(nil),
  With:  (*ast.With)(nil),
  Query: &ast.Select{
    Select:   0,
    Distinct: false,
    As:       nil,
    Results:  []ast.SelectItem{
      &ast.ExprSelectItem{
        Expr: &ast.BinaryExpr{
          Op:   "*",
          Left: &ast.ParenExpr{
            Lparen: 7,
            Rparen: 11,
            Expr:   &ast.BadNode{
              NodePos: 8,
              NodeEnd: 11,
              Raw:     "1 +",
            },
          },
          Right: &ast.ParenExpr{
            Lparen: 15,
            Rparen: 19,
            Expr:   &ast.BadNode{
              NodePos: 16,
              NodeEnd: 19,
              Raw:     "% 2",
            },
          },
        },
      },
    },
    From:    (*ast.From)(nil),
    Where:   (*ast.Where)(nil),
    GroupBy: (*ast.GroupBy)(nil),
    Having:  (*ast.Having)(nil),
    OrderBy: (*ast.OrderBy)(nil),
    Limit:   (*ast.Limit)(nil),
  },
}

--- SQL
SELECT (1 +) * (% 2)