Binding and balance
Binding and balance
Bind a token to a register and require a later token to equal it, and match balanced brackets as one atom. Both run in one pass with no backtracking.
Bind and compare
A:name binds what A matched to the register name; =name later matches a token equal to
it. A word repeated:
$ trex scan '\W:x =x' --text 'the the cat'
[0..7] "the the" captures: x="the"
=shape name, =case name and the other groups compare up to a symmetry: \W:w =shape w pairs
cat with bat (match up to a symmetry).
Balanced brackets
The lexer pairs (), [] and {}, so a balanced group at any depth is the one atom \B, and
\B(P), \B[P], \B{P} a group of one kind whose inside matches P. With a binding, a tag
pair whose closing name must equal its opening one:
$ trex scan '\B' --text 'f(a, b) g(c)'
[1..7] "(a, b)"
[9..12] "(c)"
$ trex scan '<\W:t>.*</=t>' --text '<div>hi</div> <b>x</span>'
[0..13] "<div>hi</div>" captures: t="div"
<b>x</span> does not match, since span is not b.
Look ahead for a literal
~"lit" holds where the literal occurs somewhere ahead, and !~"lit" where it does not:
$ trex scan '\W ~"END"' --text 'begin middle END'
[0..5] "begin"
[6..12] "middle"
Where next
Axes and tools reads scale, recurrence and structure, and rewrites what a pattern finds.