mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-12 07:35:16 +00:00
Let located
only for python located stuff
This commit is contained in:
parent
a3d9d8cb14
commit
1d366d52ab
7 changed files with 38 additions and 38 deletions
|
@ -391,9 +391,9 @@ class FoldTraitDefVisitor(EmitVisitor):
|
||||||
)
|
)
|
||||||
self.emit(
|
self.emit(
|
||||||
"""
|
"""
|
||||||
fn map_located<T>(&mut self, located: Attributed<T, U>) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
|
fn map_attributed<T>(&mut self, attributed: Attributed<T, U>) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
|
||||||
let custom = self.map_user(located.custom)?;
|
let custom = self.map_user(attributed.custom)?;
|
||||||
Ok(Attributed { range: located.range, custom, node: located.node })
|
Ok(Attributed { range: attributed.range, custom, node: attributed.node })
|
||||||
}""",
|
}""",
|
||||||
depth + 1,
|
depth + 1,
|
||||||
)
|
)
|
||||||
|
@ -423,11 +423,11 @@ class FoldTraitDefVisitor(EmitVisitor):
|
||||||
class FoldImplVisitor(EmitVisitor):
|
class FoldImplVisitor(EmitVisitor):
|
||||||
def visitModule(self, mod, depth):
|
def visitModule(self, mod, depth):
|
||||||
self.emit(
|
self.emit(
|
||||||
"fn fold_located<U, F: Fold<U> + ?Sized, T, MT>(folder: &mut F, node: Attributed<T, U>, f: impl FnOnce(&mut F, T) -> Result<MT, F::Error>) -> Result<Attributed<MT, F::TargetU>, F::Error> {",
|
"fn fold_attributed<U, F: Fold<U> + ?Sized, T, MT>(folder: &mut F, node: Attributed<T, U>, f: impl FnOnce(&mut F, T) -> Result<MT, F::Error>) -> Result<Attributed<MT, F::TargetU>, F::Error> {",
|
||||||
depth,
|
depth,
|
||||||
)
|
)
|
||||||
self.emit(
|
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,
|
depth + 1,
|
||||||
)
|
)
|
||||||
self.emit("}", depth)
|
self.emit("}", depth)
|
||||||
|
@ -459,7 +459,7 @@ class FoldImplVisitor(EmitVisitor):
|
||||||
depth,
|
depth,
|
||||||
)
|
)
|
||||||
if typeinfo.has_attributes:
|
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)
|
self.emit("match node {", depth + 1)
|
||||||
for cons in sum.types:
|
for cons in sum.types:
|
||||||
|
@ -501,7 +501,7 @@ class FoldImplVisitor(EmitVisitor):
|
||||||
depth,
|
depth,
|
||||||
)
|
)
|
||||||
if has_attributes:
|
if has_attributes:
|
||||||
self.emit("fold_located(folder, node, |folder, node| {", depth)
|
self.emit("fold_attributed(folder, node, |folder, node| {", depth)
|
||||||
rustname = structname + "Data"
|
rustname = structname + "Data"
|
||||||
else:
|
else:
|
||||||
rustname = structname
|
rustname = structname
|
||||||
|
|
|
@ -1020,15 +1020,15 @@ pub mod fold {
|
||||||
type Error;
|
type Error;
|
||||||
fn map_user(&mut self, user: U) -> Result<Self::TargetU, Self::Error>;
|
fn map_user(&mut self, user: U) -> Result<Self::TargetU, Self::Error>;
|
||||||
|
|
||||||
fn map_located<T>(
|
fn map_attributed<T>(
|
||||||
&mut self,
|
&mut self,
|
||||||
located: Attributed<T, U>,
|
attributed: Attributed<T, U>,
|
||||||
) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
|
) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
|
||||||
let custom = self.map_user(located.custom)?;
|
let custom = self.map_user(attributed.custom)?;
|
||||||
Ok(Attributed {
|
Ok(Attributed {
|
||||||
range: located.range,
|
range: attributed.range,
|
||||||
custom,
|
custom,
|
||||||
node: located.node,
|
node: attributed.node,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1114,12 +1114,12 @@ pub mod fold {
|
||||||
fold_type_ignore(self, node)
|
fold_type_ignore(self, node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn fold_located<U, F: Fold<U> + ?Sized, T, MT>(
|
fn fold_attributed<U, F: Fold<U> + ?Sized, T, MT>(
|
||||||
folder: &mut F,
|
folder: &mut F,
|
||||||
node: Attributed<T, U>,
|
node: Attributed<T, U>,
|
||||||
f: impl FnOnce(&mut F, T) -> Result<MT, F::Error>,
|
f: impl FnOnce(&mut F, T) -> Result<MT, F::Error>,
|
||||||
) -> Result<Attributed<MT, F::TargetU>, F::Error> {
|
) -> Result<Attributed<MT, F::TargetU>, F::Error> {
|
||||||
let node = folder.map_located(node)?;
|
let node = folder.map_attributed(node)?;
|
||||||
Ok(Attributed {
|
Ok(Attributed {
|
||||||
custom: node.custom,
|
custom: node.custom,
|
||||||
range: node.range,
|
range: node.range,
|
||||||
|
@ -1171,7 +1171,7 @@ pub mod fold {
|
||||||
#[allow(unused)] folder: &mut F,
|
#[allow(unused)] folder: &mut F,
|
||||||
node: Stmt<U>,
|
node: Stmt<U>,
|
||||||
) -> Result<Stmt<F::TargetU>, F::Error> {
|
) -> Result<Stmt<F::TargetU>, F::Error> {
|
||||||
fold_located(folder, node, |folder, node| match node {
|
fold_attributed(folder, node, |folder, node| match node {
|
||||||
StmtKind::FunctionDef(StmtFunctionDef {
|
StmtKind::FunctionDef(StmtFunctionDef {
|
||||||
name,
|
name,
|
||||||
args,
|
args,
|
||||||
|
@ -1375,7 +1375,7 @@ pub mod fold {
|
||||||
#[allow(unused)] folder: &mut F,
|
#[allow(unused)] folder: &mut F,
|
||||||
node: Expr<U>,
|
node: Expr<U>,
|
||||||
) -> Result<Expr<F::TargetU>, F::Error> {
|
) -> Result<Expr<F::TargetU>, 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 {
|
ExprKind::BoolOp(ExprBoolOp { op, values }) => Ok(ExprKind::BoolOp(ExprBoolOp {
|
||||||
op: Foldable::fold(op, folder)?,
|
op: Foldable::fold(op, folder)?,
|
||||||
values: Foldable::fold(values, folder)?,
|
values: Foldable::fold(values, folder)?,
|
||||||
|
@ -1675,7 +1675,7 @@ pub mod fold {
|
||||||
#[allow(unused)] folder: &mut F,
|
#[allow(unused)] folder: &mut F,
|
||||||
node: Excepthandler<U>,
|
node: Excepthandler<U>,
|
||||||
) -> Result<Excepthandler<F::TargetU>, F::Error> {
|
) -> Result<Excepthandler<F::TargetU>, F::Error> {
|
||||||
fold_located(folder, node, |folder, node| match node {
|
fold_attributed(folder, node, |folder, node| match node {
|
||||||
ExcepthandlerKind::ExceptHandler(ExcepthandlerExceptHandler { type_, name, body }) => {
|
ExcepthandlerKind::ExceptHandler(ExcepthandlerExceptHandler { type_, name, body }) => {
|
||||||
Ok(ExcepthandlerKind::ExceptHandler(
|
Ok(ExcepthandlerKind::ExceptHandler(
|
||||||
ExcepthandlerExceptHandler {
|
ExcepthandlerExceptHandler {
|
||||||
|
@ -1732,7 +1732,7 @@ pub mod fold {
|
||||||
#[allow(unused)] folder: &mut F,
|
#[allow(unused)] folder: &mut F,
|
||||||
node: Arg<U>,
|
node: Arg<U>,
|
||||||
) -> Result<Arg<F::TargetU>, F::Error> {
|
) -> Result<Arg<F::TargetU>, F::Error> {
|
||||||
fold_located(folder, node, |folder, node| {
|
fold_attributed(folder, node, |folder, node| {
|
||||||
let ArgData {
|
let ArgData {
|
||||||
arg,
|
arg,
|
||||||
annotation,
|
annotation,
|
||||||
|
@ -1758,7 +1758,7 @@ pub mod fold {
|
||||||
#[allow(unused)] folder: &mut F,
|
#[allow(unused)] folder: &mut F,
|
||||||
node: Keyword<U>,
|
node: Keyword<U>,
|
||||||
) -> Result<Keyword<F::TargetU>, F::Error> {
|
) -> Result<Keyword<F::TargetU>, F::Error> {
|
||||||
fold_located(folder, node, |folder, node| {
|
fold_attributed(folder, node, |folder, node| {
|
||||||
let KeywordData { arg, value } = node;
|
let KeywordData { arg, value } = node;
|
||||||
Ok(KeywordData {
|
Ok(KeywordData {
|
||||||
arg: Foldable::fold(arg, folder)?,
|
arg: Foldable::fold(arg, folder)?,
|
||||||
|
@ -1779,7 +1779,7 @@ pub mod fold {
|
||||||
#[allow(unused)] folder: &mut F,
|
#[allow(unused)] folder: &mut F,
|
||||||
node: Alias<U>,
|
node: Alias<U>,
|
||||||
) -> Result<Alias<F::TargetU>, F::Error> {
|
) -> Result<Alias<F::TargetU>, F::Error> {
|
||||||
fold_located(folder, node, |folder, node| {
|
fold_attributed(folder, node, |folder, node| {
|
||||||
let AliasData { name, asname } = node;
|
let AliasData { name, asname } = node;
|
||||||
Ok(AliasData {
|
Ok(AliasData {
|
||||||
name: Foldable::fold(name, folder)?,
|
name: Foldable::fold(name, folder)?,
|
||||||
|
@ -1846,7 +1846,7 @@ pub mod fold {
|
||||||
#[allow(unused)] folder: &mut F,
|
#[allow(unused)] folder: &mut F,
|
||||||
node: Pattern<U>,
|
node: Pattern<U>,
|
||||||
) -> Result<Pattern<F::TargetU>, F::Error> {
|
) -> Result<Pattern<F::TargetU>, F::Error> {
|
||||||
fold_located(folder, node, |folder, node| match node {
|
fold_attributed(folder, node, |folder, node| match node {
|
||||||
PatternKind::MatchValue(PatternMatchValue { value }) => {
|
PatternKind::MatchValue(PatternMatchValue { value }) => {
|
||||||
Ok(PatternKind::MatchValue(PatternMatchValue {
|
Ok(PatternKind::MatchValue(PatternMatchValue {
|
||||||
value: Foldable::fold(value, folder)?,
|
value: Foldable::fold(value, folder)?,
|
||||||
|
|
|
@ -7,10 +7,10 @@ impl crate::fold::Fold<()> for SourceLocator<'_> {
|
||||||
|
|
||||||
#[cold]
|
#[cold]
|
||||||
fn map_user(&mut self, _user: ()) -> Result<Self::TargetU, Self::Error> {
|
fn map_user(&mut self, _user: ()) -> Result<Self::TargetU, Self::Error> {
|
||||||
unreachable!("implemented map_located");
|
unreachable!("implemented map_attributed");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_located<T>(
|
fn map_attributed<T>(
|
||||||
&mut self,
|
&mut self,
|
||||||
node: Attributed<T, ()>,
|
node: Attributed<T, ()>,
|
||||||
) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
|
) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
|
||||||
|
|
|
@ -203,12 +203,12 @@ pub type LexResult = Result<Spanned, LexicalError>;
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn lex(source: &str, mode: Mode) -> impl Iterator<Item = LexResult> + '_ {
|
pub fn lex(source: &str, mode: Mode) -> impl Iterator<Item = LexResult> + '_ {
|
||||||
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.
|
/// Create a new lexer from a source string, starting at a given location.
|
||||||
/// You probably want to use [`lex`] instead.
|
/// You probably want to use [`lex`] instead.
|
||||||
pub fn lex_located(
|
pub fn lex_starts_at(
|
||||||
source: &str,
|
source: &str,
|
||||||
mode: Mode,
|
mode: Mode,
|
||||||
start_offset: TextSize,
|
start_offset: TextSize,
|
||||||
|
|
|
@ -125,7 +125,7 @@ mod string;
|
||||||
mod token;
|
mod token;
|
||||||
|
|
||||||
pub use parser::{
|
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,
|
ParseError, ParseErrorType,
|
||||||
};
|
};
|
||||||
pub use string::FStringErrorType;
|
pub use string::FStringErrorType;
|
||||||
|
|
|
@ -70,7 +70,7 @@ pub fn parse_program(source: &str, source_path: &str) -> Result<ast::Suite, Pars
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse_expression(source: &str, path: &str) -> Result<ast::Expr, ParseError> {
|
pub fn parse_expression(source: &str, path: &str) -> Result<ast::Expr, ParseError> {
|
||||||
parse_expression_located(source, path, TextSize::default())
|
parse_expression_at(source, path, TextSize::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a Python expression from a given location.
|
/// Parses a Python expression from a given location.
|
||||||
|
@ -84,17 +84,17 @@ pub fn parse_expression(source: &str, path: &str) -> Result<ast::Expr, ParseErro
|
||||||
/// somewhat silly, location:
|
/// somewhat silly, location:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use rustpython_parser::{text_size::TextSize, parse_expression_located};
|
/// use rustpython_parser::{text_size::TextSize, parse_expression_at};
|
||||||
///
|
///
|
||||||
/// let expr = parse_expression_located("1 + 2", "<embedded>", TextSize::from(400));
|
/// let expr = parse_expression_at("1 + 2", "<embedded>", TextSize::from(400));
|
||||||
/// assert!(expr.is_ok());
|
/// assert!(expr.is_ok());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse_expression_located(
|
pub fn parse_expression_at(
|
||||||
source: &str,
|
source: &str,
|
||||||
path: &str,
|
path: &str,
|
||||||
offset: TextSize,
|
offset: TextSize,
|
||||||
) -> Result<ast::Expr, ParseError> {
|
) -> Result<ast::Expr, ParseError> {
|
||||||
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,
|
ast::Mod::Expression(ast::ModExpression { body }) => *body,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
|
@ -132,7 +132,7 @@ pub fn parse_expression_located(
|
||||||
/// assert!(program.is_ok());
|
/// assert!(program.is_ok());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result<ast::Mod, ParseError> {
|
pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result<ast::Mod, ParseError> {
|
||||||
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`].
|
/// 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<ast::Mod, Pa
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use rustpython_parser::{text_size::TextSize, Mode, parse_located};
|
/// use rustpython_parser::{text_size::TextSize, Mode, parse_starts_at};
|
||||||
///
|
///
|
||||||
/// let source = r#"
|
/// let source = r#"
|
||||||
/// def fib(i):
|
/// def fib(i):
|
||||||
|
@ -154,16 +154,16 @@ pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result<ast::Mod, Pa
|
||||||
///
|
///
|
||||||
/// print(fib(42))
|
/// print(fib(42))
|
||||||
/// "#;
|
/// "#;
|
||||||
/// let program = parse_located(source, Mode::Module, "<embedded>", TextSize::from(0));
|
/// let program = parse_starts_at(source, Mode::Module, "<embedded>", TextSize::from(0));
|
||||||
/// assert!(program.is_ok());
|
/// assert!(program.is_ok());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse_located(
|
pub fn parse_starts_at(
|
||||||
source: &str,
|
source: &str,
|
||||||
mode: Mode,
|
mode: Mode,
|
||||||
source_path: &str,
|
source_path: &str,
|
||||||
offset: TextSize,
|
offset: TextSize,
|
||||||
) -> Result<ast::Mod, ParseError> {
|
) -> Result<ast::Mod, ParseError> {
|
||||||
let lxr = lexer::lex_located(source, mode, offset);
|
let lxr = lexer::lex_starts_at(source, mode, offset);
|
||||||
parse_tokens(lxr, mode, source_path)
|
parse_tokens(lxr, mode, source_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, Constant, Expr, ExprKind},
|
ast::{self, Constant, Expr, ExprKind},
|
||||||
lexer::{LexicalError, LexicalErrorType},
|
lexer::{LexicalError, LexicalErrorType},
|
||||||
parser::{parse_expression_located, LalrpopError, ParseError, ParseErrorType},
|
parser::{parse_expression_at, LalrpopError, ParseError, ParseErrorType},
|
||||||
token::{StringKind, Tok},
|
token::{StringKind, Tok},
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
@ -575,7 +575,7 @@ impl<'a> StringParser<'a> {
|
||||||
fn parse_fstring_expr(source: &str, location: TextSize) -> Result<Expr, ParseError> {
|
fn parse_fstring_expr(source: &str, location: TextSize) -> Result<Expr, ParseError> {
|
||||||
let fstring_body = format!("({source})");
|
let fstring_body = format!("({source})");
|
||||||
let start = location - TextSize::from(1);
|
let start = location - TextSize::from(1);
|
||||||
parse_expression_located(&fstring_body, "<fstring>", start)
|
parse_expression_at(&fstring_body, "<fstring>", start)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_string(
|
fn parse_string(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue