@name and @@keyword not working with rule annotations

81 Views Asked by At

I'm trying to use @@keyword and @name in my grammar but tatsu seems to ignore it if rules tagged @name are annotated.

Am I missing something?

To reproduce the behaviour, I provide the following example:

This one works:

import tatsu

GRAMMAR = '''
@@grammar::TestGrammar
@@keyword :: if var
@@whitespace :: /[\t ]+/

    start =
        var identifier ";" { var identifier ";" }* $
        ;

    if = "if";

    var = "var";

    @name
    identifier = 
            /[a-z]+/;
'''


if __name__ == '__main__':
    import pprint
    import json
    from tatsu import parse
    from tatsu.util import asjson

    ast = parse(GRAMMAR, 'var xyz; var if;')
    pprint.pprint(ast, indent=2, width=20)

As expected, tatsu will report


tatsu.exceptions.FailedParse: (1:16) "if" is a reserved word :
var xyz; var if;
               ^

If I annotate the identifier rule by

    @name
    identifier = 
        id:
            /[a-z]+/;

the same python program will output


( 'var',
  {'id': 'xyz'},
  ';',
  [ [ 'var',
      {'id': 'if'},
      ';']])
1

There are 1 best solutions below

1
On

Basically, you should not define:

@name
identifier = 
    id:
       /[a-z]+/;

TatSu will match the ast resulting from the rule against the names @@keyword, and the output of the above rule will always be {'id': 'something'}, which will not match any keyword.

Perhaps what you want is something like:

identifier = id:_identifier;

@name
_identifier = /[a-z]+/;