Match everything else doesn't work

18 views
Skip to first unread message

Linus Oleander

unread,
Sep 26, 2014, 8:33:48 PM9/26/14
to treet...@googlegroups.com
I'm trying to get this code to work: https://gist.github.com/oleander/5d17f18e314b036ed2e6 matching questions on form "<number>. <question>?" surrounded by garbage.

I've manage to match an array of questions given no garbage using this top rule.

rule everything
  expression+
end

But the given example in my gist doesn't work. Why is that?

Clifford Heath

unread,
Sep 26, 2014, 10:34:51 PM9/26/14
to treet...@googlegroups.com
You can’t must prevent your repetition (+) from eating too much.
PEG parsers never backtrack on input that has been matched,
only on whole rules. One way to prevent that is the following:

rule question
( !following repeatable )+ following
end

rule following
‘?’
end

rule repeatable
.
end

(obviously you can redefine repeatable and following to match any
content)

Clifford Heath.
> --
> You received this message because you are subscribed to the Google Groups "Treetop Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to treetop-dev...@googlegroups.com.
> To post to this group, send email to treet...@googlegroups.com.
> Visit this group at http://groups.google.com/group/treetop-dev.
> For more options, visit https://groups.google.com/d/optout.

markus

unread,
Sep 26, 2014, 10:38:13 PM9/26/14
to treet...@googlegroups.com


> But the given example in my gist doesn't work. Why is that?

I'm not somewhere where I can look at it closely, but at a glance it
looks like the problem may be that your text rule doesn't consume any
input. You might try something like:

rule text
( !expression . )+
end

To skip over characters until it reaches an expression instead of just
sitting where it doesn't match.

-- Markus


Reply all
Reply to author
Forward
0 new messages