-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathast.h
More file actions
620 lines (537 loc) · 15.4 KB
/
Copy pathast.h
File metadata and controls
620 lines (537 loc) · 15.4 KB
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
/***************************************************************************
* This file is part of KDevelop *
* Copyright 2007 Andreas Pakulat <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
// The Python 2.6 Language Reference was used as basis for this AST
#ifndef PYTHON_AST_H
#define PYTHON_AST_H
#include <QList>
#include <QMap>
#include <QString>
#include <QStringList>
#include <QPair>
#include "parserexport.h"
namespace KDevelop
{
class DUContext;
}
namespace Python {
class StatementAst;
class FunctionDefinitionAst;
class AssignmentAst;
class PrintAst;
class PassAst;
class ExpressionAst;
class NameAst;
class CallAst;
class AttributeAst;
class ArgumentsAst;
class KeywordAst;
class ExpressionAst;
class StatementAst;
class Ast;
class ExceptionHandlerAst;
class AliasAst;
class ComprehensionAst;
class SliceAstBase;
class SliceAst;
}
namespace Python
{
// Base class for all other Abstract Syntax Tree classes
class KDEVPYTHONPARSER_EXPORT Ast
{
public:
enum AstType
{
FunctionDefinitionAstType,
AssignmentAstType,
PrintAstType,
PassAstType,
NameAstType,
CallAstType,
AttributeAstType,
ArgumentsAstType,
KeywordAstType,
ClassDefinitionAstType,
ReturnAstType,
DeleteAstType,
ForAstType,
WhileAstType,
IfAstType,
WithAstType,
RaiseAstType,
TryExceptAstType,
TryFinallyAstType,
ImportAstType,
ImportFromAstType,
ExecAstType,
GlobalAstType,
BreakAstType,
ContinueAstType,
AssertionAstType,
AugmentedAssignmentAstType,
DictionaryComprehensionAstType,
ExtendedSliceAstType,
CodeAstType,
StatementAstType,
ExpressionAstType,
BooleanOperationAstType,
BinaryOperationAstType,
UnaryOperationAstType,
LambdaAstType,
IfExpressionAstType, // the short one, if a then b else c
DictAstType,
SetAstType,
ListComprehensionAstType,
SetComprehensionAstType,
GeneratorExpressionAstType,
YieldAstType,
CompareAstType,
ReprAstType,
NumberAstType,
StringAstType,
SubscriptAstType,
ListAstType,
TupleAstType,
SliceAstType,
EllipsisAstType,
IndexAstType,
ComprehensionAstType,
ExceptionHandlerAstType,
AliasAstType // for imports
};
enum BooleanOperationTypes {
BooleanAnd,
BooleanOr,
BooleanInvalidOperation
};
enum OperatorTypes {
OperatorAdd,
OperatorSub,
OperatorMult,
OperatorDiv,
OperatorMod,
OperatorPow,
OperatorLeftShift,
OperatorRightShift,
OperatorBitwiseOr,
OperatorBitwiseXor,
OperatorBitwiseAnd,
OperatorFloorDivision,
OperatorInvalid
};
enum UnaryOperatorTypes {
UnaryOperatorInvert,
UnaryOperatorNot,
UnaryOperatorAdd,
UnaryOperatorSub,
UnaryOperatorInvalid
};
enum ComparisonOperatorTypes {
ComparisonOperatorEquals,
ComparisonOperatorNotEquals,
ComparisonOperatorLessThan,
ComparisonOperatorLessThanEqual,
ComparisonOperatorGreaterThan,
ComparisonOperatorGreaterThanEqual,
ComparisonOperatorIs,
ComparisonOperatorIsNot,
ComparisonOperatorIn,
ComparisonOperatorNotIn,
ComparisonOperatorInvalid
};
Ast(Ast* parent, AstType type);
Ast();
virtual ~Ast();
Ast* parent;
AstType astType;
qint64 startCol;
qint64 startLine;
qint64 endCol;
qint64 endLine;
bool hasUsefulRangeInformation;
KDevelop::DUContext* context;
};
class KDEVPYTHONPARSER_EXPORT Identifier : public Ast {
public:
Identifier(QString value);
QString value;
};
// this replaces ModuleAst
class KDEVPYTHONPARSER_EXPORT CodeAst : public Ast {
public:
CodeAst();
QList<StatementAst*> body;
};
/** Statement classes **/
class KDEVPYTHONPARSER_EXPORT StatementAst : public Ast {
public:
StatementAst(Ast* parent, AstType type);
};
class KDEVPYTHONPARSER_EXPORT FunctionDefinitionAst : public StatementAst {
public:
FunctionDefinitionAst(Ast* parent);
Identifier* name;
ArgumentsAst* arguments;
QList<NameAst*> decorators;
QList<StatementAst*> body;
};
class KDEVPYTHONPARSER_EXPORT ClassDefinitionAst : public StatementAst {
public:
ClassDefinitionAst(Ast* parent);
Identifier* name;
QList<ExpressionAst*> baseClasses;
QList<StatementAst*> body;
QList<ExpressionAst*> decorators;
};
class KDEVPYTHONPARSER_EXPORT ReturnAst : public StatementAst {
public:
ReturnAst(Ast* parent);
ExpressionAst* value;
};
class KDEVPYTHONPARSER_EXPORT DeleteAst : public StatementAst {
public:
DeleteAst(Ast* parent);
QList<ExpressionAst*> targets;
};
class KDEVPYTHONPARSER_EXPORT AssignmentAst : public StatementAst {
public:
AssignmentAst(Ast* parent);
QList<ExpressionAst*> targets;
ExpressionAst* value;
};
class KDEVPYTHONPARSER_EXPORT AugmentedAssignmentAst : public StatementAst {
public:
AugmentedAssignmentAst(Ast* parent);
ExpressionAst* target;
Ast::OperatorTypes op;
ExpressionAst* value;
};
class KDEVPYTHONPARSER_EXPORT ForAst : public StatementAst {
public:
ForAst(Ast* parent);
ExpressionAst* target;
ExpressionAst* iterator;
QList<StatementAst*> body;
QList<StatementAst*> orelse;
};
class KDEVPYTHONPARSER_EXPORT WhileAst : public StatementAst {
public:
WhileAst(Ast* parent);
ExpressionAst* condition;
QList<StatementAst*> body;
QList<StatementAst*> orelse;
};
class KDEVPYTHONPARSER_EXPORT IfAst : public StatementAst {
public:
IfAst(Ast* parent);
ExpressionAst* condition;
QList<StatementAst*> body;
QList<StatementAst*> orelse;
};
class KDEVPYTHONPARSER_EXPORT WithAst : public StatementAst {
public:
WithAst(Ast* parent);
ExpressionAst* contextExpression;
ExpressionAst* optionalVars;
QList<StatementAst*> body;
};
class KDEVPYTHONPARSER_EXPORT RaiseAst : public StatementAst {
public:
RaiseAst(Ast* parent);
ExpressionAst* type;
// TODO check what the other things in the grammar actually are and add them
};
class KDEVPYTHONPARSER_EXPORT TryExceptAst : public StatementAst {
public:
TryExceptAst(Ast* parent);
QList<StatementAst*> body;
QList<ExceptionHandlerAst*> handlers;
QList<StatementAst*> orelse;
};
class KDEVPYTHONPARSER_EXPORT TryFinallyAst : public StatementAst {
public:
TryFinallyAst(Ast* parent);
QList<StatementAst*> body;
QList<StatementAst*> finalbody;
};
class KDEVPYTHONPARSER_EXPORT AssertionAst : public StatementAst {
public:
AssertionAst(Ast* parent);
ExpressionAst* condition;
ExpressionAst* message;
};
class KDEVPYTHONPARSER_EXPORT ImportAst : public StatementAst {
public:
ImportAst(Ast* parent);
QList<AliasAst*> names;
};
class KDEVPYTHONPARSER_EXPORT ImportFromAst : public StatementAst {
public:
ImportFromAst(Ast* parent);
Identifier* module;
QList<AliasAst*> names;
int level;
};
class KDEVPYTHONPARSER_EXPORT ExecAst : public StatementAst {
public:
ExecAst(Ast* parent);
ExpressionAst* body;
ExpressionAst* globals;
ExpressionAst* locals;
};
class KDEVPYTHONPARSER_EXPORT GlobalAst : public StatementAst {
public:
GlobalAst(Ast* parent);
QList<Identifier*> names;
};
// TODO what's stmt::Expr(expr value) in the grammar and what do we need it for?
class KDEVPYTHONPARSER_EXPORT BreakAst : public StatementAst {
public:
BreakAst(Ast* parent);
};
class KDEVPYTHONPARSER_EXPORT ContinueAst : public StatementAst {
public:
ContinueAst(Ast* parent);
};
class KDEVPYTHONPARSER_EXPORT PrintAst : public StatementAst {
public:
PrintAst(Ast* parent);
ExpressionAst* destination;
QList<ExpressionAst*> values;
bool newline;
};
class KDEVPYTHONPARSER_EXPORT PassAst : public StatementAst {
public:
PassAst(Ast* parent);
};
/** Expression classes **/
class KDEVPYTHONPARSER_EXPORT ExpressionAst : public Ast {
public:
ExpressionAst(Ast* parent, AstType type = Ast::ExpressionAstType);
enum Context {
Load, // the object is read
Store, // the object is written
Delete, // the object is deleted
Parameter, // the object is passed as a parameter
AugLoad, AugStore, // Augmented assignments, like a += 1
Invalid
};
ExpressionAst* value;
};
class KDEVPYTHONPARSER_EXPORT BooleanOperationAst : public ExpressionAst {
public:
BooleanOperationAst(Ast* parent);
Ast::BooleanOperationTypes type;
QList<ExpressionAst*> values;
};
class KDEVPYTHONPARSER_EXPORT BinaryOperationAst : public ExpressionAst {
public:
BinaryOperationAst(Ast* parent);
Ast::OperatorTypes type;
ExpressionAst* lhs;
ExpressionAst* rhs;
};
class KDEVPYTHONPARSER_EXPORT UnaryOperationAst : public ExpressionAst {
public:
UnaryOperationAst(Ast* parent);
Ast::UnaryOperatorTypes type;
ExpressionAst* operand;
};
class KDEVPYTHONPARSER_EXPORT LambdaAst : public ExpressionAst {
public:
LambdaAst(Ast* parent);
ArgumentsAst* arguments;
ExpressionAst* body;
};
class KDEVPYTHONPARSER_EXPORT IfExpressionAst : public ExpressionAst {
public:
IfExpressionAst(Ast* parent);
ExpressionAst* condition;
ExpressionAst* body;
ExpressionAst* orelse;
};
class KDEVPYTHONPARSER_EXPORT DictAst : public ExpressionAst {
public:
DictAst(Ast* parent);
QList<ExpressionAst*> keys;
QList<ExpressionAst*> values;
};
class KDEVPYTHONPARSER_EXPORT SetAst : public ExpressionAst {
public:
SetAst(Ast* parent);
QList<ExpressionAst*> elements;
};
class KDEVPYTHONPARSER_EXPORT ListComprehensionAst : public ExpressionAst {
public:
ListComprehensionAst(Ast* parent);
ExpressionAst* element;
QList<ComprehensionAst*> generators;
};
class KDEVPYTHONPARSER_EXPORT SetComprehensionAst : public ExpressionAst {
public:
SetComprehensionAst(Ast* parent);
ExpressionAst* element;
QList<ComprehensionAst*> generators;
};
class KDEVPYTHONPARSER_EXPORT DictionaryComprehensionAst : public ExpressionAst {
public:
DictionaryComprehensionAst(Ast* parent);
ExpressionAst* key;
ExpressionAst* value;
QList<ComprehensionAst*> generators;
};
class KDEVPYTHONPARSER_EXPORT GeneratorExpressionAst : public ExpressionAst {
public:
GeneratorExpressionAst(Ast* parent);
ExpressionAst* element;
QList<ComprehensionAst*> generators;
};
class KDEVPYTHONPARSER_EXPORT CompareAst : public ExpressionAst {
public:
CompareAst(Ast* parent);
ExpressionAst* leftmostElement;
QList<ComparisonOperatorTypes> operators;
QList<ExpressionAst*> comparands;
};
// TODO whats this exactly?
class KDEVPYTHONPARSER_EXPORT ReprAst : public ExpressionAst {
public:
ReprAst(Ast* parent);
ExpressionAst* value;
};
class KDEVPYTHONPARSER_EXPORT NumberAst : public ExpressionAst {
public:
NumberAst(Ast* parent);
QString value; // everything else would be even more strange
};
class KDEVPYTHONPARSER_EXPORT StringAst : public ExpressionAst {
public:
StringAst(Ast* parent);
QString value;
};
class KDEVPYTHONPARSER_EXPORT YieldAst : public ExpressionAst {
public:
YieldAst(Ast* parent);
ExpressionAst* value;
};
class KDEVPYTHONPARSER_EXPORT NameAst : public ExpressionAst {
public:
NameAst(Ast* parent);
Identifier* identifier;
ExpressionAst::Context context;
};
class KDEVPYTHONPARSER_EXPORT CallAst : public ExpressionAst {
public:
CallAst(Ast* parent);
ExpressionAst* function;
QList<ExpressionAst*> arguments;
QList<KeywordAst*> keywords;
ExpressionAst* keywordArguments;
ExpressionAst* starArguments;
};
class KDEVPYTHONPARSER_EXPORT AttributeAst : public ExpressionAst {
public:
AttributeAst(Ast* parent);
ExpressionAst* value;
Identifier* attribute;
ExpressionAst::Context context;
};
class KDEVPYTHONPARSER_EXPORT SubscriptAst : public ExpressionAst {
public:
SubscriptAst(Ast* parent);
ExpressionAst* value;
SliceAst* slice;
ExpressionAst::Context context;
};
class KDEVPYTHONPARSER_EXPORT ListAst : public ExpressionAst {
public:
ListAst(Ast* parent);
QList<ExpressionAst*> elements;
ExpressionAst::Context context;
};
class KDEVPYTHONPARSER_EXPORT TupleAst : public ExpressionAst {
public:
TupleAst(Ast* parent);
QList<ExpressionAst*> elements;
ExpressionAst::Context context;
};
/** Slice classes **/
class KDEVPYTHONPARSER_EXPORT SliceAstBase : public Ast {
public:
SliceAstBase(Ast* parent, AstType type);
};
class KDEVPYTHONPARSER_EXPORT EllipsisAst : public SliceAstBase {
public:
EllipsisAst(Ast* parent);
};
class KDEVPYTHONPARSER_EXPORT SliceAst : public SliceAstBase {
public:
SliceAst(Ast* parent);
ExpressionAst* lower;
ExpressionAst* upper;
ExpressionAst* step;
};
class KDEVPYTHONPARSER_EXPORT ExtendedSliceAst : public SliceAstBase {
public:
ExtendedSliceAst(Ast* parent);
QList<SliceAst*> dims;
};
class KDEVPYTHONPARSER_EXPORT IndexAst : public SliceAstBase {
public:
IndexAst(Ast* parent);
ExpressionAst* value;
};
/** Independent classes **/
class KDEVPYTHONPARSER_EXPORT ArgumentsAst : public Ast {
public:
ArgumentsAst(Ast* parent);
QList<ExpressionAst*> arguments;
QList<ExpressionAst*> defaultValues;
Identifier* vararg;
Identifier* kwarg;
};
class KDEVPYTHONPARSER_EXPORT KeywordAst : public Ast {
public:
KeywordAst(Ast* parent);
Identifier* argumentName;
ExpressionAst* value;
};
class KDEVPYTHONPARSER_EXPORT ComprehensionAst : public Ast {
public:
ComprehensionAst(Ast* parent);
ExpressionAst* target;
ExpressionAst* iterator;
QList<ExpressionAst*> conditions;
};
class KDEVPYTHONPARSER_EXPORT ExceptionHandlerAst : public Ast {
public:
ExceptionHandlerAst(Ast* parent);
ExpressionAst* type;
ExpressionAst* name;
QList<StatementAst*> body;
};
class KDEVPYTHONPARSER_EXPORT AliasAst : public Ast {
public:
AliasAst(Ast* parent);
Identifier* name;
NameAst* asName;
};
}
#endif