Commits

Anonymous committed 42a8a06

adding features: contiguous() and separated() functions

Comments (0)

Files changed (5)

docs/grammar_elements.en.yhtml2

         ◊    second;◊
         }
         ||
+
+    h3 id=contiguous > Function contiguous()
+
+    h4 > Synopsis
+    p > «contiguous(*thing)»
+
+    p   >>
+        Temporary disable automated whitespace removing while parsing «thing».
+        >>
+
+    h4 > Parsing
+
+    p   >>
+        While parsing whitespace removing is disabled. That means, if
+        whitespace is not part of the grammar, it will lead to a «SyntaxError»
+        if whitespace will be found between the parsed objects.
+        >>
+
+    p > Example:
+
+    Code
         ||
+        class Path(List):
+            grammar = flag("relative", "."), maybe_some(Symbol, ".")
+
+        class Reference(GrammarElement):
+            grammar = ◊contiguous(◊attr("path", Path), name()◊)◊
+        ||
+
+    h4 > Composing
+
+    p   >>
+        While composing the «contiguous» function has no effect.
+        >>
+
+    h3 id=separated > Function separated()
+
+    h4 > Synopsis
+    p > «separated(*thing)»
+
+    p   >>
+        Temporary enable automated whitespace removing while parsing «thing».
+        Whitespace removing is enabled by default. This function is for
+        temporary enabling whitespace removing after it was disabled with the
+        ¬#contiguous contiguous function¬.
+        >>
+
+    h4 > Parsing
+
+    p   >>
+        While parsing whitespace removing is enabled again. That means, if
+        whitespace is not part of the grammar, it will be omitted if whitespace
+        will be found between parsed objects.
+        >>
+
+    h4 > Composing
+
+    p   >>
+        While composing the «separated» function has no effect.
+        >>
 
     h2 id=callbacks > Callback functions
     

docs/heading.en.yinc2

 div id="headline" {
     p > pyPEG – a PEG Parser-Interpreter in Python
     div class="small" {
-        "pyPEG 2.5.7 of Tu Jun 19 2012 – Copyleft 2009-2012, "
+        "pyPEG 2.6.0 of We Jun 20 2012 – Copyleft 2009-2012, "
         a "http://fdik.org", "Volker Birk";
     }
     div id=python1 p

pypeg2/__init__.py

     pass
 
 
-__version__ = 2.5
+__version__ = 2.6
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
     return _card(-3, thing)
 
 
+def contiguous(*thing):
+    """Disable automated whitespace matching.
+    Inserts -4 as cardinality before thing.
+    """
+    return _card(-4, thing)
+
+
+def separated(*thing):
+    """Enable automated whitespace matching.
+    Inserts -5 as cardinality before thing.
+    """
+    return _card(-5, thing)
+
+
 endl = lambda thing, parser: "\n"
 """End of line marker for composing text."""
 
         length, card = 0, 1
         for e in grammar:
             if type(e) == int:
-                if e < -3:
+                if e < -5:
                     raise GrammarValueError(
                         "illegal cardinality value in grammar: " + str(e))
-                if e in (-1, -2):
+                if e in (-5, -4, -3):
+                    pass
+                elif e in (-1, -2):
                     card = 2
-                elif e in (-3, 0):
+                elif e == 0:
                     card = 1
                 else:
                     card = min(e, 2)
         self.filename = None
         self._memory = {}
         self._got_endl = True
+        self._contiguous = False
 
     def parse(self, text, thing, filename=None):
         """(Partially) parse text following thing as grammar and return the
         t2 = None
         t = text
         while t2 != t:
-            if self.whitespace:
+            if self.whitespace and not self._contiguous:
                 t, r = self._parse(t, self.whitespace, pos)
             t2 = t
             if self.comment:
 
     def _parse(self, text, thing, pos=[1, 0]):
         # Parser implementation
+        parse = None
 
         def update_pos(text, t, pos):
             # Calculate where we are in the text
                     result = t, r
                     update_pos(text, t, pos)
             else:
-                result = text, syntax_error("expecting " + type(thing).__name__)
+                result = text, syntax_error("expecting " + thing.__name__)
 
         # non-terminal constructs
 
             t = text
             flag = True
             _min, _max = 1, 1
+            contiguous = self._contiguous
             for e in thing:
                 if type(e) == int:
-                    if e < -3:
+                    if e < -5:
                         raise GrammarValueError(
                             "illegal cardinality value in grammar: " + str(e))
-                    if e == -3:
-                        _min, _max = 1, 1
+                    if e == -5:
+                        self._contiguous = False
+                        t = self._skip(t)
+                    elif e == -4:
+                        self._contiguous = True
+                    elif e == -3:
+                        pass
                     elif e == -2:
                         _min, _max = 1, sys.maxsize
                     elif e == -1:
                     break
                 _min, _max = 1, 1
             if flag:
+                if self._contiguous and not contiguous:
+                    self._contiguous = False
+                    t = self._skip(t)
                 if len(L) > 1 or how_many(thing) > 1:
                     result = t, L
                 elif not L:
                     result = t, L[0]
             else:
                 result = text, r
+            self._contiguous = contiguous
 
         elif isinstance(thing, list):
             found = False
                             self.indention_level -= indenting
                             self.indenting = 0
                     elif type(g) == int:
-                        if g < -3:
+                        if g < -5:
                             raise GrammarValueError(
                                 "illegal cardinality value in grammar: " +
                                 str(g)
                         card = g
                         if g in (-2, -1):
                             multiple = sys.maxsize
-                        elif g in (-3, 0):
+                        elif g in (-5, -4, -3, 0):
                             multiple = 1
                             if g == -3:
                                 self.indention_level += 1
     pass
 
 
-__version__ = 2.5
+__version__ = 2.6
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
 
 setup(
     name='pyPEG2',
-    version='2.5.7',
+    version='2.6.0',
     author='Volker Birk',
     author_email='vb@dingens.org',
     packages=['pypeg2', 'pypeg2.test'],