farinap5 / boolexpr

Library to evaluate boolean expressions and match text

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Boolexpr

Overview

boolexpr is a lightweight library designed for validating strings against patterns using a simple, intuitive syntax. Unlike regular expressions, boolexpr uses straightforward logical operators to combine patterns, making it easier to write and understand complex validation rules. Syntax

boolexpr patterns are constructed using the following symbols:

  • AND: & (both conditions must be true)
  • OR: | (at least one condition must be true)
  • Grouping: () (used to control the precedence of operations)

BNF Grammar

The syntax of boolexpr is defined by the following Backus-Naur Form (BNF) grammar:

<exp> ::= 
    <group> "&" <exp>
    | <group> "|" <exp>
    | <group>

<group> ::=
    "(" <exp> ")"
    | <data>

<data> ::=
    [a-z]+

Example

The following code must return false from the Eval() since there is no x in the string validated.

l := b.Init("(((b&x|x)&x|x&q&q|x)&((x|(o&(o&x)))&q)&(q&b)&(((o&q)&a|l)))")

data := "Qualquer dado"
c, err := l.Eval(data)
if err != nil {
	fmt.Panic(err.Error())
}

The following code must return true since the string validated has a.

l := b.Init("a | z")

data := "Qualquer dado"
c, err := l.Eval(data)
if err != nil {
	fmt.Panic(err.Error())
}

The grammar must generate the following sentences.

((xy&g)|g|(vhs&tp)&e&h)&s|mb|b
(m&jua)
((wiaae|(r&dj&h)&(i&p))|((i&g|r)))

((o|b|b&b)&(l))|(a)|o
((b&b|l)&o|l&q&q|l)&((q|(o))&q)|(q&b)&(((o&q)&a|l))

BNF Playground

https://www.inf.ufpr.br/lesoliveira/download/c-completo-total.pdf

About

Library to evaluate boolean expressions and match text

License:MIT License


Languages

Language:Go 100.0%