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 section Functions
compile(predicate, token_type \\ :string_key_inst)
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" forisblank
comparison. Defaults to["", nil]
.
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]]
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}
matches?(predicate)
matches?(predicate, context)
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
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)