1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
use std::fmt::{self, Display};
use ruff_python_ast::PythonVersion;
use ruff_python_ast::token::TokenKind;
use ruff_text_size::{Ranged, TextRange};
use crate::string::InterpolatedStringKind;
/// Represents represent errors that occur during parsing and are
/// returned by the `parse_*` functions.
#[derive(Debug, PartialEq, Eq, Clone, get_size2::GetSize)]
pub struct ParseError {
pub error: ParseErrorType,
pub location: TextRange,
}
impl std::ops::Deref for ParseError {
type Target = ParseErrorType;
fn deref(&self) -> &Self::Target {
&self.error
}
}
impl std::error::Error for ParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.error)
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{} at byte range {:?}", &self.error, self.location)
}
}
impl From<LexicalError> for ParseError {
fn from(error: LexicalError) -> Self {
ParseError {
location: error.location(),
error: ParseErrorType::Lexical(error.into_error()),
}
}
}
impl Ranged for ParseError {
fn range(&self) -> TextRange {
self.location
}
}
impl ParseError {
pub fn error(self) -> ParseErrorType {
self.error
}
}
/// Represents the different types of errors that can occur during parsing of an f-string or t-string.
#[derive(Debug, Clone, PartialEq, Eq, get_size2::GetSize)]
pub enum InterpolatedStringErrorType {
/// Expected a right brace after an opened left brace.
UnclosedLbrace,
/// An invalid conversion flag was encountered.
InvalidConversionFlag,
/// A single right brace was encountered.
SingleRbrace,
/// Unterminated string.
UnterminatedString,
/// Unterminated triple-quoted string.
UnterminatedTripleQuotedString,
/// A lambda expression without parentheses was encountered.
LambdaWithoutParentheses,
/// Conversion flag does not immediately follow exclamation.
ConversionFlagNotImmediatelyAfterExclamation,
/// Newline inside of a format spec for a single quoted f- or t-string.
NewlineInFormatSpec,
}
impl std::fmt::Display for InterpolatedStringErrorType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::UnclosedLbrace => write!(f, "expecting `}}`"),
Self::InvalidConversionFlag => write!(f, "invalid conversion character"),
Self::SingleRbrace => write!(f, "single `}}` is not allowed"),
Self::UnterminatedString => write!(f, "unterminated string"),
Self::UnterminatedTripleQuotedString => write!(f, "unterminated triple-quoted string"),
Self::LambdaWithoutParentheses => {
write!(f, "lambda expressions are not allowed without parentheses")
}
Self::ConversionFlagNotImmediatelyAfterExclamation => write!(
f,
"conversion type must come right after the exclamation mark"
),
Self::NewlineInFormatSpec => {
write!(
f,
"newlines are not allowed in format specifiers when using single quotes"
)
}
}
}
}
/// Represents the different types of errors that can occur during parsing.
#[derive(Debug, PartialEq, Eq, Clone, get_size2::GetSize)]
pub enum ParseErrorType {
/// An unexpected error occurred.
OtherError(String),
/// An empty slice was found during parsing, e.g `data[]`.
EmptySlice,
/// An empty global names list was found during parsing.
EmptyGlobalNames,
/// An empty nonlocal names list was found during parsing.
EmptyNonlocalNames,
/// An empty delete targets list was found during parsing.
EmptyDeleteTargets,
/// An empty import names list was found during parsing.
EmptyImportNames,
/// An empty type parameter list was found during parsing.
EmptyTypeParams,
/// An unparenthesized named expression was found where it is not allowed.
UnparenthesizedNamedExpression,
/// An unparenthesized tuple expression was found where it is not allowed.
UnparenthesizedTupleExpression,
/// An unparenthesized generator expression was found where it is not allowed.
UnparenthesizedGeneratorExpression,
/// An invalid usage of a lambda expression was found.
InvalidLambdaExpressionUsage,
/// An invalid usage of a yield expression was found.
InvalidYieldExpressionUsage,
/// An invalid usage of a starred expression was found.
InvalidStarredExpressionUsage,
/// A star pattern was found outside a sequence pattern.
InvalidStarPatternUsage,
/// A parameter was found after a vararg.
ParamAfterVarKeywordParam,
/// A non-default parameter follows a default parameter.
NonDefaultParamAfterDefaultParam,
/// A default value was found for a `*` or `**` parameter.
VarParameterWithDefault,
/// A keyword argument was repeated.
DuplicateKeywordArgumentError(String),
/// An invalid expression was found in the assignment target.
InvalidAssignmentTarget,
/// An invalid expression was found in the named assignment target.
InvalidNamedAssignmentTarget,
/// An invalid expression was found in the annotated assignment target.
InvalidAnnotatedAssignmentTarget,
/// An invalid expression was found in the augmented assignment target.
InvalidAugmentedAssignmentTarget,
/// An invalid expression was found in the delete target.
InvalidDeleteTarget,
/// A positional argument was found after a keyword argument.
PositionalAfterKeywordArgument,
/// A positional argument was found after a keyword argument unpacking.
PositionalAfterKeywordUnpacking,
/// An iterable argument unpacking was found after keyword argument unpacking.
InvalidArgumentUnpackingOrder,
/// An invalid usage of iterable unpacking in a comprehension was found.
IterableUnpackingInComprehension,
/// Multiple simple statements were found in the same line without a `;` separating them.
SimpleStatementsOnSameLine,
/// A simple statement and a compound statement was found in the same line.
SimpleAndCompoundStatementOnSameLine,
/// Expected one or more keyword parameter after `*` separator.
ExpectedKeywordParam,
/// Expected a real number for a complex literal pattern.
ExpectedRealNumber,
/// Expected an imaginary number for a complex literal pattern.
ExpectedImaginaryNumber,
/// Expected an expression at the current parser location.
ExpectedExpression,
/// The parser expected a specific token that was not found.
ExpectedToken {
expected: TokenKind,
found: TokenKind,
},
/// An unexpected indentation was found during parsing.
UnexpectedIndentation,
/// The statement being parsed cannot be `async`.
UnexpectedTokenAfterAsync(TokenKind),
/// Ipython escape command was found
UnexpectedIpythonEscapeCommand,
/// An unexpected token was found at the end of an expression parsing
UnexpectedExpressionToken,
/// An f-string error containing the [`InterpolatedStringErrorType`].
FStringError(InterpolatedStringErrorType),
/// A t-string error containing the [`InterpolatedStringErrorType`].
TStringError(InterpolatedStringErrorType),
/// Parser encountered an error during lexing.
Lexical(LexicalErrorType),
}
impl ParseErrorType {
pub(crate) fn from_interpolated_string_error(
error: InterpolatedStringErrorType,
string_kind: InterpolatedStringKind,
) -> Self {
match string_kind {
InterpolatedStringKind::FString => Self::FStringError(error),
InterpolatedStringKind::TString => Self::TStringError(error),
}
}
}
impl std::error::Error for ParseErrorType {}
impl std::fmt::Display for ParseErrorType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ParseErrorType::OtherError(msg) => write!(f, "{msg}"),
ParseErrorType::ExpectedToken { found, expected } => {
write!(f, "Expected {expected}, found {found}",)
}
ParseErrorType::Lexical(lex_error) => write!(f, "{lex_error}"),
ParseErrorType::SimpleStatementsOnSameLine => {
f.write_str("Simple statements must be separated by newlines or semicolons")
}
ParseErrorType::SimpleAndCompoundStatementOnSameLine => f.write_str(
"Compound statements are not allowed on the same line as simple statements",
),
ParseErrorType::UnexpectedTokenAfterAsync(kind) => {
write!(
f,
"Expected `def`, `with` or `for` to follow `async`, found {kind}",
)
}
ParseErrorType::InvalidArgumentUnpackingOrder => {
f.write_str("Iterable argument unpacking cannot follow keyword argument unpacking")
}
ParseErrorType::IterableUnpackingInComprehension => {
f.write_str("Iterable unpacking cannot be used in a comprehension")
}
ParseErrorType::UnparenthesizedNamedExpression => {
f.write_str("Unparenthesized named expression cannot be used here")
}
ParseErrorType::UnparenthesizedTupleExpression => {
f.write_str("Unparenthesized tuple expression cannot be used here")
}
ParseErrorType::UnparenthesizedGeneratorExpression => {
f.write_str("Unparenthesized generator expression cannot be used here")
}
ParseErrorType::InvalidYieldExpressionUsage => {
f.write_str("Yield expression cannot be used here")
}
ParseErrorType::InvalidLambdaExpressionUsage => {
f.write_str("Lambda expression cannot be used here")
}
ParseErrorType::InvalidStarredExpressionUsage => {
f.write_str("Starred expression cannot be used here")
}
ParseErrorType::PositionalAfterKeywordArgument => {
f.write_str("Positional argument cannot follow keyword argument")
}
ParseErrorType::PositionalAfterKeywordUnpacking => {
f.write_str("Positional argument cannot follow keyword argument unpacking")
}
ParseErrorType::EmptySlice => f.write_str("Expected index or slice expression"),
ParseErrorType::EmptyGlobalNames => {
f.write_str("Global statement must have at least one name")
}
ParseErrorType::EmptyNonlocalNames => {
f.write_str("Nonlocal statement must have at least one name")
}
ParseErrorType::EmptyDeleteTargets => {
f.write_str("Delete statement must have at least one target")
}
ParseErrorType::EmptyImportNames => {
f.write_str("Expected one or more symbol names after import")
}
ParseErrorType::EmptyTypeParams => f.write_str("Type parameter list cannot be empty"),
ParseErrorType::ParamAfterVarKeywordParam => {
f.write_str("Parameter cannot follow var-keyword parameter")
}
ParseErrorType::NonDefaultParamAfterDefaultParam => {
f.write_str("Parameter without a default cannot follow a parameter with a default")
}
ParseErrorType::ExpectedKeywordParam => {
f.write_str("Expected one or more keyword parameter after `*` separator")
}
ParseErrorType::VarParameterWithDefault => {
f.write_str("Parameter with `*` or `**` cannot have default value")
}
ParseErrorType::InvalidStarPatternUsage => {
f.write_str("Star pattern cannot be used here")
}
ParseErrorType::ExpectedRealNumber => {
f.write_str("Expected a real number in complex literal pattern")
}
ParseErrorType::ExpectedImaginaryNumber => {
f.write_str("Expected an imaginary number in complex literal pattern")
}
ParseErrorType::ExpectedExpression => f.write_str("Expected an expression"),
ParseErrorType::UnexpectedIndentation => f.write_str("Unexpected indentation"),
ParseErrorType::InvalidAssignmentTarget => f.write_str("Invalid assignment target"),
ParseErrorType::InvalidAnnotatedAssignmentTarget => {
f.write_str("Invalid annotated assignment target")
}
ParseErrorType::InvalidNamedAssignmentTarget => {
f.write_str("Assignment expression target must be an identifier")
}
ParseErrorType::InvalidAugmentedAssignmentTarget => {
f.write_str("Invalid augmented assignment target")
}
ParseErrorType::InvalidDeleteTarget => f.write_str("Invalid delete target"),
ParseErrorType::DuplicateKeywordArgumentError(arg_name) => {
write!(f, "Duplicate keyword argument {arg_name:?}")
}
ParseErrorType::UnexpectedIpythonEscapeCommand => {
f.write_str("IPython escape commands are only allowed in `Mode::Ipython`")
}
ParseErrorType::FStringError(fstring_error) => {
write!(f, "f-string: {fstring_error}")
}
ParseErrorType::TStringError(tstring_error) => {
write!(f, "t-string: {tstring_error}")
}
ParseErrorType::UnexpectedExpressionToken => {
write!(f, "Unexpected token at the end of an expression")
}
}
}
}
/// Represents an error that occur during lexing and are
/// returned by the `parse_*` functions in the iterator in the
/// [lexer] implementation.
///
/// [lexer]: crate::lexer
#[derive(Debug, Clone, PartialEq)]
pub struct LexicalError {
/// The type of error that occurred.
error: LexicalErrorType,
/// The location of the error.
location: TextRange,
}
impl LexicalError {
/// Creates a new `LexicalError` with the given error type and location.
pub fn new(error: LexicalErrorType, location: TextRange) -> Self {
Self { error, location }
}
pub fn error(&self) -> &LexicalErrorType {
&self.error
}
pub fn into_error(self) -> LexicalErrorType {
self.error
}
pub fn location(&self) -> TextRange {
self.location
}
}
impl std::ops::Deref for LexicalError {
type Target = LexicalErrorType;
fn deref(&self) -> &Self::Target {
self.error()
}
}
impl std::error::Error for LexicalError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.error())
}
}
impl std::fmt::Display for LexicalError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{} at byte offset {}",
self.error(),
u32::from(self.location().start())
)
}
}
/// Represents the different types of errors that can occur during lexing.
#[derive(Debug, Clone, PartialEq, Eq, get_size2::GetSize)]
pub enum LexicalErrorType {
// TODO: Can probably be removed, the places it is used seem to be able
// to use the `UnicodeError` variant instead.
#[doc(hidden)]
StringError,
/// A string literal without the closing quote.
UnclosedStringError,
/// Decoding of a unicode escape sequence in a string literal failed.
UnicodeError,
/// Missing the `{` for unicode escape sequence.
MissingUnicodeLbrace,
/// Missing the `}` for unicode escape sequence.
MissingUnicodeRbrace,
/// The indentation is not consistent.
IndentationError,
/// An unrecognized token was encountered.
UnrecognizedToken { tok: char },
/// An f-string error containing the [`InterpolatedStringErrorType`].
FStringError(InterpolatedStringErrorType),
/// A t-string error containing the [`InterpolatedStringErrorType`].
TStringError(InterpolatedStringErrorType),
/// Invalid character encountered in a byte literal.
InvalidByteLiteral,
/// An unexpected character was encountered after a line continuation.
LineContinuationError,
/// An unexpected end of file was encountered.
Eof,
/// An unexpected error occurred.
OtherError(Box<str>),
}
impl std::error::Error for LexicalErrorType {}
impl LexicalErrorType {
pub(crate) fn from_interpolated_string_error(
error: InterpolatedStringErrorType,
string_kind: InterpolatedStringKind,
) -> Self {
match string_kind {
InterpolatedStringKind::FString => Self::FStringError(error),
InterpolatedStringKind::TString => Self::TStringError(error),
}
}
}
impl std::fmt::Display for LexicalErrorType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::StringError => write!(f, "Got unexpected string"),
Self::FStringError(error) => write!(f, "f-string: {error}"),
Self::TStringError(error) => write!(f, "t-string: {error}"),
Self::InvalidByteLiteral => {
write!(f, "bytes can only contain ASCII literal characters")
}
Self::UnicodeError => write!(f, "Got unexpected unicode"),
Self::IndentationError => {
write!(f, "unindent does not match any outer indentation level")
}
Self::UnrecognizedToken { tok } => {
write!(f, "Got unexpected token {tok}")
}
Self::LineContinuationError => {
write!(f, "Expected a newline after line continuation character")
}
Self::Eof => write!(f, "unexpected EOF while parsing"),
Self::OtherError(msg) => write!(f, "{msg}"),
Self::UnclosedStringError => {
write!(f, "missing closing quote in string literal")
}
Self::MissingUnicodeLbrace => {
write!(f, "Missing `{{` in Unicode escape sequence")
}
Self::MissingUnicodeRbrace => {
write!(f, "Missing `}}` in Unicode escape sequence")
}
}
}
}
/// Represents a version-related syntax error detected during parsing.
///
/// An example of a version-related error is the use of a `match` statement before Python 3.10, when
/// it was first introduced. See [`UnsupportedSyntaxErrorKind`] for other kinds of errors.
#[derive(Debug, PartialEq, Clone, get_size2::GetSize)]
pub struct UnsupportedSyntaxError {
pub kind: UnsupportedSyntaxErrorKind,
pub range: TextRange,
/// The target [`PythonVersion`] for which this error was detected.
pub target_version: PythonVersion,
}
impl Ranged for UnsupportedSyntaxError {
fn range(&self) -> TextRange {
self.range
}
}
/// The type of tuple unpacking for [`UnsupportedSyntaxErrorKind::StarTuple`].
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, get_size2::GetSize)]
pub enum StarTupleKind {
Return,
Yield,
}
/// The type of PEP 701 f-string error for [`UnsupportedSyntaxErrorKind::Pep701FString`].
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, get_size2::GetSize)]
pub enum FStringKind {
Backslash,
Comment,
NestedQuote,
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, get_size2::GetSize)]
pub enum UnparenthesizedNamedExprKind {
SequenceIndex,
SetLiteral,
SetComprehension,
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, get_size2::GetSize)]
pub enum UnsupportedSyntaxErrorKind {
Match,
Walrus,
ExceptStar,
/// Represents the use of an unparenthesized named expression (`:=`) in a set literal, set
/// comprehension, or sequence index before Python 3.10.
///
/// ## Examples
///
/// These are allowed on Python 3.10:
///
/// ```python
/// {x := 1, 2, 3} # set literal
/// {last := x for x in range(3)} # set comprehension
/// lst[x := 1] # sequence index
/// ```
///
/// But on Python 3.9 the named expression needs to be parenthesized:
///
/// ```python
/// {(x := 1), 2, 3} # set literal
/// {(last := x) for x in range(3)} # set comprehension
/// lst[(x := 1)] # sequence index
/// ```
///
/// However, unparenthesized named expressions are never allowed in slices:
///
/// ```python
/// lst[x:=1:-1] # syntax error
/// lst[1:x:=1] # syntax error
/// lst[1:3:x:=1] # syntax error
///
/// lst[(x:=1):-1] # ok
/// lst[1:(x:=1)] # ok
/// lst[1:3:(x:=1)] # ok
/// ```
///
/// ## References
///
/// - [Python 3.10 Other Language Changes](https://docs.python.org/3/whatsnew/3.10.html#other-language-changes)
UnparenthesizedNamedExpr(UnparenthesizedNamedExprKind),
/// Represents the use of a parenthesized keyword argument name after Python 3.8.
///
/// ## Example
///
/// From [BPO 34641] it sounds like this was only accidentally supported and was removed when
/// noticed. Code like this used to be valid:
///
/// ```python
/// f((a)=1)
/// ```
///
/// After Python 3.8, you have to omit the parentheses around `a`:
///
/// ```python
/// f(a=1)
/// ```
///
/// [BPO 34641]: https://github.com/python/cpython/issues/78822
ParenthesizedKeywordArgumentName,
/// Represents the use of unparenthesized tuple unpacking in a `return` statement or `yield`
/// expression before Python 3.8.
///
/// ## Examples
///
/// Before Python 3.8, this syntax was allowed:
///
/// ```python
/// rest = (4, 5, 6)
///
/// def f():
/// t = 1, 2, 3, *rest
/// return t
///
/// def g():
/// t = 1, 2, 3, *rest
/// yield t
/// ```
///
/// But this was not:
///
/// ```python
/// rest = (4, 5, 6)
///
/// def f():
/// return 1, 2, 3, *rest
///
/// def g():
/// yield 1, 2, 3, *rest
/// ```
///
/// Instead, parentheses were required in the `return` and `yield` cases:
///
/// ```python
/// rest = (4, 5, 6)
///
/// def f():
/// return (1, 2, 3, *rest)
///
/// def g():
/// yield (1, 2, 3, *rest)
/// ```
///
/// This was reported in [BPO 32117] and updated in Python 3.8 to allow the unparenthesized
/// form.
///
/// [BPO 32117]: https://github.com/python/cpython/issues/76298
StarTuple(StarTupleKind),
/// Represents the use of a "relaxed" [PEP 614] decorator before Python 3.9.
///
/// ## Examples
///
/// Prior to Python 3.9, decorators were defined to be [`dotted_name`]s, optionally followed by
/// an argument list. For example:
///
/// ```python
/// @buttons.clicked.connect
/// def foo(): ...
///
/// @buttons.clicked.connect(1, 2, 3)
/// def foo(): ...
/// ```
///
/// As pointed out in the PEP, this prevented reasonable extensions like subscripts:
///
/// ```python
/// buttons = [QPushButton(f'Button {i}') for i in range(10)]
///
/// @buttons[0].clicked.connect
/// def spam(): ...
/// ```
///
/// Python 3.9 removed these restrictions and expanded the [decorator grammar] to include any
/// assignment expression and include cases like the example above.
///
/// [PEP 614]: https://peps.python.org/pep-0614/
/// [`dotted_name`]: https://docs.python.org/3.8/reference/compound_stmts.html#grammar-token-dotted-name
/// [decorator grammar]: https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-decorator
RelaxedDecorator(RelaxedDecoratorError),
/// Represents the use of a [PEP 570] positional-only parameter before Python 3.8.
///
/// ## Examples
///
/// Python 3.8 added the `/` syntax for marking preceding parameters as positional-only:
///
/// ```python
/// def foo(a, b, /, c): ...
/// ```
///
/// This means `a` and `b` in this case can only be provided by position, not by name. In other
/// words, this code results in a `TypeError` at runtime:
///
/// ```pycon
/// >>> def foo(a, b, /, c): ...
/// ...
/// >>> foo(a=1, b=2, c=3)
/// Traceback (most recent call last):
/// File "<python-input-3>", line 1, in <module>
/// foo(a=1, b=2, c=3)
/// ~~~^^^^^^^^^^^^^^^
/// TypeError: foo() got some positional-only arguments passed as keyword arguments: 'a, b'
/// ```
///
/// [PEP 570]: https://peps.python.org/pep-0570/
PositionalOnlyParameter,
/// Represents the use of a [type parameter list] before Python 3.12.
///
/// ## Examples
///
/// Before Python 3.12, generic parameters had to be declared separately using a class like
/// [`typing.TypeVar`], which could then be used in a function or class definition:
///
/// ```python
/// from typing import Generic, TypeVar
///
/// T = TypeVar("T")
///
/// def f(t: T): ...
/// class C(Generic[T]): ...
/// ```
///
/// [PEP 695], included in Python 3.12, introduced the new type parameter syntax, which allows
/// these to be written more compactly and without a separate type variable:
///
/// ```python
/// def f[T](t: T): ...
/// class C[T]: ...
/// ```
///
/// [type parameter list]: https://docs.python.org/3/reference/compound_stmts.html#type-parameter-lists
/// [PEP 695]: https://peps.python.org/pep-0695/
/// [`typing.TypeVar`]: https://docs.python.org/3/library/typing.html#typevar
TypeParameterList,
LazyImportStatement,
TypeAliasStatement,
TypeParamDefault,
/// Represents the use of a [PEP 701] f-string before Python 3.12.
///
/// ## Examples
///
/// As described in the PEP, each of these cases were invalid before Python 3.12:
///
/// ```python
/// # nested quotes
/// f'Magic wand: { bag['wand'] }'
///
/// # escape characters
/// f"{'\n'.join(a)}"
///
/// # comments
/// f'''A complex trick: {
/// bag['bag'] # recursive bags!
/// }'''
///
/// # arbitrary nesting
/// f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"
/// ```
///
/// These restrictions were lifted in Python 3.12, meaning that all of these examples are now
/// valid.
///
/// [PEP 701]: https://peps.python.org/pep-0701/
Pep701FString(FStringKind),
/// Represents the use of a parenthesized `with` item before Python 3.9.
///
/// ## Examples
///
/// As described in [BPO 12782], `with` uses like this were not allowed on Python 3.8:
///
/// ```python
/// with (open("a_really_long_foo") as foo,
/// open("a_really_long_bar") as bar):
/// pass
/// ```
///
/// because parentheses were not allowed within the `with` statement itself (see [this comment]
/// in particular). However, parenthesized expressions were still allowed, including the cases
/// below, so the issue can be pretty subtle and relates specifically to parenthesized items
/// with `as` bindings.
///
/// ```python
/// with (foo, bar): ... # okay
/// with (
/// open('foo.txt')) as foo: ... # also okay
/// with (
/// foo,
/// bar,
/// baz,
/// ): ... # also okay, just a tuple
/// with (
/// foo,
/// bar,
/// baz,
/// ) as tup: ... # also okay, binding the tuple
/// ```
///
/// This restriction was lifted in 3.9 but formally included in the [release notes] for 3.10.
///
/// [BPO 12782]: https://github.com/python/cpython/issues/56991
/// [this comment]: https://github.com/python/cpython/issues/56991#issuecomment-1093555141
/// [release notes]: https://docs.python.org/3/whatsnew/3.10.html#summary-release-highlights
ParenthesizedContextManager,
/// Represents the use of a [PEP 646] star expression in an index.
///
/// ## Examples
///
/// Before Python 3.11, star expressions were not allowed in index/subscript operations (within
/// square brackets). This restriction was lifted in [PEP 646] to allow for star-unpacking of
/// `typing.TypeVarTuple`s, also added in Python 3.11. As such, this is the primary motivating
/// example from the PEP:
///
/// ```python
/// from typing import TypeVar, TypeVarTuple
///
/// DType = TypeVar('DType')
/// Shape = TypeVarTuple('Shape')
///
/// class Array(Generic[DType, *Shape]): ...
/// ```
///
/// But it applies to simple indexing as well:
///
/// ```python
/// vector[*x]
/// array[a, *b]
/// ```
///
/// [PEP 646]: https://peps.python.org/pep-0646/#change-1-star-expressions-in-indexes
StarExpressionInIndex,
/// Represents the use of a [PEP 646] star annotations in a function definition.
///
/// ## Examples
///
/// Before Python 3.11, star annotations were not allowed in function definitions. This
/// restriction was lifted in [PEP 646] to allow type annotations for `typing.TypeVarTuple`,
/// also added in Python 3.11:
///
/// ```python
/// from typing import TypeVarTuple
///
/// Ts = TypeVarTuple('Ts')
///
/// def foo(*args: *Ts): ...
/// ```
///
/// Unlike [`UnsupportedSyntaxErrorKind::StarExpressionInIndex`], this does not include any
/// other annotation positions:
///
/// ```python
/// x: *Ts # Syntax error
/// def foo(x: *Ts): ... # Syntax error
/// ```
///
/// [PEP 646]: https://peps.python.org/pep-0646/#change-2-args-as-a-typevartuple
StarAnnotation,
/// Represents the use of iterable unpacking inside a list comprehension
/// before Python 3.15.
///
/// ## Examples
///
/// Before Python 3.15, list comprehensions could not use iterable
/// unpacking in their element expression:
///
/// ```python
/// [*x for x in y] # SyntaxError
/// ```
///
/// Starting with Python 3.15, [PEP 798] allows iterable unpacking within
/// list comprehensions:
///
/// ```python
/// [*x for x in y]
/// ```
///
/// [PEP 798]: https://peps.python.org/pep-0798/
IterableUnpackingInListComprehension,
/// Represents the use of tuple unpacking in a `for` statement iterator clause before Python
/// 3.9.
///
/// ## Examples
///
/// Like [`UnsupportedSyntaxErrorKind::StarTuple`] in `return` and `yield` statements, prior to
/// Python 3.9, tuple unpacking in the iterator clause of a `for` statement required
/// parentheses:
///
/// ```python
/// # valid on Python 3.8 and earlier
/// for i in (*a, *b): ...
/// ```
///
/// Omitting the parentheses was invalid:
///
/// ```python
/// for i in *a, *b: ... # SyntaxError
/// ```
///
/// This was changed as part of the [PEG parser rewrite] included in Python 3.9 but not
/// documented directly until the [Python 3.11 release].
///
/// [PEG parser rewrite]: https://peps.python.org/pep-0617/
/// [Python 3.11 release]: https://docs.python.org/3/whatsnew/3.11.html#other-language-changes
UnparenthesizedUnpackInFor,
/// Represents the use of multiple exception names in an except clause without an `as` binding, before Python 3.14.
///
/// ## Examples
/// Before Python 3.14, catching multiple exceptions required
/// parentheses like so:
///
/// ```python
/// try:
/// ...
/// except (ExceptionA, ExceptionB, ExceptionC):
/// ...
/// ```
///
/// Starting with Python 3.14, thanks to [PEP 758], it was permitted
/// to omit the parentheses:
///
/// ```python
/// try:
/// ...
/// except ExceptionA, ExceptionB, ExceptionC:
/// ...
/// ```
///
/// However, parentheses are still required in the presence of an `as`:
///
/// ```python
/// try:
/// ...
/// except (ExceptionA, ExceptionB, ExceptionC) as e:
/// ...
/// ```
///
///
/// [PEP 758]: https://peps.python.org/pep-0758/
UnparenthesizedExceptionTypes,
/// Represents the use of a template string (t-string)
/// literal prior to the implementation of [PEP 750]
/// in Python 3.14.
///
/// [PEP 750]: https://peps.python.org/pep-0750/
TemplateStrings,
}
impl Display for UnsupportedSyntaxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = match self.kind {
UnsupportedSyntaxErrorKind::Match => "Cannot use `match` statement",
UnsupportedSyntaxErrorKind::Walrus => "Cannot use named assignment expression (`:=`)",
UnsupportedSyntaxErrorKind::ExceptStar => "Cannot use `except*`",
UnsupportedSyntaxErrorKind::UnparenthesizedNamedExpr(
UnparenthesizedNamedExprKind::SequenceIndex,
) => "Cannot use unparenthesized assignment expression in a sequence index",
UnsupportedSyntaxErrorKind::UnparenthesizedNamedExpr(
UnparenthesizedNamedExprKind::SetLiteral,
) => "Cannot use unparenthesized assignment expression as an element in a set literal",
UnsupportedSyntaxErrorKind::UnparenthesizedNamedExpr(
UnparenthesizedNamedExprKind::SetComprehension,
) => {
"Cannot use unparenthesized assignment expression as an element in a set comprehension"
}
UnsupportedSyntaxErrorKind::ParenthesizedKeywordArgumentName => {
"Cannot use parenthesized keyword argument name"
}
UnsupportedSyntaxErrorKind::StarTuple(StarTupleKind::Return) => {
"Cannot use iterable unpacking in return statements"
}
UnsupportedSyntaxErrorKind::StarTuple(StarTupleKind::Yield) => {
"Cannot use iterable unpacking in yield expressions"
}
UnsupportedSyntaxErrorKind::RelaxedDecorator(relaxed_decorator_error) => {
return match relaxed_decorator_error {
RelaxedDecoratorError::CallExpression => {
write!(
f,
"Cannot use a call expression in a decorator on Python {} \
unless it is the top-level expression or it occurs \
in the argument list of a top-level call expression \
(relaxed decorator syntax was {changed})",
self.target_version,
changed = self.kind.changed_version(),
)
}
RelaxedDecoratorError::Other(description) => write!(
f,
"Cannot use {description} outside function call arguments in a decorator on Python {} \
(syntax was {changed})",
self.target_version,
changed = self.kind.changed_version(),
),
};
}
UnsupportedSyntaxErrorKind::PositionalOnlyParameter => {
"Cannot use positional-only parameter separator"
}
UnsupportedSyntaxErrorKind::TypeParameterList => "Cannot use type parameter lists",
UnsupportedSyntaxErrorKind::LazyImportStatement => "Cannot use `lazy` import statement",
UnsupportedSyntaxErrorKind::TypeAliasStatement => "Cannot use `type` alias statement",
UnsupportedSyntaxErrorKind::TypeParamDefault => {
"Cannot set default type for a type parameter"
}
UnsupportedSyntaxErrorKind::Pep701FString(FStringKind::Backslash) => {
"Cannot use an escape sequence (backslash) in f-strings"
}
UnsupportedSyntaxErrorKind::Pep701FString(FStringKind::Comment) => {
"Cannot use comments in f-strings"
}
UnsupportedSyntaxErrorKind::Pep701FString(FStringKind::NestedQuote) => {
"Cannot reuse outer quote character in f-strings"
}
UnsupportedSyntaxErrorKind::ParenthesizedContextManager => {
"Cannot use parentheses within a `with` statement"
}
UnsupportedSyntaxErrorKind::StarExpressionInIndex => {
"Cannot use star expression in index"
}
UnsupportedSyntaxErrorKind::StarAnnotation => "Cannot use star annotation",
UnsupportedSyntaxErrorKind::IterableUnpackingInListComprehension => {
"Cannot use iterable unpacking in a list comprehension"
}
UnsupportedSyntaxErrorKind::UnparenthesizedUnpackInFor => {
"Cannot use iterable unpacking in `for` statements"
}
UnsupportedSyntaxErrorKind::UnparenthesizedExceptionTypes => {
"Multiple exception types must be parenthesized"
}
UnsupportedSyntaxErrorKind::TemplateStrings => "Cannot use t-strings",
};
write!(
f,
"{kind} on Python {} (syntax was {changed})",
self.target_version,
changed = self.kind.changed_version(),
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, get_size2::GetSize)]
pub enum RelaxedDecoratorError {
CallExpression,
Other(&'static str),
}
/// Represents the kind of change in Python syntax between versions.
enum Change {
Added(PythonVersion),
Removed(PythonVersion),
}
impl Display for Change {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Change::Added(version) => write!(f, "added in Python {version}"),
Change::Removed(version) => write!(f, "removed in Python {version}"),
}
}
}
impl UnsupportedSyntaxErrorKind {
/// Returns the Python version when the syntax associated with this error was changed, and the
/// type of [`Change`] (added or removed).
const fn changed_version(self) -> Change {
match self {
UnsupportedSyntaxErrorKind::Match => Change::Added(PythonVersion::PY310),
UnsupportedSyntaxErrorKind::Walrus => Change::Added(PythonVersion::PY38),
UnsupportedSyntaxErrorKind::ExceptStar => Change::Added(PythonVersion::PY311),
UnsupportedSyntaxErrorKind::UnparenthesizedNamedExpr(_) => {
Change::Added(PythonVersion::PY39)
}
UnsupportedSyntaxErrorKind::StarTuple(_) => Change::Added(PythonVersion::PY38),
UnsupportedSyntaxErrorKind::RelaxedDecorator { .. } => {
Change::Added(PythonVersion::PY39)
}
UnsupportedSyntaxErrorKind::PositionalOnlyParameter => {
Change::Added(PythonVersion::PY38)
}
UnsupportedSyntaxErrorKind::ParenthesizedKeywordArgumentName => {
Change::Removed(PythonVersion::PY38)
}
UnsupportedSyntaxErrorKind::TypeParameterList => Change::Added(PythonVersion::PY312),
UnsupportedSyntaxErrorKind::LazyImportStatement => Change::Added(PythonVersion::PY315),
UnsupportedSyntaxErrorKind::TypeAliasStatement => Change::Added(PythonVersion::PY312),
UnsupportedSyntaxErrorKind::TypeParamDefault => Change::Added(PythonVersion::PY313),
UnsupportedSyntaxErrorKind::Pep701FString(_) => Change::Added(PythonVersion::PY312),
UnsupportedSyntaxErrorKind::ParenthesizedContextManager => {
Change::Added(PythonVersion::PY39)
}
UnsupportedSyntaxErrorKind::StarExpressionInIndex => {
Change::Added(PythonVersion::PY311)
}
UnsupportedSyntaxErrorKind::StarAnnotation => Change::Added(PythonVersion::PY311),
UnsupportedSyntaxErrorKind::IterableUnpackingInListComprehension => {
Change::Added(PythonVersion::PY315)
}
UnsupportedSyntaxErrorKind::UnparenthesizedUnpackInFor => {
Change::Added(PythonVersion::PY39)
}
UnsupportedSyntaxErrorKind::UnparenthesizedExceptionTypes => {
Change::Added(PythonVersion::PY314)
}
UnsupportedSyntaxErrorKind::TemplateStrings => Change::Added(PythonVersion::PY314),
}
}
/// Returns whether or not this kind of syntax is unsupported on `target_version`.
pub(crate) fn is_unsupported(self, target_version: PythonVersion) -> bool {
match self.changed_version() {
Change::Added(version) => target_version < version,
Change::Removed(version) => target_version >= version,
}
}
/// Returns `true` if this kind of syntax is supported on `target_version`.
pub(crate) fn is_supported(self, target_version: PythonVersion) -> bool {
!self.is_unsupported(target_version)
}
}
#[cfg(target_pointer_width = "64")]
mod sizes {
use crate::error::{LexicalError, LexicalErrorType};
use static_assertions::assert_eq_size;
assert_eq_size!(LexicalErrorType, [u8; 24]);
assert_eq_size!(LexicalError, [u8; 32]);
}