predicator v0.9.0 Predicator

Documentation for Predicator.

Lexer and Parser currently compatible with 1.1.0 predicate syntax

Link to this section Summary

Functions

Takes a predicate set, a context struct and options.

leex_and_parse/1 takes a string or charlist and does all lexing and parsing then returns the predicate.

leex_string/1 takes string or charlist and returns a lexed tuple for parsing.

Takes in a predicate and context and returns the match result. Context can be either a list or map. Accepts same options as eval/3.

parse_lexed/1 takes a leexed token(list or tup) and returns a predicate. It also can take optional atom for type of token keys to return. options are :string_ey_inst & :atom_key_inst

Link to this section Types

Link to this type

predicate()

predicate() :: String.t() | charlist()
Link to this type

token_key_t()

token_key_t() :: :atom_key_inst | :string_key_inst

Link to this section Functions

Link to this function

compile(predicate, token_type \\ :string_key_inst)

Link to this function

eval(inst, context \\ %{}, opts \\ [map_type: :string])

Takes a predicate set, a context struct and options.

Eval options:

  • map_type: type of keys for context map, :atom or :string. Defaults to :string.
  • nil_values: list of values considered "blank" for isblank comparison. Defaults to ["", nil].
Link to this function

leex_and_parse(str, token_type \\ :string_key_inst)

leex_and_parse/1 takes a string or charlist and does all lexing and parsing then returns the predicate.

iex> leex_and_parse("13 > 12") [["lit", 13], ["lit", 12], ["compare", "GT"]]

iex> leex_and_parse('532 == 532', :atom_key_inst) [[:lit, 532], [:lit, 532], [:compare, :EQ]]

Link to this function

leex_string(str)

leex_string(predicate()) :: {:ok | :error, list() | tuple(), non_neg_integer()}

leex_string/1 takes string or charlist and returns a lexed tuple for parsing.

iex> leex_string('10 > 5') {:ok, [{:lit, 1, 10}, {:compare, 1, :GT}, {:lit, 1, 5}], 1}

iex> leex_string("apple > 5532") {:ok, [{:load, 1, :apple}, {:compare, 1, :GT}, {:lit, 1, 5532}], 1}

Link to this function

matches?(predicate)

Link to this function

matches?(predicate, context)

Link to this function

matches?(predicate, context, eval_opts)

Takes in a predicate and context and returns the match result. Context can be either a list or map. Accepts same options as eval/3.

iex> matches?("fruit in ['apple', 'pear']", %{"fruit" => "watermelon"}, [map_type: :string])
false

iex> matches?("fruit in ['apple', 'pear']", [fruit: "pear"], [map_type: :atom])
true

iex> matches?("fruit is blank", [fruit: nil], [map_type: :atom, nil_values: [nil]])
true
Link to this function

parse_lexed(token, opt \\ :string_key_inst)

parse_lexed(list(), token_key_t()) :: {:ok | :error, list() | tuple()}

parse_lexed/1 takes a leexed token(list or tup) and returns a predicate. It also can take optional atom for type of token keys to return. options are :string_ey_inst & :atom_key_inst

iex> parse_lexed({:ok, [{:load, 1, :apple}, {:compare, 1, :GT}, {:lit, 1, 5532}], 1})

iex> parse_lexed({:ok, [{:load, 1, :apple}, {:compare, 1, :GT}, {:lit, 1, 5532}], 1}, :string_key_inst)

iex> parse_lexed([{:load, 1, :apple}, {:compare, 1, :GT}, {:lit, 1, 5532}], :atom_key_inst)