Pull in RustPython parser (#6099)

This commit is contained in:
Micha Reiser 2023-07-27 11:29:11 +02:00 committed by GitHub
parent 86539c1fc5
commit 40f54375cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
779 changed files with 108400 additions and 2078 deletions

View file

@ -1,4 +1,4 @@
use rustpython_ast::{
use crate::{
self as ast, Alias, Arg, ArgWithDefault, Arguments, BoolOp, CmpOp, Comprehension, Constant,
Decorator, ElifElseClause, ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Pattern,
Stmt, TypeIgnore, TypeParam, TypeParamTypeVar, UnaryOp, WithItem,
@ -981,292 +981,3 @@ where
V: PreorderVisitor<'a> + ?Sized,
{
}
#[cfg(test)]
mod tests {
use std::fmt::{Debug, Write};
use insta::assert_snapshot;
use rustpython_parser::lexer::lex;
use rustpython_parser::{parse_tokens, Mode};
use crate::node::AnyNodeRef;
use crate::visitor::preorder::{
walk_alias, walk_arg, walk_arguments, walk_comprehension, walk_except_handler, walk_expr,
walk_keyword, walk_match_case, walk_module, walk_pattern, walk_stmt, walk_type_ignore,
walk_type_param, walk_with_item, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension,
Constant, ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Pattern, PreorderVisitor,
Stmt, TypeIgnore, TypeParam, UnaryOp, WithItem,
};
#[test]
fn function_arguments() {
let source = r#"def a(b, c,/, d, e = 20, *args, named=5, other=20, **kwargs): pass"#;
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn function_positional_only_with_default() {
let source = r#"def a(b, c = 34,/, e = 20, *args): pass"#;
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn compare() {
let source = r#"4 < x < 5"#;
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn list_comprehension() {
let source = "[x for x in numbers]";
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn dict_comprehension() {
let source = "{x: x**2 for x in numbers}";
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn set_comprehension() {
let source = "{x for x in numbers}";
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn match_class_pattern() {
let source = r#"
match x:
case Point2D(0, 0):
...
case Point3D(x=0, y=0, z=0):
...
"#;
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn decorators() {
let source = r#"
@decorator
def a():
pass
@test
class A:
pass
"#;
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn type_aliases() {
let source = r#"type X[T: str, U, *Ts, **P] = list[T]"#;
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn class_type_parameters() {
let source = r#"class X[T: str, U, *Ts, **P]: ..."#;
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn function_type_parameters() {
let source = r#"def X[T: str, U, *Ts, **P](): ..."#;
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
fn trace_preorder_visitation(source: &str) -> String {
let tokens = lex(source, Mode::Module);
let parsed = parse_tokens(tokens, Mode::Module, "test.py").unwrap();
let mut visitor = RecordVisitor::default();
visitor.visit_mod(&parsed);
visitor.output
}
/// Emits a `tree` with a node for every visited AST node (labelled by the AST node's kind)
/// and leafs for attributes.
#[derive(Default)]
struct RecordVisitor {
depth: usize,
output: String,
}
impl RecordVisitor {
fn enter_node<'a, T>(&mut self, node: T)
where
T: Into<AnyNodeRef<'a>>,
{
self.emit(&node.into().kind());
self.depth += 1;
}
fn exit_node(&mut self) {
self.depth -= 1;
}
fn emit(&mut self, text: &dyn Debug) {
for _ in 0..self.depth {
self.output.push_str(" ");
}
writeln!(self.output, "- {text:?}").unwrap();
}
}
impl PreorderVisitor<'_> for RecordVisitor {
fn visit_mod(&mut self, module: &Mod) {
self.enter_node(module);
walk_module(self, module);
self.exit_node();
}
fn visit_stmt(&mut self, stmt: &Stmt) {
self.enter_node(stmt);
walk_stmt(self, stmt);
self.exit_node();
}
fn visit_annotation(&mut self, expr: &Expr) {
self.enter_node(expr);
walk_expr(self, expr);
self.exit_node();
}
fn visit_expr(&mut self, expr: &Expr) {
self.enter_node(expr);
walk_expr(self, expr);
self.exit_node();
}
fn visit_constant(&mut self, constant: &Constant) {
self.emit(&constant);
}
fn visit_bool_op(&mut self, bool_op: &BoolOp) {
self.emit(&bool_op);
}
fn visit_operator(&mut self, operator: &Operator) {
self.emit(&operator);
}
fn visit_unary_op(&mut self, unary_op: &UnaryOp) {
self.emit(&unary_op);
}
fn visit_cmp_op(&mut self, cmp_op: &CmpOp) {
self.emit(&cmp_op);
}
fn visit_comprehension(&mut self, comprehension: &Comprehension) {
self.enter_node(comprehension);
walk_comprehension(self, comprehension);
self.exit_node();
}
fn visit_except_handler(&mut self, except_handler: &ExceptHandler) {
self.enter_node(except_handler);
walk_except_handler(self, except_handler);
self.exit_node();
}
fn visit_format_spec(&mut self, format_spec: &Expr) {
self.enter_node(format_spec);
walk_expr(self, format_spec);
self.exit_node();
}
fn visit_arguments(&mut self, arguments: &Arguments) {
self.enter_node(arguments);
walk_arguments(self, arguments);
self.exit_node();
}
fn visit_arg(&mut self, arg: &Arg) {
self.enter_node(arg);
walk_arg(self, arg);
self.exit_node();
}
fn visit_keyword(&mut self, keyword: &Keyword) {
self.enter_node(keyword);
walk_keyword(self, keyword);
self.exit_node();
}
fn visit_alias(&mut self, alias: &Alias) {
self.enter_node(alias);
walk_alias(self, alias);
self.exit_node();
}
fn visit_with_item(&mut self, with_item: &WithItem) {
self.enter_node(with_item);
walk_with_item(self, with_item);
self.exit_node();
}
fn visit_match_case(&mut self, match_case: &MatchCase) {
self.enter_node(match_case);
walk_match_case(self, match_case);
self.exit_node();
}
fn visit_pattern(&mut self, pattern: &Pattern) {
self.enter_node(pattern);
walk_pattern(self, pattern);
self.exit_node();
}
fn visit_type_ignore(&mut self, type_ignore: &TypeIgnore) {
self.enter_node(type_ignore);
walk_type_ignore(self, type_ignore);
self.exit_node();
}
fn visit_type_param(&mut self, type_param: &TypeParam) {
self.enter_node(type_param);
walk_type_param(self, type_param);
self.exit_node();
}
}
}

View file

@ -1,15 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtClassDef
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- StmtExpr
- ExprConstant
- Ellipsis

View file

@ -1,15 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtExpr
- ExprCompare
- ExprConstant
- Int(4)
- Lt
- ExprName
- Lt
- ExprConstant
- Int(5)

View file

@ -1,13 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtFunctionDef
- ExprName
- Arguments
- StmtPass
- StmtClassDef
- ExprName
- StmtPass

View file

@ -1,17 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtExpr
- ExprDictComp
- ExprName
- ExprBinOp
- ExprName
- Pow
- ExprConstant
- Int(2)
- Comprehension
- ExprName
- ExprName

View file

@ -1,23 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtFunctionDef
- Arguments
- Arg
- Arg
- Arg
- Arg
- ExprConstant
- Int(20)
- Arg
- Arg
- ExprConstant
- Int(5)
- Arg
- ExprConstant
- Int(20)
- Arg
- StmtPass

View file

@ -1,17 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtFunctionDef
- Arguments
- Arg
- Arg
- ExprConstant
- Int(34)
- Arg
- ExprConstant
- Int(20)
- Arg
- StmtPass

View file

@ -1,16 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtFunctionDef
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- Arguments
- StmtExpr
- ExprConstant
- Ellipsis

View file

@ -1,12 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtExpr
- ExprListComp
- ExprName
- Comprehension
- ExprName
- ExprName

View file

@ -1,35 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtMatch
- ExprName
- MatchCase
- PatternMatchClass
- ExprName
- PatternMatchValue
- ExprConstant
- Int(0)
- PatternMatchValue
- ExprConstant
- Int(0)
- StmtExpr
- ExprConstant
- Ellipsis
- MatchCase
- PatternMatchClass
- ExprName
- PatternMatchValue
- ExprConstant
- Int(0)
- PatternMatchValue
- ExprConstant
- Int(0)
- PatternMatchValue
- ExprConstant
- Int(0)
- StmtExpr
- ExprConstant
- Ellipsis

View file

@ -1,12 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtExpr
- ExprSetComp
- ExprName
- Comprehension
- ExprName
- ExprName

View file

@ -1,16 +0,0 @@
---
source: crates/ruff_python_ast/src/visitor/preorder.rs
expression: trace
---
- ModModule
- StmtTypeAlias
- ExprName
- TypeParamTypeVar
- ExprName
- TypeParamTypeVar
- TypeParamTypeVarTuple
- TypeParamParamSpec
- ExprSubscript
- ExprName
- ExprName