instaparse

If you're not trampolining your parser, why bother getting up in the morning?
sova-soars-the-sora 2020-05-27T18:05:35.017800Z

Hi everyone, I'm interesting in dynamically defining grammar components. I have a big list of nouns, I'd like to incorporate them into my grammar without too much crazy. Is there some way I can add in a dynamic rule with something like nouns = coll?

aengelberg 2020-05-27T18:05:53.018100Z

You probably want instaparse.combinators

sova-soars-the-sora 2020-05-27T18:06:39.018400Z

thanks!

1šŸ‘
sova-soars-the-sora 2020-05-27T18:06:52.018600Z

i shall take a gander

aengelberg 2020-05-27T18:07:14.018800Z

https://github.com/engelberg/instaparse#combinators

sova-soars-the-sora 2020-05-27T18:07:44.019300Z

Would it be ok to do something like (str/join "|" nouns-seq) in the grammar def

aengelberg 2020-05-27T18:08:25.019800Z

that might be more error-prone and hard to read, but it wouldnā€™t be more or less efficient than the combinator version

sova-soars-the-sora 2020-05-27T18:09:02.020600Z

Okay cool

aengelberg 2020-05-27T18:09:26.021300Z

How many nouns are we talking? Iā€™d be wary of putting too many into a parser rule

aengelberg 2020-05-27T18:10:11.022Z

Because to parse text that satisfies the rule, it ultimately has to iterate through every option at every point in your text that could potentially be a noun

aengelberg 2020-05-27T18:11:19.022400Z

And if you pass a string to the parser that doesnā€™t successfully parse, the ā€œfailureā€ message will be really long

sova-soars-the-sora 2020-05-27T18:12:12.022900Z

I'm using about 5-20 nouns

aengelberg 2020-05-27T18:12:24.023200Z

ah ok, thatā€™s definitely manageable

sova-soars-the-sora 2020-05-27T18:13:01.023900Z

I'm trying to just do string join on the nouns list I have, but they lose their quotes

aengelberg 2020-05-27T18:13:32.024400Z

maybe (apply str/join "|" (map #(str "'" % "'") nouns-seq))

sova-soars-the-sora 2020-05-27T18:14:03.024600Z

ah beautiful

sova-soars-the-sora 2020-05-27T18:14:05.024800Z

thank you very much

1šŸ‘
sova-soars-the-sora 2020-05-27T18:19:30.025700Z

almost perfect, it gives me a ('list' 'of' 'nouns') .. not sure how to make it digestible for the parser

aengelberg 2020-05-27T18:19:47.026Z

I think thatā€™s where the apply str/join comes in

aengelberg 2020-05-27T18:20:17.026200Z

oops, I was wrong, you donā€™t need apply

aengelberg 2020-05-27T18:20:29.026600Z

just (str/join "|" ā€¦)

sova-soars-the-sora 2020-05-27T18:22:13.026800Z

Excellent! Thank you.

sova-soars-the-sora 2020-05-27T18:22:28.027100Z

I'm looking forward to showing you guys what I've come up with, once it's done! šŸ˜ƒ

1ā¤ļø
aengelberg 2020-05-27T18:22:37.027400Z

canā€™t wait to see!

sova-soars-the-sora 2020-05-27T18:23:32.028100Z

it's a great wonder to have the power of magic at your fingertips! šŸ˜„

1āœØ
sova-soars-the-sora 2020-05-27T19:02:05.028600Z

I keep ending up with a vector although I would like to use a map... I don't know if it's clojurescript

aengelberg 2020-05-27T19:02:31.029Z

you mean for the input or the output?

sova-soars-the-sora 2020-05-27T19:03:29.029400Z

the output of insta/parse

aengelberg 2020-05-27T19:03:37.029600Z

you can set :output-format :enlive to get a map out of the parser

sova-soars-the-sora 2020-05-27T19:04:03.029800Z

wizardry

sova-soars-the-sora 2020-05-27T19:08:11.030400Z

Hmm, not bad, but it has :tag and :content, I could do with simply swapping [] with {}

sova-soars-the-sora 2020-05-27T19:11:57.030700Z

Thanks for all the help, I'll scratch around

sova-soars-the-sora 2020-05-27T23:15:57.031500Z

Can instaparse flatten a recursive structure with a rule?