1+ //! enums and structures that store the syntax tree outputed by the parser.
2+
13use std:: fmt;
24
35#[ cfg( feature="bigint" ) ]
@@ -21,46 +23,9 @@ pub type PyStringContent = String;
2123#[ cfg( not( feature="wtf8" ) ) ]
2224pub type PyStringCodePoint = char ;
2325
24- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
25- pub enum ArgumentError {
26- KeywordExpression ,
27- PositionalAfterKeyword ,
28- StarargsAfterKeyword ,
29- }
30-
31- impl ArgumentError {
32- pub fn to_string ( self ) -> & ' static str {
33- match self {
34- ArgumentError :: KeywordExpression => "Keyword cannot be an expression." ,
35- ArgumentError :: PositionalAfterKeyword => "Positional argument after keyword argument or **kwargs." ,
36- ArgumentError :: StarargsAfterKeyword => "*args after keyword argument or **kwargs." ,
37- }
38- }
39- }
40-
41- impl From < u32 > for ArgumentError {
42- fn from ( i : u32 ) -> ArgumentError {
43- match i {
44- 1 => ArgumentError :: KeywordExpression ,
45- 2 => ArgumentError :: PositionalAfterKeyword ,
46- 3 => ArgumentError :: StarargsAfterKeyword ,
47- _ => panic ! ( "Invalid error code." )
48- }
49- }
50- }
51-
52- impl From < ArgumentError > for u32 {
53- fn from ( e : ArgumentError ) -> u32 {
54- match e {
55- ArgumentError :: KeywordExpression => 1 ,
56- ArgumentError :: PositionalAfterKeyword => 2 ,
57- ArgumentError :: StarargsAfterKeyword => 3 ,
58- }
59- }
60- }
61-
6226pub type Name = String ;
6327
28+ /// Represents whether a function signature has `*`, `*args`, or none of these.
6429#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
6530pub enum StarParams < T > {
6631 /// No single star
@@ -77,6 +42,7 @@ impl<T> Default for StarParams<T> {
7742 }
7843}
7944
45+ /// The list of parameters of a function definition.
8046#[ derive( Clone , Debug , PartialEq , Default , ) ]
8147pub struct TypedArgsList {
8248 pub positional_args : Vec < ( Name , Option < Expression > , Option < Expression > ) > ,
@@ -85,6 +51,7 @@ pub struct TypedArgsList {
8551 pub star_kwargs : Option < ( Name , Option < Expression > ) > ,
8652}
8753
54+ /// The list of parameters of a lambda definition.
8855#[ derive( Clone , Debug , PartialEq , Default ) ]
8956pub struct UntypedArgsList {
9057 pub positional_args : Vec < ( Name , Option < Expression > ) > ,
@@ -93,12 +60,14 @@ pub struct UntypedArgsList {
9360 pub star_kwargs : Option < Name > ,
9461}
9562
63+ /// A function or class decorator.
9664#[ derive( Clone , Debug , PartialEq ) ]
9765pub struct Decorator {
9866 pub name : Vec < Name > ,
9967 pub args : Option < Vec < Argument > > ,
10068}
10169
70+ /// An argument to a function call
10271#[ derive( Clone , Debug , PartialEq ) ]
10372pub enum Argument {
10473 Positional ( Expression ) ,
@@ -107,13 +76,18 @@ pub enum Argument {
10776 Kwargs ( Expression ) ,
10877}
10978
79+ /// The `foo[bar]` syntax.
11080#[ derive( Clone , Debug , PartialEq ) ]
11181pub enum Subscript {
82+ /// `foo[i]`
11283 Simple ( Expression ) ,
84+ /// `foo[start:end]`, `foo[start:]`, etc.
11385 Double ( Option < Expression > , Option < Expression > ) ,
86+ /// `foo[start:end:step]`, `foo[start::]`, etc.
11487 Triple ( Option < Expression > , Option < Expression > , Option < Expression > ) ,
11588}
11689
90+ /// Unary operators.
11791#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
11892pub enum Uop {
11993 Plus ,
@@ -134,6 +108,7 @@ impl fmt::Display for Uop {
134108 }
135109}
136110
111+ /// Binary operators.
137112#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
138113pub enum Bop {
139114 Add ,
@@ -199,30 +174,37 @@ impl fmt::Display for Bop {
199174 }
200175}
201176
177+ /// One of the `if` or `for` clause(s) of a comprehension list/dict/set or
178+ /// generator expression.
202179#[ derive( Clone , Debug , PartialEq ) ]
203180pub enum ComprehensionChunk {
204181 If { cond : Expression } ,
205182 For { async : bool , item : Vec < Expression > , iterator : Expression } ,
206183}
207184
185+ /// `**foo` or `foo:bar`, as in a dict comprehension.
208186#[ derive( Clone , Debug , PartialEq ) ]
209187pub enum DictItem {
210188 Star ( Expression ) ,
211189 Unique ( Expression , Expression ) ,
212190}
213191
192+ /// `*foo` or `foo`, as in a list/set comprehension or a generator expression.
214193#[ derive( Clone , Debug , PartialEq ) ]
215194pub enum SetItem {
216195 Star ( Expression ) ,
217196 Unique ( Expression ) ,
218197}
219198
199+ /// A Python string. See the doc of the crate for the boring speech about
200+ /// encoding stuff.
220201#[ derive( Clone , Debug , PartialEq , Eq ) ]
221202pub struct PyString {
222203 pub prefix : String ,
223204 pub content : PyStringContent ,
224205}
225206
207+ /// The big thing: a Python expression.
226208#[ derive( Clone , Debug , PartialEq ) ]
227209pub enum Expression {
228210 Ellipsis ,
@@ -265,6 +247,7 @@ pub enum Expression {
265247 Lambdef ( UntypedArgsList , Box < Expression > ) ,
266248}
267249
250+ /// An import statement.
268251#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
269252pub enum Import {
270253 /// `from x import y`
@@ -287,6 +270,7 @@ pub enum Import {
287270 Import { names : Vec < ( Vec < Name > , Option < Name > ) > } ,
288271}
289272
273+ /// `+=` and its friends.
290274#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
291275pub enum AugAssignOp {
292276 Add ,
@@ -324,6 +308,7 @@ impl fmt::Display for AugAssignOp {
324308 }
325309}
326310
311+ /// A Python statement.
327312#[ derive( Clone , Debug , PartialEq ) ]
328313pub enum Statement {
329314 Pass ,
@@ -349,6 +334,7 @@ pub enum Statement {
349334 Compound ( Box < CompoundStatement > ) ,
350335}
351336
337+ /// A function definition, including its decorators.
352338#[ derive( Clone , Debug , PartialEq ) ]
353339pub struct Funcdef {
354340 pub async : bool ,
@@ -359,6 +345,7 @@ pub struct Funcdef {
359345 pub code : Vec < Statement > ,
360346}
361347
348+ /// A class definition, including its decorators.
362349#[ derive( Clone , Debug , PartialEq ) ]
363350pub struct Classdef {
364351 pub decorators : Vec < Decorator > ,
@@ -367,6 +354,7 @@ pub struct Classdef {
367354 pub code : Vec < Statement > ,
368355}
369356
357+ /// A try block.
370358#[ derive( Clone , Debug , PartialEq ) ]
371359pub struct Try {
372360 pub try_block : Vec < Statement > ,
@@ -380,6 +368,7 @@ pub struct Try {
380368 pub finally_block : Vec < Statement > ,
381369}
382370
371+ /// Statements with blocks.
383372#[ derive( Clone , Debug , PartialEq ) ]
384373pub enum CompoundStatement {
385374 If ( Vec < ( Expression , Vec < Statement > ) > , Option < Vec < Statement > > ) ,
0 commit comments