I want to write a bunch of rules that check strings of ;-separated words like "word1;word2;word3;word4", using logical expressions with braces like: "word1&word2|(!prefix3*&!word4)". Writing this out in regex generates very long and buggy expressions. However, code for each expression looks simple, there is just too many of them, so I want to shorten it. Optimally, just write some macro and apply it to a bunch of string rules to generate functions. What is a common way to do it?
Have you considered a parser expression grammar like PEG.jl ?
I just don't get their examples and how to define functions based on their AST.
So that is what I basically need - generating Julia function from PEG parser. But for now I don't understand, what additional rules are needed to parse prefixes and braces:
using PEG
@rule word = r"\w+" |> w-> :(contains(str, $(w)))
@rule formula = word & operation & word |> calculate
@rule operation = "|", "&"
# rule for "prefix*" ?
# rule for any braces ?
function calculate(expression)
left, op, right = expression
sym_op = Symbol(op)
return :($(sym_op)($(left), $(right)))
end
pattern1 = "hello|world"
@generated function compiled_pattern1(str)
parse_whole(formula, pattern1)
end
compiled_pattern1("hello there")
# What rules should I add for expressions with braces and prefixes?
# "(jul*|rust)&!(cpp)&(hello|hi)"
Hmm, there might be some examples in here that could be useful to you.
Last updated: Nov 07 2025 at 04:42 UTC