Belp v0.2.2 Belp View Source

Belp is a simple Boolean Expression Lexer and Parser.

Link to this section Summary

Types

A type that describes which primitives can be used as expression.

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.

Link to this section Types

A type that describes which primitives can be used as expression.

Link to this section Functions

Link to this function

eval(expr, vars \\ %{})

View Source
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}}
Link to this function

eval!(expr, vars \\ %{})

View Source
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
Link to this function

valid_expression?(expr)

View Source
valid_expression?(expr()) :: boolean()

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
Link to this function

validate(expr)

View Source (since 0.2.0)
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}}
Link to this function

variables(expr)

View Source
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}}
Link to this function

variables!(expr)

View Source
variables!(expr()) :: [String.t()] | no_return()

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