mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-12 15:45:22 +00:00
Use proper locations for sub-expressions in constant matches
This commit is contained in:
parent
8b9f57906a
commit
fdea016ea7
2 changed files with 50 additions and 151 deletions
|
@ -576,6 +576,41 @@ StarPattern: ast::PatternKind = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConstantAtom: ast::Expr = {
|
||||||
|
<location:@L> <value:Constant> <end_location:@R> => ast::Expr {
|
||||||
|
location,
|
||||||
|
end_location: Some(end_location),
|
||||||
|
custom: (),
|
||||||
|
node: ast::ExprKind::Constant { value, kind: None }
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ConstantExpr: ast::Expr = {
|
||||||
|
ConstantAtom,
|
||||||
|
<location:@L> "-" <operand:ConstantAtom> <end_location:@R> => ast::Expr {
|
||||||
|
location,
|
||||||
|
end_location: Some(end_location),
|
||||||
|
custom: (),
|
||||||
|
node: ast::ExprKind::UnaryOp {
|
||||||
|
op: ast::Unaryop::USub,
|
||||||
|
operand: Box::new(operand)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
AddOpExpr: ast::Expr = {
|
||||||
|
<location:@L> <left:ConstantExpr> <op:AddOp> <right:ConstantAtom> <end_location:@R> => ast::Expr {
|
||||||
|
location,
|
||||||
|
end_location: Some(end_location),
|
||||||
|
custom: (),
|
||||||
|
node: ast::ExprKind::BinOp {
|
||||||
|
left: Box::new(left),
|
||||||
|
op,
|
||||||
|
right: Box::new(right),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
LiteralPattern: ast::PatternKind = {
|
LiteralPattern: ast::PatternKind = {
|
||||||
"None" => ast::PatternKind::MatchSingleton {
|
"None" => ast::PatternKind::MatchSingleton {
|
||||||
value: ast::Constant::None
|
value: ast::Constant::None
|
||||||
|
@ -586,81 +621,11 @@ LiteralPattern: ast::PatternKind = {
|
||||||
"False" => ast::PatternKind::MatchSingleton {
|
"False" => ast::PatternKind::MatchSingleton {
|
||||||
value: false.into()
|
value: false.into()
|
||||||
},
|
},
|
||||||
<location:@L> <value:Constant> <end_location:@R> => ast::PatternKind::MatchValue {
|
<location:@L> <value:ConstantExpr> <end_location:@R> => ast::PatternKind::MatchValue {
|
||||||
value: Box::new(ast::Expr {
|
value: Box::new(value)
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value, kind: None }
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
<location:@L> "-" <value:Constant> <end_location:@R> => ast::PatternKind::MatchValue {
|
<location:@L> <value:AddOpExpr> => ast::PatternKind::MatchValue {
|
||||||
value: Box::new(ast::Expr {
|
value: Box::new(value)
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::UnaryOp {
|
|
||||||
op: ast::Unaryop::USub,
|
|
||||||
operand: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value, kind: None }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
<location:@L> <left:Constant> <op:AddOp> <right:Constant> <end_location:@R> => ast::PatternKind::MatchValue {
|
|
||||||
value: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::BinOp {
|
|
||||||
left: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value: left, kind: None }
|
|
||||||
}),
|
|
||||||
op,
|
|
||||||
right: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value: right, kind: None }
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
<location:@L> "-" <left:Constant> <op:AddOp> <right:Constant> <end_location:@R> => ast::PatternKind::MatchValue {
|
|
||||||
value: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::BinOp {
|
|
||||||
left: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::UnaryOp {
|
|
||||||
op: ast::Unaryop::USub,
|
|
||||||
operand: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value: left, kind: None }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
op,
|
|
||||||
right: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value: right, kind: None }
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
<location:@L> <s:(@L string @R)+> =>? Ok(ast::PatternKind::MatchValue {
|
<location:@L> <s:(@L string @R)+> =>? Ok(ast::PatternKind::MatchValue {
|
||||||
value: Box::new(parse_strings(s)?)
|
value: Box::new(parse_strings(s)?)
|
||||||
|
@ -713,6 +678,9 @@ ValuePattern: ast::PatternKind = {
|
||||||
}
|
}
|
||||||
|
|
||||||
MappingKey: ast::Expr = {
|
MappingKey: ast::Expr = {
|
||||||
|
ConstantExpr,
|
||||||
|
AddOpExpr,
|
||||||
|
MatchNameOrAttr,
|
||||||
<location:@L> "None" <end_location:@R> => ast::Expr {
|
<location:@L> "None" <end_location:@R> => ast::Expr {
|
||||||
location,
|
location,
|
||||||
end_location: Some(end_location),
|
end_location: Some(end_location),
|
||||||
|
@ -740,76 +708,7 @@ MappingKey: ast::Expr = {
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
<location:@L> <value:Constant> <end_location:@R> => ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value, kind: None }
|
|
||||||
},
|
|
||||||
<location:@L> "-" <value:Constant> <end_location:@R> => ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::UnaryOp {
|
|
||||||
op: ast::Unaryop::USub,
|
|
||||||
operand: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value, kind: None }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
<location:@L> <left:Constant> <op:AddOp> <right:Constant> <end_location:@R> => ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::BinOp {
|
|
||||||
left: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value: left, kind: None }
|
|
||||||
}),
|
|
||||||
op,
|
|
||||||
right: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value: right, kind: None }
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
<location:@L> "-" <left:Constant> <op:AddOp> <right:Constant> <end_location:@R> => ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::BinOp {
|
|
||||||
left: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::UnaryOp {
|
|
||||||
op: ast::Unaryop::USub,
|
|
||||||
operand: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value: left, kind: None }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
op,
|
|
||||||
right: Box::new(ast::Expr {
|
|
||||||
location,
|
|
||||||
end_location: Some(end_location),
|
|
||||||
custom: (),
|
|
||||||
node: ast::ExprKind::Constant { value: right, kind: None }
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
<location:@L> <s:(@L string @R)+> =>? Ok(parse_strings(s)?),
|
<location:@L> <s:(@L string @R)+> =>? Ok(parse_strings(s)?),
|
||||||
MatchNameOrAttr,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MatchMappingEntry: (ast::Expr, ast::Pattern) = {
|
MatchMappingEntry: (ast::Expr, ast::Pattern) = {
|
||||||
|
|
|
@ -65,7 +65,7 @@ expression: parse_ast
|
||||||
operand: Located {
|
operand: Located {
|
||||||
location: Location {
|
location: Location {
|
||||||
row: 5,
|
row: 5,
|
||||||
column: 9,
|
column: 10,
|
||||||
},
|
},
|
||||||
end_location: Some(
|
end_location: Some(
|
||||||
Location {
|
Location {
|
||||||
|
@ -2161,7 +2161,7 @@ expression: parse_ast
|
||||||
end_location: Some(
|
end_location: Some(
|
||||||
Location {
|
Location {
|
||||||
row: 41,
|
row: 41,
|
||||||
column: 21,
|
column: 13,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
custom: (),
|
custom: (),
|
||||||
|
@ -2176,7 +2176,7 @@ expression: parse_ast
|
||||||
right: Located {
|
right: Located {
|
||||||
location: Location {
|
location: Location {
|
||||||
row: 41,
|
row: 41,
|
||||||
column: 9,
|
column: 16,
|
||||||
},
|
},
|
||||||
end_location: Some(
|
end_location: Some(
|
||||||
Location {
|
Location {
|
||||||
|
@ -2320,7 +2320,7 @@ expression: parse_ast
|
||||||
operand: Located {
|
operand: Located {
|
||||||
location: Location {
|
location: Location {
|
||||||
row: 45,
|
row: 45,
|
||||||
column: 9,
|
column: 10,
|
||||||
},
|
},
|
||||||
end_location: Some(
|
end_location: Some(
|
||||||
Location {
|
Location {
|
||||||
|
@ -4738,7 +4738,7 @@ expression: parse_ast
|
||||||
end_location: Some(
|
end_location: Some(
|
||||||
Location {
|
Location {
|
||||||
row: 95,
|
row: 95,
|
||||||
column: 22,
|
column: 14,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
custom: (),
|
custom: (),
|
||||||
|
@ -4747,12 +4747,12 @@ expression: parse_ast
|
||||||
operand: Located {
|
operand: Located {
|
||||||
location: Location {
|
location: Location {
|
||||||
row: 95,
|
row: 95,
|
||||||
column: 9,
|
column: 10,
|
||||||
},
|
},
|
||||||
end_location: Some(
|
end_location: Some(
|
||||||
Location {
|
Location {
|
||||||
row: 95,
|
row: 95,
|
||||||
column: 22,
|
column: 14,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
custom: (),
|
custom: (),
|
||||||
|
@ -4769,7 +4769,7 @@ expression: parse_ast
|
||||||
right: Located {
|
right: Located {
|
||||||
location: Location {
|
location: Location {
|
||||||
row: 95,
|
row: 95,
|
||||||
column: 9,
|
column: 17,
|
||||||
},
|
},
|
||||||
end_location: Some(
|
end_location: Some(
|
||||||
Location {
|
Location {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue