pythontextx

Lookahead inside or clause failing in textx


The following throws an exception for me (on line 9), complaining that Expected '.' at position (5, 15) => 'k foo bar *end '.:

mm = metamodel_from_str('''
File: Line*;
Line: Block | Sentence;
Sentence: 'foo' 'bar' ( '.' | &'end' );
Block: 'block' Line* 'end';
''', skipws=True)
program = mm.model_from_str('''\
foo bar .
block
    foo bar .
end
block foo bar end
''')

However, it parses successfully if I write what I think is an equivalent grammar:

File: Line*;
Line: Block | InnerSentence | Sentence;
Sentence: 'foo' 'bar' '.';
InnerSentence: 'foo' 'bar' &'end';
Block: 'block' Line* 'end';

Is this a bug, or am I missing something?


Solution

  • Positive lookahead tries to match the given input and succeeds if the match succeeds but it never consumes the input. It is meant to be used as a part of the sequence to continue matching subsequent elements of the sequence only if the expression given to lookahead can be matched. It is not very useful on its own.

    In the rule Sentence: 'foo' 'bar' ( '.' | &'end' ); the ordered choice at the end would try to match . and after that lookahead end would succeed but there is nothing to match in that sequence, the match is empty and that branch of ordered choice fails. To overcome the issue you can change your rule to be:

    Sentence: 'foo' 'bar' ( '.' | &'end' '');
    

    Now you have an explicit empty string match after the lookahead which provides the result of the ordered choice alternative.