Ejemplo n.º 1
0
    def verify_ast_generation(self, source: str, stmt: str, tmp_path: PurePath) -> None:
        grammar = parse_string(source, GrammarParser)
        extension = generate_parser_c_extension(grammar, Path(tmp_path))

        expected_ast = ast.parse(stmt)
        actual_ast = extension.parse_string(stmt, mode=1)
        self.assertEqual(ast_dump(expected_ast), ast_dump(actual_ast))
Ejemplo n.º 2
0
 def test_with_stmt_with_paren(self) -> None:
     grammar_source = """
     start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
     statements[asdl_seq*]: a=statement+ { _PyPegen_seq_flatten(p, a) }
     statement[asdl_seq*]: a=compound_stmt { _PyPegen_singleton_seq(p, a) }
     compound_stmt[stmt_ty]: with_stmt
     with_stmt[stmt_ty]: (
         a='with' '(' b=','.with_item+ ')' ':' c=block {
             _Py_With(b, _PyPegen_singleton_seq(p, c), NULL, EXTRA) }
     )
     with_item[withitem_ty]: (
         e=NAME o=['as' t=NAME { t }] { _Py_withitem(e, _PyPegen_set_expr_context(p, o, Store), p->arena) }
     )
     block[stmt_ty]: a=pass_stmt NEWLINE { a } | NEWLINE INDENT a=pass_stmt DEDENT { a }
     pass_stmt[stmt_ty]: a='pass' { _Py_Pass(EXTRA) }
     """
     stmt = "with (\n    a as b,\n    c as d\n): pass"
     grammar = parse_string(grammar_source, GrammarParser)
     extension = generate_parser_c_extension(grammar, Path(self.tmp_path))
     the_ast = extension.parse_string(stmt, mode=1)
     self.assertTrue(
         ast_dump(the_ast).startswith(
             "Module(body=[With(items=[withitem(context_expr=Name(id='a', ctx=Load()), optional_vars=Name(id='b', ctx=Store())), "
             "withitem(context_expr=Name(id='c', ctx=Load()), optional_vars=Name(id='d', ctx=Store()))]"
         ))
Ejemplo n.º 3
0
    def test_same_name_different_types(self) -> None:
        source = """
        start[mod_ty]: a=import_from+ NEWLINE ENDMARKER { Module(a, NULL, p->arena)}
        import_from[stmt_ty]: ( a='from' !'import' c=simple_name 'import' d=import_as_names_from {
                                _Py_ImportFrom(c->v.Name.id, d, 0, EXTRA) }
                            | a='from' '.' 'import' c=import_as_names_from {
                                _Py_ImportFrom(NULL, c, 1, EXTRA) }
                            )
        simple_name[expr_ty]: NAME
        import_as_names_from[asdl_seq*]: a=','.import_as_name_from+ { a }
        import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _Py_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, p->arena) }
        """
        grammar = parse_string(source, GrammarParser)
        extension = generate_parser_c_extension(grammar, Path(self.tmp_path))

        for stmt in ("from a import b as c", "from . import a as b"):
            expected_ast = ast.parse(stmt)
            actual_ast = extension.parse_string(stmt, mode=1)
            self.assertEqual(ast_dump(expected_ast), ast_dump(actual_ast))
Ejemplo n.º 4
0
    def test_c_parser(self) -> None:
        grammar_source = """
        start[mod_ty]: a=stmt* $ { Module(a, NULL, p->arena) }
        stmt[stmt_ty]: a=expr_stmt { a }
        expr_stmt[stmt_ty]: a=expression NEWLINE { _Py_Expr(a, EXTRA) }
        expression[expr_ty]: ( l=expression '+' r=term { _Py_BinOp(l, Add, r, EXTRA) }
                            | l=expression '-' r=term { _Py_BinOp(l, Sub, r, EXTRA) }
                            | t=term { t }
                            )
        term[expr_ty]: ( l=term '*' r=factor { _Py_BinOp(l, Mult, r, EXTRA) }
                    | l=term '/' r=factor { _Py_BinOp(l, Div, r, EXTRA) }
                    | f=factor { f }
                    )
        factor[expr_ty]: ('(' e=expression ')' { e }
                        | a=atom { a }
                        )
        atom[expr_ty]: ( n=NAME { n }
                    | n=NUMBER { n }
                    | s=STRING { s }
                    )
        """
        grammar = parse_string(grammar_source, GrammarParser)
        extension = generate_parser_c_extension(grammar, Path(self.tmp_path))

        expressions = [
            "4+5",
            "4-5",
            "4*5",
            "1+4*5",
            "1+4/5",
            "(1+1) + (1+1)",
            "(1+1) - (1+1)",
            "(1+1) * (1+1)",
            "(1+1) / (1+1)",
        ]

        for expr in expressions:
            the_ast = extension.parse_string(expr, mode=1)
            expected_ast = ast.parse(expr)
            self.assertEqual(ast_dump(the_ast), ast_dump(expected_ast))