View Source Belp (Belp v1.0.0)
Belp is a simple Boolean Expression Lexer and Parser.
Summary
Functions
Evaluates the given expression.
Evaluates the given expression. Raises when the expression is invalid or variables are undefined.
Checks whether the given expression is valid.
Validates the given expression, returning an error tuple in case the expression is invalid.
Gets a list of variable names that are present in the given expression.
Gets a list of variable names that are present in the given expression. Raises when the expression is invalid or variables are undefined.
Types
A type that describes which primitives can be used as expression.
Functions
@spec eval( expr(), Keyword.t(as_boolean(any())) | %{optional(atom() | String.t()) => as_boolean(any())} ) :: {:ok, boolean()} | {:error, Belp.InvalidCharError.t() | Belp.SyntaxError.t() | Belp.UndefinedVariableError.t()}
Evaluates the given expression.
Examples
iex> Belp.eval("foo and bar", foo: true, bar: false)
{:ok, false}
iex> Belp.eval("foo or bar", %{"foo" => true, "bar" => false})
{:ok, true}
iex> Belp.eval("bar", %{foo: true})
{:error, %Belp.UndefinedVariableError{var: "bar"}}
iex> Belp.eval("invalid expression")
{:error, %Belp.SyntaxError{line: 1, token: "expression"}}
iex> Belp.eval("foo || bar")
{:error, %Belp.InvalidCharError{char: "|", line: 1}}
@spec eval!( expr(), Keyword.t(as_boolean(any())) | %{optional(atom() | String.t()) => as_boolean(any())} ) :: boolean() | no_return()
Evaluates the given expression. Raises when the expression is invalid or variables are undefined.
Examples
iex> Belp.eval!("foo and bar", foo: true, bar: false)
false
iex> Belp.eval!("foo or bar", %{"foo" => true, "bar" => false})
true
iex> Belp.eval!("bar", %{foo: true})
** (Belp.UndefinedVariableError) Undefined variable: bar
iex> Belp.eval!("invalid expression")
** (Belp.SyntaxError) Syntax error near token "expression" on line 1
iex> Belp.eval!("foo || bar")
** (Belp.InvalidCharError) Invalid character "|" on line 1
Checks whether the given expression is valid.
Examples
iex> Belp.valid_expression?("foo and bar")
true
iex> Belp.valid_expression?("invalid expression")
false
iex> Belp.valid_expression?("foo || bar")
false
@spec validate(expr()) :: :ok | {:error, Belp.InvalidCharError.t() | Belp.SyntaxError.t()}
Validates the given expression, returning an error tuple in case the expression is invalid.
Examples
iex> Belp.validate("foo and bar")
:ok
iex> Belp.validate("invalid expression")
{:error, %Belp.SyntaxError{line: 1, token: "expression"}}
iex> Belp.validate("foo || bar")
{:error, %Belp.InvalidCharError{char: "|", line: 1}}
@spec variables(expr()) :: {:ok, [String.t()]} | {:error, Belp.InvalidCharError.t() | Belp.SyntaxError.t()}
Gets a list of variable names that are present in the given expression.
Examples
iex> Belp.variables("(foo and bar) or !foo")
{:ok, ["foo", "bar"]}
iex> Belp.variables("invalid expression")
{:error, %Belp.SyntaxError{line: 1, token: "expression"}}
iex> Belp.variables("foo || bar")
{:error, %Belp.InvalidCharError{char: "|", line: 1}}
Gets a list of variable names that are present in the given expression. Raises when the expression is invalid or variables are undefined.
Examples
iex> Belp.variables!("(foo and bar) or !foo")
["foo", "bar"]
iex> Belp.variables!("invalid expression")
** (Belp.SyntaxError) Syntax error near token "expression" on line 1
iex> Belp.variables!("foo || bar")
** (Belp.InvalidCharError) Invalid character "|" on line 1