Commits

Volker Birk  committed 24857af

release 2.1: ignore() function improved

  • Participants
  • Parent commits 0d93625

Comments (0)

Files changed (9)

 
 deploy: dist
 	rm -f pyPEG2.tar.gz
-	ln -s dist/pyPEG2-*.tar.gz pyPEG2.tar.gz
+	ln -s `ls dist/pyPEG2-*.tar.gz | tail -n1` pyPEG2.tar.gz
 	scp docs/*.html docs/format.css pyPEG2.tar.gz *.txt samples/* dragon:fdik.org/pyPEG2/
 
 register:

File docs/grammar_elements.en.yhtml2

 
     p > Ignore what matches to the grammar.
 
+    h4 > Parsing
+
     p   >>
-        Usually you shouldn't use this function. It is for some handy tricks
-        only. In common grammars it will not be there.
+        Parse what's to be ignored. The result is added to an attribute
+        named «"_ignore" + str(i)» with i as a serial number.
+        >>
+
+    h4 > Composing
+
+    p   >>
+        Compose the result as with any «attr()».
         >>
 
     h3 id=indent > Function indent()

File docs/heading.en.yinc2

 div id="headline" {
     p > pyPEG – a PEG Parser-Interpreter in Python
     div class="small" {
-        "pyPEG 2.0 of Mo May 21 2012 – Copyleft 2009-2012, "
+        "pyPEG 2.1 of Mi May 23 2012 – Copyleft 2009-2012, "
         a "http://fdik.org", "Volker Birk";
     }
     div id=python1 p {

File docs/parser_engine.en.yhtml2

     h3 id=attributes > Function attributes()
 
     h4 > Synopsis
-    p > «attributes(grammar)»
+    p > «attributes(grammar, invisible=False
 
     p > Iterates all attributes of a «grammar».
 
     p   >>
         This function can be used to iterate through all attributes which
-        will be generated for the top level object of the «grammar».
+        will be generated for the top level object of the «grammar». If
+        invisible is «False» omit attributes whose names are starting with
+        an underscore «_».
         >>
 
     p > Example:

File pypeg2/__init__.py

 This program is under GNU General Public License 2.0.
 """
 
-__version__ = 2.0
+__version__ = 2.1
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
     return attr(name, thing, "Flag")
 
 
-def attributes(grammar):
+def attributes(grammar, invisible=False):
     """Iterates all attributes of a grammar."""
-    if type(grammar) == attr.Class:
+    if type(grammar) == attr.Class and (invisible or grammar.name[0] != "_"):
         yield grammar
     elif type(grammar) == tuple:
         for e in grammar:
-            for a in attributes(e):
+            for a in attributes(e, invisible):
                 yield a
 
 
 
     def __setitem__(self, key, value):
         """x.__setitem__(i, y) <==> x[i]=y"""
-        value.name = key
+        value.name = Symbol(key)
         try:
             value.namespace
         except AttributeError:
     return attr("name", Symbol)
 
 
-class _Ignore:
-    pass
-
-
-def ignore(*grammar):
+def ignore(grammar):
     """Ignore what matches to the grammar."""
 
     try:
         ignore.serial += 1
     except AttributeError:
         ignore.serial = 1
-    return type("_Ignore" + str(ignore.serial), (_Ignore,),
-            dict(grammar=optional(grammar)))
+    return attr("_ignore" + str(ignore.serial), grammar)
 
 
 def indent(*thing):
     elif isinstance(grammar, attr.Class):
         return 0
 
-    elif _issubclass(grammar, _Ignore) or type(grammar) == FunctionType:
+    elif type(grammar) == FunctionType:
         return 0
 
     elif _issubclass(grammar, object):
             else:
                 result = text, r
 
-        elif _issubclass(thing, _Ignore):
-            t, r = self._parse(text, thing.grammar, pos)
-            if type(r) == SyntaxError:
-                result = t, r
-            else:
-                result = t, None
-
         elif _issubclass(thing, object):
             try:
                 g = thing.grammar
                 else:
                     result = ""
             else:
-                result = self.compose(getattr(thing, grammar.name))
+                result = self.compose(getattr(thing, grammar.name), grammar.thing)
 
         elif type(grammar) == list:
             found = False
             else:
                 result = compose_tuple(thing, thing, grammar)
 
-        elif _issubclass(grammar, _Ignore):
-            result = ""
-
         elif _issubclass(grammar, object):
             if isinstance(thing, grammar):
                 try:

File pypeg2/test/test_pyPEG2.py

         with self.assertRaises(SyntaxError):
             t, r = parser.parse("float", EnumTest)
 
+class ParseInvisibleTestCase(ParserTestCase):
+    class C1(str):
+        grammar = pypeg2.ignore("!"), pypeg2.restline
+    def runTest(self):
+        r = pypeg2.parse("!all", type(self).C1)
+        self.assertEqual(str(r), "all")
+        self.assertEqual(r._ignore1, None)
+
 class ComposeTestCase(unittest.TestCase): pass
 
 class ComposeString:
 class ComposeAttributeTestCase(ComposeTestCase):
     def runTest(self):
         x = ComposeAttribute()
-        x.name = "hello"
+        x.name = pypeg2.Symbol("hello")
         t = pypeg2.compose(x)
         self.assertEqual(t, "hello")
 
         t = pypeg2.compose(x)
         self.assertEqual(t, "hello")
 
+class C2(str):
+    grammar = pypeg2.attr("some", "!"), pypeg2.restline
+
+class ComposeInvisibleTestCase(ParserTestCase):
+    def runTest(self):
+        r = pypeg2.parse("!all", C2)
+        self.assertEqual(str(r), "all")
+        self.assertEqual(r.some, None)
+        t = pypeg2.compose(r, C2)
+        self.assertEqual(t, "!all")
+
 if __name__ == '__main__':
     unittest.main()

File pypeg2/test/test_xmlast.py

         else:
             self.assertEqual(pypeg2.xmlast.etree.tostring(root), b'<SomethingElse name="hello"><Another name="bla" value="blub"/><Another name="foo" value="bar"/></SomethingElse>')
 
+class Thing2XMLTestCase3(unittest.TestCase):
+    class C1(str):
+        grammar = pypeg2.ignore("!"), pypeg2.restline
+    def runTest(self):
+        r = pypeg2.parse("!all", type(self).C1)
+        xml = pypeg2.xmlast.thing2xml(r)
+        self.assertEqual(xml, b"<C1>all</C1>")
+
 class Key(str):
     grammar = pypeg2.name(), "=", pypeg2.restline
 

File pypeg2/xmlast.py

 This program is under GNU General Public License 2.0.
 """
 
-__version__ = 2.0
+__version__ = 2.1
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
 
 setup(
     name='pyPEG2',
-    version='2.0.0',
+    version='2.1.0',
     author='Volker Birk',
     author_email='vb@dingens.org',
     packages=['pypeg2', 'pypeg2.test'],