From a5553aea436c6335ca722a0ffe2ceed03a014928 Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 25 Dec 2022 09:37:07 +0900 Subject: [PATCH] Fix slice location --- parser/python.lalrpop | 2 +- parser/src/parser.rs | 7 ++ ...stpython_parser__parser__tests__slice.snap | 115 ++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 parser/src/snapshots/rustpython_parser__parser__tests__slice.snap diff --git a/parser/python.lalrpop b/parser/python.lalrpop index 85dea57..04007f0 100644 --- a/parser/python.lalrpop +++ b/parser/python.lalrpop @@ -1018,7 +1018,7 @@ SubscriptList: ast::Expr = { Subscript: ast::Expr = { Test<"all">, - ?> ":" ?> => { + ?> ":" ?> => { let lower = e1.map(Box::new); let upper = e2.map(Box::new); let step = e3.flatten().map(Box::new); diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 27192b6..705fb99 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -233,4 +233,11 @@ class Foo(A, B): let parse_ast = parse_expression(&source, "").unwrap(); insta::assert_debug_snapshot!(parse_ast); } + + #[test] + fn test_slice() { + let source = String::from("x[1:2:3]"); + let parse_ast = parse_expression(&source, "").unwrap(); + insta::assert_debug_snapshot!(parse_ast); + } } diff --git a/parser/src/snapshots/rustpython_parser__parser__tests__slice.snap b/parser/src/snapshots/rustpython_parser__parser__tests__slice.snap new file mode 100644 index 0000000..2d404f0 --- /dev/null +++ b/parser/src/snapshots/rustpython_parser__parser__tests__slice.snap @@ -0,0 +1,115 @@ +--- +source: compiler/parser/src/parser.rs +expression: parse_ast +--- +Located { + location: Location { + row: 1, + column: 0, + }, + end_location: Some( + Location { + row: 1, + column: 8, + }, + ), + custom: (), + node: Subscript { + value: Located { + location: Location { + row: 1, + column: 0, + }, + end_location: Some( + Location { + row: 1, + column: 1, + }, + ), + custom: (), + node: Name { + id: "x", + ctx: Load, + }, + }, + slice: Located { + location: Location { + row: 1, + column: 2, + }, + end_location: Some( + Location { + row: 1, + column: 7, + }, + ), + custom: (), + node: Slice { + lower: Some( + Located { + location: Location { + row: 1, + column: 2, + }, + end_location: Some( + Location { + row: 1, + column: 3, + }, + ), + custom: (), + node: Constant { + value: Int( + 1, + ), + kind: None, + }, + }, + ), + upper: Some( + Located { + location: Location { + row: 1, + column: 4, + }, + end_location: Some( + Location { + row: 1, + column: 5, + }, + ), + custom: (), + node: Constant { + value: Int( + 2, + ), + kind: None, + }, + }, + ), + step: Some( + Located { + location: Location { + row: 1, + column: 6, + }, + end_location: Some( + Location { + row: 1, + column: 7, + }, + ), + custom: (), + node: Constant { + value: Int( + 3, + ), + kind: None, + }, + }, + ), + }, + }, + ctx: Load, + }, +}