The trouble with Markus’ proposal is that any syntax error inside an enabled IF condition
will cause the whole block to be ignored as a comment.
Instead, I suggest that this is an excellent candidate for using a semantic predicate.
Here’s how:
rule if_block
‘!IF’ expr &{|s| expression = s[1]; expression.evaluate_falseness } (!(’!ENDIF’ / !.) if_comment_line)* ('!ENDIF’ / !.)
/ ‘!IF’ expr block ‘!ENDIF'
end
rule if_comment_line
.* “\n"
end
The way this works is to parse the !IF and expression, then, before the complete rule has been
matched (and the new sequence node created and extended with methods and accessors), the
semantic predicate captures the sequence being built into the sem-pred block as “s”. Then s[1] is
the SyntaxNode of the expression, which should have the method evaluate_falseness. If this
method returns true, the first path of if_block will continue, and assuming it succeeds, the second
alternative won’t be taken.
Note that I went to some trouble with the !. rules to ensure that EOF inside an if_block is parsed
so you can complain about it specifically.
You need to define the “block” rule yourself.
Clifford Heath.
> On 2 Mar 2015, at 9:36 pm, Diman Todorov <
diman....@gmail.com> wrote:
>
> Hi,
>
> I am having the following conundrum:
>
> I have a format where someone decided it is a good idea to abuse if conditions for writing comments like this:
>
> !IF 0
> comments comments
> !ENDIF
>
> my approach to parsing the file was by building up a parse tree, adding .eval(env) in the node modules and calling eval on the lines in the branch if the "if" condition evaluated to true.
>
> Unfortunately my parser still tries to parse the code in the "if" branch that will never evaluate.
>
> I looked into semantic predicates but I couldn't find a good way to access the env there so I can evaluate the "if" condition within an env.
>
> Does this make any sense to you?
> If so, have you any advice on how best to tackle this?
>
> thanks,
> Diman
>
> --
> 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.