Dsxir.Predicate (dsxir v0.4.0)

Copy Markdown

Bounded expression DSL for runtime predicates.

Grammar (see Dsxir.Predicate.Parser for the full productions):

  • literals: integer, float, string (double-quoted), atom (:foo), boolean (true/false), nil
  • field reference: namespace.field_name; input.field_name is a shortcut for the program input namespace
  • length(field) — applies to strings or lists
  • comparisons: ==, !=, <, <=, >, >=, in, not in
  • boolean composition: and, or, not, parentheses
  • arithmetic on the RHS of a comparison is literal-only and folded at parse time

No runtime function calls, no string operations, no map indexing.

All identifier atoms are resolved through String.to_existing_atom/1. An unknown atom yields {:error, %{code: :predicate_parse_error}} — dynamic atom creation from input is prohibited.

Summary

Functions

Evaluates an AST against a runtime env.

Returns the original source string used to build the AST. Round-trips back to an equivalent AST modulo constant folding.

Parses a predicate source string.

Type-checks an AST against env. The root expression must be boolean.

Functions

eval(ast, env)

@spec eval(Dsxir.Predicate.AST.t(), Dsxir.Predicate.Evaluator.env()) ::
  {:ok, boolean()} | {:error, map()}

Evaluates an AST against a runtime env.

env is %{namespace_atom => %{field_atom => runtime_value}}. The special namespace :program_input holds the program's input values.

format(ast)

@spec format(Dsxir.Predicate.AST.t()) :: String.t()

Returns the original source string used to build the AST. Round-trips back to an equivalent AST modulo constant folding.

parse(source)

@spec parse(String.t()) :: {:ok, Dsxir.Predicate.AST.t()} | {:error, map()}

Parses a predicate source string.

Returns {:ok, %AST{}} on success. On failure returns {:error, %{code: :predicate_parse_error, message: _, line: _, col: _}}.

type_check(ast, env)

@spec type_check(Dsxir.Predicate.AST.t(), Dsxir.Predicate.TypeChecker.env()) ::
  :ok | {:error, map()}

Type-checks an AST against env. The root expression must be boolean.