Rename to include "token" in method name (#10287)

Small quality of life improvement to rename the following method:
1. `current_kind` -> `current_token_kind`
2. `current_range` -> `current_token_range`

It's a PR for visibility.
This commit is contained in:
Dhruv Manilawala
2024-03-08 07:33:18 +05:30
parent bf67f129dd
commit f6467216dc
5 changed files with 111 additions and 91 deletions

View File

@@ -109,7 +109,7 @@ impl<'src> Parser<'src> {
fn current_op(&mut self) -> (Precedence, TokenKind, Associativity) {
const NOT_AN_OP: (Precedence, TokenKind, Associativity) =
(Precedence::Unknown, TokenKind::Unknown, Associativity::Left);
let kind = self.current_kind();
let kind = self.current_token_kind();
match kind {
TokenKind::Or => (Precedence::Or, kind, Associativity::Left),
@@ -209,7 +209,7 @@ impl<'src> Parser<'src> {
pub(super) fn parse_lhs_expression(&mut self) -> ParsedExpr {
let start = self.node_start();
let mut lhs = match self.current_kind() {
let mut lhs = match self.current_token_kind() {
TokenKind::Plus | TokenKind::Minus | TokenKind::Not | TokenKind::Tilde => {
Expr::UnaryOp(self.parse_unary_expression()).into()
}
@@ -237,7 +237,7 @@ impl<'src> Parser<'src> {
}
pub(super) fn parse_identifier(&mut self) -> ast::Identifier {
let range = self.current_range();
let range = self.current_token_range();
if self.at(TokenKind::Name) {
let (Tok::Name { name }, _) = self.next_token() else {
@@ -248,7 +248,7 @@ impl<'src> Parser<'src> {
range,
}
} else {
if self.current_kind().is_keyword() {
if self.current_token_kind().is_keyword() {
let (tok, range) = self.next_token();
self.add_error(
ParseErrorType::OtherError(format!(
@@ -278,7 +278,7 @@ impl<'src> Parser<'src> {
fn parse_atom(&mut self) -> ParsedExpr {
let start = self.node_start();
let lhs = match self.current_kind() {
let lhs = match self.current_token_kind() {
TokenKind::Float => {
let (Tok::Float { value }, _) = self.bump(TokenKind::Float) else {
unreachable!()
@@ -374,7 +374,7 @@ impl<'src> Parser<'src> {
} else {
self.add_error(
ParseErrorType::OtherError("Expression expected.".to_string()),
self.current_range(),
self.current_token_range(),
);
Expr::Name(ast::ExprName {
range: self.missing_node_range(),
@@ -390,7 +390,7 @@ impl<'src> Parser<'src> {
fn parse_postfix_expression(&mut self, mut lhs: Expr, start: TextSize) -> Expr {
loop {
lhs = match self.current_kind() {
lhs = match self.current_token_kind() {
TokenKind::Lpar => Expr::Call(self.parse_call_expression(lhs, start)),
TokenKind::Lsqb => Expr::Subscript(self.parse_subscript_expression(lhs, start)),
TokenKind::Dot => Expr::Attribute(self.parse_attribute_expression(lhs, start)),
@@ -401,7 +401,7 @@ impl<'src> Parser<'src> {
/// See: <https://docs.python.org/3/reference/expressions.html#calls>
fn parse_call_expression(&mut self, lhs: Expr, start: TextSize) -> ast::ExprCall {
assert_eq!(self.current_kind(), TokenKind::Lpar);
assert_eq!(self.current_token_kind(), TokenKind::Lpar);
let arguments = self.parse_arguments();
ast::ExprCall {
@@ -444,7 +444,7 @@ impl<'src> Parser<'src> {
let start = parser.node_start();
let mut parsed_expr = parser.parse_named_expression_or_higher();
match parser.current_kind() {
match parser.current_token_kind() {
TokenKind::Async | TokenKind::For => {
parsed_expr = Expr::Generator(parser.parse_generator_expression(
parsed_expr.expr,
@@ -528,7 +528,7 @@ impl<'src> Parser<'src> {
// Create an error when receiving a empty slice to parse, e.g. `l[]`
if !self.at(TokenKind::Colon) && !self.at_expr() {
let slice_range = TextRange::empty(self.current_range().start());
let slice_range = TextRange::empty(self.current_token_range().start());
self.expect(TokenKind::Rsqb);
let range = self.node_range(start);
@@ -629,9 +629,9 @@ impl<'src> Parser<'src> {
fn parse_unary_expression(&mut self) -> ast::ExprUnaryOp {
let start = self.node_start();
let op = UnaryOp::try_from(self.current_kind())
let op = UnaryOp::try_from(self.current_token_kind())
.expect("Expected operator to be a unary operator token.");
self.bump(self.current_kind());
self.bump(self.current_token_kind());
let operand = if matches!(op, UnaryOp::Not) {
self.parse_expression_with_precedence(Precedence::Not)
@@ -681,7 +681,7 @@ impl<'src> Parser<'src> {
let parsed_expr = self.parse_expression_with_precedence(op_bp);
values.push(parsed_expr.expr);
if self.current_kind() != op {
if self.current_token_kind() != op {
break;
}
@@ -703,7 +703,7 @@ impl<'src> Parser<'src> {
op_bp: Precedence,
) -> ast::ExprCompare {
let mut comparators = vec![];
let op = token_kind_to_cmp_op([op, self.current_kind()]).unwrap();
let op = token_kind_to_cmp_op([op, self.current_token_kind()]).unwrap();
let mut ops = vec![op];
if matches!(op, CmpOp::IsNot | CmpOp::NotIn) {
@@ -717,7 +717,7 @@ impl<'src> Parser<'src> {
let parsed_expr = self.parse_expression_with_precedence(op_bp);
comparators.push(parsed_expr.expr);
if let Ok(op) = token_kind_to_cmp_op([self.current_kind(), self.peek_nth(1)]) {
if let Ok(op) = token_kind_to_cmp_op([self.current_token_kind(), self.peek_nth(1)]) {
if matches!(op, CmpOp::IsNot | CmpOp::NotIn) {
self.next_token();
}
@@ -901,7 +901,7 @@ impl<'src> Parser<'src> {
while !self.at_ts(FSTRING_END_SET) {
progress.assert_progressing(self);
let element = match self.current_kind() {
let element = match self.current_token_kind() {
TokenKind::Lbrace => {
FStringElement::Expression(self.parse_fstring_expression_element())
}
@@ -947,7 +947,7 @@ impl<'src> Parser<'src> {
}
fn parse_fstring_expression_element(&mut self) -> ast::FStringExpressionElement {
let range = self.current_range();
let range = self.current_token_range();
let has_open_brace = self.eat(TokenKind::Lbrace);
let value = self.parse_expression();
@@ -961,7 +961,8 @@ impl<'src> Parser<'src> {
let leading_range = range
.add_start("{".text_len())
.cover_offset(value.range().start());
let trailing_range = TextRange::new(value.range().end(), self.current_range().start());
let trailing_range =
TextRange::new(value.range().end(), self.current_token_range().start());
Some(ast::DebugText {
leading: self.src_text(leading_range).to_string(),
trailing: self.src_text(trailing_range).to_string(),
@@ -999,7 +1000,7 @@ impl<'src> Parser<'src> {
None
};
let close_brace_range = self.current_range();
let close_brace_range = self.current_token_range();
if has_open_brace && !self.eat(TokenKind::Rbrace) {
self.add_error(
ParseErrorType::FStringError(FStringErrorType::UnclosedLbrace),
@@ -1026,7 +1027,7 @@ impl<'src> Parser<'src> {
if self.at_ts(NEWLINE_EOF_SET) {
self.add_error(
ParseErrorType::OtherError("missing closing bracket `]`".to_string()),
self.current_range(),
self.current_token_range(),
);
}
@@ -1041,7 +1042,7 @@ impl<'src> Parser<'src> {
let parsed_expr = self.parse_named_expression_or_higher();
match self.current_kind() {
match self.current_token_kind() {
TokenKind::Async | TokenKind::For => {
Expr::ListComp(self.parse_list_comprehension_expression(parsed_expr.expr, start))
}
@@ -1058,7 +1059,7 @@ impl<'src> Parser<'src> {
if self.at_ts(NEWLINE_EOF_SET) {
self.add_error(
ParseErrorType::OtherError("missing closing brace `}`".to_string()),
self.current_range(),
self.current_token_range(),
);
}
@@ -1079,7 +1080,7 @@ impl<'src> Parser<'src> {
let key_or_value = self.parse_named_expression_or_higher();
match self.current_kind() {
match self.current_token_kind() {
TokenKind::Async | TokenKind::For => {
Expr::SetComp(self.parse_set_comprehension_expression(key_or_value.expr, start))
}
@@ -1087,7 +1088,7 @@ impl<'src> Parser<'src> {
self.bump(TokenKind::Colon);
let value = self.parse_conditional_expression_or_higher();
if matches!(self.current_kind(), TokenKind::Async | TokenKind::For) {
if matches!(self.current_token_kind(), TokenKind::Async | TokenKind::For) {
Expr::DictComp(self.parse_dictionary_comprehension_expression(
key_or_value.expr,
value.expr,
@@ -1114,7 +1115,7 @@ impl<'src> Parser<'src> {
// Nice error message when having a unclosed open parenthesis `(`
if self.at_ts(NEWLINE_EOF_SET) {
let range = self.current_range();
let range = self.current_token_range();
self.add_error(
ParseErrorType::OtherError("missing closing parenthesis `)`".to_string()),
range,
@@ -1136,7 +1137,7 @@ impl<'src> Parser<'src> {
let mut parsed_expr = self.parse_named_expression_or_higher();
let parsed = match self.current_kind() {
let parsed = match self.current_token_kind() {
TokenKind::Comma => {
let tuple = self.parse_tuple_expression(
parsed_expr.expr,
@@ -1523,19 +1524,19 @@ impl<'src> Parser<'src> {
self.expect(TokenKind::Colon);
// Check for forbidden tokens in the `lambda`'s body
match self.current_kind() {
match self.current_token_kind() {
TokenKind::Yield => self.add_error(
ParseErrorType::OtherError(
"`yield` not allowed in a `lambda` expression".to_string(),
),
self.current_range(),
self.current_token_range(),
),
TokenKind::Star => {
self.add_error(
ParseErrorType::OtherError(
"starred expression not allowed in a `lambda` expression".to_string(),
),
self.current_range(),
self.current_token_range(),
);
}
TokenKind::DoubleStar => {
@@ -1544,7 +1545,7 @@ impl<'src> Parser<'src> {
"double starred expression not allowed in a `lambda` expression"
.to_string(),
),
self.current_range(),
self.current_token_range(),
);
}
_ => {}

View File

@@ -211,7 +211,7 @@ impl<'src> Parser<'src> {
// If it's not, you probably forgot to call `clear_ctx` somewhere.
assert_eq!(self.ctx, ParserCtxFlags::empty());
assert_eq!(
self.current_kind(),
self.current_token_kind(),
TokenKind::EndOfFile,
"Parser should be at the end of the file."
);
@@ -279,7 +279,7 @@ impl<'src> Parser<'src> {
/// Returns the start position for a node that starts at the current token.
fn node_start(&self) -> TextSize {
self.current_range().start()
self.current_token_range().start()
}
fn node_range(&self, start: TextSize) -> TextRange {
@@ -326,7 +326,7 @@ impl<'src> Parser<'src> {
fn peek_nth(&self, offset: usize) -> TokenKind {
if offset == 0 {
self.current_kind()
self.current_token_kind()
} else {
self.tokens
.peek_nth(offset - 1)
@@ -334,40 +334,50 @@ impl<'src> Parser<'src> {
}
}
/// Returns the current token kind along with its range.
///
/// Use `current_token_kind` or `current_token_range` to only get the kind or range
/// respectively.
#[inline]
fn current_token(&self) -> (TokenKind, TextRange) {
(self.current_kind(), self.current_range())
(self.current_token_kind(), self.current_token_range())
}
/// Returns the current token kind.
#[inline]
fn current_kind(&self) -> TokenKind {
fn current_token_kind(&self) -> TokenKind {
// TODO: Converting the token kind over and over again can be expensive.
TokenKind::from_token(&self.current.0)
}
/// Returns the range of the current token.
#[inline]
fn current_range(&self) -> TextRange {
fn current_token_range(&self) -> TextRange {
self.current.1
}
/// Eat the current token if it is of the given kind, returning `true` in
/// that case. Otherwise, return `false`.
fn eat(&mut self, kind: TokenKind) -> bool {
if !self.at(kind) {
return false;
if self.at(kind) {
self.next_token();
true
} else {
false
}
self.next_token();
true
}
/// Bumps the current token assuming it is of the given kind.
///
/// # Panics
///
/// If the current token is not of the given kind.
///
/// # Returns
///
/// The current token
fn bump(&mut self, kind: TokenKind) -> (Tok, TextRange) {
assert_eq!(self.current_kind(), kind);
assert_eq!(self.current_token_kind(), kind);
self.next_token()
}
@@ -382,7 +392,7 @@ impl<'src> Parser<'src> {
///
/// The current token.
fn bump_ts(&mut self, ts: TokenSet) -> (Tok, TextRange) {
assert!(ts.contains(self.current_kind()));
assert!(ts.contains(self.current_token_kind()));
self.next_token()
}
@@ -430,11 +440,11 @@ impl<'src> Parser<'src> {
}
fn at(&self, kind: TokenKind) -> bool {
self.current_kind() == kind
self.current_token_kind() == kind
}
fn at_ts(&self, ts: TokenSet) -> bool {
ts.contains(self.current_kind())
ts.contains(self.current_token_kind())
}
fn src_text<T>(&self, ranged: T) -> &'src str
@@ -487,7 +497,7 @@ impl<'src> Parser<'src> {
// Not a recognised element. Add an error and either skip the token or break parsing the list
// if the token is recognised as an element or terminator of an enclosing list.
let error = kind.create_error(self);
self.add_error(error, self.current_range());
self.add_error(error, self.current_token_range());
if should_recover {
break;
@@ -518,12 +528,12 @@ impl<'src> Parser<'src> {
loop {
progress.assert_progressing(self);
self.current_kind();
self.current_token_kind();
if kind.is_list_element(self) {
elements.push(parse_element(self));
let maybe_comma_range = self.current_range();
let maybe_comma_range = self.current_token_range();
if self.eat(TokenKind::Comma) {
trailing_comma_range = Some(maybe_comma_range);
continue;
@@ -544,14 +554,14 @@ impl<'src> Parser<'src> {
// Not a recognised element. Add an error and either skip the token or break parsing the list
// if the token is recognised as an element or terminator of an enclosing list.
let error = kind.create_error(self);
self.add_error(error, self.current_range());
self.add_error(error, self.current_token_range());
if should_recover {
break;
}
if self.at(TokenKind::Comma) {
trailing_comma_range = Some(self.current_range());
trailing_comma_range = Some(self.current_token_range());
} else {
trailing_comma_range = None;
}
@@ -640,7 +650,7 @@ impl<'src> Parser<'src> {
fn is_current_token_postfix(&self) -> bool {
matches!(
self.current_kind(),
self.current_token_kind(),
TokenKind::Lpar | TokenKind::Lsqb | TokenKind::Dot
)
}
@@ -712,19 +722,19 @@ impl RecoveryContextKind {
RecoveryContextKind::Elif => p.at(TokenKind::Else),
RecoveryContextKind::Except => {
matches!(p.current_kind(), TokenKind::Finally | TokenKind::Else)
matches!(p.current_token_kind(), TokenKind::Finally | TokenKind::Else)
}
// TODO: Should `semi` be part of the simple statement recovery set instead?
RecoveryContextKind::AssignmentTargets => {
matches!(p.current_kind(), TokenKind::Newline | TokenKind::Semi)
matches!(p.current_token_kind(), TokenKind::Newline | TokenKind::Semi)
}
// Tokens other than `]` are for better error recovery: For example, recover when we find the `:` of a clause header or
// the equal of a type assignment.
RecoveryContextKind::TypeParams => {
matches!(
p.current_kind(),
p.current_token_kind(),
TokenKind::Rsqb
| TokenKind::Newline
| TokenKind::Colon
@@ -733,7 +743,7 @@ impl RecoveryContextKind {
)
}
RecoveryContextKind::ImportNames => {
matches!(p.current_kind(), TokenKind::Rpar | TokenKind::Newline)
matches!(p.current_token_kind(), TokenKind::Rpar | TokenKind::Newline)
}
}
}
@@ -747,7 +757,7 @@ impl RecoveryContextKind {
RecoveryContextKind::AssignmentTargets => p.at(TokenKind::Equal),
RecoveryContextKind::TypeParams => p.is_at_type_param(),
RecoveryContextKind::ImportNames => {
matches!(p.current_kind(), TokenKind::Star | TokenKind::Name)
matches!(p.current_token_kind(), TokenKind::Star | TokenKind::Name)
}
}
}
@@ -770,7 +780,7 @@ impl RecoveryContextKind {
.to_string(),
),
RecoveryContextKind::AssignmentTargets => {
if p.current_kind().is_keyword() {
if p.current_token_kind().is_keyword() {
ParseErrorType::OtherError(
"The keyword is not allowed as a variable declaration name".to_string(),
)

View File

@@ -74,7 +74,7 @@ impl<'src> Parser<'src> {
fn parse_match_pattern_lhs(&mut self) -> Pattern {
let start = self.node_start();
let mut lhs = match self.current_kind() {
let mut lhs = match self.current_token_kind() {
TokenKind::Lbrace => Pattern::MatchMapping(self.parse_match_pattern_mapping()),
TokenKind::Star => Pattern::MatchStar(self.parse_match_pattern_star()),
TokenKind::Lpar | TokenKind::Lsqb => self.parse_delimited_match_pattern(),
@@ -261,13 +261,16 @@ impl<'src> Parser<'src> {
SequenceMatchPatternParentheses::List
};
if matches!(self.current_kind(), TokenKind::Newline | TokenKind::Colon) {
if matches!(
self.current_token_kind(),
TokenKind::Newline | TokenKind::Colon
) {
self.add_error(
ParseErrorType::OtherError(format!(
"missing `{closing}`",
closing = if parentheses.is_list() { "]" } else { ")" }
)),
self.current_range(),
self.current_token_range(),
);
}
@@ -330,7 +333,7 @@ impl<'src> Parser<'src> {
fn parse_match_pattern_literal(&mut self) -> Pattern {
let start = self.node_start();
match self.current_kind() {
match self.current_token_kind() {
TokenKind::None => {
self.bump(TokenKind::None);
Pattern::MatchSingleton(ast::PatternMatchSingleton {
@@ -460,7 +463,7 @@ impl<'src> Parser<'src> {
} else {
self.add_error(
ParseErrorType::OtherError("Expression expected.".to_string()),
self.current_range(),
self.current_token_range(),
);
Expr::Name(ast::ExprName {
range: self.missing_node_range(),
@@ -478,7 +481,7 @@ impl<'src> Parser<'src> {
}
fn parse_attr_expr_for_match_pattern(&mut self, mut lhs: Expr, start: TextSize) -> Expr {
while self.current_kind() == TokenKind::Dot {
while self.current_token_kind() == TokenKind::Dot {
lhs = Expr::Attribute(self.parse_attribute_expression(lhs, start));
}

View File

@@ -12,7 +12,7 @@ impl ParserProgress {
fn has_progressed(self, p: &Parser) -> bool {
match self.0 {
None => true,
Some(snapshot) => snapshot != (p.current_kind(), p.current_range().start()),
Some(snapshot) => snapshot != (p.current_token_kind(), p.current_token_range().start()),
}
}
@@ -26,11 +26,11 @@ impl ParserProgress {
assert!(
self.has_progressed(p),
"The parser is no longer progressing. Stuck at '{}' {:?}:{:?}",
p.src_text(p.current_range()),
p.current_kind(),
p.current_range(),
p.src_text(p.current_token_range()),
p.current_token_kind(),
p.current_token_range(),
);
self.0 = Some((p.current_kind(), p.current_range().start()));
self.0 = Some((p.current_token_kind(), p.current_token_range().start()));
}
}

View File

@@ -85,7 +85,7 @@ impl<'src> Parser<'src> {
/// Parses a compound or a simple statement.
pub(super) fn parse_statement(&mut self) -> Stmt {
let start_offset = self.node_start();
match self.current_kind() {
match self.current_token_kind() {
TokenKind::If => Stmt::If(self.parse_if_statement()),
TokenKind::For => Stmt::For(self.parse_for_statement(start_offset)),
TokenKind::While => Stmt::While(self.parse_while_statement()),
@@ -113,7 +113,7 @@ impl<'src> Parser<'src> {
let has_eaten_newline = self.eat(TokenKind::Newline);
if !has_eaten_newline && !has_eaten_semicolon && self.at_simple_stmt() {
let range = self.current_range();
let range = self.current_token_range();
self.add_error(
ParseErrorType::SimpleStmtsInSameLine,
stmt.range().cover(range),
@@ -136,7 +136,7 @@ impl<'src> Parser<'src> {
self.add_error(
ParseErrorType::SimpleStmtAndCompoundStmtInSameLine,
stmt.range().cover(self.current_range()),
stmt.range().cover(self.current_token_range()),
);
}
@@ -179,7 +179,7 @@ impl<'src> Parser<'src> {
/// See: <https://docs.python.org/3/reference/simple_stmts.html#simple-statements>
fn parse_simple_statement(&mut self) -> Stmt {
match self.current_kind() {
match self.current_token_kind() {
TokenKind::Return => Stmt::Return(self.parse_return_statement()),
TokenKind::Import => Stmt::Import(self.parse_import_statement()),
TokenKind::From => Stmt::ImportFrom(self.parse_from_import_statement()),
@@ -203,7 +203,7 @@ impl<'src> Parser<'src> {
Stmt::Assign(self.parse_assign_statement(parsed_expr, start))
} else if self.eat(TokenKind::Colon) {
Stmt::AnnAssign(self.parse_annotated_assignment_statement(parsed_expr, start))
} else if let Ok(op) = Operator::try_from(self.current_kind()) {
} else if let Ok(op) = Operator::try_from(self.current_token_kind()) {
Stmt::AugAssign(self.parse_augmented_assignment_statement(
parsed_expr,
op,
@@ -381,7 +381,7 @@ impl<'src> Parser<'src> {
};
if level == 0 && module.is_none() {
let range = self.current_range();
let range = self.current_token_range();
self.add_error(
ParseErrorType::OtherError("missing module name".to_string()),
range,
@@ -777,7 +777,7 @@ impl<'src> Parser<'src> {
};
if !has_except && !has_finally {
let range = self.current_range();
let range = self.current_token_range();
self.add_error(
ParseErrorType::OtherError(
"expecting `except` or `finally` after `try` block".to_string(),
@@ -952,7 +952,7 @@ impl<'src> Parser<'src> {
let mut items = vec![];
if !self.at_expr() {
let range = self.current_range();
let range = self.current_token_range();
self.add_error(
ParseErrorType::OtherError("expecting expression after `with` keyword".to_string()),
range,
@@ -996,7 +996,7 @@ impl<'src> Parser<'src> {
let mut has_seen_rpar = false;
let mut has_seen_colon_equal = false;
let mut has_seen_star = false;
let mut prev_token = self.current_kind();
let mut prev_token = self.current_token_kind();
loop {
match self.peek_nth(index) {
TokenKind::Lpar => {
@@ -1170,7 +1170,7 @@ impl<'src> Parser<'src> {
self.eat(TokenKind::Newline);
if !self.eat(TokenKind::Indent) {
let range = self.current_range();
let range = self.current_token_range();
self.add_error(
ParseErrorType::OtherError(
"expected an indented block after `match` statement".to_string(),
@@ -1194,7 +1194,7 @@ impl<'src> Parser<'src> {
if !self.at(TokenKind::Case) {
self.add_error(
ParseErrorType::OtherError("expecting `case` block after `match`".to_string()),
self.current_range(),
self.current_token_range(),
);
}
@@ -1239,7 +1239,7 @@ impl<'src> Parser<'src> {
let async_start = self.node_start();
self.bump(TokenKind::Async);
match self.current_kind() {
match self.current_token_kind() {
TokenKind::Def => Stmt::FunctionDef(ast::StmtFunctionDef {
is_async: true,
..self.parse_function_definition(vec![], async_start)
@@ -1255,7 +1255,10 @@ impl<'src> Parser<'src> {
kind => {
// Although this statement is not a valid `async` statement,
// we still parse it.
self.add_error(ParseErrorType::StmtIsNotAsync(kind), self.current_range());
self.add_error(
ParseErrorType::StmtIsNotAsync(kind),
self.current_token_range(),
);
self.parse_statement()
}
}
@@ -1281,7 +1284,7 @@ impl<'src> Parser<'src> {
self.expect(TokenKind::Newline);
}
match self.current_kind() {
match self.current_token_kind() {
TokenKind::Def => {
Stmt::FunctionDef(self.parse_function_definition(decorators, start_offset))
}
@@ -1301,7 +1304,7 @@ impl<'src> Parser<'src> {
ParseErrorType::OtherError(
"expected class, function definition or async function definition after decorator".to_string(),
),
self.current_range(),
self.current_token_range(),
);
self.parse_statement()
}
@@ -1323,7 +1326,7 @@ impl<'src> Parser<'src> {
ParseErrorType::OtherError(format!(
"expected a single statement or an indented body after {parent_clause}"
)),
self.current_range(),
self.current_token_range(),
);
Vec::new()
@@ -1402,7 +1405,7 @@ impl<'src> Parser<'src> {
if has_seen_vararg {
parser.add_error(
ParseErrorType::ParamFollowsVarKeywordParam,
parser.current_range(),
parser.current_token_range(),
);
}
@@ -1423,7 +1426,7 @@ impl<'src> Parser<'src> {
if has_seen_asterisk {
parser.add_error(
ParseErrorType::OtherError("`/` must be ahead of `*`".to_string()),
parser.current_range(),
parser.current_token_range(),
);
}
std::mem::swap(&mut args, &mut posonlyargs);
@@ -1433,7 +1436,10 @@ impl<'src> Parser<'src> {
// can't place `b` after `a=1`. Non-default parameters are only allowed after
// default parameters if we have a `*` before them, e.g. `a=1, *, b`.
if param.default.is_none() && has_seen_default_param && !has_seen_asterisk {
parser.add_error(ParseErrorType::DefaultArgumentError, parser.current_range());
parser.add_error(
ParseErrorType::DefaultArgumentError,
parser.current_token_range(),
);
}
has_seen_default_param = param.default.is_some();
@@ -1447,14 +1453,14 @@ impl<'src> Parser<'src> {
return;
}
let range = parser.current_range();
let range = parser.current_token_range();
#[allow(deprecated)]
parser.skip_until(
ending_set.union(TokenSet::new([TokenKind::Comma, TokenKind::Colon])),
);
parser.add_error(
ParseErrorType::OtherError("expected parameter".to_string()),
range.cover(parser.current_range()), // TODO(micha): This goes one token too far?
range.cover(parser.current_token_range()), // TODO(micha): This goes one token too far?
);
}
});
@@ -1500,9 +1506,9 @@ impl<'src> Parser<'src> {
pub(super) fn is_at_type_param(&self) -> bool {
matches!(
self.current_kind(),
self.current_token_kind(),
TokenKind::Star | TokenKind::DoubleStar | TokenKind::Name
) || self.current_kind().is_keyword()
) || self.current_token_kind().is_keyword()
}
fn parse_type_param(&mut self) -> ast::TypeParam {