[pypy-svn] r49295 - pypy/dist/pypy/rlib/parsing/test
cfbolz at codespeak.net
cfbolz at codespeak.net
Sun Dec 2 21:02:13 CET 2007
Author: cfbolz
Date: Sun Dec 2 21:02:12 2007
New Revision: 49295
Modified:
pypy/dist/pypy/rlib/parsing/test/test_pypackrat.py
Log:
just for the fun of it: write a future parser with the python packrat parser
generator. it's not really any more readable than the one we now use, actually.
Modified: pypy/dist/pypy/rlib/parsing/test/test_pypackrat.py
==============================================================================
--- pypy/dist/pypy/rlib/parsing/test/test_pypackrat.py (original)
+++ pypy/dist/pypy/rlib/parsing/test/test_pypackrat.py Sun Dec 2 21:02:12 2007
@@ -472,3 +472,80 @@
assert expected == ['a', 'x', 'y']
+ def test_python_future(self):
+ class parser(PackratParser):
+ r"""
+ comment:
+ `#[^\r\n]*` lineend;
+ lineend:
+ `(\r|\n)+`;
+ docstring:
+ `(\"\"\"[^\\]*(\\[^\\]+)*\"\"\")|(\'\'\'[^\\]*(\\[^\\]+)*\'\'\')`
+ ignore*
+ | `(\"[^\\]*(\\[^\\]+)*\")|(\'[^\\]*(\\[^\\]+)*\')`
+ ignore*;
+ ignore:
+ `[ \t]+`;
+ ignoreline:
+ `[ \t]*[\r\n]+`;
+ fromimport:
+ 'from' ignore+
+ '__future__' ignore+
+ 'import' ignore+
+ what;
+ identifier:
+ `[a-zA-Z0-9_]+`;
+ what:
+ '(' ignoreline*
+ g = group
+ ignoreline*
+ rest = ([',' ignoreline*] group)*
+ ')'
+ return {[g] + rest}
+ | g = group
+ rest = ([',' ignore*] group)*
+ return {[g] + rest};
+ group:
+ name = identifier ignore+ 'as' ignore+ identifier ignore*
+ return {name}
+ | name = identifier ignore*
+ return {name};
+ line:
+ comment
+ return {None}
+ | docstring lineend
+ return {None}
+ | ignore lineend
+ return {None}
+ | t = fromimport
+ ignore*
+ lineend
+ return {t};
+ header:
+ l = line*
+ return {[elt for sublist in l if sublist is not None for elt in sublist]};
+ """
+ p = parser("#\n")
+ lines = p.header()
+ assert lines == []
+ p = parser('''"abc"\n''')
+ lines = p.header()
+ assert lines == []
+ p = parser(''''abc'\n''')
+ lines = p.header()
+ assert lines == []
+ p = parser(''''abc'\n''')
+ lines = p.header()
+ assert lines == []
+ p = parser('''from __future__ import division\n''')
+ lines = p.header()
+ assert lines == ['division']
+ p = parser('''from __future__ import division, generators\n''')
+ lines = p.fromimport()
+ assert lines == ['division', 'generators']
+ p = parser('''from __future__ import (division, \ngenerators)\n''')
+ lines = p.fromimport()
+ assert lines == ['division', 'generators']
+ p = parser('''from __future__ import (division as d, \ngenerators)\n''')
+ lines = p.fromimport()
+ assert lines == ['division', 'generators']
More information about the Pypy-commit
mailing list