More Perl regex stuff in E
Bill Frantz
frantz@netcom.com
Fri, 16 Apr 1999 08:43:09 -0700
At 11:21 PM -0700 4/15/99, Mark S. Miller wrote:
>I don't know whether to laugh or cry. Before I started *using* Perl regular
>expressions in E, I felt my code was readable. The following code doesn't
>look readable to me, but is this just because I'm not used to reading this
>style? In any case, it's small, was easy to write, worked the first time,
>and does something a bit complicated: parsing updoc scripts. It
>successfully parsed the output of html2txt() applied to some of my
>documentation pages. If I'd tried to write this without regex's, it might
>be locally more readable, but would also be a lot more code -- an
>interesting tradeoff. How does it compare with regex use in Perl or
>Python?
>
>Here's a readability test: By reading this code, can you easily tell what
>syntax it parses?
I can't write regex, nor read them. I would need a complete accurate
description of the syntax to decode them. (I assume that if a computer can
interpret them, I can too, but not by reading.)
>
>(If your mailer wrapped the lines, please unwrap them before reading)
I think your mailer wrapped them. Anyway, they arrived wrapped. (List
software?)
>
>
>define parseUpdoc(script) {
> define result := [] diverge
> while (script =~ rx`(?ms)(@comment.*?)\n?[ \t]*\?[
>\t]?(@code.*?)\n(@rest.*)`) {
> script := rest
> while (script =~ rx`(?ms)[ \t]*>[ \t]?(@moreCode.*?)\n(@rest.*)`) {
> code += "\n" + moreCode
> script := rest
> }
> if (script =~ rx`(?ms)[ \t]*#[ \t]+?(@label\w*):[ \t]*(@output.*?)[
>\t]*\n(@rest.*)`) {
> script := rest
> while (script =~ rx`(?ms)[ \t]*#[ \t]*(@moreOutput.*?)[
>\t]*\n(@rest.*)`) {
> output += "\n" + moreOutput
> script := rest
> }
> result push(CarrotMaker(comment, code, label, output))
> } else {
> result push(CarrotMaker(comment, code, null, null))
> }
> }
> result snapshot
>}
-------------------------------------------------------------------------
Bill Frantz | Macintosh: Didn't do every-| Periwinkle -- Consulting
(408)356-8506 | thing right, but did know | 16345 Englewood Ave.
frantz@netcom.com | the century would end. | Los Gatos, CA 95032, USA