File indexing completed on 2024-05-19 15:42:33
0001 /* 0002 SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de> 0003 SPDX-FileCopyrightText: 2012 Patrick Spendrin <ps_ml@gmx.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 // The Python 3.4 Language Reference was used as basis for this AST 0009 0010 #ifndef PYTHON_AST_H 0011 #define PYTHON_AST_H 0012 0013 #include <QList> 0014 #include <QString> 0015 #include <QSharedPointer> 0016 #include <KTextEditor/Range> 0017 #include "parserexport.h" 0018 #include "kdevpythonversion.h" 0019 0020 namespace KDevelop 0021 { 0022 class DUContext; 0023 } 0024 0025 namespace Python { 0026 class StatementAst; 0027 class FunctionDefinitionAst; 0028 class AssignmentAst; 0029 class PassAst; 0030 class NonlocalAst; 0031 class ExpressionAst; 0032 class NameAst; 0033 class CallAst; 0034 class AttributeAst; 0035 class ArgumentsAst; 0036 class KeywordAst; 0037 0038 class ExpressionAst; 0039 class StatementAst; 0040 class Ast; 0041 class ExceptionHandlerAst; 0042 class AliasAst; 0043 class ComprehensionAst; 0044 class SliceAst; 0045 } 0046 0047 namespace Python 0048 { 0049 0050 0051 // Base class for all other Abstract Syntax Tree classes 0052 class KDEVPYTHONPARSER_EXPORT Ast 0053 { 0054 public: 0055 enum AstType 0056 { 0057 StatementAstType, 0058 FunctionDefinitionAstType, 0059 AssignmentAstType, 0060 PassAstType, 0061 NonlocalAstType, 0062 ArgumentsAstType, 0063 ArgAstType, 0064 KeywordAstType, 0065 ClassDefinitionAstType, 0066 ReturnAstType, 0067 DeleteAstType, 0068 ForAstType, 0069 WhileAstType, 0070 IfAstType, 0071 WithAstType, 0072 WithItemAstType, 0073 RaiseAstType, 0074 TryAstType, 0075 TryStarAstType, 0076 ImportAstType, 0077 ImportFromAstType, 0078 GlobalAstType, 0079 BreakAstType, 0080 ContinueAstType, 0081 AssertionAstType, 0082 AugmentedAssignmentAstType, 0083 AnnotationAssignmentAstType, 0084 MatchAstType, 0085 LastStatementType, 0086 0087 ExpressionAstType, // everything below is an expression 0088 AwaitAstType, 0089 ConstantAstType, 0090 NameAstType, 0091 NameConstantAstType, 0092 CallAstType, 0093 AttributeAstType, 0094 DictionaryComprehensionAstType, 0095 BooleanOperationAstType, 0096 BinaryOperationAstType, 0097 UnaryOperationAstType, 0098 LambdaAstType, 0099 IfExpressionAstType, // the short one, if a then b else c 0100 DictAstType, 0101 SetAstType, 0102 ListComprehensionAstType, 0103 SetComprehensionAstType, 0104 GeneratorExpressionAstType, 0105 YieldAstType, 0106 CompareAstType, 0107 NumberAstType, 0108 StringAstType, 0109 JoinedStringAstType, 0110 FormattedValueAstType, 0111 BytesAstType, 0112 SubscriptAstType, 0113 StarredAstType, 0114 ListAstType, 0115 TupleAstType, 0116 YieldFromAstType, 0117 ComprehensionAstType, 0118 SliceAstType, 0119 EllipsisAstType, 0120 AssignmentExpressionAstType, 0121 LastExpressionType, // keep this at the end of the expr ast list 0122 0123 // everything below is a pattern 0124 MatchCaseAstType, 0125 PatternAstType, 0126 MatchValueAstType, 0127 MatchSingletonAstType, 0128 MatchSequenceAstType, 0129 MatchMappingAstType, 0130 MatchClassAstType, 0131 MatchStarAstType, 0132 MatchAsAstType, 0133 MatchOrAstType, 0134 LastPatternType, 0135 0136 CodeAstType, 0137 ExceptionHandlerAstType, 0138 AliasAstType, // for imports 0139 IdentifierAstType, 0140 LastAstType // the largest one, not valid! 0141 }; 0142 0143 enum BooleanOperationTypes { 0144 BooleanAnd = 1, 0145 BooleanOr, 0146 BooleanInvalidOperation 0147 }; 0148 0149 enum OperatorTypes { 0150 OperatorAdd = 1, 0151 OperatorSub, 0152 OperatorMult, 0153 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 5, 0) 0154 OperatorMatMult, 0155 #endif 0156 OperatorDiv, 0157 OperatorMod, 0158 OperatorPow, 0159 OperatorLeftShift, 0160 OperatorRightShift, 0161 OperatorBitwiseOr, 0162 OperatorBitwiseXor, 0163 OperatorBitwiseAnd, 0164 OperatorFloorDivision, 0165 OperatorInvalid 0166 }; 0167 0168 enum UnaryOperatorTypes { 0169 UnaryOperatorInvert = 1, 0170 UnaryOperatorNot, 0171 UnaryOperatorAdd, 0172 UnaryOperatorSub, 0173 UnaryOperatorInvalid 0174 }; 0175 0176 enum ComparisonOperatorTypes { 0177 ComparisonOperatorEquals = 1, 0178 ComparisonOperatorNotEquals, 0179 ComparisonOperatorLessThan, 0180 ComparisonOperatorLessThanEqual, 0181 ComparisonOperatorGreaterThan, 0182 ComparisonOperatorGreaterThanEqual, 0183 ComparisonOperatorIs, 0184 ComparisonOperatorIsNot, 0185 ComparisonOperatorIn, 0186 ComparisonOperatorNotIn, 0187 ComparisonOperatorInvalid 0188 }; 0189 0190 Ast(Ast* parent, AstType type); 0191 Ast(); 0192 virtual ~Ast() {}; 0193 Ast* parent = nullptr; 0194 AstType astType; 0195 0196 bool isExpression() const { 0197 return astType >= ExpressionAstType && astType <= LastExpressionType; 0198 } 0199 0200 void copyRange(const Ast* other) { 0201 startCol = other->startCol; 0202 endCol = other->endCol; 0203 startLine = other->startLine; 0204 endLine = other->endLine; 0205 } 0206 0207 bool appearsBefore(const Ast* other) { 0208 return startLine < other->startLine || ( startLine == other->startLine && startCol < other->startCol ); 0209 } 0210 0211 const KTextEditor::Range range() const { 0212 return KTextEditor::Range(startLine, startCol, endLine, endCol); 0213 }; 0214 0215 const KTextEditor::Cursor start() const { 0216 return {startLine, startCol}; 0217 } 0218 0219 const KTextEditor::Cursor end() const { 0220 return {endLine, endCol}; 0221 } 0222 0223 bool isChildOf(Ast* other) const { 0224 const Ast* parent = this; 0225 while ( parent ) { 0226 if ( parent == other ) { 0227 return true; 0228 } 0229 parent = parent->parent; 0230 } 0231 return false; 0232 } 0233 0234 virtual QString dump() const 0235 { 0236 QString r = "Ast(astType="; 0237 r.append(astType); 0238 r.append(", startLine="); 0239 r.append(startLine); 0240 r.append(", startCol="); 0241 r.append(startCol); 0242 r.append(", endCol="); 0243 r.append(endCol); 0244 r.append(", endLine="); 0245 r.append(endLine); 0246 r.append(")"); 0247 return r; 0248 }; 0249 0250 int startCol; 0251 int startLine; 0252 int endCol; 0253 int endLine; 0254 0255 bool hasUsefulRangeInformation; 0256 0257 KDevelop::DUContext* context; 0258 }; 0259 0260 class KDEVPYTHONPARSER_EXPORT Identifier : public Ast { 0261 public: 0262 Identifier(QString value); 0263 bool operator==(const Identifier& rhs) const { 0264 return value == rhs.value; 0265 }; 0266 bool operator==(const QString& rhs) const { 0267 return value == rhs; 0268 }; 0269 void setEndColumn() { 0270 endCol = startCol + value.length() - 1; 0271 } 0272 operator QString() const { 0273 return value; 0274 }; 0275 QString value; 0276 0277 QString dump() const override; 0278 }; 0279 0280 // this replaces ModuleAst 0281 class KDEVPYTHONPARSER_EXPORT CodeAst : public Ast { 0282 public: 0283 CodeAst(); 0284 ~CodeAst(); 0285 typedef QSharedPointer<CodeAst> Ptr; 0286 QList<Ast*> body; 0287 Identifier* name; // module name 0288 QString dump() const override; 0289 0290 }; 0291 0292 /** Statement classes **/ 0293 class KDEVPYTHONPARSER_EXPORT StatementAst : public Ast { 0294 public: 0295 StatementAst(Ast* parent, AstType type); 0296 }; 0297 0298 class KDEVPYTHONPARSER_EXPORT FunctionDefinitionAst : public StatementAst { 0299 public: 0300 FunctionDefinitionAst(Ast* parent); 0301 Identifier* name; 0302 ArgumentsAst* arguments; 0303 QList<ExpressionAst*> decorators; 0304 QList<Ast*> body; 0305 ExpressionAst* returns; 0306 bool async; 0307 QString dump() const override; 0308 }; 0309 0310 class KDEVPYTHONPARSER_EXPORT ClassDefinitionAst : public StatementAst { 0311 public: 0312 ClassDefinitionAst(Ast* parent); 0313 Identifier* name; 0314 QList<ExpressionAst*> baseClasses; 0315 QList<Ast*> body; 0316 QList<ExpressionAst*> decorators; 0317 QString dump() const override; 0318 }; 0319 0320 class KDEVPYTHONPARSER_EXPORT ReturnAst : public StatementAst { 0321 public: 0322 ReturnAst(Ast* parent); 0323 ExpressionAst* value; 0324 QString dump() const override; 0325 }; 0326 0327 class KDEVPYTHONPARSER_EXPORT DeleteAst : public StatementAst { 0328 public: 0329 DeleteAst(Ast* parent); 0330 QList<ExpressionAst*> targets; 0331 QString dump() const override; 0332 }; 0333 0334 class KDEVPYTHONPARSER_EXPORT AssignmentAst : public StatementAst { 0335 public: 0336 AssignmentAst(Ast* parent); 0337 QList<ExpressionAst*> targets; 0338 ExpressionAst* value; 0339 QString dump() const override; 0340 }; 0341 0342 class KDEVPYTHONPARSER_EXPORT AugmentedAssignmentAst : public StatementAst { 0343 public: 0344 AugmentedAssignmentAst(Ast* parent); 0345 ExpressionAst* target; 0346 Ast::OperatorTypes op; 0347 ExpressionAst* value; 0348 QString dump() const override; 0349 }; 0350 0351 class KDEVPYTHONPARSER_EXPORT AnnotationAssignmentAst : public StatementAst { 0352 public: 0353 AnnotationAssignmentAst(Ast* parent); 0354 ExpressionAst* target; 0355 ExpressionAst* value; 0356 ExpressionAst* annotation; 0357 QString dump() const override; 0358 }; 0359 0360 class KDEVPYTHONPARSER_EXPORT ForAst : public StatementAst { 0361 public: 0362 ForAst(Ast* parent); 0363 ExpressionAst* target; 0364 ExpressionAst* iterator; 0365 QList<Ast*> body; 0366 QList<Ast*> orelse; 0367 bool async; 0368 QString dump() const override; 0369 }; 0370 0371 class KDEVPYTHONPARSER_EXPORT WhileAst : public StatementAst { 0372 public: 0373 WhileAst(Ast* parent); 0374 ExpressionAst* condition; 0375 QList<Ast*> body; 0376 QList<Ast*> orelse; 0377 QString dump() const override; 0378 }; 0379 0380 class KDEVPYTHONPARSER_EXPORT IfAst : public StatementAst { 0381 public: 0382 IfAst(Ast* parent); 0383 ExpressionAst* condition; 0384 QList<Ast*> body; 0385 QList<Ast*> orelse; 0386 QString dump() const override; 0387 }; 0388 0389 class KDEVPYTHONPARSER_EXPORT WithItemAst : public Ast { 0390 public: 0391 WithItemAst(Ast* parent); 0392 ExpressionAst* contextExpression; 0393 ExpressionAst* optionalVars; 0394 }; 0395 0396 class KDEVPYTHONPARSER_EXPORT WithAst : public StatementAst { 0397 public: 0398 WithAst(Ast* parent); 0399 QList<Ast*> body; 0400 QList<WithItemAst*> items; 0401 bool async; 0402 QString dump() const override; 0403 }; 0404 0405 class KDEVPYTHONPARSER_EXPORT RaiseAst : public StatementAst { 0406 public: 0407 RaiseAst(Ast* parent); 0408 ExpressionAst* type; 0409 // TODO check what the other things in the grammar actually are and add them 0410 QString dump() const override; 0411 }; 0412 0413 class KDEVPYTHONPARSER_EXPORT TryAst : public StatementAst { 0414 public: 0415 TryAst(Ast* parent); 0416 QList<Ast*> body; 0417 QList<ExceptionHandlerAst*> handlers; 0418 QList<Ast*> orelse; 0419 QList<Ast*> finally; 0420 QString dump() const override; 0421 }; 0422 0423 class KDEVPYTHONPARSER_EXPORT AssertionAst : public StatementAst { 0424 public: 0425 AssertionAst(Ast* parent); 0426 ExpressionAst* condition; 0427 ExpressionAst* message; 0428 QString dump() const override; 0429 }; 0430 0431 class KDEVPYTHONPARSER_EXPORT TryStarAst : public StatementAst { 0432 public: 0433 TryStarAst(Ast* parent); 0434 QList<Ast*> body; 0435 QList<ExceptionHandlerAst*> handlers; 0436 QList<Ast*> orelse; 0437 QList<Ast*> finally; 0438 }; 0439 0440 class KDEVPYTHONPARSER_EXPORT ImportAst : public StatementAst { 0441 public: 0442 ImportAst(Ast* parent); 0443 QList<AliasAst*> names; 0444 QString dump() const override; 0445 }; 0446 0447 class KDEVPYTHONPARSER_EXPORT ImportFromAst : public StatementAst { 0448 public: 0449 ImportFromAst(Ast* parent); 0450 Identifier* module; 0451 QList<AliasAst*> names; 0452 int level; 0453 QString dump() const override; 0454 }; 0455 0456 class KDEVPYTHONPARSER_EXPORT GlobalAst : public StatementAst { 0457 public: 0458 GlobalAst(Ast* parent); 0459 QList<Identifier*> names; 0460 QString dump() const override; 0461 }; 0462 0463 // TODO what's stmt::Expr(expr value) in the grammar and what do we need it for? 0464 0465 class KDEVPYTHONPARSER_EXPORT BreakAst : public StatementAst { 0466 public: 0467 BreakAst(Ast* parent); 0468 QString dump() const override { return "Break()"; } 0469 }; 0470 0471 class KDEVPYTHONPARSER_EXPORT ContinueAst : public StatementAst { 0472 public: 0473 ContinueAst(Ast* parent); 0474 QString dump() const override { return "Continue()"; } 0475 }; 0476 0477 class KDEVPYTHONPARSER_EXPORT PassAst : public StatementAst { 0478 public: 0479 PassAst(Ast* parent); 0480 QString dump() const override { return "Pass()"; } 0481 }; 0482 0483 class KDEVPYTHONPARSER_EXPORT NonlocalAst : public StatementAst { 0484 public: 0485 NonlocalAst(Ast* parent); 0486 QString dump() const override { return "Nonlocal()"; } 0487 }; 0488 0489 0490 /** Expression classes **/ 0491 class KDEVPYTHONPARSER_EXPORT ExpressionAst : public Ast { 0492 public: 0493 ExpressionAst(Ast* parent, AstType type = Ast::ExpressionAstType); 0494 enum Context { 0495 Load = 1, // the object is read 0496 Store = 2, // the object is written 0497 Delete = 3, // the object is deleted 0498 Invalid = -1 0499 }; 0500 ExpressionAst* value; // WARNING this is not set in most cases! 0501 }; 0502 0503 class KDEVPYTHONPARSER_EXPORT ConstantAst : public ExpressionAst { 0504 public: 0505 ConstantAst(Ast* parent, AstType type = Ast::ConstantAstType): ExpressionAst(parent, type) {} 0506 // TODO: Python 3.8 + removed classes ast.Num, ast.Str, ast.Bytes, ast.NameConstant and ast.Ellipsis 0507 }; 0508 0509 class KDEVPYTHONPARSER_EXPORT AssignmentExpressionAst : public ExpressionAst { 0510 public: 0511 AssignmentExpressionAst(Ast* parent); 0512 ExpressionAst* target; 0513 ExpressionAst* value; 0514 QString dump() const override; 0515 }; 0516 0517 class KDEVPYTHONPARSER_EXPORT AwaitAst : public ExpressionAst { 0518 public: 0519 AwaitAst(Ast* parent); 0520 ExpressionAst* value; 0521 QString dump() const override; 0522 }; 0523 0524 class KDEVPYTHONPARSER_EXPORT YieldFromAst : public ExpressionAst { 0525 public: 0526 YieldFromAst(Ast* parent); 0527 ExpressionAst* value; 0528 QString dump() const override; 0529 }; 0530 0531 class KDEVPYTHONPARSER_EXPORT BooleanOperationAst : public ExpressionAst { 0532 public: 0533 BooleanOperationAst(Ast* parent); 0534 Ast::BooleanOperationTypes type; 0535 QList<ExpressionAst*> values; 0536 }; 0537 0538 class KDEVPYTHONPARSER_EXPORT BinaryOperationAst : public ExpressionAst { 0539 public: 0540 BinaryOperationAst(Ast* parent); 0541 Ast::OperatorTypes type; 0542 ExpressionAst* lhs; 0543 ExpressionAst* rhs; 0544 inline QString methodName() const { 0545 switch ( type ) { 0546 case Python::Ast::OperatorAdd: return QLatin1String("__add__"); 0547 case Python::Ast::OperatorBitwiseAnd: return QLatin1String("__and__"); 0548 case Python::Ast::OperatorBitwiseOr: return QLatin1String("__or__"); 0549 case Python::Ast::OperatorBitwiseXor: return QLatin1String("__xor__"); 0550 case Python::Ast::OperatorDiv: return QLatin1String("__div__"); 0551 case Python::Ast::OperatorFloorDivision: return QLatin1String("__floordiv__"); 0552 case Python::Ast::OperatorLeftShift: return QLatin1String("__lshift__"); 0553 case Python::Ast::OperatorMod: return QLatin1String("__mod__"); 0554 case Python::Ast::OperatorMult: return QLatin1String("__mul__"); 0555 #if PYTHON_VERSION >= QT_VERSION_CHECK(3, 5, 0) 0556 case Python::Ast::OperatorMatMult: return QLatin1String("__matmul__"); 0557 #endif 0558 case Python::Ast::OperatorPow: return QLatin1String("__pow__"); 0559 case Python::Ast::OperatorRightShift: return QLatin1String("__rshift__"); 0560 case Python::Ast::OperatorSub: return QLatin1String("__sub__"); 0561 case Python::Ast::OperatorInvalid: // fallthrough 0562 default: return QString(); 0563 } 0564 }; 0565 // incremental methods, for e.g. a += 3 0566 inline QString incMethodName() const { 0567 QString name = methodName(); 0568 if ( name.size() < 3 ) { 0569 return name; 0570 } 0571 name.insert(2, QLatin1Char('i')); 0572 return name; 0573 } 0574 }; 0575 0576 class KDEVPYTHONPARSER_EXPORT UnaryOperationAst : public ExpressionAst { 0577 public: 0578 UnaryOperationAst(Ast* parent); 0579 Ast::UnaryOperatorTypes type; 0580 ExpressionAst* operand; 0581 0582 QString dump() const override; 0583 }; 0584 0585 class KDEVPYTHONPARSER_EXPORT LambdaAst : public ExpressionAst { 0586 public: 0587 LambdaAst(Ast* parent); 0588 ArgumentsAst* arguments; 0589 ExpressionAst* body; 0590 }; 0591 0592 class KDEVPYTHONPARSER_EXPORT IfExpressionAst : public ExpressionAst { 0593 public: 0594 IfExpressionAst(Ast* parent); 0595 ExpressionAst* condition; 0596 ExpressionAst* body; 0597 ExpressionAst* orelse; 0598 QString dump() const override; 0599 }; 0600 0601 class KDEVPYTHONPARSER_EXPORT DictAst : public ExpressionAst { 0602 public: 0603 DictAst(Ast* parent); 0604 QList<ExpressionAst*> keys; // WARNING: Can contain null elements: `{**other}` 0605 QList<ExpressionAst*> values; 0606 QString dump() const override; 0607 }; 0608 0609 class KDEVPYTHONPARSER_EXPORT SetAst : public ExpressionAst { 0610 public: 0611 SetAst(Ast* parent); 0612 QList<ExpressionAst*> elements; 0613 QString dump() const override; 0614 }; 0615 0616 class KDEVPYTHONPARSER_EXPORT ListComprehensionAst : public ExpressionAst { 0617 public: 0618 ListComprehensionAst(Ast* parent); 0619 ExpressionAst* element; 0620 QList<ComprehensionAst*> generators; 0621 }; 0622 0623 class KDEVPYTHONPARSER_EXPORT SetComprehensionAst : public ExpressionAst { 0624 public: 0625 SetComprehensionAst(Ast* parent); 0626 ExpressionAst* element; 0627 QList<ComprehensionAst*> generators; 0628 }; 0629 0630 class KDEVPYTHONPARSER_EXPORT DictionaryComprehensionAst : public ExpressionAst { 0631 public: 0632 DictionaryComprehensionAst(Ast* parent); 0633 ExpressionAst* key; 0634 ExpressionAst* value; 0635 QList<ComprehensionAst*> generators; 0636 }; 0637 0638 class KDEVPYTHONPARSER_EXPORT GeneratorExpressionAst : public ExpressionAst { 0639 public: 0640 GeneratorExpressionAst(Ast* parent); 0641 ExpressionAst* element; 0642 QList<ComprehensionAst*> generators; 0643 }; 0644 0645 class KDEVPYTHONPARSER_EXPORT CompareAst : public ExpressionAst { 0646 public: 0647 CompareAst(Ast* parent); 0648 ExpressionAst* leftmostElement; 0649 QList<ComparisonOperatorTypes> operators; 0650 QList<ExpressionAst*> comparands; 0651 }; 0652 0653 // TODO whats this exactly? 0654 class KDEVPYTHONPARSER_EXPORT ReprAst : public ExpressionAst { 0655 public: 0656 ReprAst(Ast* parent); 0657 ExpressionAst* value; 0658 }; 0659 0660 class KDEVPYTHONPARSER_EXPORT NumberAst : public ConstantAst { 0661 public: 0662 NumberAst(Ast* parent) : ConstantAst(parent, Ast::NumberAstType) {} 0663 long value = 0; // only used for ints 0664 bool isInt = false; // otherwise it's a float 0665 QString dump() const override; 0666 }; 0667 0668 class KDEVPYTHONPARSER_EXPORT StringAst : public ConstantAst { 0669 public: 0670 StringAst(Ast* parent) : ConstantAst(parent, Ast::StringAstType) {} 0671 QString value = ""; 0672 bool usedAsComment = false; 0673 QString dump() const override { return "Str('" + value + "')"; } 0674 }; 0675 0676 class KDEVPYTHONPARSER_EXPORT JoinedStringAst : public ExpressionAst { 0677 public: 0678 JoinedStringAst(Ast* parent); 0679 QList<ExpressionAst*> values; 0680 }; 0681 0682 class KDEVPYTHONPARSER_EXPORT FormattedValueAst : public ExpressionAst { 0683 public: 0684 FormattedValueAst(Ast* parent); 0685 ExpressionAst* value; 0686 int conversion; 0687 ExpressionAst* formatSpec; 0688 }; 0689 0690 class KDEVPYTHONPARSER_EXPORT BytesAst : public ConstantAst { 0691 public: 0692 BytesAst(Ast* parent) : ConstantAst(parent, Ast::BytesAstType) {}; 0693 QString value = ""; 0694 }; 0695 0696 class KDEVPYTHONPARSER_EXPORT YieldAst : public ExpressionAst { 0697 public: 0698 YieldAst(Ast* parent); 0699 ExpressionAst* value; 0700 QString dump() const override; 0701 }; 0702 0703 class KDEVPYTHONPARSER_EXPORT NameAst : public ExpressionAst { 0704 public: 0705 NameAst(Ast* parent); 0706 Identifier* identifier; 0707 ExpressionAst::Context context; 0708 QString dump() const override; 0709 }; 0710 0711 class KDEVPYTHONPARSER_EXPORT NameConstantAst : public ConstantAst { 0712 public: 0713 NameConstantAst(Ast* parent) : ConstantAst(parent, Ast::NameConstantAstType) {} 0714 enum NameConstantTypes { 0715 False, 0716 True, 0717 None, 0718 Invalid // should not happen 0719 }; 0720 NameConstantTypes value = Invalid; 0721 QString dump() const override; 0722 }; 0723 0724 class KDEVPYTHONPARSER_EXPORT CallAst : public ExpressionAst { 0725 public: 0726 CallAst(Ast* parent); 0727 ExpressionAst* function; 0728 QList<ExpressionAst*> arguments; 0729 QList<KeywordAst*> keywords; 0730 QString dump() const override; 0731 }; 0732 0733 class KDEVPYTHONPARSER_EXPORT AttributeAst : public ExpressionAst { 0734 public: 0735 AttributeAst(Ast* parent); 0736 ExpressionAst* value; 0737 Identifier* attribute; 0738 ExpressionAst::Context context; 0739 int depth; 0740 QString dump() const override; 0741 }; 0742 0743 class KDEVPYTHONPARSER_EXPORT SubscriptAst : public ExpressionAst { 0744 public: 0745 SubscriptAst(Ast* parent); 0746 ExpressionAst* value; 0747 ExpressionAst* slice; 0748 ExpressionAst::Context context; 0749 QString dump() const override; 0750 }; 0751 0752 class KDEVPYTHONPARSER_EXPORT StarredAst : public ExpressionAst { 0753 public: 0754 StarredAst(Ast* parent); 0755 ExpressionAst* value; 0756 ExpressionAst::Context context; 0757 QString dump() const override; 0758 }; 0759 0760 class KDEVPYTHONPARSER_EXPORT ListAst : public ExpressionAst { 0761 public: 0762 ListAst(Ast* parent); 0763 QList<ExpressionAst*> elements; 0764 ExpressionAst::Context context; 0765 QString dump() const override; 0766 }; 0767 0768 class KDEVPYTHONPARSER_EXPORT TupleAst : public ExpressionAst { 0769 public: 0770 TupleAst(Ast* parent); 0771 QList<ExpressionAst*> elements; 0772 ExpressionAst::Context context; 0773 QString dump() const override; 0774 }; 0775 0776 class KDEVPYTHONPARSER_EXPORT EllipsisAst : public ConstantAst { 0777 public: 0778 EllipsisAst(Ast* parent) : ConstantAst(parent, Ast::EllipsisAstType) {}; 0779 QString dump() const override { return "Ellipsis()"; } 0780 }; 0781 0782 class KDEVPYTHONPARSER_EXPORT SliceAst : public ExpressionAst { 0783 public: 0784 SliceAst(Ast* parent); 0785 ExpressionAst* lower; 0786 ExpressionAst* upper; 0787 ExpressionAst* step; 0788 QString dump() const override; 0789 }; 0790 0791 0792 /** Independent classes **/ 0793 0794 class KDEVPYTHONPARSER_EXPORT ArgAst : public Ast { 0795 public: 0796 ArgAst(Ast* parent); 0797 Identifier* argumentName; 0798 ExpressionAst* annotation; 0799 QString dump() const override; 0800 }; 0801 0802 class KDEVPYTHONPARSER_EXPORT ArgumentsAst : public Ast { 0803 public: 0804 ArgumentsAst(Ast* parent); 0805 QList<ArgAst*> arguments; 0806 QList<ArgAst*> kwonlyargs; 0807 QList<ArgAst*> posonlyargs; 0808 QList<ExpressionAst*> defaultValues; 0809 QList<ExpressionAst*> defaultKwValues; 0810 ArgAst* vararg; 0811 ArgAst* kwarg; 0812 QString dump() const override; 0813 }; 0814 0815 class KDEVPYTHONPARSER_EXPORT KeywordAst : public Ast { 0816 public: 0817 KeywordAst(Ast* parent); 0818 Identifier* argumentName; 0819 ExpressionAst* value; 0820 QString dump() const override; 0821 }; 0822 0823 class KDEVPYTHONPARSER_EXPORT ComprehensionAst : public Ast { 0824 public: 0825 ComprehensionAst(Ast* parent); 0826 ExpressionAst* target; 0827 ExpressionAst* iterator; 0828 QList<ExpressionAst*> conditions; 0829 }; 0830 0831 class KDEVPYTHONPARSER_EXPORT ExceptionHandlerAst : public Ast { 0832 public: 0833 ExceptionHandlerAst(Ast* parent); 0834 ExpressionAst* type; 0835 Identifier* name; 0836 QList<Ast*> body; 0837 }; 0838 0839 class KDEVPYTHONPARSER_EXPORT AliasAst : public Ast { 0840 public: 0841 AliasAst(Ast* parent); 0842 Identifier* name; 0843 Identifier* asName; 0844 QString dump() const override; 0845 }; 0846 0847 0848 /** Match classes **/ 0849 0850 class KDEVPYTHONPARSER_EXPORT PatternAst : public Ast { 0851 public: 0852 PatternAst(Ast* parent, AstType type = Ast::PatternAstType): Ast(parent, type) {}; 0853 }; 0854 0855 class KDEVPYTHONPARSER_EXPORT MatchCaseAst : public Ast { 0856 public: 0857 MatchCaseAst(Ast* parent) : Ast(parent, Ast::MatchCaseAstType) {}; 0858 PatternAst* pattern = nullptr; 0859 ExpressionAst* guard = nullptr; 0860 QList<Ast*> body; 0861 QString dump() const override; 0862 }; 0863 0864 class KDEVPYTHONPARSER_EXPORT MatchAst : public Ast { 0865 public: 0866 MatchAst(Ast* parent) : Ast(parent, Ast::MatchAstType) {}; 0867 ExpressionAst* subject = nullptr; 0868 QList<MatchCaseAst*> cases; 0869 QString dump() const override; 0870 }; 0871 0872 class KDEVPYTHONPARSER_EXPORT MatchValueAst : public PatternAst { 0873 public: 0874 MatchValueAst(Ast* parent): PatternAst(parent, Ast::MatchValueAstType) {}; 0875 ExpressionAst* value = nullptr; 0876 QString dump() const override; 0877 }; 0878 0879 class KDEVPYTHONPARSER_EXPORT MatchSingletonAst : public PatternAst { 0880 public: 0881 MatchSingletonAst(Ast* parent): PatternAst(parent, Ast::MatchSingletonAstType) {}; 0882 NameConstantAst::NameConstantTypes value = NameConstantAst::NameConstantTypes::Invalid; 0883 QString dump() const override; 0884 }; 0885 0886 class KDEVPYTHONPARSER_EXPORT MatchSequenceAst : public PatternAst { 0887 public: 0888 MatchSequenceAst(Ast* parent) : PatternAst(parent, Ast::MatchSequenceAstType) {}; 0889 QList<PatternAst*> patterns; 0890 QString dump() const override; 0891 }; 0892 0893 class KDEVPYTHONPARSER_EXPORT MatchMappingAst : public PatternAst { 0894 public: 0895 MatchMappingAst(Ast* parent) : PatternAst(parent, Ast::MatchMappingAstType) {}; 0896 QList<ExpressionAst*> keys; 0897 Identifier *rest = nullptr; 0898 QList<PatternAst*> patterns; 0899 QString dump() const override; 0900 }; 0901 0902 0903 class KDEVPYTHONPARSER_EXPORT MatchClassAst : public PatternAst { 0904 public: 0905 MatchClassAst(Ast* parent): PatternAst(parent, Ast::MatchClassAstType) {}; 0906 ExpressionAst* cls = nullptr; 0907 QList<PatternAst*> patterns; 0908 Identifier *kwdAttrs = nullptr; 0909 QList<PatternAst*> kwdPatterns; 0910 QString dump() const override; 0911 }; 0912 0913 class KDEVPYTHONPARSER_EXPORT MatchStarAst : public PatternAst { 0914 public: 0915 MatchStarAst(Ast* parent): PatternAst(parent, Ast::MatchStarAstType) {}; 0916 Identifier* name = nullptr; 0917 QString dump() const override; 0918 }; 0919 0920 class KDEVPYTHONPARSER_EXPORT MatchAsAst : public PatternAst { 0921 public: 0922 MatchAsAst(Ast* parent): PatternAst(parent, Ast::MatchAsAstType) {}; 0923 PatternAst* pattern = nullptr; 0924 Identifier* name = nullptr; 0925 QString dump() const override; 0926 }; 0927 0928 0929 class KDEVPYTHONPARSER_EXPORT MatchOrAst : public PatternAst { 0930 public: 0931 MatchOrAst(Ast* parent): PatternAst(parent, Ast::MatchOrAstType) {}; 0932 QList<PatternAst*> patterns; 0933 QString dump() const override; 0934 }; 0935 0936 } // end namespace Python 0937 0938 #endif