Commits

Anonymous committed a5f840e

class int as grammar, supporting optional attributes in xml2thing() and thing2xml()

  • Participants
  • Parent commits ba2dfb9

Comments (0)

Files changed (5)

docs/heading.en.yinc2

 div id="headline" {
     p > pyPEG – a PEG Parser-Interpreter in Python
     div class="small" {
-        "pyPEG 2.9.0 of Su Dec 09 2012 – Copyleft 2009-2012, "
+        "pyPEG 2.10.0 of Sa Feb 09 2012 – Copyleft 2009-2013, "
         a "http://fdik.org", "Volker Birk";
     }
     div id=python1 p

pypeg2/__init__.py

     pass
 
 
-__version__ = 2.9
+__version__ = 2.10
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
     """Generate an Attribute with that name, referencing the thing.
 
     Instance variables:
-        Class       reference to Attribute class generated by namedtuple()
+        Class       Attribute class generated by namedtuple()
     """
     if __debug__:
         if isinstance(thing, tuple):
     def __setitem__(self, key, value):
         """x.__setitem__(i, y) <==> x[i]=y"""
         value.name = Symbol(key)
+        if key:
+            name = Symbol(key)
+        else:
+            name = id(key)
+        try:
+            value.name = name
+        except AttributeError:
+            pass
         try:
             value.namespace
         except AttributeError:
-            value.namespace = weakref.ref(self)
+            try:
+                value.namespace = weakref.ref(self)
+            except AttributeError:
+                pass
         else:
             if not(value.namespace):
                 value.namespace = weakref.ref(self)
-        super(Namespace, self).__setitem__(key, value)
+        super(Namespace, self).__setitem__(name, value)
 
     def __delitem__(self, key):
         """x.__delitem__(y) <==> del x[y]"""
         elif isinstance(thing, list):
             found = False
             for e in thing:
-                t, r = self._parse(text, e, pos)
-                if type(r) != SyntaxError:
-                    found = True
-                    break
+                try:
+                    t, r = self._parse(text, e, pos)
+                    if type(r) != SyntaxError:
+                        found = True
+                        break
+                except GrammarValueError:
+                    raise
+                except ValueError:
+                    pass
             if found:
                 result = t, r
             else:
                         if type(e) == attr.Class:
                             setattr(obj, e.name, e.thing)
                         else:
-                            obj[e.name] = e
+                            try:
+                                obj[e.name] = e
+                            except AttributeError:
+                                obj[None] = e
+
                     try:
                         obj.polish()
                     except AttributeError:
                         + grammar.pattern)
             self._got_regex = True
 
-        elif isinstance(grammar, (str, Literal)):
+        elif isinstance(grammar, (str, int, Literal)):
             result = terminal_indent() + str(grammar)
 
         elif isinstance(grammar, Enum):

pypeg2/test/test_pyPEG2.py

         t = pypeg2.compose(x)
         self.assertEqual(t, "hello")
 
+class ComposeIntTestCase(ComposeTestCase):
+    def runTest(self):
+        x = pypeg2.compose(23, int)
+        self.assertEqual(x, "23")
+
 class C2(str):
     grammar = pypeg2.attr("some", "!"), pypeg2.restline
 
     pass
 
 
-__version__ = 2.9
+__version__ = 2.10
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
     try:
         grammar = type(thing).grammar
     except AttributeError:
-        if isinstance(thing, pypeg2.List):
-            grammar = pypeg2.csl(name())
+        if isinstance(thing, list):
+            grammar = pypeg2.csl(pypeg2.name())
         else:
-            grammar = word
+            grammar = pypeg2.word
 
     name = type(thing).__name__
 
         if object_names and e.name == "name":
             if name != type(thing).__name__:
                 continue
-        key, value = e.name, getattr(thing, e.name)
-        found = False
-        for tp in (str, int, float, complex, bool, bytes):
-            if isinstance(value, tp):
+        key, value = e.name, getattr(thing, e.name, None)
+        if value is not None:
+            if pypeg2._issubclass(e.thing, (str, int, pypeg2.Literal)) \
+                    or type(e.thing) == pypeg2._RegEx:
                 me.set(key, str(value))
-                found = True
-                break
-        if not found:
-            create_tree(value, me, object_names)
+            else:
+                create_tree(value, me, object_names)
 
     if isinstance(thing, list):
         things = thing
         thing = C()
     
     subs = iter(element)
+    iterated_already = False
 
     try:
         grammar = C.grammar
     else:
         for e in pypeg2.attributes(grammar):
             key = e.name
-            try:
-                value = element.attrib[e.name]
-            except KeyError:
-                sub = next(subs)
-                t = create_thing(sub, symbol_table)
-                setattr(thing, key, t)
+            if pypeg2._issubclass(e.thing, (str, int, pypeg2.Literal)) \
+                    or type(e.thing) == pypeg2._RegEx:
+                try:
+                    value = element.attrib[e.name]
+                except KeyError:
+                    pass
+                else:
+                    setattr(thing, key, e.thing(value))
             else:
-                setattr(thing, key, e.thing(value))
+                try:
+                    if not iterated_already:
+                        iterated_already = True
+                        sub = next(subs)
+                except StopIteration:
+                    pass
+                if sub.tag == e.thing.__name__:
+                    iterated_already = False
+                    t = create_thing(sub, symbol_table)
+                    setattr(thing, key, t)
 
     if issubclass(C, list) or issubclass(C, pypeg2.Namespace):
         try:
             while True:
-                sub = next(subs)
+                if iterated_already:
+                    iterated_alread = False
+                else:
+                    sub = next(subs)
                 t = create_thing(sub, symbol_table)
                 if isinstance(thing, pypeg2.List):
                     thing.append(t)
 from distutils.core import setup
 
-_version = '2.9.0'
+_version = '2.10.0'
 
 setup(
     name='pyPEG2',