| 143 | ) |
| 144 | |
| 145 | def test_c_parser(self) -> None: |
| 146 | grammar_source = """ |
| 147 | start[mod_ty]: a[asdl_stmt_seq*]=stmt* $ { _PyAST_Module(a, NULL, p->arena) } |
| 148 | stmt[stmt_ty]: a=expr_stmt { a } |
| 149 | expr_stmt[stmt_ty]: a=expression NEWLINE { _PyAST_Expr(a, EXTRA) } |
| 150 | expression[expr_ty]: ( l=expression '+' r=term { _PyAST_BinOp(l, Add, r, EXTRA) } |
| 151 | | l=expression '-' r=term { _PyAST_BinOp(l, Sub, r, EXTRA) } |
| 152 | | t=term { t } |
| 153 | ) |
| 154 | term[expr_ty]: ( l=term '*' r=factor { _PyAST_BinOp(l, Mult, r, EXTRA) } |
| 155 | | l=term '/' r=factor { _PyAST_BinOp(l, Div, r, EXTRA) } |
| 156 | | f=factor { f } |
| 157 | ) |
| 158 | factor[expr_ty]: ('(' e=expression ')' { e } |
| 159 | | a=atom { a } |
| 160 | ) |
| 161 | atom[expr_ty]: ( n=NAME { n } |
| 162 | | n=NUMBER { n } |
| 163 | | s=STRING { s } |
| 164 | ) |
| 165 | """ |
| 166 | test_source = """ |
| 167 | expressions = [ |
| 168 | "4+5", |
| 169 | "4-5", |
| 170 | "4*5", |
| 171 | "1+4*5", |
| 172 | "1+4/5", |
| 173 | "(1+1) + (1+1)", |
| 174 | "(1+1) - (1+1)", |
| 175 | "(1+1) * (1+1)", |
| 176 | "(1+1) / (1+1)", |
| 177 | ] |
| 178 | |
| 179 | for expr in expressions: |
| 180 | the_ast = parse.parse_string(expr, mode=1) |
| 181 | expected_ast = ast.parse(expr) |
| 182 | self.assertEqual(ast_dump(the_ast), ast_dump(expected_ast)) |
| 183 | """ |
| 184 | self.run_test(grammar_source, test_source) |
| 185 | |
| 186 | def test_lookahead(self) -> None: |
| 187 | grammar_source = """ |