[pypy-svn] r42570 - in pypy/dist/pypy/lang/js: . test
santagada at codespeak.net
santagada at codespeak.net
Wed May 2 05:57:04 CEST 2007
Author: santagada
Date: Wed May 2 05:57:03 2007
New Revision: 42570
Modified:
pypy/dist/pypy/lang/js/jsgrammar.txt
pypy/dist/pypy/lang/js/newparser.py
pypy/dist/pypy/lang/js/test/test_new_parser.py
Log:
started testing of expressions... we are getting closer to the point where this parser is going to take the place of using narcissus + spidermonkey
Modified: pypy/dist/pypy/lang/js/jsgrammar.txt
==============================================================================
--- pypy/dist/pypy/lang/js/jsgrammar.txt (original)
+++ pypy/dist/pypy/lang/js/jsgrammar.txt Wed May 2 05:57:03 2007
@@ -15,9 +15,9 @@
statement : <block>
| <variablestatement> [";"]
- | [ ";" ]
+ | <emptystatement>
| <expressionstatement> [";"]
- | <ifstatement> [";"]
+ | <ifstatement> [";"]*
| <iterationstatement> [";"]
| <continuestatement> [";"]
| <breakstatement> [";"]
@@ -49,6 +49,9 @@
initialiser : ["="] assignmentexpression
;
+emptystatement : ";"
+ ;
+
expressionstatement : expression
;
@@ -88,7 +91,11 @@
;
# XXX this looks wrong to me
-caseclauses : caseclause caseclauses
+# ans: it was defined in the spec as
+# CaseClauses :
+# CaseClause
+# CaseClauses CaseClause
+caseclauses : caseclause+
;
caseclause : ["case"] expression [":"] statementlist?
Modified: pypy/dist/pypy/lang/js/newparser.py
==============================================================================
--- pypy/dist/pypy/lang/js/newparser.py (original)
+++ pypy/dist/pypy/lang/js/newparser.py Wed May 2 05:57:03 2007
@@ -15,7 +15,7 @@
newrules = [Rule("hacked_first_symbol", [[start, "EOF"]])] + rules
return newrules
-parse = make_parse_function(regexs, setstartrule(rules, start="expression"), eof=True)
+parse = make_parse_function(regexs, setstartrule(rules, start="statement"), eof=True)
#parse = make_parse_function(regexs, rules)
print rules[2].nonterminal
Modified: pypy/dist/pypy/lang/js/test/test_new_parser.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_new_parser.py (original)
+++ pypy/dist/pypy/lang/js/test/test_new_parser.py Wed May 2 05:57:03 2007
@@ -53,7 +53,7 @@
self.counts = {}
def general_nonterminal_visit(self, node):
- print node
+ print node.symbol
self.counts[node.symbol] = self.counts.get(node.symbol, 0) + 1
for child in node.children:
self.dispatch(child)
@@ -157,6 +157,8 @@
])
self.parse_and_compare("++5", 6)
self.parse_and_compare("~3", -4)
+ self.parse("x = 3")
+ self.parse("x")
def test_chained(self):
self.parse_and_eval_all(["1 + 2 * 3",
@@ -167,3 +169,39 @@
"30 | 3 & 5",
])
+class TestStatements(BaseGrammarTest):
+ def setup_class(cls):
+ cls.parse = parse_func('statement')
+
+ def parse_count(self, s):
+ "parse the expression and return the CountingVisitor"
+ cv = CountingVisitor()
+ self.parse(s).visit(cv)
+ return cv.counts
+
+ def test_block(self):
+ r = self.parse_count("{x;return;true;if(x);}")
+ assert r['block'] == 1
+
+ def test_vardecl(self):
+ r = self.parse_count("var x;")
+ assert r['variablestatement'] == 1
+
+ r = self.parse_count("var x = 2;")
+ assert r['variablestatement'] == 1
+
+ def test_empty(self):
+ for i in range(1,10):
+ r = self.parse_count('{%s}'%(';'*i))
+ assert r['emptystatement'] == i
+
+ def test_if(self):
+ r = self.parse_count("if(x)return;;")
+ assert r['ifstatement'] == 1
+ assert r.get('emptystatement', 0) == 0
+ r = self.parse_count("if(x)if(i)return;")
+ assert r['ifstatement'] == 2
+ r = self.parse_count("if(x)return;else return;")
+ assert r['ifstatement'] == 1
+
+
More information about the pypy-svn
mailing list