Skip to content

Commit 3209b85

Browse files
LaBatata101dhruvmanila
authored andcommitted
Replace LALRPOP parser with hand-written parser
1 parent e1928be commit 3209b85

File tree

286 files changed

+37844
-6732
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

286 files changed

+37844
-6732
lines changed

Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ruff/tests/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ from module import =
521521
----- stdout -----
522522
523523
----- stderr -----
524-
error: Failed to parse main.py:2:20: Unexpected token '='
524+
error: Failed to parse main.py:2:20: expecting an identifier
525525
"###);
526526

527527
Ok(())

crates/ruff/tests/integration_test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,11 @@ fn stdin_parse_error() {
727727
success: false
728728
exit_code: 1
729729
----- stdout -----
730-
-:1:17: E999 SyntaxError: Unexpected token '='
730+
-:1:17: E999 SyntaxError: expecting an identifier
731731
Found 1 error.
732732
733733
----- stderr -----
734-
error: Failed to parse at 1:17: Unexpected token '='
734+
error: Failed to parse at 1:17: expecting an identifier
735735
"###);
736736
}
737737

crates/ruff_linter/src/checkers/ast/analyze/expression.rs

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
134134
elts,
135135
ctx,
136136
range: _,
137+
parenthesized: _,
137138
})
138139
| Expr::List(ast::ExprList {
139140
elts,

crates/ruff_linter/src/checkers/ast/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,7 @@ where
12981298
elts,
12991299
ctx,
13001300
range: _,
1301+
parenthesized: _,
13011302
}) = slice.as_ref()
13021303
{
13031304
let mut iter = elts.iter();

crates/ruff_linter/src/logging.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl DisplayParseError {
194194
// Translate the byte offset to a location in the originating source.
195195
let location =
196196
if let Some(jupyter_index) = source_kind.as_ipy_notebook().map(Notebook::index) {
197-
let source_location = source_code.source_location(error.offset);
197+
let source_location = source_code.source_location(error.location.start());
198198

199199
ErrorLocation::Cell(
200200
jupyter_index
@@ -208,7 +208,7 @@ impl DisplayParseError {
208208
},
209209
)
210210
} else {
211-
ErrorLocation::File(source_code.source_location(error.offset))
211+
ErrorLocation::File(source_code.source_location(error.location.start()))
212212
};
213213

214214
Self {
@@ -275,27 +275,7 @@ impl<'a> DisplayParseErrorType<'a> {
275275

276276
impl Display for DisplayParseErrorType<'_> {
277277
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
278-
match self.0 {
279-
ParseErrorType::Eof => write!(f, "Expected token but reached end of file."),
280-
ParseErrorType::ExtraToken(ref tok) => write!(
281-
f,
282-
"Got extraneous token: {tok}",
283-
tok = TruncateAtNewline(&tok)
284-
),
285-
ParseErrorType::InvalidToken => write!(f, "Got invalid token"),
286-
ParseErrorType::UnrecognizedToken(ref tok, ref expected) => {
287-
if let Some(expected) = expected.as_ref() {
288-
write!(
289-
f,
290-
"Expected '{expected}', but got {tok}",
291-
tok = TruncateAtNewline(&tok)
292-
)
293-
} else {
294-
write!(f, "Unexpected token {tok}", tok = TruncateAtNewline(&tok))
295-
}
296-
}
297-
ParseErrorType::Lexical(ref error) => write!(f, "{error}"),
298-
}
278+
write!(f, "{}", TruncateAtNewline(&self.0))
299279
}
300280
}
301281

crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ fn type_pattern(elts: Vec<&Expr>) -> Expr {
109109
elts: elts.into_iter().cloned().collect(),
110110
ctx: ExprContext::Load,
111111
range: TextRange::default(),
112+
parenthesized: true,
112113
}
113114
.into()
114115
}

crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ pub(crate) fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) {
173173
.collect(),
174174
ctx: ExprContext::Load,
175175
range: TextRange::default(),
176+
parenthesized: true,
176177
});
177178
let node1 = Expr::Name(ast::ExprName {
178179
id: arg_name.into(),

crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp
7272
elts,
7373
range: _,
7474
ctx: _,
75+
parenthesized: _,
7576
}) = slice.as_ref()
7677
{
7778
for expr in elts {
@@ -123,6 +124,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp
123124
elts: literal_exprs.into_iter().cloned().collect(),
124125
range: TextRange::default(),
125126
ctx: ExprContext::Load,
127+
parenthesized: true,
126128
})),
127129
range: TextRange::default(),
128130
ctx: ExprContext::Load,
@@ -148,6 +150,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp
148150
elts,
149151
range: TextRange::default(),
150152
ctx: ExprContext::Load,
153+
parenthesized: true,
151154
})),
152155
range: TextRange::default(),
153156
ctx: ExprContext::Load,

crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
130130
.collect(),
131131
ctx: ExprContext::Load,
132132
range: TextRange::default(),
133+
parenthesized: true,
133134
})),
134135
ctx: ExprContext::Load,
135136
range: TextRange::default(),
@@ -151,6 +152,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
151152
elts: exprs.into_iter().cloned().collect(),
152153
ctx: ExprContext::Load,
153154
range: TextRange::default(),
155+
parenthesized: true,
154156
})),
155157
ctx: ExprContext::Load,
156158
range: TextRange::default(),

crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ fn is_empty_or_null_fstring_element(element: &ast::FStringElement) -> bool {
7474
ast::FStringElement::Expression(ast::FStringExpressionElement { expression, .. }) => {
7575
is_empty_or_null_string(expression)
7676
}
77+
#[allow(deprecated)]
78+
ast::FStringElement::Invalid(_) => false,
7779
}
7880
}
7981

crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs

+2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
337337
.collect(),
338338
ctx: ExprContext::Load,
339339
range: TextRange::default(),
340+
parenthesized: true,
340341
});
341342
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
342343
format!("({})", checker.generator().expr(&node)),
@@ -444,6 +445,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
444445
elts: elts.clone(),
445446
ctx: ExprContext::Load,
446447
range: TextRange::default(),
448+
parenthesized: true,
447449
});
448450
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
449451
format!("({})", checker.generator().expr(&node)),

crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ pub(crate) fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) {
428428
.collect(),
429429
ctx: ExprContext::Load,
430430
range: TextRange::default(),
431+
parenthesized: true,
431432
};
432433
let node1 = ast::ExprName {
433434
id: "isinstance".into(),
@@ -543,6 +544,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
543544
elts: comparators.into_iter().map(Clone::clone).collect(),
544545
ctx: ExprContext::Load,
545546
range: TextRange::default(),
547+
parenthesized: true,
546548
};
547549
let node1 = ast::ExprName {
548550
id: id.into(),
@@ -718,7 +720,7 @@ fn get_short_circuit_edit(
718720
generator.expr(expr)
719721
};
720722
Edit::range_replacement(
721-
if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _}) if !elts.is_empty())
723+
if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _, parenthesized: _}) if !elts.is_empty())
722724
{
723725
format!("({content})")
724726
} else {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
---
22
source: crates/ruff_linter/src/rules/isort/mod.rs
33
---
4+
bom_sorted.py:1:1: I001 [*] Import block is un-sorted or un-formatted
5+
|
6+
1 | import bar
7+
| _^
8+
2 | | import foo
9+
|
10+
= help: Organize imports
11+
12+
Safe fix
13+
1 |-import bar
14+
2 |-import foo
15+
1 |+import rt
16+
417

crates/ruff_linter/src/rules/isort/snapshots/ruff_linter__rules__isort__tests__bom_unsorted.py.snap

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ bom_unsorted.py:1:1: I001 [*] Import block is un-sorted or un-formatted
1212
Safe fix
1313
1 |-import foo
1414
2 |-import bar
15-
1 |+import bar
16-
2 |+import foo
15+
1 |+import rt
1716

1817

crates/ruff_linter/src/rules/pycodestyle/rules/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) fn syntax_error(
8181
parse_error: &ParseError,
8282
locator: &Locator,
8383
) {
84-
let rest = locator.after(parse_error.offset);
84+
let rest = locator.after(parse_error.location.start());
8585

8686
// Try to create a non-empty range so that the diagnostic can print a caret at the
8787
// right position. This requires that we retrieve the next character, if any, and take its length
@@ -95,6 +95,6 @@ pub(crate) fn syntax_error(
9595
SyntaxError {
9696
message: format!("{}", DisplayParseErrorType::new(&parse_error.error)),
9797
},
98-
TextRange::at(parse_error.offset, len),
98+
TextRange::at(parse_error.location.start(), len),
9999
));
100100
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
---
22
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
33
---
4-
E999.py:3:1: E999 SyntaxError: unindent does not match any outer indentation level
5-
|
6-
2 | def x():
7-
3 |
8-
| ^ E999
9-
4 |
10-
|
4+
E999.py:5:1: E999 SyntaxError: expected a single statement or an indented body after function definition
5+
|
6+
|
117

128

crates/ruff_linter/src/rules/pylint/rules/assert_on_string_literal.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ pub(crate) fn assert_on_string_literal(checker: &mut Checker, test: &Expr) {
7777
ast::FStringElement::Literal(ast::FStringLiteralElement {
7878
value, ..
7979
}) => value.is_empty(),
80-
ast::FStringElement::Expression(_) => false,
80+
#[allow(deprecated)]
81+
ast::FStringElement::Expression(_) | ast::FStringElement::Invalid(_) => {
82+
false
83+
}
8184
})
8285
}
8386
}) {
@@ -89,7 +92,10 @@ pub(crate) fn assert_on_string_literal(checker: &mut Checker, test: &Expr) {
8992
ast::FStringElement::Literal(ast::FStringLiteralElement {
9093
value, ..
9194
}) => !value.is_empty(),
92-
ast::FStringElement::Expression(_) => false,
95+
#[allow(deprecated)]
96+
ast::FStringElement::Expression(_) | ast::FStringElement::Invalid(_) => {
97+
false
98+
}
9399
})
94100
}
95101
}) {

crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ fn assignment_targets_from_expr<'a>(
305305
ctx: ExprContext::Store,
306306
elts,
307307
range: _,
308+
parenthesized: _,
308309
}) => Box::new(
309310
elts.iter()
310311
.flat_map(|elt| assignment_targets_from_expr(elt, dummy_variable_rgx)),

crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast:
143143
elts: comparators.iter().copied().cloned().collect(),
144144
range: TextRange::default(),
145145
ctx: ExprContext::Load,
146+
parenthesized: true,
146147
})]),
147148
range: bool_op.range(),
148149
})),

crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E
127127
elts: remaining,
128128
ctx: ExprContext::Load,
129129
range: TextRange::default(),
130+
parenthesized: true,
130131
};
131132
format!("({})", checker.generator().expr(&node.into()))
132133
};

crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E
141141
elts: remaining,
142142
ctx: ExprContext::Load,
143143
range: TextRange::default(),
144+
parenthesized: true,
144145
};
145146
format!("({})", checker.generator().expr(&node.into()))
146147
};

crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs

+2
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,7 @@ fn is_allowed_value(expr: &Expr) -> bool {
200200
| Expr::Starred(_)
201201
| Expr::Slice(_)
202202
| Expr::IpyEscapeCommand(_) => false,
203+
#[allow(deprecated)]
204+
Expr::Invalid(_) => false,
203205
}
204206
}

crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub(crate) fn non_pep695_type_alias(checker: &mut Checker, stmt: &StmtAnnAssign)
127127
range: TextRange::default(),
128128
elts: constraints.into_iter().cloned().collect(),
129129
ctx: ast::ExprContext::Load,
130+
parenthesized: true,
130131
})))
131132
}
132133
None => None,

0 commit comments

Comments
 (0)