textX icon indicating copy to clipboard operation
textX copied to clipboard

Boolean assignment '?=' in ordered choice '|'

Open zilakmarian opened this issue 7 years ago • 1 comments
trafficstars

I was reviewing my code and found something I'm not sure how should work. I used boolean assignment as one possible value for ordered choice. Example:

from textx.metamodel import metamodel_from_str
grammar = """
foo:
    'foo' ( bar?='bar' | items+=STRING[','] )
;
"""
test_list = ["foo 'aaa'",
             "foo bar",
             "foo"]
meta_model = metamodel_from_str(grammar)
for t in test_list:
    try:
        model = meta_model.model_from_str(t)
        print model.bar, model.items
    except Exception as e:
        print e

First two strings are parsed as expected. Either boolean value is set to True and empty items list, or boolean value is False and non empty item list. And this is exactly, what i want. I feared that this would also allow boolean False AND empty items list. But parsing third test string raises syntax error. I figure that ordered choice overrules boolean assignment being optional. I'm not saying this is a bug. I just want to be sure that this behavior is intended and will not change in the future.

zilakmarian avatar Jun 22 '18 14:06 zilakmarian

Yes, you are right. Alternatives in ordered choices are tried from left to right and the first one which succeed is chosen. Boolean assignments are considered unsuccessful, if there is no match, from the point of ordered choice so the search continues with the next alternative. In this example you could either make entire ordered choice optional, or use *= for items.

The underlying Arpeggio code is here. I don't have intention to change this semantics but I'm always open for suggestions. :) Anyway, I'm thinking to start adhering to semantic versioning more strictly and to increase major version number on backward incompatible changes. That would provide smoother updates.

I guess it would be good to make the note about this somewhere in the docs. So I would leave this open until the docs are updated. Thanks for raising this issue.

igordejanovic avatar Jun 30 '18 09:06 igordejanovic