Fork me on GitHub
#instaparse
<
2020-05-27
>
sova-soars-the-sora18:05:35

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?

aengelberg18:05:53

You probably want instaparse.combinators

sova-soars-the-sora18:05:52

i shall take a gander

sova-soars-the-sora18:05:44

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

aengelberg18:05:25

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

aengelberg18:05:26

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

aengelberg18:05:11

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

aengelberg18:05:19

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

sova-soars-the-sora18:05:12

I'm using about 5-20 nouns

aengelberg18:05:24

ah ok, thatā€™s definitely manageable

sova-soars-the-sora18:05:01

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

aengelberg18:05:32

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

sova-soars-the-sora18:05:05

thank you very much

šŸ‘ 4
sova-soars-the-sora18:05:30

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

aengelberg18:05:47

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

aengelberg18:05:17

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

aengelberg18:05:29

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

sova-soars-the-sora18:05:13

Excellent! Thank you.

sova-soars-the-sora18:05:28

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

ā¤ļø 4
aengelberg18:05:37

canā€™t wait to see!

sova-soars-the-sora18:05:32

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

āœØ 4
sova-soars-the-sora19:05:05

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

aengelberg19:05:31

you mean for the input or the output?

sova-soars-the-sora19:05:29

the output of insta/parse

aengelberg19:05:37

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

sova-soars-the-sora19:05:11

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

sova-soars-the-sora19:05:57

Thanks for all the help, I'll scratch around

sova-soars-the-sora23:05:57

Can instaparse flatten a recursive structure with a rule?