EctoQueryParser.Pipe.Parser (EctoQueryParser v0.5.1)

Copy Markdown View Source

NimbleParsec-based parser for the staged pipe language.

A pipe query is a source followed by zero or more |-separated stages:

orders
| filter status == "paid"
| group customer.region { total = sum(amount), n = count() }
| sort -total
| limit 10

Whitespace (including newlines) is insignificant around |. The filter stage embeds the existing filter grammar (EctoQueryParser.Parser) verbatim, so everything valid in a plain filter string — parameters and optional groups included — is valid inside | filter.

Identifiers, aliases, and stage keywords in the pipe grammar are annotated with their source position (%{line: l, column: c, offset: o}), so stage-level validation errors can point at the offending token. Stage AST nodes:

  • {:filter, filter_ast, pos}filter_ast is the untouched filter AST.
  • {:select, items, pos} — items are {:pcol, name, pos} or {:aliased, alias, pos, {:pfunc, fname, pos, args}}.
  • {:group, breakouts, aggs, pos} — breakouts share the select item shapes plus bare {:pfunc, ...}; aggs are {:agg, alias, pos, fun, arg_or_nil} with fun one of count, count_distinct, sum, avg, min, max.
  • {:sort, keys, pos} — keys are {:key, :asc | :desc, name, pos}.

  • {:limit, n, pos} / {:offset, n, pos} — non-negative integers only.

Parse failures return {:error, %EctoQueryParser.ParseError{}} with the usual line/column/byte-offset diagnostics.

Summary

Functions

Parses a pipe query string into a EctoQueryParser.Pipe.Query struct.

Parses the given binary as parse_raw.

Functions

parse(input)

Parses a pipe query string into a EctoQueryParser.Pipe.Query struct.

Returns {:ok, %EctoQueryParser.Pipe.Query{}} or {:error, %EctoQueryParser.ParseError{}}.

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