instaparse icon indicating copy to clipboard operation
instaparse copied to clipboard

Can :keyword implicitly mean `(nt :keyword)`?

Open theronic opened this issue 3 years ago • 2 comments

I exclusively use combinators because I struggle to get ABNF definitions to do what I want, but I keep forgetting to wrap keywords in non-terminal nt clause and then get, "no matching clause."

Unless they have another purpose, can :keywords imply non-terminals?

It will also make my grammars far more terse :).

theronic avatar Feb 14 '21 09:02 theronic

I can see how that would be convenient, but it would definitely make the code more complex. Right now, parsers are all represented internally as maps, with a :tag identifying the type of parser, and the rest of the map providing the info relevant to that parser. The combinators are a convenient way to produce a map that conforms to the expected format. So, for example, calling (nt :A) simply produces the map {:tag :nt, :keyword :A}. You could write the parser directly as maps, but the combinators are a bit more concise.

So every place where the code utilizes or traverses the parser in any way, it's doing it by taking cases on the map's :tag.

(case (:tag parser)
 ...)

What you're suggesting is certainly doable, but the consequence is that everywhere I'd have to take cases on "Is it a map? If so, use the :tag to figure out what it is. If it's a keyword, treat it as a non-terminal parser." I'm not crazy about doing that to the internals. Also, it's possible others have written supporting code at this point that traverses parsers, making use of this structure.

Conceptually, I think it's helpful to imagine that parsers constitute a type. In an OO programming language, they'd be some sort of Parser object. In Clojure, they happen to be represented as data -- maps, specifically. The point is, a keyword is not a parser, but it can be converted into one.

Engelberg avatar Feb 14 '21 10:02 Engelberg

Hmmk, I guess I can write a macro to transform all keywords into (nt :keyword) if :keyword appears on left-size of map, and also append :start :S.

theronic avatar Feb 14 '21 15:02 theronic