instaparse 2020-05-27

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?

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

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

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

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

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

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

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

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

I think that’s where the apply str/join comes in

oops, I was wrong, you don’t need apply

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

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

you mean for the input or the output?

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

the output of insta/parse

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?