diff options
author | Eli Bendersky <eliben@gmail.com> | 2012-01-24 06:06:24 +0200 |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2012-01-24 06:06:24 +0200 |
commit | bf4fae89bcf4ac3452453b8adce308b42ed8cbe0 (patch) | |
tree | 157056135ccd5f86f74db250f53644947aa8fc13 | |
parent | 9899a3e3bb845b580d6dce1f323409ce09fc103e (diff) | |
download | pycparser-bf4fae89bcf4ac3452453b8adce308b42ed8cbe0.tar.gz |
fix the case where the first statement in a file is empty (just a semicolon). Fix provided by Andreas Kloeckner
-rw-r--r-- | examples/c-to-c.py | 3 | ||||
-rw-r--r-- | pycparser/c_parser.py | 4 | ||||
-rw-r--r-- | tests/test_c_generator.py | 2 | ||||
-rw-r--r-- | tests/test_c_parser.py | 4 |
4 files changed, 11 insertions, 2 deletions
diff --git a/examples/c-to-c.py b/examples/c-to-c.py index ca76cc5..c40d162 100644 --- a/examples/c-to-c.py +++ b/examples/c-to-c.py @@ -33,6 +33,7 @@ def zz_test_translate(): int main(void) { unsigned int long k = 4; + int p = - - k; return 0; } ''' @@ -52,7 +53,7 @@ int main(void) #------------------------------------------------------------------------------ if __name__ == "__main__": - #zz_test_translate() + zz_test_translate() if len(sys.argv) > 1: translate_to_c(sys.argv[1]) else: diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py index 7205842..75381ca 100644 --- a/pycparser/c_parser.py +++ b/pycparser/c_parser.py @@ -355,7 +355,9 @@ class CParser(PLYParser): def p_translation_unit_2(self, p): """ translation_unit : translation_unit external_declaration """ - if p[2] is not None: + if p[1].ext is None: + p[1].ext = p[2] + elif p[2] is not None: p[1].ext.extend(p[2]) p[0] = p[1] diff --git a/tests/test_c_generator.py b/tests/test_c_generator.py index 0606d6a..e321bb6 100644 --- a/tests/test_c_generator.py +++ b/tests/test_c_generator.py @@ -79,11 +79,13 @@ class TestCtoC(unittest.TestCase): }''') def test_statements(self): + # note two minuses here self._assert_ctoc_correct(r''' int main() { int a; a = 5; ; + b = - - a; return a; }''') diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py index 1abf1cb..f14d542 100644 --- a/tests/test_c_parser.py +++ b/tests/test_c_parser.py @@ -113,6 +113,10 @@ class TestCParser_fundamentals(TestCParser_base): self.failUnless(isinstance(t2, FileAST)) self.assertEqual(len(t2.ext), 0) + # First statement empty + t = self.parse('; char c;') + self.assertEqual(len(t.ext), 1) + def test_empty_toplevel_decl(self): code = 'int foo;;' t = self.parse(code) |