EctoQueryParser.Parser (EctoQueryParser v0.5.1)

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, [...]}, ...]}
  • Parameters: {{name}}{:param, "name"} (the name stays a string — no atoms)
  • Optional groups: a == 1 [[AND b == {{p}}]]{:and, [op1, {:optional, :and, [op2]}]}. The connector lives inside the brackets; a connector-less [[expr]] may start a filter (or an AND chain) and parses as {:optional, :and, [expr]}. Optional groups do not nest.

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

or_expr__0(rest, acc, stack, context, line, offset)

or_expr__1(rest, acc, stack, context, line, offset)

or_expr__2(rest, acc, stack, context, line, offset)

or_expr__3(_, _, list, _, _, _)

or_expr__4(rest, acc, stack, context, line, offset)

or_expr__5(inner_rest, inner_acc, list, inner_context, inner_line, inner_offset)

or_expr__6(rest, acc, stack, context, line, offset)

or_expr__7(rest, acc, stack, context, line, offset)

or_expr__8(rest, acc, stack, context, line, offset)

or_expr__9(rest, acc, stack, context, comb__line, comb__offset)

or_expr__10(rest, user_acc, list, context, line, offset)

or_expr__11(rest, acc, stack, context, line, offset)

or_expr__12(rest, user_acc, list, context, line, offset)

or_expr__13(rest, acc, stack, context, comb__line, comb__offset)

or_expr__14(rest, acc, stack, context, line, offset)

or_expr__15(rest, acc, stack, context, line, offset)

or_expr__16(rest, acc, stack, context, comb__line, comb__offset)

or_expr__17(rest, user_acc, list, context, line, offset)

or_expr__18(rest, acc, stack, context, line, offset)

or_expr__19(rest, user_acc, list, context, line, offset)

or_expr__20(rest, acc, stack, context, line, offset)

or_expr__21(rest, acc, stack, context, comb__line, comb__offset)

or_expr__22(rest, user_acc, list, context, line, offset)

or_expr__23(rest, acc, stack, context, line, offset)

or_expr__24(rest, acc, stack, context, line, offset)

or_expr__25(rest, acc, stack, context, line, offset)

or_expr__26(_, _, list, _, _, _)

or_expr__27(rest, acc, stack, context, line, offset)

or_expr__28(rest, acc, stack, context, comb__line, comb__offset)

or_expr__29(rest, user_acc, list, context, line, offset)

or_expr__30(rest, acc, stack, context, line, offset)

or_expr__31(rest, acc, stack, context, line, offset)

or_expr__32(inner_rest, inner_acc, list, inner_context, inner_line, inner_offset)

or_expr__33(rest, acc, stack, context, comb__line, comb__offset)

or_expr__34(rest, acc, stack, context, line, offset)

or_expr__35(rest, acc, stack, context, line, offset)

or_expr__36(rest, acc, stack, context, comb__line, comb__offset)

or_expr__37(rest, user_acc, list, context, line, offset)

or_expr__38(rest, acc, stack, context, line, offset)

or_expr__39(rest, user_acc, list, context, line, offset)

or_expr__40(rest, user_acc, list, context, line, offset)

or_expr__41(rest, acc, list, context, line, offset)

or_expr__42(_, _, stack, _, _, _)

or_expr__43(rest, acc, stack, context, line, offset)

or_expr__44(rest, acc, stack, context, comb__line, comb__offset)

or_expr__45(rest, user_acc, list, context, line, offset)

or_expr__46(rest, acc, stack, context, line, offset)

or_expr__47(rest, acc, stack, context, line, offset)

or_expr__48(rest, acc, list, context, line, offset)

or_expr__49(rest, user_acc, list, context, line, offset)

or_expr__50(rest, acc, stack, context, line, offset)

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