diff --git a/parser/python.lalrpop b/parser/python.lalrpop index c8936d3..32c0880 100644 --- a/parser/python.lalrpop +++ b/parser/python.lalrpop @@ -67,23 +67,21 @@ SmallStatement: ast::Stmt = { PassStatement: ast::Stmt = { "pass" => { - ast::Stmt { + ast::Stmt::new( location, - end_location: Some(end_location), - custom: (), - node: ast::StmtKind::Pass, - } + end_location, + ast::StmtKind::Pass, + ) }, }; DelStatement: ast::Stmt = { "del" => { - ast::Stmt { + ast::Stmt::new( location, - end_location: Some(end_location), - custom: (), - node: ast::StmtKind::Delete { targets: targets.into_iter().map(|expr| set_context(expr, ast::ExprContext::Del)).collect() }, - } + end_location, + ast::StmtKind::Delete { targets: targets.into_iter().map(|expr| set_context(expr, ast::ExprContext::Del)).collect() }, + ) }, }; @@ -91,12 +89,11 @@ ExpressionStatement: ast::Stmt = { => { // Just an expression, no assignment: if suffix.is_empty() { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Expr { value: Box::new(expression) } - } + end_location, + ast::StmtKind::Expr { value: Box::new(expression) } + ) } else { let mut targets = vec![set_context(expression, ast::ExprContext::Store)]; let mut values = suffix; @@ -107,39 +104,36 @@ ExpressionStatement: ast::Stmt = { let value = Box::new(values.into_iter().next().unwrap()); - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Assign { targets, value, type_comment: None }, - } + end_location, + ast::StmtKind::Assign { targets, value, type_comment: None }, + ) } }, => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::AugAssign { + end_location, + ast::StmtKind::AugAssign { target: Box::new(set_context(target, ast::ExprContext::Store)), op, value: Box::new(rhs) }, - } + ) }, > ":" > => { let simple = matches!(target.node, ast::ExprKind::Name { .. }); - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::AnnAssign { + end_location, + ast::StmtKind::AnnAssign { target: Box::new(set_context(target, ast::ExprContext::Store)), annotation: Box::new(annotation), value: rhs.map(Box::new), simple: if simple { 1 } else { 0 }, }, - } + ) }, }; @@ -191,80 +185,72 @@ AugAssign: ast::Operator = { FlowStatement: ast::Stmt = { "break" => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Break, - } + end_location, + ast::StmtKind::Break, + ) }, "continue" => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Continue, - } + end_location, + ast::StmtKind::Continue, + ) }, "return" => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Return { value: value.map(Box::new) }, - } + end_location, + ast::StmtKind::Return { value: value.map(Box::new) }, + ) }, => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Expr { value: Box::new(expression) }, - } + end_location, + ast::StmtKind::Expr { value: Box::new(expression) }, + ) }, RaiseStatement, }; RaiseStatement: ast::Stmt = { "raise" => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Raise { exc: None, cause: None }, - } + end_location, + ast::StmtKind::Raise { exc: None, cause: None }, + ) }, "raise" > )?> => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Raise { exc: Some(Box::new(t)), cause: c.map(|x| Box::new(x.1)) }, - } + end_location, + ast::StmtKind::Raise { exc: Some(Box::new(t)), cause: c.map(|x| Box::new(x.1)) }, + ) }, }; ImportStatement: ast::Stmt = { "import" >> => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Import { names }, - } + end_location, + ast::StmtKind::Import { names }, + ) }, "from" "import" => { let (level, module) = source; - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::ImportFrom { + end_location, + ast::StmtKind::ImportFrom { level, module, names }, - } + ) }, }; @@ -312,37 +298,34 @@ DottedName: String = { GlobalStatement: ast::Stmt = { "global" > => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Global { names } - } + end_location, + ast::StmtKind::Global { names } + ) }, }; NonlocalStatement: ast::Stmt = { "nonlocal" > => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Nonlocal { names } - } + end_location, + ast::StmtKind::Nonlocal { names } + ) }, }; AssertStatement: ast::Stmt = { "assert" > )?> => { - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Assert { + end_location, + ast::StmtKind::Assert { test: Box::new(test), msg: msg.map(|e| Box::new(e.1)) } - } + ) }, }; @@ -367,15 +350,14 @@ MatchStatement: ast::Stmt = { .unwrap() .end_location .unwrap(); - ast::Stmt { + ast::Stmt::new( location, - end_location: Some(end_location), - custom: (), - node: ast::StmtKind::Match { + end_location, + ast::StmtKind::Match { subject: Box::new(subject), cases } - } + ) }, "match" "," ":" "\n" Indent Dedent => { let end_location = cases @@ -386,15 +368,14 @@ MatchStatement: ast::Stmt = { .unwrap() .end_location .unwrap(); - ast::Stmt { + ast::Stmt::new( location, - end_location: Some(end_location), - custom: (), - node: ast::StmtKind::Match { + end_location, + ast::StmtKind::Match { subject: Box::new(subject), cases } - } + ) }, "match" "," > ","? ":" "\n" Indent Dedent => { let end_location = cases @@ -407,23 +388,21 @@ MatchStatement: ast::Stmt = { .unwrap(); let mut subjects = subjects; subjects.insert(0, subject); - ast::Stmt { - custom: (), + ast::Stmt::new( location, - end_location: Some(end_location), - node: ast::StmtKind::Match { - subject: Box::new(ast::Expr { + end_location, + ast::StmtKind::Match { + subject: Box::new(ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Tuple { + end_location, + ast::ExprKind::Tuple { elts: subjects, ctx: ast::ExprContext::Load, }, - }), + )), cases } - } + ) } } @@ -444,25 +423,23 @@ Guard: ast::Expr = { } Patterns: ast::Pattern = { - "," => ast::Pattern { + "," => ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), - node: ast::PatternKind::MatchSequence { + end_location, + ast::PatternKind::MatchSequence { patterns: vec![pattern] }, - }, + ), "," > ","? => { let mut patterns = patterns; patterns.insert(0, pattern); - ast::Pattern { + ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), - node: ast::PatternKind::MatchSequence { + end_location, + ast::PatternKind::MatchSequence { patterns }, - } + ) }, => pattern } @@ -480,15 +457,14 @@ AsPattern: ast::Pattern = { location, })? } else { - Ok(ast::Pattern { + Ok(ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), - node: ast::PatternKind::MatchAs { + end_location, + ast::PatternKind::MatchAs { pattern: Some(Box::new(pattern)), name: Some(name), }, - }) + )) } }, } @@ -498,58 +474,50 @@ OrPattern: ast::Pattern = { )+> => { let mut patterns = patterns; patterns.insert(0, pattern); - ast::Pattern { + ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), - node: ast::PatternKind::MatchOr { patterns } - } + end_location, + ast::PatternKind::MatchOr { patterns } + ) } } ClosedPattern: ast::Pattern = { - => ast::Pattern { + => ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), + end_location, node, - }, - => ast::Pattern { + ), + => ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), + end_location, node, - }, - => ast::Pattern { + ), + => ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), + end_location, node, - }, - => ast::Pattern { + ), + => ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), + end_location, node, - }, - => ast::Pattern { + ), + => ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), + end_location, node, - }, - => ast::Pattern { + ), + => ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), + end_location, node, - }, - => ast::Pattern { + ), + => ast::Pattern::new( location, - end_location: Some(end_location), - custom: (), + end_location, node, - }, + ), } SequencePattern: ast::PatternKind = { @@ -577,38 +545,35 @@ StarPattern: ast::PatternKind = { } ConstantAtom: ast::Expr = { - => ast::Expr { + => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Constant { value, kind: None } - }, + end_location, + ast::ExprKind::Constant { value, kind: None } + ), } ConstantExpr: ast::Expr = { ConstantAtom, - "-" => ast::Expr { + "-" => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::UnaryOp { + end_location, + ast::ExprKind::UnaryOp { op: ast::Unaryop::USub, operand: Box::new(operand) } - }, + ), } AddOpExpr: ast::Expr = { - => ast::Expr { + => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { + end_location, + ast::ExprKind::BinOp { left: Box::new(left), op, right: Box::new(right), } - }, + ), } LiteralPattern: ast::PatternKind = { @@ -640,35 +605,32 @@ CapturePattern: ast::PatternKind = { } MatchName: ast::Expr = { - => ast::Expr { + => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Name { id: name, ctx: ast::ExprContext::Load }, - }, + end_location, + ast::ExprKind::Name { id: name, ctx: ast::ExprContext::Load }, + ), } MatchNameOrAttr: ast::Expr = { - "." => ast::Expr { + "." => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Attribute { + end_location, + ast::ExprKind::Attribute { value: Box::new(name), attr, ctx: ast::ExprContext::Load, }, - }, - "." => ast::Expr { + ), + "." => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Attribute { + end_location, + ast::ExprKind::Attribute { value: Box::new(e), attr, ctx: ast::ExprContext::Load, } - } + ) } ValuePattern: ast::PatternKind = { @@ -681,33 +643,30 @@ MappingKey: ast::Expr = { ConstantExpr, AddOpExpr, MatchNameOrAttr, - "None" => ast::Expr { + "None" => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Constant { + end_location, + ast::ExprKind::Constant { value: ast::Constant::None, kind: None, }, - }, - "True" => ast::Expr { + ), + "True" => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Constant { + end_location, + ast::ExprKind::Constant { value: true.into(), kind: None, }, - }, - "False" => ast::Expr { + ), + "False" => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Constant { + end_location, + ast::ExprKind::Constant { value: false.into(), kind: None, }, - }, + ), =>? Ok(parse_strings(s)?), } @@ -844,24 +803,23 @@ IfStatement: ast::Stmt = { .or_else(|| s2.last().and_then(|last| last.4.last())) .or_else(|| body.last()) .unwrap() - .end_location; + .end_location + .unwrap(); // handle elif: for i in s2.into_iter().rev() { - let x = ast::Stmt { - custom: (), - location: i.0, + let x = ast::Stmt::new( + i.0, end_location, - node: ast::StmtKind::If { test: Box::new(i.2), body: i.4, orelse: last }, - }; + ast::StmtKind::If { test: Box::new(i.2), body: i.4, orelse: last }, + ); last = vec![x]; } - ast::Stmt { - custom: (), + ast::Stmt::new( location, end_location, - node: ast::StmtKind::If { test: Box::new(test), body, orelse: last } - } + ast::StmtKind::If { test: Box::new(test), body, orelse: last } + ) }, }; @@ -872,17 +830,17 @@ WhileStatement: ast::Stmt = { .last() .or_else(|| body.last()) .unwrap() - .end_location; - ast::Stmt { - custom: (), + .end_location + .unwrap(); + ast::Stmt::new( location, end_location, - node: ast::StmtKind::While { + ast::StmtKind::While { test: Box::new(test), body, orelse }, - } + ) }, }; @@ -913,21 +871,20 @@ TryStatement: ast::Stmt = { let finalbody = finally.map(|s| s.2).unwrap_or_default(); let end_location = finalbody .last() - .map(|last| last.end_location) - .or_else(|| orelse.last().map(|last| last.end_location)) - .or_else(|| handlers.last().map(|last| last.end_location)) + .and_then(|last| last.end_location) + .or_else(|| orelse.last().and_then(|last| last.end_location)) + .or_else(|| handlers.last().and_then(|last| last.end_location)) .unwrap(); - ast::Stmt { - custom: (), + ast::Stmt::new( location, end_location, - node: ast::StmtKind::Try { + ast::StmtKind::Try { body, handlers, orelse, finalbody, }, - } + ) }, "try" ":" => { let orelse = else_suite.map(|s| s.2).unwrap_or_default(); @@ -935,37 +892,35 @@ TryStatement: ast::Stmt = { let end_location = finalbody .last() .or_else(|| orelse.last()) - .map(|last| last.end_location) - .or_else(|| handlers.last().map(|last| last.end_location)) + .and_then(|last| last.end_location) + .or_else(|| handlers.last().and_then(|last| last.end_location)) .unwrap(); - ast::Stmt { - custom: (), + ast::Stmt::new( location, end_location, - node: ast::StmtKind::TryStar { + ast::StmtKind::TryStar { body, handlers, orelse, finalbody, }, - } + ) }, "try" ":" => { let handlers = vec![]; let orelse = vec![]; let finalbody = finally.2; - let end_location = finalbody.last().unwrap().end_location; - ast::Stmt { - custom: (), + let end_location = finalbody.last().unwrap().end_location.unwrap(); + ast::Stmt::new( location, end_location, - node: ast::StmtKind::Try { + ast::StmtKind::Try { body, handlers, orelse, finalbody, }, - } + ) }, }; @@ -1242,19 +1197,18 @@ ClassDef: ast::Stmt = { Some((_, arg, _)) => (arg.args, arg.keywords), None => (vec![], vec![]), }; - let end_location = body.last().unwrap().end_location; - ast::Stmt { - custom: (), + let end_location = body.last().unwrap().end_location.unwrap(); + ast::Stmt::new( location, end_location, - node: ast::StmtKind::ClassDef { + ast::StmtKind::ClassDef { name, bases, keywords, body, decorator_list, }, - } + ) }, }; @@ -1266,31 +1220,28 @@ Decorator: ast::Expr = { }; YieldExpr: ast::Expr = { - "yield" => ast::Expr { + "yield" => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Yield { value: value.map(Box::new) } - }, - "yield" "from" > => ast::Expr { + end_location, + ast::ExprKind::Yield { value: value.map(Box::new) } + ), + "yield" "from" > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::YieldFrom { value: Box::new(e) } - }, + end_location, + ast::ExprKind::YieldFrom { value: Box::new(e) } + ), }; Test: ast::Expr = { - > "if" > "else" > => ast::Expr { + > "if" > "else" > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::IfExp { + end_location, + ast::ExprKind::IfExp { test: Box::new(test), body: Box::new(body), orelse: Box::new(orelse), } - }, + ), OrTest, LambdaDef, }; @@ -1334,15 +1285,14 @@ LambdaDef: ast::Expr = { } ))?; - Ok(ast::Expr { + Ok(ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Lambda { + end_location, + ast::ExprKind::Lambda { args: Box::new(p), body: Box::new(body) } - }) + )) } } @@ -1350,12 +1300,11 @@ OrTest: ast::Expr = { > )+> => { let mut values = vec![e1]; values.extend(e2.into_iter().map(|e| e.1)); - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BoolOp { op: ast::Boolop::Or, values } - } + end_location, + ast::ExprKind::BoolOp { op: ast::Boolop::Or, values } + ) }, AndTest, }; @@ -1364,35 +1313,32 @@ AndTest: ast::Expr = { > )+> => { let mut values = vec![e1]; values.extend(e2.into_iter().map(|e| e.1)); - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BoolOp { op: ast::Boolop::And, values } - } + end_location, + ast::ExprKind::BoolOp { op: ast::Boolop::And, values } + ) }, NotTest, }; NotTest: ast::Expr = { - "not" > => ast::Expr { + "not" > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::UnaryOp { operand: Box::new(e), op: ast::Unaryop::Not } - }, + end_location, + ast::ExprKind::UnaryOp { operand: Box::new(e), op: ast::Unaryop::Not } + ), Comparison, }; Comparison: ast::Expr = { > )+> => { let (ops, comparators) = comparisons.into_iter().unzip(); - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Compare { left: Box::new(left), ops, comparators } - } + end_location, + ast::ExprKind::Compare { left: Box::new(left), ops, comparators } + ) }, Expression, }; @@ -1411,42 +1357,38 @@ CompOp: ast::Cmpop = { }; Expression: ast::Expr = { - > "|" > => ast::Expr { + > "|" > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { left: Box::new(e1), op: ast::Operator::BitOr, right: Box::new(e2) } - }, + end_location, + ast::ExprKind::BinOp { left: Box::new(e1), op: ast::Operator::BitOr, right: Box::new(e2) } + ), XorExpression, }; XorExpression: ast::Expr = { - > "^" > => ast::Expr { + > "^" > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { left: Box::new(e1), op: ast::Operator::BitXor, right: Box::new(e2) } - }, + end_location, + ast::ExprKind::BinOp { left: Box::new(e1), op: ast::Operator::BitXor, right: Box::new(e2) } + ), AndExpression, }; AndExpression: ast::Expr = { - > "&" > => ast::Expr { + > "&" > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { left: Box::new(e1), op: ast::Operator::BitAnd, right: Box::new(e2) } - }, + end_location, + ast::ExprKind::BinOp { left: Box::new(e1), op: ast::Operator::BitAnd, right: Box::new(e2) } + ), ShiftExpression, }; ShiftExpression: ast::Expr = { - > > => ast::Expr { + > > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { left: Box::new(e1), op, right: Box::new(e2) } - }, + end_location, + ast::ExprKind::BinOp { left: Box::new(e1), op, right: Box::new(e2) } + ), ArithmeticExpression, }; @@ -1456,12 +1398,11 @@ ShiftOp: ast::Operator = { }; ArithmeticExpression: ast::Expr = { - > > => ast::Expr { + > > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { left: Box::new(a), op, right: Box::new(b) } - }, + end_location, + ast::ExprKind::BinOp { left: Box::new(a), op, right: Box::new(b) } + ), Term, }; @@ -1471,12 +1412,11 @@ AddOp: ast::Operator = { }; Term: ast::Expr = { - > > => ast::Expr { + > > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { left: Box::new(a), op, right: Box::new(b) } - }, + end_location, + ast::ExprKind::BinOp { left: Box::new(a), op, right: Box::new(b) } + ), Factor, }; @@ -1489,12 +1429,11 @@ MulOp: ast::Operator = { }; Factor: ast::Expr = { - > => ast::Expr { + > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::UnaryOp { operand: Box::new(e), op } - }, + end_location, + ast::ExprKind::UnaryOp { operand: Box::new(e), op } + ), Power, }; @@ -1505,23 +1444,21 @@ UnaryOp: ast::Unaryop = { }; Power: ast::Expr = { - > "**" > => ast::Expr { + > "**" > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) } - }, + end_location, + ast::ExprKind::BinOp { left: Box::new(e), op: ast::Operator::Pow, right: Box::new(b) } + ), AtomExpr, }; AtomExpr: ast::Expr = { "await" > => { - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Await { value: Box::new(atom) } - } + end_location, + ast::ExprKind::Await { value: Box::new(atom) } + ) }, AtomExpr2, } @@ -1529,25 +1466,22 @@ AtomExpr: ast::Expr = { AtomExpr2: ast::Expr = { Atom, > "(" ")" => { - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Call { func: Box::new(f), args: a.args, keywords: a.keywords } - } + end_location, + ast::ExprKind::Call { func: Box::new(f), args: a.args, keywords: a.keywords } + ) }, - > "[" "]" => ast::Expr { + > "[" "]" => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Subscript { value: Box::new(e), slice: Box::new(s), ctx: ast::ExprContext::Load } - }, - > "." => ast::Expr { + end_location, + ast::ExprKind::Subscript { value: Box::new(e), slice: Box::new(s), ctx: ast::ExprContext::Load } + ), + > "." => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Attribute { value: Box::new(e), attr, ctx: ast::ExprContext::Load } - }, + end_location, + ast::ExprKind::Attribute { value: Box::new(e), attr, ctx: ast::ExprContext::Load } + ), }; SubscriptList: ast::Expr = { @@ -1560,12 +1494,11 @@ SubscriptList: ast::Expr = { dims.push(x.1) } - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Tuple { elts: dims, ctx: ast::ExprContext::Load }, - } + end_location, + ast::ExprKind::Tuple { elts: dims, ctx: ast::ExprContext::Load }, + ) } } }; @@ -1576,12 +1509,11 @@ Subscript: ast::Expr = { let lower = e1.map(Box::new); let upper = e2.map(Box::new); let step = e3.flatten().map(Box::new); - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Slice { lower, upper, step } - } + end_location, + ast::ExprKind::Slice { lower, upper, step } + ) } }; @@ -1591,34 +1523,30 @@ SliceOp: Option = { Atom: ast::Expr = { =>? Ok(parse_strings(s)?), - => ast::Expr { + => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Constant { value, kind: None } - }, - => ast::Expr { + end_location, + ast::ExprKind::Constant { value, kind: None } + ), + => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Name { id: name, ctx: ast::ExprContext::Load } - }, + end_location, + ast::ExprKind::Name { id: name, ctx: ast::ExprContext::Load } + ), "[" "]" => { let elts = e.unwrap_or_default(); - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::List { elts, ctx: ast::ExprContext::Load } - } + end_location, + ast::ExprKind::List { elts, ctx: ast::ExprContext::Load } + ) }, "[" "]" => { - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::ListComp { elt: Box::new(elt), generators } - } + end_location, + ast::ExprKind::ListComp { elt: Box::new(elt), generators } + ) }, "(" >> ")" if Goal != "no-withitems" => { if elts.len() == 1 && trailing_comma.is_none() { @@ -1656,12 +1584,11 @@ Atom: ast::Expr = { ), "(" ")" => e, "(" ")" => { - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::GeneratorExp { elt: Box::new(elt), generators } - } + end_location, + ast::ExprKind::GeneratorExp { elt: Box::new(elt), generators } + ) }, "(" "**" > ")" =>? { Err(LexicalError{ @@ -1675,38 +1602,34 @@ Atom: ast::Expr = { .into_iter() .map(|(k, v)| (k.map(|x| *x), v)) .unzip(); - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Dict { keys, values } - } + end_location, + ast::ExprKind::Dict { keys, values } + ) }, "{" "}" => { - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::DictComp { + end_location, + ast::ExprKind::DictComp { key: Box::new(e1.0), value: Box::new(e1.1), generators, } - } + ) }, - "{" "}" => ast::Expr { + "{" "}" => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Set { elts } - }, + end_location, + ast::ExprKind::Set { elts } + ), "{" "}" => { - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::SetComp { elt: Box::new(elt), generators } - } + end_location, + ast::ExprKind::SetComp { elt: Box::new(elt), generators } + ) }, "True" => ast::Expr::new(location, end_location, ast::ExprKind::Constant { value: true.into(), kind: None }), "False" => ast::Expr::new(location, end_location, ast::ExprKind::Constant { value: false.into(), kind: None }), @@ -1762,24 +1685,22 @@ GenericList: ast::Expr = { if elts.len() == 1 && trailing_comma.is_none() { elts.into_iter().next().unwrap() } else { - ast::Expr { + ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Tuple { elts, ctx: ast::ExprContext::Load } - } + end_location, + ast::ExprKind::Tuple { elts, ctx: ast::ExprContext::Load } + ) } } } // Test StarExpr: ast::Expr = { - "*" > => ast::Expr { + "*" > => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::Starred { value: Box::new(e), ctx: ast::ExprContext::Load }, - } + end_location, + ast::ExprKind::Starred { value: Box::new(e), ctx: ast::ExprContext::Load }, + ) }; // Comprehensions: @@ -1810,15 +1731,14 @@ ArgumentList: ArgumentList = { FunctionArgument: (Option<(ast::Location, ast::Location, Option)>, ast::Expr) = { => { let expr = match c { - Some(c) => ast::Expr { + Some(c) => ast::Expr::new( location, - end_location: Some(end_location), - custom: (), - node: ast::ExprKind::GeneratorExp { + end_location, + ast::ExprKind::GeneratorExp { elt: Box::new(e), generators: c, } - }, + ), None => e, }; (None, expr)