EctoQueryParser.Parser (EctoQueryParser v0.4.0)

Copy Markdown View Source

NimbleParsec-based parser for the Ecto query language.

Parses data types into an AST representation:

  • Strings: "foo"{:string, "foo"}
  • Integers: 42{:integer, 42}
  • Floats: 3.14{:float, 3.14}
  • Booleans: true/TRUE{:boolean, true}
  • Identifiers: column_1{:identifier, "column_1"}
  • Lists: [1, 2]{:list, [{:integer, 1}, {:integer, 2}]}
  • Functions: TO_UPPER(col){:function, "to_upper", [{:identifier, "col"}]}
  • Operators: col == 1{:op, :==, {:identifier, "col"}, {:integer, 1}}
  • AND/OR: a == 1 AND b == 2{:and, [op1, op2]}
  • NOT: NOT a == 1{:not, {:op, :==, ...}}
  • IS NULL: col IS NULL{:is_null, {:identifier, "col"}}
  • IS NOT NULL: col IS NOT NULL{:is_not_null, {:identifier, "col"}}
  • BETWEEN: col BETWEEN 1 AND 10{:between, {:identifier, "col"}, {:integer, 1}, {:integer, 10}}
  • Grouping: (a == 1 OR b == 2) AND c == 3{:and, [{:or, [...]}, ...]}

Parse failures return {:error, %EctoQueryParser.ParseError{}} with line, column, and byte-offset information.

Summary

Functions

Parses an input string into an AST node.

Parses the given binary as parse_raw.

Functions

parse(input)

Parses an input string into an AST node.

Returns {:ok, ast_node} on success or {:error, %EctoQueryParser.ParseError{}} on failure. The error struct carries the 1-based line and column, the byte_offset, and the (truncated) unconsumed rest of the input.

parse_raw(binary, opts \\ [])

@spec parse_raw(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses the given binary as parse_raw.

Returns {:ok, [token], rest, context, position, byte_offset} or {:error, reason, rest, context, line, byte_offset} where position describes the location of the parse_raw (start position) as {line, offset_to_start_of_line}.

To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line.

Options

  • :byte_offset - the byte offset for the whole binary, defaults to 0
  • :line - the line and the byte offset into that line, defaults to {1, byte_offset}
  • :context - the initial context value. It will be converted to a map