1. hideki nara
  2. tk.phpautodoc

Commits

Takeshi Komiya  committed 4475289

Handle "parse error"

  • Participants
  • Parent commits 992f429
  • Branches default

Comments (0)

Files changed (3)

File src/sphinxcontrib_phpautodoc.py

View file
             msg = '%s cannot read source code: %s' % (self.directive_name, self.options['filename'])
             return [self.state.document.reporter.warning(msg, line=self.lineno)]
 
-        tree = self.parse_code(filename)
-        self.traverse(tree)
-        self.state.document.settings.env.note_dependency(filename)
+        try:
+            tree = self.parse_code(filename)
+            self.traverse(tree)
+            self.state.document.settings.env.note_dependency(filename)
 
-        if self.content:
-            for line in self.content:
-                self.add_line(line, 1)
+            if self.content:
+                for line in self.content:
+                    self.add_line(line, 1)
+
+                self.add_line('')
 
             self.add_line('')
 
-        self.add_line('')
+            node = nodes.paragraph()
+            node.document = self.state.document
+            self.state.nested_parse(self.result, 0, node)
 
-        node = nodes.paragraph()
-        node.document = self.state.document
-        self.state.nested_parse(self.result, 0, node)
-
-        return node.children
+            return node.children
+        except SyntaxError as exc:
+            msg = 'phpautodoc parse error [%s]: %s' % (filename, exc)
+            return [self.state_machine.reporter.warning(msg, line=self.lineno)]
 
     def traverse(self, tree, indent=0):
         pass

File tests/inputs/syntax_error.php

View file
+<?php
+
+function doc_hello() {

File tests/run_tests.py

View file
 
 import os
 import re
+import sys
 import shutil
 import unittest
+from cStringIO import StringIO
 from mock import Mock, patch
 from tempfile import mkdtemp
 from docutils.parsers.rst import Parser
     def tearDown(self):
         shutil.rmtree(self.settings.env.doctreedir)
 
+    def test_syntax_error(self):
+        try:
+            orig_stderr = sys.stderr
+            sys.stderr = StringIO()
+
+            doc = new_document('<test>', self.settings)
+            src = ".. phpautomodule::\n   :filename: inputs/syntax_error.php\n"
+            self.parser.parse(src, doc)
+        finally:
+            sys.stderr = orig_stderr
+
     @classmethod
     def append(cls, name):
         @patch("sphinxcontrib_phpautodoc.ViewList")