2 ms·
Printable, readable abstract syntax for regex is a thing. This is the TXR Lisp interactive listener of TXR 248. Quit with :quit or Ctrl-D on an empty line.
by kazinator 6y ago
Printable, readable abstract syntax for regex is a thing.
This is the TXR Lisp interactive listener of TXR 248.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
TXR contains many small parts, unsuitable for children under 12 months.
1> (regex-source #/a.*[\s\d:]\d+bcd/)
(compound #\a (0+ wild) (set :space :digit
#\:)
(1+ :digit) #\b #\c #\d)
2> *1
(compound #\a (0+ wild) (set :space :digit
#\:)
(1+ :digit) #\b #\c #\d)
3> (regex-compile *1) ;; compile source back to regex
#/a.*[\s\d:]\d+bcd/
4> (typeof *1)
cons
5> (typeof *3)
regex
(Insert Racket example and others here).
- TeMPOraL 6y ago> (Insert Racket example and others here). Common Lisp (via CL-PPCRE): CL-USER> (parse-string "a(?i)b(?-i)c") (:SEQUENCE #\a (:SEQUENCE (:FLAGS :CASE-INSENSITIVE-P) (:SEQUENCE #\b (:SEQUENCE (:FLAGS :CASE-SENSITIVE-P) #\c)))) You can use both notations to compile and execute regexes. Also, bonus point for any Lisp that does it: you don't have to monkey around with ".end()" and such to turn a "fluent syntax" - which is inherently linear - into something resembling a tree, because s-expressions are trees and you get that for free.
- kazinator 6y agoThe .end() occurs because the approach taken procedural construction via a "linear" path. And that's because that maps nicely to the chained.function().syntax(). That gets ugly if the terms have multiple arguments. (S (NP (N DOG)) (VP (V KICKS)) (NP (N MAN))) becomes: (S DOG.N().NP() KICKS.V().VP() MAN.N().NP()) so far not bad, but now we deal with S, that now becomes a method of DOG.(N).(NP). DOG.N().NP().S(KICKS.V().VP(), MAN.N().NP()) The main connective S is now buried in the middle. We start with DOG, a fourth-level leaf element, make a N out of it, the NP, and now we start a sentence construction, where we bring in other parts. The structure reveals the evaluation order, not the actual structure. BTW, the TXR Lisp version of this syntax is a tad more readable: DOG.(N).(N).(S KICKS.(V).(VP) MAN.(N).(N)) Even when we have obj.fun(arg) as a given, we should at least move the parenthesis before the function: obj.(fun arg).