Let located only for python located stuff

This commit is contained in:
Jeong YunWon 2023-05-10 03:17:56 +09:00
parent a3d9d8cb14
commit 1d366d52ab
7 changed files with 38 additions and 38 deletions

View file

@ -203,12 +203,12 @@ pub type LexResult = Result<Spanned, LexicalError>;
/// ```
#[inline]
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.
/// You probably want to use [`lex`] instead.
pub fn lex_located(
pub fn lex_starts_at(
source: &str,
mode: Mode,
start_offset: TextSize,

View file

@ -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;

View file

@ -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> {
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<ast::Expr, ParseErro
/// 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());
/// ```
pub fn parse_expression_located(
pub fn parse_expression_at(
source: &str,
path: &str,
offset: TextSize,
) -> 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,
_ => 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<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`].
@ -143,7 +143,7 @@ pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result<ast::Mod, Pa
/// # Example
///
/// ```
/// use rustpython_parser::{text_size::TextSize, Mode, parse_located};
/// use rustpython_parser::{text_size::TextSize, Mode, parse_starts_at};
///
/// let source = r#"
/// def fib(i):
@ -154,16 +154,16 @@ pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result<ast::Mod, Pa
///
/// 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());
/// ```
pub fn parse_located(
pub fn parse_starts_at(
source: &str,
mode: Mode,
source_path: &str,
offset: TextSize,
) -> 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)
}

View file

@ -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<Expr, ParseError> {
let fstring_body = format!("({source})");
let start = location - TextSize::from(1);
parse_expression_located(&fstring_body, "<fstring>", start)
parse_expression_at(&fstring_body, "<fstring>", start)
}
fn parse_string(