Commits

Anonymous committed 99175f0

generate_syntax_error() method added for generating pretty error messages

Comments (0)

Files changed (6)

docs/grammar_elements.en.yhtml2

 
     Code | from __future__ import print_function
 
+    h2 id=common > Common methods for grammar elements
+
+    p   >>
+        If a method of the following is present in a grammar element, it will
+        override the standard behaviour.
+        >>
+
+    h3 id=override_parse > parse() class method of a grammar element
+
+    h4 > Synopsis
+    p > «parse(cls, parser, text, pos)»
+
+    p   >>
+        Overwrites the parsing behaviour. If present, this class method is
+        called at each place the grammar references the grammar element instead
+        of automatic parsing.
+        >>
+
+    glossary {
+        term "cls" > class object of the grammar element
+        term "parser" > parser object which is calling
+        term "text" > text to be parsed
+        term "pos" > «(lineNo, charInText)» with positioning information
+    }
+
+    h3 id=override_compose > compose() method of a grammar element
+
     h2 id=basic > Basic Grammar Elements
 
     h3 id=literals > str instances and Literal

docs/heading.en.yinc2

 div id="headline" {
     p > pyPEG – a PEG Parser-Interpreter in Python
     div class="small" {
-        "pyPEG 2.11.1 of Sa May 04 2013 – Copyleft 2009-2013, "
+        "pyPEG 2.12.0 of Tu Jul 02 2013 – Copyleft 2009-2013, "
         a "http://fdik.org", "Volker Birk";
     }
     div id=python1 p

docs/parser_engine.en.yhtml2

         'hello, world'
         ||
 
+    h3 id=gen_syntax_error > Method generate_syntax_error()
+
+    h4 > Synopsis
+    p > «generate_syntax_error(self, msg, pos)»
+
+    p > Generate a syntax error construct.
+
+    glossary {
+        term "msg" > string with error message
+        term "pos" > «(lineNo, charInText)» with positioning information
+    }
+
+    h4 > Returns
+    p > Instance of «SyntaxError»
+
     h2 id=convenience > Convenience functions
 
     h3 id=parse > Function parse()

pypeg2/__init__.py

     pass
 
 
-__version__ = 2.11
+__version__ = 2.12
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
                 t, r = self._parse(t, self.comment, pos)
         return t
 
+    def generate_syntax_error(self, msg, pos):
+            """Generate a syntax error construct with
+
+            msg         string with error message
+            pos         (lineNo, charInText) with positioning information
+            """
+
+            result = SyntaxError(msg)
+            if pos:
+                result.lineno = pos[0]
+                start = max(pos[1] - 19, 0)
+                end   = min(pos[1] + 20, len(self.text))
+                result.text = self.text[start:end]
+                result.offset = pos[1] - start + 1
+                while "\n" in result.text:
+                    lf = result.text.find("\n")
+                    if lf >= result.offset:
+                        result.text = result.text[:result.offset-1]
+                        break;
+                    else:
+                        L = len(result.text)
+                        result.text = result.text[lf+1:]
+                        result.offset -= L - len(result.text)
+                if self.filename:
+                    result.filename = self.filename
+            return result
+
     def _parse(self, text, thing, pos=[1, 0]):
         # Parser implementation
 
             current_pos = None
 
         def syntax_error(msg):
-            # Create a syntax error construct with sensible attributes
-            result = SyntaxError(msg)
-            if pos:
-                result.lineno = pos[0]
-                start = max(pos[1] - 19, 0)
-                end   = min(pos[1] + 20, len(self.text))
-                result.text = self.text[start:end]
-                result.offset = pos[1] - start + 1
-                while "\n" in result.text:
-                    lf = result.text.find("\n")
-                    if lf >= result.offset:
-                        result.text = result.text[:result.offset-1]
-                        break;
-                    else:
-                        L = len(result.text)
-                        result.text = result.text[lf+1:]
-                        result.offset -= L - len(result.text)
-                if self.filename:
-                    result.filename = self.filename
-            return result
+            return self.generate_syntax_error(msg, pos)
+
+        try:
+            thing.parse
+        except AttributeError:
+            pass
+        else:
+            t, r = thing.parse(self, text, pos)
+            if not isinstance(r, SyntaxError):
+                t = self._skip(t)
+                update_pos(text, t, pos)
+            return t, r
 
         # terminal symbols
 
         except AttributeError:
             pass
         else:
-            return terminal_indent() + thing.compose()
+            return terminal_indent() + thing.compose(self)
 
         if not grammar:
             try:
     pass
 
 
-__version__ = 2.11
+__version__ = 2.12
 __author__ = "Volker Birk"
 __license__ = "This program is under GNU General Public License 2.0."
 __url__ = "http://fdik.org/pyPEG"
 from distutils.core import setup
 
-_version = '2.11.1'
+_version = '2.12.0'
 
 setup(
     name='pyPEG2',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.