Commits

Volker Birk  committed 26a991b

omit() function and documentation

  • Participants
  • Parent commits 61b0c10
  • Tags 2.12.0

Comments (0)

Files changed (3)

File docs/grammar_elements.en.yhtml2

         While composing the «separated» function has no effect.
         >>
 
+    h3 id=omit > Function omit()
+
+    h4 > Synopsis
+    p > «omit(*thing)»
+
+    p   >>
+        Omit what matches the grammar. This function cuts out «thing» and
+        throws it away.
+        >>
+
+    h4 > Parsing
+
+    p   >>
+        While parsing «omit()» cuts out what matches the grammar «thing» and 
+        throws it away.
+        >>
+
+    p > Example:
+
+    Code
+        ||
+        >>> p = parse("hello", omit(word))
+        >>> print(p)
+        None
+        >>> _
+        ||
+
+    h4 > Composing
+
+    p   >>
+        While composing «omit()» does not compose text for what matches the
+        grammar «thing».
+        >>
+
+    p > Example:
+
+    Code
+        ||
+        >>> compose('hello', omit(word))
+        ''
+        >>> _
+        ||
+
     h2 id=callbacks > Callback functions
     
     p   >>

File pypeg2/__init__.py

     return _card(-5, thing)
 
 
-def omit(grammar):
+def omit(*thing):
     """Omit what matches to the grammar."""
     return _card(-6, thing)
 
                 elif e == 0:
                     card = 1
                 elif e == -6:
-                    card = 0
+                    return 0
                 else:
                     card = min(e, 2)
             else:
             flag = True
             _min, _max = 1, 1
             contiguous = self._contiguous
+            omit = False
             for e in thing:
-                omit = False
                 if type(e) == int:
                     if e < -6:
                         raise GrammarValueError(
                     flag = False
                     break
                 _min, _max = 1, 1
+                omit = False
             if flag:
                 if self._contiguous and not contiguous:
                     self._contiguous = False

File pypeg2/test/test_pyPEG2.py

         self.assertEqual(str(r), "all")
         self.assertEqual(r._ignore1, None)
 
+class ParseOmitTestCase(ParserTestCase):
+    def runTest(self):
+        r = pypeg2.parse("hello", pypeg2.omit(pypeg2.word))
+        self.assertEqual(r, None)
+
 class ComposeTestCase(unittest.TestCase): pass
 
 class ComposeString(object):
         t = pypeg2.compose(r, C2)
         self.assertEqual(t, "!all")
 
+class ComposeOmitTestCase(ParserTestCase):
+    def runTest(self):
+        t = pypeg2.compose('hello', pypeg2.omit(pypeg2.word))
+        self.assertEqual(t, "")
+
 class CslPython32Compatibility(ParserTestCase):
     def runTest(self):
         try: