Commits

Anonymous committed 6fb8528

Python 2.7 compatibility

  • Participants
  • Parent commits 9246ee1
  • Tags 2.5.0

Comments (0)

Files changed (9)

 	make register
 
 register:
+	$(PYTHON) setup.py check
 	$(PYTHON) setup.py register
 
 test_docs:

docs/heading.en.yinc2

 div id="headline" {
     p > pyPEG – a PEG Parser-Interpreter in Python
     div class="small" {
-        "pyPEG 2.4.3 of Fr Jun 01 2012 – Copyleft 2009-2012, "
+        "pyPEG 2.5.0 of Mo Jun 11 2012 – Copyleft 2009-2012, "
         a "http://fdik.org", "Volker Birk";
     }
     div id=python1 p {
-        > for Python version 2.x see 
-        a href="http://fdik.org/pyPEG1" > pyPEG version 1
+        > This version supports Python 2.7 and 3.x.
     }
 }
 

pypeg2/__init__.py

 This program is under GNU General Public License 2.0.
 """
 
-__version__ = 2.4
+
+from __future__ import unicode_literals
+try:
+    range = xrange
+    str = unicode
+except NameError:
+    pass
+
+
+__version__ = 2.5
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
 
+
 import re
 import collections
 import sys
                 yield a
 
 
-class RegEx:
+class RegEx(object):
     """Regular Expression.
 
     Instance Variables:
     """
 
     def __init__(self, value):
-        self.regex = re.compile(value)
+        self.regex = re.compile(value, re.U)
         self.search = self.regex.search
         self.match = self.regex.match
         self.split = self.regex.split
         return type(self).__name__ + "(" + repr(self.pattern) + ")"
 
 
-class Literal:
+class Literal(object):
     """Literal value."""
     _basic_types = (bool, int, float, complex, str, bytes, bytearray, list,
             tuple, range, set, frozenset, dict)
 
     def __str__(self):
         if isinstance(self, Literal._basic_types):
-            return super().__str__()
+            return super(Literal, self).__str__()
         else:
             return str(self.value)
 
     def __repr__(self):
         if isinstance(self, Literal._basic_types):
-            return type(self).__name__ + "(" + super().__repr__() + ")"
+            return type(self).__name__ + "(" + super(Literal, self).__repr__() + ")"
         else:
             return type(self).__name__ + "(" + repr(self.value) + ")"
 
             if isinstance(args[0], str):
                 self.append(args[0])
             else:
-                super().__init__(args[0])
+                super(List, self).__init__(args[0])
         else:
-            super().__init__(args)
+            super(List, self).__init__(args)
 
         for k, v in kwargs:
             setattr(self, k, v)
 
     def __repr__(self):
         """x.__repr__() <==> repr(x)"""
-        return ''.join((type(self).__name__, "(", super().__repr__(), ")"))
+        return ''.join((type(self).__name__, "(", super(List, self).__repr__(), ")"))
 
 
-class _UserDict:
+class _UserDict(object):
     # collections.UserDict cannot be used because of metaclass conflicts
     def __init__(self, *args, **kwargs):
         self.data = dict(*args, **kwargs)
         else:
             if not(value.namespace):
                 value.namespace = weakref.ref(self)
-        super().__setitem__(key, value)
+        super(Namespace, self).__setitem__(key, value)
 
     def __delitem__(self, key):
         """x.__delitem__(y) <==> del x[y]"""
         self[key].namespace = None
-        super().__delitem__(key)
+        super(Namespace, self).__delitem__(key)
 
     def __repr__(self):
         """x.__repr__() <==> repr(x)"""
                 thing = Symbol(thing)
             if not isinstance(thing, Symbol):
                 raise TypeError(repr(thing) + " is not a Symbol")
-            super().__setitem__(thing.name, thing)
+            super(Enum, self).__setitem__(thing.name, thing)
 
     def __repr__(self):
         """x.__repr__() <==> repr(x)"""
         return False
 
 
-class Parser:
+class Parser(object):
     """Offers parsing and composing capabilities. Implements a Packrat parser.
     
     Instance variables:
             pos[0] += d_text.count("\n")
             pos[1] += len(d_text)
 
-        def has_grammar(thing):
-            try:
-                thing.grammar
-            except AttributeError:
-                return False
-            if isinstance(thing.grammar, Enum):
-                return False
-            else:
-                return True
-
         try:
             return self._memory[(text, id(thing))]
         except KeyError:
         if thing is None or type(thing) == FunctionType:
             result = text, None
 
-        elif isinstance(thing, Keyword):
+        elif isinstance(thing, Symbol):
             m = type(thing).regex.match(text)
             if m and m.group(0) == str(thing):
                 t, r = text[len(thing):], None
                         try:
                             for r in range(multiple):
                                 if isinstance(g, (str, Symbol, Literal)):
-                                    text.append(terminal_indent() + str(g))
+                                    text.append(self.compose(thing, g))
                                     if card < 1:
                                         break
                                 elif isinstance(g, FunctionType):

pypeg2/test/test_pyPEG2.py

+from __future__ import unicode_literals
+
 import unittest
 import pypeg2
 import re
 class ParseRegexTestCase1(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
-        r = parser.parse("hello, world", re.compile(r"h.[lx]l\S"))
+        r = parser.parse("hello, world", re.compile(r"h.[lx]l\S", re.U))
         self.assertEqual(r, (", world", "hello"))
 
 class ParseRegexTestCase2(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
         with self.assertRaises(SyntaxError):
-            r = parser.parse("hello, world", re.compile(r"\d"))
+            r = parser.parse("hello, world", re.compile(r"\d", re.U))
 
 class ParseSymbolTestCase1(ParserTestCase):
     def runTest(self):
 class ParseSomeTestCase1(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
-        r = parser.parse("hello, world", pypeg2.some(re.compile(r"\w")))
+        r = parser.parse("hello, world", pypeg2.some(re.compile(r"\w", re.U)))
         self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))
 
 class ParseSomeTestCase2(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
         with self.assertRaises(SyntaxError):
-            r = parser.parse("hello, world", pypeg2.some(re.compile(r"\d")))
+            r = parser.parse("hello, world", pypeg2.some(re.compile(r"\d", re.U)))
 
 class ParseMaybeSomeTestCase1(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
-        r = parser.parse("hello, world", pypeg2.maybe_some(re.compile(r"\w")))
+        r = parser.parse("hello, world", pypeg2.maybe_some(re.compile(r"\w", re.U)))
         self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))
 
 class ParseMaybeSomeTestCase2(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
-        r = parser.parse("hello, world", pypeg2.maybe_some(re.compile(r"\d")))
+        r = parser.parse("hello, world", pypeg2.maybe_some(re.compile(r"\d", re.U)))
         self.assertEqual(r, ('hello, world', []))
 
 class ParseCardinalityTestCase1(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
-        r = parser.parse("hello, world", (5, re.compile(r"\w")))
+        r = parser.parse("hello, world", (5, re.compile(r"\w", re.U)))
         self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))
 
 class ParseCardinalityTestCase2(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
         with self.assertRaises(SyntaxError):
-            r = parser.parse("hello, world", (6, re.compile(r"\w")))
+            r = parser.parse("hello, world", (6, re.compile(r"\w", re.U)))
 
 class ParseOptionsTestCase1(ParserTestCase):
     def runTest(self):
         parser = pypeg2.Parser()
-        r = parser.parse("hello, world", [re.compile(r"\d+"), pypeg2.word])
+        r = parser.parse("hello, world", [re.compile(r"\d+", re.U), pypeg2.word])
         self.assertEqual(r, (', world', 'hello'))
 
 class ParseOptionsTestCase2(ParserTestCase):
 
 class ParseListTestCase1(ParserTestCase):
     class Chars(pypeg2.List):
-        grammar = pypeg2.some(re.compile(r"\w")), pypeg2.attr("comma", ",")
+        grammar = pypeg2.some(re.compile(r"\w", re.U)), pypeg2.attr("comma", ",")
 
     def runTest(self):
         parser = pypeg2.Parser()
 
 class ParseListTestCase2(ParserTestCase):
     class Digits(pypeg2.List):
-        grammar = pypeg2.some(re.compile(r"\d"))
+        grammar = pypeg2.some(re.compile(r"\d", re.U))
 
     def runTest(self):
         parser = pypeg2.Parser()
         self.assertTrue(r[1].polished)
         self.assertEqual(r[1].comma, None)
 
-class Parm:
+class Parm(object):
     grammar = pypeg2.name(), "=", pypeg2.attr("value", int)
 
 class Parms(pypeg2.Namespace):
 
 class ComposeTestCase(unittest.TestCase): pass
 
-class ComposeString:
+class ComposeString(object):
     grammar = "something"
 
 class ComposeStringTestCase(ComposeTestCase):
         t = pypeg2.compose(x)
         self.assertEqual(t, "something")
 
-class ComposeKeyword:
+class ComposeKeyword(object):
     grammar = pypeg2.K("hallo")
 
 class ComposeKeywordTestCase(ComposeTestCase):
         t = pypeg2.compose(x)
         self.assertEqual(t, "hello")
 
-class ComposeAttribute:
+class ComposeAttribute(object):
     grammar = pypeg2.name()
 
 class ComposeAttributeTestCase(ComposeTestCase):
         t = pypeg2.compose(x)
         self.assertEqual(t, "hello")
 
-class ComposeFlag:
+class ComposeFlag(object):
     grammar = pypeg2.flag("mark", "MARK")
 
 class ComposeFlagTestCase1(ComposeTestCase):
         self.assertEqual(t, "hello, world")
 
 class ComposeList(str):
-    grammar = [ re.compile(r"\d+"), pypeg2.word ]
+    grammar = [ re.compile(r"\d+", re.U), pypeg2.word ]
 
 class ComposeListTestCase(ComposeTestCase):
     def runTest(self):

pypeg2/test/test_xmlast.py

+from __future__ import unicode_literals
+try:
+    str = unicode
+except NameError:
+    pass
+
 import unittest
 import re, sys
 import pypeg2, pypeg2.xmlast
 
-class Another:
+class Another(object):
     grammar = pypeg2.name(), "=", pypeg2.attr("value")
 
 class Something(pypeg2.List):
 
 class Instruction(str): pass
 
-class Parameter:
+class Parameter(object):
     grammar = pypeg2.attr("typing", str), pypeg2.name()
 
 class Parameters(pypeg2.Namespace):
 This program is under GNU General Public License 2.0.
 """
 
-__version__ = 2.4
+
+from __future__ import unicode_literals
+try:
+    str = unicode
+except NameError:
+    pass
+
+
+__version__ = 2.5
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
 
+
 try:
     import lxml
     from lxml import etree

samples/sample1.py

 >>> f = parse("int f(int a, long b) { do_this; do_that; }", Function, comment=comment_c)
 
 Because function has a name() in its grammar, we can access this now as an
-attribute.
+attribute. With Python 2.7 this gives Symbol(u'f'), with Python 3.2 it gives Symbol('f'):
 
 >>> f.name
-Symbol('f')
+Symbol(...'f')
 
 A Function has an Attribute "parms" in its grammar, which directs to class
 Parameters.
 
 >>> f.parms
-Parameters([(Symbol('a'), <__main__.Parameter object at 0x...>), (Symbol('b'), <__main__.Parameter object at 0x...>)])
+Parameters([(Symbol(...'a'), <__main__.Parameter object at 0x...>), (Symbol(...'b'), <__main__.Parameter object at 0x...>)])
 
 Because Parameters is a Namespace, we can access its content by name.
 
 Its content are Parameter instances. Parameter has an Attribute "typing".
 
 >>> f.parms["b"].typing
-Type('long')
+Type(...'long')
 
 The Instructions of our small sample are just words. Because Function is a
 List, we can access them one by one.
 
 >>> f
-Function(['do_this', 'do_that'])
->>> f[0]
-'do_this'
+Function([...'do_this', ...'do_that'])
+>>> print("f is " + repr(f[0]))
+f is ...'do_this'
 
 The result can be composed to a text again.
 
 >>> xml = b'<Function typing="long" name="g"><Parameters><Parameter name="x" typing="int"/></Parameters><Instruction>return</Instruction></Function>'
 >>> g = xml2thing(xml, globals())
 >>> g.name
-Symbol('g')
+Symbol(...'g')
 >>> g.typing
-Type('long')
+Type(...'long')
 >>> g.parms["x"].typing
-Type('int')
->>> g[0]
-'return'
+Type(...'int')
+>>> print("g[0] is " + repr(g[0]))
+g[0] is ...'return'
 """
 
+from __future__ import unicode_literals, print_function
 from pypeg2 import *
 
 # A Symbol can be an arbitrary word or one word of an Enum.
 # compose(). parse() ignores callback functions. blank inserts " ".
 # name() generates a name attribute.
 
-class Parameter:
+class Parameter(object):
     grammar = attr("typing", Type), blank, name()
 
 # A Namespace is a container for named things.

samples/sample2.py

 Because IniFile and Section are Namespaces, we can access their content by
 name.
 
->>> ini_file["Number 1"]["that"]
-'something else'
+>>> print("found: " + repr(ini_file["Number 1"]["that"]))
+found: ...'something else'
 
 pyPEG is measuring the position of each object in the input text with a
 tuple (line_number, offset).
 ...
 """
 
+from __future__ import unicode_literals, print_function
 from pypeg2 import *
 import re
 
 
 setup(
     name='pyPEG2',
-    version='2.4.1',
+    version='2.5.0',
     author='Volker Birk',
     author_email='vb@dingens.org',
     packages=['pypeg2', 'pypeg2.test'],