Repository: neogeny/TatSu

Issue:
@@nameguard not honored in simple grammar
I am puzzled by this behavior:

```
from pprint import pprint
from tatsu import parse

GRAMMAR = """
sequence = {fingering}+ ;
fingering = '1' | '2' | '3' | '4' | '5' | 'x' ;
"""

test = "22"
ast = parse(GRAMMAR, test)
pprint(ast)  # Prints ['2', '2']

test = "xx"
ast = parse(GRAMMAR, test)
pprint(ast)  # tatsu.exceptions.FailedParse: (1:1) no available options :
```

Setting the grammar to the following works as expected:

```
GRAMMAR = """
sequence = {fingering}+ ;
fingering = /[1-5x]/ ; 
"""
```

Am I missing something?

Discussion:

Patch:
diff --git a/tatsu/buffering.py b/tatsu/buffering.py
index 571cc0c..abd303c 100644
--- a/tatsu/buffering.py
+++ b/tatsu/buffering.py
@@ -44,14 +44,13 @@ class Buffer(object):
         self.comments_re = comments_re
         self.eol_comments_re = eol_comments_re
         self.ignorecase = ignorecase
-        self.nameguard = (nameguard
-                          if nameguard is not None
-                          else bool(self.whitespace_re))
+        self.nameguard = (
+            nameguard if nameguard is not None
+            else bool(self.whitespace_re) or bool(namechars)
+        )
         self.comment_recovery = comment_recovery
-        self.namechars = namechars
-        self._namechar_set = set(namechars)
-        if namechars:
-            self.nameguard = True
+        self.namechars = namechars if namechars is not None else ''
+        self._namechar_set = set(self.namechars)
 
         self._pos = 0
         self._len = 0
diff --git a/tatsu/grammars.py b/tatsu/grammars.py
index 039cb22..3646fff 100644
--- a/tatsu/grammars.py
+++ b/tatsu/grammars.py
@@ -6,8 +6,10 @@ import functools
 from collections import defaultdict, Mapping
 from copy import copy
 
-from tatsu.util import indent, trim, ustr, urepr, strtype, compress_seq, chunks
-from tatsu.util import re
+from tatsu.util import (
+    indent, trim, ustr, urepr, strtype, compress_seq, chunks,
+    re, notnone,
+)
 from tatsu.exceptions import FailedRef, GrammarError
 from tatsu.ast import AST
 from tatsu.contexts import ParseContext
@@ -799,6 +801,7 @@ class Grammar(Model):
                  filename='Unknown',
                  whitespace=None,
                  nameguard=None,
+                 namechars=None,
                  left_recursion=None,
                  comments_re=None,
                  eol_comments_re=None,
@@ -829,6 +832,10 @@ class Grammar(Model):
             nameguard = directives.get('nameguard')
         self.nameguard = nameguard
 
+        if namechars is None:
+            namechars = directives.get('namechars')
+        self.namechars = namechars
+
         if left_recursion is None:
             left_recursion = directives.get('left_recursion', True)
         self.left_recursion = left_recursion
@@ -902,6 +909,8 @@ class Grammar(Model):
               comments_re=None,
               eol_comments_re=None,
               parseinfo=None,
+              nameguard=None,
+              namechars=None,
               **kwargs):
         start = start if start is not None else rule_name
         start = start if start is not None else self.rules[0].name
@@ -912,26 +921,17 @@ class Grammar(Model):
             keywords=self.keywords,
             **kwargs)
 
-        if semantics is None:
-            semantics = self.semantics
-
-        if whitespace is None:
-            whitespace = self.whitespace
+        semantics = notnone(semantics, self.semantics)
+        left_recursion = notnone(left_recursion, self.left_recursion)
+        parseinfo = notnone(parseinfo, self._use_parseinfo)
+        comments_re = notnone(comments_re, self.comments_re)
+        eol_comments_re = notnone(eol_comments_re, self.eol_comments_re)
+        nameguard = notnone(nameguard, self.nameguard)
+        namechars = notnone(namechars, self.namechars)
+        whitespace = notnone(whitespace, self.whitespace)
         if whitespace:
             whitespace = re.compile(whitespace)
 
-        if left_recursion is None:
-            left_recursion = self.left_recursion
-
-        if parseinfo is None:
-            parseinfo = self._use_parseinfo
-
-        if comments_re is None:
-            comments_re = self.comments_re
-
-        if eol_comments_re is None:
-            eol_comments_re = self.eol_comments_re
-
         return ctx.parse(
             text,
             rule_name=start,
@@ -943,6 +943,8 @@ class Grammar(Model):
             eol_comments_re=eol_comments_re,
             left_recursion=left_recursion,
             parseinfo=parseinfo,
+            nameguard=nameguard,
+            namechars=namechars,
             **kwargs
         )
 
diff --git a/tatsu/tool.py b/tatsu/tool.py
index 34f0621..5d32bb5 100644
--- a/tatsu/tool.py
+++ b/tatsu/tool.py
@@ -167,7 +167,7 @@ def compile(grammar, name=None, semantics=None, asmodel=False, **kwargs):
 
 
 def parse(grammar, input, start=None, name=None, semantics=None, asmodel=False, **kwargs):
-    model = compile(grammar, name=name, semantics=semantics, asmodel=asmodel, **kwargs)
+    model = compile(grammar, name=name, semantics=semantics, asmodel=asmodel)
     return model.parse(input, start=start, semantics=semantics, **kwargs)
 
 


Test patch:
diff --git a/test/grammar/directive_test.py b/test/grammar/directive_test.py
index cbc1bd3..b91e86e 100644
--- a/test/grammar/directive_test.py
+++ b/test/grammar/directive_test.py
@@ -134,3 +134,19 @@ class DirectiveTests(unittest.TestCase):
         code = codegen(model)
         self.assertTrue('parseinfo=False' in code)
         compile(code, 'test.py', EXEC)
+
+    def test_nameguard_directive(self):
+        GRAMMAR = '''
+            @@grammar :: test
+            @@nameguard :: False
+            @@namechars :: ''
+
+            start = sequence $ ;
+            sequence = {digit}+ ;
+            digit = 'x' | '1' | '2' | '3' | '4' | '5' ;
+        '''
+
+        model = tatsu.compile(GRAMMAR)
+        self.assertFalse(model.nameguard)
+        self.assertEquals(['2', '3'], model.parse('23'))
+        self.assertEquals(['x', 'x'], model.parse('xx'))