From 1d366d52ab96b6fb01923f38e02e34ae9ec8444c Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 10 May 2023 03:17:56 +0900 Subject: [PATCH] Let `located` only for python located stuff --- ast/asdl_rs.py | 14 +++++++------- ast/src/gen/generic.rs | 28 ++++++++++++++-------------- ast/src/source_locator.rs | 4 ++-- parser/src/lexer.rs | 4 ++-- parser/src/lib.rs | 2 +- parser/src/parser.rs | 20 ++++++++++---------- parser/src/string.rs | 4 ++-- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index ce21642..b8f5c9a 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -391,9 +391,9 @@ class FoldTraitDefVisitor(EmitVisitor): ) self.emit( """ - fn map_located(&mut self, located: Attributed) -> Result, Self::Error> { - let custom = self.map_user(located.custom)?; - Ok(Attributed { range: located.range, custom, node: located.node }) + fn map_attributed(&mut self, attributed: Attributed) -> Result, Self::Error> { + let custom = self.map_user(attributed.custom)?; + Ok(Attributed { range: attributed.range, custom, node: attributed.node }) }""", depth + 1, ) @@ -423,11 +423,11 @@ class FoldTraitDefVisitor(EmitVisitor): class FoldImplVisitor(EmitVisitor): def visitModule(self, mod, depth): self.emit( - "fn fold_located + ?Sized, T, MT>(folder: &mut F, node: Attributed, f: impl FnOnce(&mut F, T) -> Result) -> Result, F::Error> {", + "fn fold_attributed + ?Sized, T, MT>(folder: &mut F, node: Attributed, f: impl FnOnce(&mut F, T) -> Result) -> Result, F::Error> {", depth, ) self.emit( - "let node = folder.map_located(node)?; Ok(Attributed { custom: node.custom, range: node.range, node: f(folder, node.node)? })", + "let node = folder.map_attributed(node)?; Ok(Attributed { custom: node.custom, range: node.range, node: f(folder, node.node)? })", depth + 1, ) self.emit("}", depth) @@ -459,7 +459,7 @@ class FoldImplVisitor(EmitVisitor): depth, ) if typeinfo.has_attributes: - self.emit("fold_located(folder, node, |folder, node| {", depth) + self.emit("fold_attributed(folder, node, |folder, node| {", depth) self.emit("match node {", depth + 1) for cons in sum.types: @@ -501,7 +501,7 @@ class FoldImplVisitor(EmitVisitor): depth, ) if has_attributes: - self.emit("fold_located(folder, node, |folder, node| {", depth) + self.emit("fold_attributed(folder, node, |folder, node| {", depth) rustname = structname + "Data" else: rustname = structname diff --git a/ast/src/gen/generic.rs b/ast/src/gen/generic.rs index 4e497eb..be48836 100644 --- a/ast/src/gen/generic.rs +++ b/ast/src/gen/generic.rs @@ -1020,15 +1020,15 @@ pub mod fold { type Error; fn map_user(&mut self, user: U) -> Result; - fn map_located( + fn map_attributed( &mut self, - located: Attributed, + attributed: Attributed, ) -> Result, Self::Error> { - let custom = self.map_user(located.custom)?; + let custom = self.map_user(attributed.custom)?; Ok(Attributed { - range: located.range, + range: attributed.range, custom, - node: located.node, + node: attributed.node, }) } @@ -1114,12 +1114,12 @@ pub mod fold { fold_type_ignore(self, node) } } - fn fold_located + ?Sized, T, MT>( + fn fold_attributed + ?Sized, T, MT>( folder: &mut F, node: Attributed, f: impl FnOnce(&mut F, T) -> Result, ) -> Result, F::Error> { - let node = folder.map_located(node)?; + let node = folder.map_attributed(node)?; Ok(Attributed { custom: node.custom, range: node.range, @@ -1171,7 +1171,7 @@ pub mod fold { #[allow(unused)] folder: &mut F, node: Stmt, ) -> Result, F::Error> { - fold_located(folder, node, |folder, node| match node { + fold_attributed(folder, node, |folder, node| match node { StmtKind::FunctionDef(StmtFunctionDef { name, args, @@ -1375,7 +1375,7 @@ pub mod fold { #[allow(unused)] folder: &mut F, node: Expr, ) -> Result, F::Error> { - fold_located(folder, node, |folder, node| match node { + fold_attributed(folder, node, |folder, node| match node { ExprKind::BoolOp(ExprBoolOp { op, values }) => Ok(ExprKind::BoolOp(ExprBoolOp { op: Foldable::fold(op, folder)?, values: Foldable::fold(values, folder)?, @@ -1675,7 +1675,7 @@ pub mod fold { #[allow(unused)] folder: &mut F, node: Excepthandler, ) -> Result, F::Error> { - fold_located(folder, node, |folder, node| match node { + fold_attributed(folder, node, |folder, node| match node { ExcepthandlerKind::ExceptHandler(ExcepthandlerExceptHandler { type_, name, body }) => { Ok(ExcepthandlerKind::ExceptHandler( ExcepthandlerExceptHandler { @@ -1732,7 +1732,7 @@ pub mod fold { #[allow(unused)] folder: &mut F, node: Arg, ) -> Result, F::Error> { - fold_located(folder, node, |folder, node| { + fold_attributed(folder, node, |folder, node| { let ArgData { arg, annotation, @@ -1758,7 +1758,7 @@ pub mod fold { #[allow(unused)] folder: &mut F, node: Keyword, ) -> Result, F::Error> { - fold_located(folder, node, |folder, node| { + fold_attributed(folder, node, |folder, node| { let KeywordData { arg, value } = node; Ok(KeywordData { arg: Foldable::fold(arg, folder)?, @@ -1779,7 +1779,7 @@ pub mod fold { #[allow(unused)] folder: &mut F, node: Alias, ) -> Result, F::Error> { - fold_located(folder, node, |folder, node| { + fold_attributed(folder, node, |folder, node| { let AliasData { name, asname } = node; Ok(AliasData { name: Foldable::fold(name, folder)?, @@ -1846,7 +1846,7 @@ pub mod fold { #[allow(unused)] folder: &mut F, node: Pattern, ) -> Result, F::Error> { - fold_located(folder, node, |folder, node| match node { + fold_attributed(folder, node, |folder, node| match node { PatternKind::MatchValue(PatternMatchValue { value }) => { Ok(PatternKind::MatchValue(PatternMatchValue { value: Foldable::fold(value, folder)?, diff --git a/ast/src/source_locator.rs b/ast/src/source_locator.rs index bc7055d..8a81e29 100644 --- a/ast/src/source_locator.rs +++ b/ast/src/source_locator.rs @@ -7,10 +7,10 @@ impl crate::fold::Fold<()> for SourceLocator<'_> { #[cold] fn map_user(&mut self, _user: ()) -> Result { - unreachable!("implemented map_located"); + unreachable!("implemented map_attributed"); } - fn map_located( + fn map_attributed( &mut self, node: Attributed, ) -> Result, Self::Error> { diff --git a/parser/src/lexer.rs b/parser/src/lexer.rs index 333c5a5..1f72667 100644 --- a/parser/src/lexer.rs +++ b/parser/src/lexer.rs @@ -203,12 +203,12 @@ pub type LexResult = Result; /// ``` #[inline] pub fn lex(source: &str, mode: Mode) -> impl Iterator + '_ { - lex_located(source, mode, TextSize::default()) + lex_starts_at(source, mode, TextSize::default()) } /// Create a new lexer from a source string, starting at a given location. /// You probably want to use [`lex`] instead. -pub fn lex_located( +pub fn lex_starts_at( source: &str, mode: Mode, start_offset: TextSize, diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 8119d01..5cfa4a6 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -125,7 +125,7 @@ mod string; mod token; pub use parser::{ - parse, parse_expression, parse_expression_located, parse_located, parse_program, parse_tokens, + parse, parse_expression, parse_expression_at, parse_program, parse_starts_at, parse_tokens, ParseError, ParseErrorType, }; pub use string::FStringErrorType; diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 726afa6..d13c884 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -70,7 +70,7 @@ pub fn parse_program(source: &str, source_path: &str) -> Result Result { - parse_expression_located(source, path, TextSize::default()) + parse_expression_at(source, path, TextSize::default()) } /// Parses a Python expression from a given location. @@ -84,17 +84,17 @@ pub fn parse_expression(source: &str, path: &str) -> Result", TextSize::from(400)); +/// let expr = parse_expression_at("1 + 2", "", TextSize::from(400)); /// assert!(expr.is_ok()); /// ``` -pub fn parse_expression_located( +pub fn parse_expression_at( source: &str, path: &str, offset: TextSize, ) -> Result { - parse_located(source, Mode::Expression, path, offset).map(|top| match top { + parse_starts_at(source, Mode::Expression, path, offset).map(|top| match top { ast::Mod::Expression(ast::ModExpression { body }) => *body, _ => unreachable!(), }) @@ -132,7 +132,7 @@ pub fn parse_expression_located( /// assert!(program.is_ok()); /// ``` pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result { - parse_located(source, mode, source_path, TextSize::default()) + parse_starts_at(source, mode, source_path, TextSize::default()) } /// Parse the given Python source code using the specified [`Mode`] and [`Location`]. @@ -143,7 +143,7 @@ pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result Result", TextSize::from(0)); +/// let program = parse_starts_at(source, Mode::Module, "", TextSize::from(0)); /// assert!(program.is_ok()); /// ``` -pub fn parse_located( +pub fn parse_starts_at( source: &str, mode: Mode, source_path: &str, offset: TextSize, ) -> Result { - let lxr = lexer::lex_located(source, mode, offset); + let lxr = lexer::lex_starts_at(source, mode, offset); parse_tokens(lxr, mode, source_path) } diff --git a/parser/src/string.rs b/parser/src/string.rs index 03d3ae2..9127917 100644 --- a/parser/src/string.rs +++ b/parser/src/string.rs @@ -6,7 +6,7 @@ use crate::{ ast::{self, Constant, Expr, ExprKind}, lexer::{LexicalError, LexicalErrorType}, - parser::{parse_expression_located, LalrpopError, ParseError, ParseErrorType}, + parser::{parse_expression_at, LalrpopError, ParseError, ParseErrorType}, token::{StringKind, Tok}, }; use itertools::Itertools; @@ -575,7 +575,7 @@ impl<'a> StringParser<'a> { fn parse_fstring_expr(source: &str, location: TextSize) -> Result { let fstring_body = format!("({source})"); let start = location - TextSize::from(1); - parse_expression_located(&fstring_body, "", start) + parse_expression_at(&fstring_body, "", start) } fn parse_string(