Repository: neogeny/TatSu

Issue:
ModelBuilderSemantics doesn't include optional attributes
If I have a rule that with an optional named component, that component shows up as `None` when using the AST, but is missing from the model class when using `ModelBuilderSemantics`, see the code below:

```py
import tatsu
from tatsu.model import ModelBuilderSemantics

grammar = r"""
foo::Foo = left:identifier [ ':' right:identifier ] $ ;
identifier = /\w+/ ;
"""

grammar = tatsu.compile(grammar)

a = grammar.parse('foo : bar', semantics=ModelBuilderSemantics())
assert a.left == 'foo'
assert a.right == 'bar'

b = grammar.parse('foo', semantics=ModelBuilderSemantics())
assert b.left == 'foo'
assert b.right is None  # AttributeError: 'Foo' object has no attribute 'right'
```

Discussion:

Patch:
diff --git a/tatsu/grammars.py b/tatsu/grammars.py
index 8843994..aef1e77 100644
--- a/tatsu/grammars.py
+++ b/tatsu/grammars.py
@@ -296,7 +296,7 @@ class Token(Model):
         return ctx._token(self.token)
 
     def _first(self, k, f):
-        return set([(self.token,)])
+        return {(self.token,)}
 
     def _to_str(self, lean=False):
         return urepr(self.token)
@@ -333,7 +333,7 @@ class Pattern(Model):
         return ctx._pattern(self.pattern)
 
     def _first(self, k, f):
-        return set([(self.pattern,)])
+        return {(self.pattern,)}
 
     def _to_str(self, lean=False):
         parts = []
@@ -710,7 +710,7 @@ class OverrideList(NamedList):
 
 class Special(Model):
     def _first(self, k, f):
-        return set([(self.value,)])
+        return {(self.value,)}
 
     def _to_str(self, lean=False):
         return '?%s?' % self.value
@@ -780,6 +780,7 @@ class Rule(Decorator):
 
     def parse(self, ctx):
         result = self._parse_rhs(ctx, self.exp)
+        self._add_defined_attributes(result)
         if self.is_name:
             ctx._check_name()
         return result
@@ -787,14 +788,16 @@ class Rule(Decorator):
     def _parse_rhs(self, ctx, exp):
         ruleinfo = RuleInfo(self.name, exp.parse, self.is_leftrec, self.is_memoizable, self.params, self.kwparams)
         result = ctx._call(ruleinfo)
-        if isinstance(result, AST):
-            defines = compress_seq(self.defines())
-            result._define(
-                [d for d, l in defines if not l],
-                [d for d, l in defines if l]
-            )
         return result
 
+    def _add_defined_attributes(self, ast):
+        defines = compress_seq(self.defines())
+        if not isinstance(ast, (AST, Node)):
+            return
+        for d, l in defines:
+            if not hasattr(ast, d):
+                setattr(ast, d, [] if l else None)
+
     def _first(self, k, f):
         if self._first_set:
             return self._first_set


Test patch:
diff --git a/test/grammar/semantics_test.py b/test/grammar/semantics_test.py
index 7ee837f..040336f 100644
--- a/test/grammar/semantics_test.py
+++ b/test/grammar/semantics_test.py
@@ -91,3 +91,19 @@ class SemanticsTests(unittest.TestCase):
         self.assertTrue(hasattr(ast, "b"))
 
         self.assertTrue(issubclass(D, (A, B, C)))
+
+    def test_optional_attributes(self):
+        grammar = r"""
+            foo::Foo = left:identifier [ ':' right:identifier ] $ ;
+            identifier = /\w+/ ;
+        """
+
+        grammar = compile(grammar)
+
+        a = grammar.parse('foo : bar', semantics=ModelBuilderSemantics())
+        assert a.left == 'foo'
+        assert a.right == 'bar'
+
+        b = grammar.parse('foo', semantics=ModelBuilderSemantics())
+        self.assertEquals(b.left, 'foo')
+        self.assertIsNone(b.right)