Fork me on GitHub
#instaparse
<
2017-11-23
>
jeremys16:11:17

Hey guys, I am playing with instaparse and I have a problem contructucting a grammar.

jeremys16:11:38

Here is what I am going for

(insta/defparser ex7
  "
  doc = (text | tag)*
  text = #'[^@]*'
  tag = '@' #'[a-z]*' inner-text*
  inner-text = '{' #'[^}]*' '}'
  ")

(ex7 "some text @toto{inner text}")

jeremys16:11:34

The problem is the parser when parsing a tag rule won’t consider the inner-text rule giving me the parse

[:doc [:text "some text "] [:tag "@" "toto"] [:text "{inner text}"]]

jeremys16:11:15

instead of the desired

[:doc [:text "some text "] [:tag "@" "toto" [:inner-text "{" "inner text" "}"]]]

jeremys17:11:52

Any Idea how I can modify the grammar to consider the inner-text rule before going back to the text one ?

aengelberg22:11:17

@jeremys Maybe change inner-text* to inner-text* !inner-text, to ensure that it parses as many inner-texts as it can.

jeremys23:11:13

@aengelberg Thx Alex I’ll try to use the lookahead, I haven’t played with that yet.