FunctionCall rule consuming all comments after match
The FunctionCall
rule ends with this __expr = ''
. This last match consumes all comments after this match until end of file or next rule match. This is no problem for the generated FunctionCall
object. However, the attribute _tx_position_end
is wrong and it is the position of the last consumed character. The model processor that includes the program source code to the workflow includes all these comments.
Here is an example reproducing this behavior:
from textx import metamodel_from_str
from textx import get_children_of_type
grammar_str = 'Program: objects *= Object[/^|;/]; Object: "object" name=ID expr=""; Comment: /#.*$/ | /(?ms)\"{3}(.*?)\"{3}/;'
meta = metamodel_from_str(grammar_str, auto_init_attributes=False)
model_str = '# comment\nobject a # blah \n# comment\nobject b; object c\n"""\nblahblah\n"""\n # more comment'
model = meta.model_from_str(model_str)
obj_list = get_children_of_type('Object', model)
for obj in obj_list:
slc = slice(obj._tx_position, obj._tx_position_end)
print(obj._tx_position, obj._tx_position_end, model_str[slc].rstrip())
Output:
10 37 object a # blah
# comment
37 45 object b
47 88 object c
"""
blahblah
"""
# more comment
By moving expr=""
to the beginning of the Object
rule avoids consuming the comments in this rule match, i.e. with
grammar_str = 'Program: objects *= Object[/^|;/]; Object: expr="" "object" name=ID; Comment: /#.*$/ | /(?ms)\"{3}(.*?)\"{3}/;'
we get this (the expected) output:
10 18 object a
37 45 object b
47 55 object c
Note that _tx_position_end
is correct for object b
because there is no comment after it in the source code.