diff --git a/ast/src/optimizer.rs b/ast/src/optimizer.rs index 4013a0f..f871734 100644 --- a/ast/src/optimizer.rs +++ b/ast/src/optimizer.rs @@ -52,6 +52,7 @@ impl crate::fold::Fold for ConstantOptimizer { #[cfg(test)] mod tests { use num_bigint::BigInt; + use rustpython_parser_core::text_size::TextRange; #[cfg(feature = "constant-optimization")] #[test] diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 141dc2a..90f267f 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -23,7 +23,9 @@ use crate::{ use itertools::Itertools; use std::iter; +use crate::text_size::TextRange; pub(super) use lalrpop_util::ParseError as LalrpopError; +use rustpython_ast::OptionalRange; /// Parse a full Python program usually consisting of multiple lines. /// @@ -318,6 +320,11 @@ impl ParseErrorType { } } +#[inline(always)] +pub(super) fn optional_range(start: TextSize, end: TextSize) -> OptionalRange { + OptionalRange::::new(start, end) +} + #[cfg(test)] mod tests { use super::*; diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index 2e0061d..8953ad5 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -4,13 +4,13 @@ // See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword use crate::{ - ast::{self as ast, Ranged, OptionalRange}, + ast::{self as ast, Ranged}, lexer::{LexicalError, LexicalErrorType}, function::{ArgumentList, parse_args, parse_params, validate_arguments}, context::set_context, string::parse_strings, token::{self, StringKind}, - text_size::TextSize, + text_size::TextSize, parser::optional_range }; use num_bigint::BigInt; @@ -20,9 +20,9 @@ grammar; // For each public entry point, a full parse table is generated. // By having only a single pub function, we reduce this to one. pub Top: ast::Mod = { - StartModule => ast::ModModule { body, type_ignores: vec![], range: OptionalRange::new(start, end) }.into(), - StartInteractive => ast::ModInteractive { body, range: OptionalRange::new(start, end) }.into(), - StartExpression ("\n")* => ast::ModExpression { body: Box::new(body), range: OptionalRange::new(start, end) }.into() + StartModule => ast::ModModule { body, type_ignores: vec![], range: optional_range(start, end) }.into(), + StartInteractive => ast::ModInteractive { body, range: optional_range(start, end) }.into(), + StartExpression ("\n")* => ast::ModExpression { body: Box::new(body), range: optional_range(start, end) }.into() }; Program: ast::Suite = { @@ -371,7 +371,7 @@ MatchCase: ast::MatchCase = { pattern, guard: guard.map(Box::new), body, - range: OptionalRange::new(start, end) + range: optional_range(start, end) } }, } @@ -929,15 +929,15 @@ WithItems: Vec = { #[inline] WithItemsNoAs: Vec = { >> => { - all.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) }).collect() + all.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None, range: optional_range(location, end_location) }).collect() }, } WithItem: ast::Withitem = { - > if Goal != "as" => ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) }, + > if Goal != "as" => ast::Withitem { context_expr, optional_vars: None, range: optional_range(location, end_location) }, > "as" > => { let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store))); - ast::Withitem { context_expr, optional_vars, range: OptionalRange::new(location, end_location) } + ast::Withitem { context_expr, optional_vars, range: optional_range(location, end_location) } }, }; @@ -966,7 +966,7 @@ Parameters: ast::Arguments = { kw_defaults: vec![], kwarg: None, defaults: vec![], - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) }) )?; @@ -991,7 +991,7 @@ ParameterList: ast::Arguments = { kwarg, defaults, kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) }) }, > )> ","? =>? { @@ -1011,7 +1011,7 @@ ParameterList: ast::Arguments = { kwarg, defaults, kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) }) }, > ","? => { @@ -1024,7 +1024,7 @@ ParameterList: ast::Arguments = { kwarg, defaults: vec![], kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } }, > ","? => { @@ -1036,7 +1036,7 @@ ParameterList: ast::Arguments = { kwarg, defaults: vec![], kw_defaults: vec![], - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } }, }; @@ -1194,7 +1194,7 @@ LambdaDef: ast::Expr = { kw_defaults: vec![], kwarg: None, defaults: vec![], - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } } ))?; @@ -1564,7 +1564,7 @@ SingleForComprehension: ast::Comprehension = { iter, ifs, is_async, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } } }; diff --git a/parser/src/python.rs b/parser/src/python.rs index b648da7..71b4c2b 100644 --- a/parser/src/python.rs +++ b/parser/src/python.rs @@ -1,13 +1,13 @@ // auto-generated: "lalrpop 0.20.0" -// sha3: dcd27eae3d0dc16a3ba0744e12b96ef7e802b7fe3eff03cbd5e86f6bb02cc325 +// sha3: 2a588ed2309b95ae978a172dc2a1234d79c6f56440b66bd964f4818b3e021e00 use crate::{ - ast::{self as ast, Ranged, OptionalRange}, + ast::{self as ast, Ranged}, lexer::{LexicalError, LexicalErrorType}, function::{ArgumentList, parse_args, parse_params, validate_arguments}, context::set_context, string::parse_strings, token::{self, StringKind}, - text_size::TextSize, + text_size::TextSize, parser::optional_range }; use num_bigint::BigInt; #[allow(unused_extern_crates)] @@ -22,13 +22,13 @@ extern crate alloc; mod __parse__Top { use crate::{ - ast::{self as ast, Ranged, OptionalRange}, + ast::{self as ast, Ranged}, lexer::{LexicalError, LexicalErrorType}, function::{ArgumentList, parse_args, parse_params, validate_arguments}, context::set_context, string::parse_strings, token::{self, StringKind}, - text_size::TextSize, + text_size::TextSize, parser::optional_range }; use num_bigint::BigInt; #[allow(unused_extern_crates)] @@ -36623,7 +36623,7 @@ fn __action1< (_, end, _): (TextSize, TextSize, TextSize), ) -> ast::Mod { - ast::ModModule { body, type_ignores: vec![], range: OptionalRange::new(start, end) }.into() + ast::ModModule { body, type_ignores: vec![], range: optional_range(start, end) }.into() } #[allow(clippy::too_many_arguments)] @@ -36635,7 +36635,7 @@ fn __action2< (_, end, _): (TextSize, TextSize, TextSize), ) -> ast::Mod { - ast::ModInteractive { body, range: OptionalRange::new(start, end) }.into() + ast::ModInteractive { body, range: optional_range(start, end) }.into() } #[allow(clippy::too_many_arguments)] @@ -36648,7 +36648,7 @@ fn __action3< (_, end, _): (TextSize, TextSize, TextSize), ) -> ast::Mod { - ast::ModExpression { body: Box::new(body), range: OptionalRange::new(start, end) }.into() + ast::ModExpression { body: Box::new(body), range: optional_range(start, end) }.into() } #[allow(clippy::too_many_arguments)] @@ -37615,7 +37615,7 @@ fn __action80< pattern, guard: guard.map(Box::new), body, - range: OptionalRange::new(start, end) + range: optional_range(start, end) } } } @@ -38887,7 +38887,7 @@ fn __action153< ) -> Vec { { - all.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) }).collect() + all.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None, range: optional_range(location, end_location) }).collect() } } @@ -38938,7 +38938,7 @@ fn __action155< kw_defaults: vec![], kwarg: None, defaults: vec![], - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) }) )?; @@ -39124,7 +39124,7 @@ fn __action166< kw_defaults: vec![], kwarg: None, defaults: vec![], - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } } ))?; @@ -39555,7 +39555,7 @@ fn __action206< iter, ifs, is_async, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } } } @@ -39974,7 +39974,7 @@ fn __action240< kwarg, defaults, kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) }) } } @@ -40006,7 +40006,7 @@ fn __action241< kwarg, defaults, kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) }) } } @@ -40030,7 +40030,7 @@ fn __action242< kwarg, defaults: vec![], kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } } } @@ -40053,7 +40053,7 @@ fn __action243< kwarg, defaults: vec![], kw_defaults: vec![], - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } } } @@ -40198,7 +40198,7 @@ fn __action256< kwarg, defaults, kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) }) } } @@ -40230,7 +40230,7 @@ fn __action257< kwarg, defaults, kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) }) } } @@ -40254,7 +40254,7 @@ fn __action258< kwarg, defaults: vec![], kw_defaults, - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } } } @@ -40277,7 +40277,7 @@ fn __action259< kwarg, defaults: vec![], kw_defaults: vec![], - range: OptionalRange::new(location, end_location) + range: optional_range(location, end_location) } } } @@ -40371,7 +40371,7 @@ fn __action268< (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Withitem { - ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) } + ast::Withitem { context_expr, optional_vars: None, range: optional_range(location, end_location) } } #[allow(clippy::too_many_arguments)] @@ -40386,7 +40386,7 @@ fn __action269< { { let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store))); - ast::Withitem { context_expr, optional_vars, range: OptionalRange::new(location, end_location) } + ast::Withitem { context_expr, optional_vars, range: optional_range(location, end_location) } } } @@ -40427,7 +40427,7 @@ fn __action273< (_, end_location, _): (TextSize, TextSize, TextSize), ) -> ast::Withitem { - ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) } + ast::Withitem { context_expr, optional_vars: None, range: optional_range(location, end_location) } } #[allow(clippy::too_many_arguments)] @@ -40442,7 +40442,7 @@ fn __action274< { { let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store))); - ast::Withitem { context_expr, optional_vars, range: OptionalRange::new(location, end_location) } + ast::Withitem { context_expr, optional_vars, range: optional_range(location, end_location) } } } @@ -40458,7 +40458,7 @@ fn __action275< { { let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store))); - ast::Withitem { context_expr, optional_vars, range: OptionalRange::new(location, end_location) } + ast::Withitem { context_expr, optional_vars, range: optional_range(location, end_location) } } }