MattCheely/boon/1.1.0

NOTE: the latest version is 2.0.1

Boon

Parsing and evaulation of Boon strings.

Terminology

In the boon expression:

this OR that

OR is an operator, and this and that are identifiers.

Parse

A parsed Boon expression. Can be evaluated many times

parse : String -> Result String Expression

Try to parse a string as a Boon expression, failing on invalid input.

Evaluate

evalFromDict : Dict String Bool -> Expression -> Bool

Evaluate an expression by looking up identifiers in a Dict String Bool. If a key is absent, it will evaluate to False

values =
    Dict.fromList
        [ ( "foo", True )
        , ( "bar", False )
        ]


-- Evaluates to True
parse "foo OR bar"
    |> evalFromDict values


-- Evaluates to False
parse "baz OR bar"
    |> evalFromDict values
evalFromSet : Set String -> Expression -> Bool

Evalute an expression by looking up identifiers in a Set String. Identifiers present in the set are True, any others are False.

values = Set.fromList ["foo", "bar"]


-- Evaluates to True
parse "foo AND bar"
    |> evalFromSet values


-- Evaluates to False
parse "bar AND baz"
    |> evalFromSet values
eval : (String -> Bool) -> Expression -> Bool

Evaluate an expression based on a user-provided lookup function that maps identifiers to boolean values.

trueOrFalse str =
    if (str == "true") then
        True
    else
        False


-- Evaluates to False
parse "true AND false"
    |> eval trueOrFalse
identifiers : Expression -> Set String

Get the set of identifiers used in an expression. This can be useful if you need to fetch these identifiers from some external source with a Cmd prior to evaluation, or validate that they meet some specific format.