SyntaxNode doesn't have expected method

12 views
Skip to first unread message

mmmmbeeeeer

unread,
Jun 1, 2009, 10:29:21 PM6/1/09
to Treetop Development
Hi again,

Thanks all for your help so far - I've encountered another behaviour
that I didn't expect which I hope someone can explain...

I have something like the following rules:

rule requirement
constraint? set_of_units (' and ' requirement)?
end

rule constraint
number ' credit points from ' /
number ' unit' [s]? ' from '
end

rule set_of_units
unit_code tail:(' and ' unit_code)*
end

Given a sample requirement string: '12 credit points from UNIT0001 and
UNIT0002'

I expected the SyntaxNode generated for 'requirement' to have method
'constraint' if a constraint was present in the parsed text. Whilst it
has a linked SyntaxNode for the constraint '12 credit points from' it
doesn't add the constraint method.

I'm assuming that this is related to the constraint being optional;
can anyone shed any light on this?

In my spec I was trying to use something along the lines of the
following:

requirement.constraint.number.value.should == 12

understanding that I would need a value method to convert the string
to an int.

Hoping one of you can point me in the right direction :-)

Cheers,

Pete

mmmmbeeeeer

unread,
Jun 1, 2009, 10:34:46 PM6/1/09
to Treetop Development
ps. I'm running treetop 1.2.5

Clifford Heath

unread,
Jun 1, 2009, 10:39:54 PM6/1/09
to treet...@googlegroups.com
Pete,

On 02/06/2009, at 12:29 PM, mmmmbeeeeer wrote:
> I expected the SyntaxNode generated for 'requirement' to have method
> 'constraint' if a constraint was present in the parsed text. Whilst it
> has a linked SyntaxNode for the constraint '12 credit points from' it
> doesn't add the constraint method.
>
> I'm assuming that this is related to the constraint being optional;
> can anyone shed any light on this?

Your diagnosis is right, and I'd classify this as a known problem.
In fact it caught me out again yesterday.

The easiest fix is to add an tag name to the optional node:

rule requirement
c:constraint? set_of_units (' and ' requirement)?
end

and then you can check "c.text_value.empty?" to see if anything was
matched, if necessary.

My expected behaviour would be that the constaint method would always
be added, and would return nil if the optional rule wasn't matched.

Clifford Heath.

mmmmbeeeeer

unread,
Jun 2, 2009, 2:00:37 AM6/2/09
to Treetop Development
Excellent - thanks Clifford!

It does seem as though I'm starting to get the hang of this :-)

Thanks again for your generous help.

Cheers,

Pete
Reply all
Reply to author
Forward
0 new messages