QGIS API Documentation 4.3.0-Master (0cef2469b72)
Loading...
Searching...
No Matches
qgsvbscript.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvbscript.cpp
3 -----------------------
4 begin : November 2026
5 copyright : (C) 2026 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsvbscript.h"
17
18#include <cmath>
19
20#include "qgis.h"
21#include "qgsvariantutils.h"
22
23#include <QRegularExpression>
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
28// external parser entry point
29extern QgsVbScriptBlockStatement *parseVbScript( const QString &scriptText, QString &parserErrorMessage );
30
31const char *QgsVbScriptBinaryOperatorExpression::BINARY_OPERATOR_TEXT[] = { "+", "-", "*", "/", "\\", "Mod", "^", "&", "=", "<>", "<", ">", "<=", ">=", "Is", "And", "Or", "Xor", "Eqv", "Imp" };
32
33const char *QgsVbScriptUnaryOperatorExpression::UNARY_OPERATOR_TEXT[] = { "Not", "-", "+" };
34
35//
36// QgsVbScriptBlockStatement
37//
38
40 : mStatements( statements )
41{}
42
44{
45 qDeleteAll( mStatements );
46}
47
49{
50 mStatements.append( statement );
51}
52
54{
55 QStringList parts;
56 parts.reserve( mStatements.size() );
57 for ( QgsVbScriptStatement *statement : std::as_const( mStatements ) )
58 {
59 parts << statement->dump();
60 }
61 return parts.join( '\n' );
62}
63
65{
66 QList<QgsVbScriptStatement *> clonedList;
67 clonedList.reserve( mStatements.size() );
68 for ( QgsVbScriptStatement *statement : std::as_const( mStatements ) )
69 {
70 clonedList.append( dynamic_cast<QgsVbScriptStatement *>( statement->clone() ) );
71 }
72 return new QgsVbScriptBlockStatement( clonedList );
73}
74
76{
77 visitor.visit( *this );
78}
79
80//
81// QgsVbScriptVariableDeclarationStatement
82//
83
85 : mVariableNames( { variableName } )
86{}
87
91
93{
94 return u"Dim %1"_s.arg( mVariableNames.join( ", "_L1 ) );
95}
96
101
103{
104 visitor.visit( *this );
105}
106
107//
108// QgsVbScriptAssignmentStatement
109//
110
116
118{
119 return u"%1%2 = %3"_s.arg( mIsSet ? "Set "_L1 : ""_L1, mTarget ? mTarget->dump() : QString(), mValue ? mValue->dump() : QString() );
120}
121
123{
124 QgsVbScriptExpression *clonedTarget = mTarget ? dynamic_cast<QgsVbScriptExpression *>( mTarget->clone() ) : nullptr;
125 QgsVbScriptExpression *clonedValue = mValue ? dynamic_cast<QgsVbScriptExpression *>( mValue->clone() ) : nullptr;
126 return new QgsVbScriptAssignmentStatement( clonedTarget, clonedValue, mIsSet );
127}
128
130{
131 visitor.visit( *this );
132}
133
134//
135// QgsVbScriptFunctionDeclarationStatement
136//
137
139 : mFunctionName( functionName )
140 , mParameters( parameters )
141 , mBody( body )
142 , mIsSub( isSub )
143{}
144
146{
147 const QString bodyDump = mBody ? mBody->dump() : QString();
148 const QString keyword = mIsSub ? u"Sub"_s : u"Function"_s;
149
150 return u"%1 %2(%3)\n%4\nEnd %1"_s.arg( keyword, mFunctionName, mParameters.join( ", "_L1 ), bodyDump );
151}
152
154{
155 return new QgsVbScriptFunctionDeclarationStatement( mFunctionName, mParameters, mBody ? dynamic_cast<QgsVbScriptBlockStatement *>( mBody->clone() ) : nullptr, mIsSub );
156}
157
159{
160 visitor.visit( *this );
161}
162
163//
164// QgsVbScriptExpressionStatement
165//
166
170
172{
173 return mExpression ? mExpression->dump() : QString();
174}
175
177{
178 return new QgsVbScriptExpressionStatement( mExpression ? dynamic_cast<QgsVbScriptExpression *>( mExpression->clone() ) : nullptr );
179}
180
182{
183 visitor.visit( *this );
184}
185
186//
187// QgsVbScriptLiteralExpression
188//
189
193
195{
196 if ( QgsVariantUtils::isNull( mValue ) )
197 return u"Null"_s;
198
199 switch ( mValue.userType() )
200 {
201 case QMetaType::Type::Int:
202 return QString::number( mValue.toInt() );
203 case QMetaType::Type::LongLong:
204 return QString::number( mValue.toLongLong() );
205 case QMetaType::Type::Double:
206 return qgsDoubleToString( mValue.toDouble() );
207 case QMetaType::Type::QString:
208 return mValue.toString();
209 case QMetaType::Type::Bool:
210 return mValue.toBool() ? u"True"_s : u"False"_s;
211 default:
212 return mValue.toString();
213 }
214}
215
217{
218 if ( QgsVariantUtils::isNull( mValue ) )
219 return u"Null"_s;
220
221 if ( mValue.userType() == QMetaType::Type::QString )
222 return QgsVbScript::quotedString( mValue.toString() );
223
224 return valueAsString();
225}
226
231
233{
234 visitor.visit( *this );
235}
236
237//
238// QgsVbScriptVariableReferenceExpression
239//
240
244
246{
247 return mName;
248}
249
254
256{
257 visitor.visit( *this );
258}
259
260//
261// QgsVbScriptMemberAccessExpression
262//
263
268
270{
271 return u"%1.%2"_s.arg( mContainerExpression ? mContainerExpression->dump() : QString(), mMemberName );
272}
273
275{
276 QgsVbScriptExpression *clonedContainer = mContainerExpression ? dynamic_cast<QgsVbScriptExpression *>( mContainerExpression->clone() ) : nullptr;
277 return new QgsVbScriptMemberAccessExpression( clonedContainer, mMemberName );
278}
279
281{
282 visitor.visit( *this );
283}
284
285//
286// QgsVbScriptBinaryOperatorExpression
287//
288
294
296{
297 return BINARY_OPERATOR_TEXT[static_cast<int>( mBinaryOperator )];
298}
299
301{
302 switch ( mBinaryOperator )
303 {
305 return 1;
307 return 2;
309 return 3;
311 return 4;
313 return 5;
321 return 6;
323 return 7;
326 return 8;
328 return 9;
330 return 10;
333 return 11;
335 return 12;
336 }
337 return -1;
338}
339
341{
342 return true;
343}
344
346{
347 QgsVbScriptBinaryOperatorExpression *leftBinary = dynamic_cast<QgsVbScriptBinaryOperatorExpression *>( mLeftOperand.get() );
348 QgsVbScriptBinaryOperatorExpression *rightBinary = dynamic_cast<QgsVbScriptBinaryOperatorExpression *>( mRightOperand.get() );
349
350 const QString leftDump = leftBinary && ( leftBinary->precedence() < precedence() ) ? u"(%1)"_s.arg( mLeftOperand->dump() ) : mLeftOperand->dump();
351
352 const QString rightDump = rightBinary && ( rightBinary->precedence() <= precedence() ) ? u"(%1)"_s.arg( mRightOperand->dump() ) : mRightOperand->dump();
353
354 return u"%1 %2 %3"_s.arg( leftDump, text(), rightDump );
355}
356
358{
359 QgsVbScriptExpression *leftClone = mLeftOperand ? dynamic_cast<QgsVbScriptExpression *>( mLeftOperand->clone() ) : nullptr;
360 QgsVbScriptExpression *rightClone = mRightOperand ? dynamic_cast<QgsVbScriptExpression *>( mRightOperand->clone() ) : nullptr;
361 return new QgsVbScriptBinaryOperatorExpression( mBinaryOperator, leftClone, rightClone );
362}
363
365{
366 visitor.visit( *this );
367}
368
369//
370// QgsVbScriptUnaryOperatorExpression
371//
372
377
379{
380 return UNARY_OPERATOR_TEXT[static_cast<int>( mUnaryOperator )];
381}
382
384{
385 if ( mUnaryOperator == UnaryOperator::Not )
386 return u"Not %1"_s.arg( mOperand ? mOperand->dump() : QString() );
387 else
388 return u"%1%2"_s.arg( text(), mOperand ? mOperand->dump() : QString() );
389}
390
392{
393 return new QgsVbScriptUnaryOperatorExpression( mUnaryOperator, mOperand ? dynamic_cast<QgsVbScriptExpression *>( mOperand->clone() ) : nullptr );
394}
395
397{
398 visitor.visit( *this );
399}
400
401//
402// QgsVbScriptFunctionCallExpression
403//
404
406 : mTargetExpression( targetExpression )
407 , mArguments( arguments )
408{}
409
414
416{
417 QStringList argumentStrings;
418 argumentStrings.reserve( mArguments.size() );
419 for ( QgsVbScriptExpression *argument : std::as_const( mArguments ) )
420 {
421 argumentStrings.append( argument->dump() );
422 }
423 return u"%1(%2)"_s.arg( mTargetExpression ? mTargetExpression->dump() : QString(), argumentStrings.join( ", "_L1 ) );
424}
425
427{
428 QList<QgsVbScriptExpression *> clonedArguments;
429 clonedArguments.reserve( mArguments.size() );
430 for ( QgsVbScriptExpression *argument : std::as_const( mArguments ) )
431 {
432 clonedArguments.append( dynamic_cast<QgsVbScriptExpression *>( argument->clone() ) );
433 }
434 return new QgsVbScriptFunctionCallExpression( mTargetExpression ? dynamic_cast<QgsVbScriptExpression *>( mTargetExpression->clone() ) : nullptr, clonedArguments );
435}
436
438{
439 visitor.visit( *this );
440}
441
442//
443// QgsVbScriptIndexExpression
444//
445
450
452{
453 return u"%1[%2]"_s.arg( mContainerExpression ? mContainerExpression->dump() : QString(), mIndexExpression ? mIndexExpression->dump() : QString() );
454}
455
457{
458 QgsVbScriptExpression *clonedContainer = mContainerExpression ? dynamic_cast<QgsVbScriptExpression *>( mContainerExpression->clone() ) : nullptr;
459 QgsVbScriptExpression *clonedIndex = mIndexExpression ? dynamic_cast<QgsVbScriptExpression *>( mIndexExpression->clone() ) : nullptr;
460 return new QgsVbScriptIndexExpression( clonedContainer, clonedIndex );
461}
462
464{
465 visitor.visit( *this );
466}
467
468//
469// QgsVbScriptArrayLiteralExpression
470//
471
473 : mElements( elements )
474{}
475
480
482{
483 QStringList elementStrings;
484 elementStrings.reserve( mElements.size() );
485 for ( QgsVbScriptExpression *element : std::as_const( mElements ) )
486 {
487 elementStrings.append( element->dump() );
488 }
489 return u"Array(%1)"_s.arg( elementStrings.join( ", "_L1 ) );
490}
491
493{
494 QList<QgsVbScriptExpression *> clonedElements;
495 clonedElements.reserve( mElements.size() );
496 for ( QgsVbScriptExpression *element : std::as_const( mElements ) )
497 {
498 clonedElements.append( dynamic_cast<QgsVbScriptExpression *>( element->clone() ) );
499 }
500 return new QgsVbScriptArrayLiteralExpression( clonedElements );
501}
502
504{
505 visitor.visit( *this );
506}
507
508
509//
510// QgsVbScriptIfStatement
511//
512
519
521{
522 if ( mIsInline )
523 {
524 QString result = u"If %1 Then %2"_s.arg( mCondition ? mCondition->dump() : QString(), mTrueBlock ? mTrueBlock->dump() : QString() );
525 if ( mFalseBlock )
526 result += u" Else %1"_s.arg( mFalseBlock->dump() );
527 return result;
528 }
529
530 QString result = u"If %1 Then\n%2"_s.arg( mCondition ? mCondition->dump() : QString(), mTrueBlock ? mTrueBlock->dump() : QString() );
531 if ( mFalseBlock )
532 {
533 if ( mFalseBlock->count() == 1 && mFalseBlock->statements().at( 0 )->nodeType() == NodeType::IfStatement )
534 {
535 const QgsVbScriptIfStatement *elseIfStatement = dynamic_cast<const QgsVbScriptIfStatement *>( mFalseBlock->statements().at( 0 ) );
536 if ( elseIfStatement && !elseIfStatement->isInline() )
537 {
538 result += u"\nElse%1"_s.arg( elseIfStatement->dump() );
539 return result;
540 }
541 }
542 result += u"\nElse\n%1"_s.arg( mFalseBlock->dump() );
543 }
544 result += "\nEnd If"_L1;
545 return result;
546}
547
549{
550 QgsVbScriptExpression *clonedCondition = mCondition ? dynamic_cast<QgsVbScriptExpression *>( mCondition->clone() ) : nullptr;
551 QgsVbScriptBlockStatement *clonedTrueBlock = mTrueBlock ? dynamic_cast<QgsVbScriptBlockStatement *>( mTrueBlock->clone() ) : nullptr;
552 QgsVbScriptBlockStatement *clonedFalseBlock = mFalseBlock ? dynamic_cast<QgsVbScriptBlockStatement *>( mFalseBlock->clone() ) : nullptr;
553 return new QgsVbScriptIfStatement( clonedCondition, clonedTrueBlock, clonedFalseBlock, mIsInline );
554}
555
557{
558 visitor.visit( *this );
559}
560
561//
562// QgsVbScriptForStatement
563//
564
574
576{
577 QString stepString;
578 if ( mStepExpression )
579 stepString = u" Step %1"_s.arg( mStepExpression->dump() );
580
581 return u"For %1 = %2 To %3%4\n%5\nNext"_s
582 .arg( mVariableName, mStartExpression ? mStartExpression->dump() : QString(), mEndExpression ? mEndExpression->dump() : QString(), stepString, mBody ? mBody->dump() : QString() );
583}
584
586{
587 QgsVbScriptExpression *clonedStart = mStartExpression ? dynamic_cast<QgsVbScriptExpression *>( mStartExpression->clone() ) : nullptr;
588 QgsVbScriptExpression *clonedEnd = mEndExpression ? dynamic_cast<QgsVbScriptExpression *>( mEndExpression->clone() ) : nullptr;
589 QgsVbScriptExpression *clonedStep = mStepExpression ? dynamic_cast<QgsVbScriptExpression *>( mStepExpression->clone() ) : nullptr;
590 QgsVbScriptBlockStatement *clonedBody = mBody ? dynamic_cast<QgsVbScriptBlockStatement *>( mBody->clone() ) : nullptr;
591 return new QgsVbScriptForStatement( mVariableName, clonedStart, clonedEnd, clonedStep, clonedBody );
592}
593
595{
596 visitor.visit( *this );
597}
598
599//
600// QgsVbScriptForEachStatement
601//
602
608
610{
611 return u"For Each %1 In %2\n%3\nNext"_s.arg( mVariableName, mCollection ? mCollection->dump() : QString(), mBody ? mBody->dump() : QString() );
612}
613
615{
616 QgsVbScriptExpression *clonedCollection = mCollection ? dynamic_cast<QgsVbScriptExpression *>( mCollection->clone() ) : nullptr;
617 QgsVbScriptBlockStatement *clonedBody = mBody ? dynamic_cast<QgsVbScriptBlockStatement *>( mBody->clone() ) : nullptr;
618 return new QgsVbScriptForEachStatement( mVariableName, clonedCollection, clonedBody );
619}
620
622{
623 visitor.visit( *this );
624}
625
626//
627// QgsVbScriptWhileStatement
628//
629
635
637{
638 const QString bodyDump = mBody ? mBody->dump() : QString();
639 const QString conditionDump = mCondition ? mCondition->dump() : QString();
640
641 switch ( mLoopType )
642 {
644 return u"While %1\n%2\nWend"_s.arg( conditionDump, bodyDump );
646 return u"Do While %1\n%2\nLoop"_s.arg( conditionDump, bodyDump );
648 return u"Do Until %1\n%2\nLoop"_s.arg( conditionDump, bodyDump );
650 return u"Do\n%1\nLoop While %2"_s.arg( bodyDump, conditionDump );
652 return u"Do\n%1\nLoop Until %2"_s.arg( bodyDump, conditionDump );
653 }
654 return QString();
655}
656
658{
659 QgsVbScriptExpression *clonedCondition = mCondition ? dynamic_cast<QgsVbScriptExpression *>( mCondition->clone() ) : nullptr;
660 QgsVbScriptBlockStatement *clonedBody = mBody ? dynamic_cast<QgsVbScriptBlockStatement *>( mBody->clone() ) : nullptr;
661 return new QgsVbScriptWhileStatement( mLoopType, clonedCondition, clonedBody );
662}
663
665{
666 visitor.visit( *this );
667}
668
669
670//
671// QgsVbScriptExitStatement
672//
673
677
679{
680 switch ( mExitType )
681 {
682 case ExitType::ExitDo:
683 return u"Exit Do"_s;
685 return u"Exit For"_s;
687 return u"Exit Function"_s;
689 return u"Exit Sub"_s;
690 }
691 return QString();
692}
693
695{
696 return new QgsVbScriptExitStatement( mExitType );
697}
698
700{
701 visitor.visit( *this );
702}
703
704//
705// QgsVbScriptFieldReferenceExpression
706//
707
711
713{
714 return QgsVbScript::quotedFieldReference( mFieldName );
715}
716
721
723{
724 visitor.visit( *this );
725}
726
727
728//
729// QgsVbScriptRecursiveVisitor
730//
731
733{
734 const QList<QgsVbScriptStatement *> statements = node.statements();
735 for ( QgsVbScriptStatement *statement : statements )
736 {
737 statement->accept( *this );
738 }
739}
740
743
745{
746 if ( node.target() )
747 node.target()->accept( *this );
748 if ( node.value() )
749 node.value()->accept( *this );
750}
751
753{
754 if ( node.body() )
755 node.body()->accept( *this );
756}
757
759{
760 if ( node.expression() )
761 node.expression()->accept( *this );
762}
763
766
769
775
777{
778 if ( node.leftOperand() )
779 node.leftOperand()->accept( *this );
780 if ( node.rightOperand() )
781 node.rightOperand()->accept( *this );
782}
783
785{
786 if ( node.operand() )
787 node.operand()->accept( *this );
788}
789
791{
792 if ( node.targetExpression() )
793 node.targetExpression()->accept( *this );
794 const QList<QgsVbScriptExpression *> arguments = node.arguments();
795 for ( QgsVbScriptExpression *argument : arguments )
796 {
797 argument->accept( *this );
798 }
799}
800
802{
803 if ( node.containerExpression() )
804 node.containerExpression()->accept( *this );
805 if ( node.indexExpression() )
806 node.indexExpression()->accept( *this );
807}
808
810{
811 const QList<QgsVbScriptExpression *> elements = node.elements();
812 for ( QgsVbScriptExpression *element : elements )
813 {
814 element->accept( *this );
815 }
816}
817
819{
820 if ( node.condition() )
821 node.condition()->accept( *this );
822 if ( node.trueBlock() )
823 node.trueBlock()->accept( *this );
824 if ( node.falseBlock() )
825 node.falseBlock()->accept( *this );
826}
827
829{
830 if ( node.startExpression() )
831 node.startExpression()->accept( *this );
832 if ( node.endExpression() )
833 node.endExpression()->accept( *this );
834 if ( node.stepExpression() )
835 node.stepExpression()->accept( *this );
836 if ( node.body() )
837 node.body()->accept( *this );
838}
839
841{
842 if ( node.collection() )
843 node.collection()->accept( *this );
844 if ( node.body() )
845 node.body()->accept( *this );
846}
847
849{
850 if ( node.condition() )
851 node.condition()->accept( *this );
852 if ( node.body() )
853 node.body()->accept( *this );
854}
855
858
861
862//
863// QgsVbScriptScript
864//
865
866QgsVbScript::QgsVbScript( const QString &scriptText )
867 : mScript( scriptText )
868{
869 if ( !scriptText.isEmpty() )
870 {
871 mRootBlock.reset( parseVbScript( scriptText, mParserErrorString ) );
872 }
873}
874
876 : mScript( other.mScript )
878{
879 if ( other.mRootBlock )
880 {
881 mRootBlock.reset( dynamic_cast<QgsVbScriptBlockStatement *>( other.mRootBlock->clone() ) );
882 }
883}
884
886{
887 if ( this != &other )
888 {
889 mScript = other.mScript;
891 if ( other.mRootBlock )
892 {
893 mRootBlock.reset( dynamic_cast<QgsVbScriptBlockStatement *>( other.mRootBlock->clone() ) );
894 }
895 else
896 {
897 mRootBlock.reset();
898 }
899 }
900 return *this;
901}
902
903QgsVbScript::~QgsVbScript() = default;
904
906{
907 return !mParserErrorString.isEmpty() || ( !mScript.trimmed().isEmpty() && !mRootBlock );
908}
909
911{
912 return mParserErrorString;
913}
914
916{
917 return mRootBlock.get();
918}
919
920QString QgsVbScript::script() const
921{
922 if ( !mScript.isEmpty() )
923 return mScript;
924
925 return dump();
926}
927
928QString QgsVbScript::dump() const
929{
930 if ( !mRootBlock )
931 return QString();
932
933 return mRootBlock->dump();
934}
935
937{
938 if ( mRootBlock )
939 mRootBlock->accept( visitor );
940}
941
942QString QgsVbScript::quotedString( const QString &text )
943{
944 QString escaped = text;
945 escaped.replace( '\"'_L1, "\"\""_L1 );
946 return u"\"%1\""_s.arg( escaped );
947}
948
949QString QgsVbScript::quotedFieldReference( const QString &fieldName )
950{
951 QString escaped = fieldName;
952 escaped.replace( ']', "]]"_L1 );
953 return u"[%1]"_s.arg( escaped );
954}
955
956QString QgsVbScript::extractFieldReference( const QString &text )
957{
958 QString t = text.trimmed();
959 if ( t.startsWith( '[' ) && t.endsWith( ']' ) && t.length() >= 2 )
960 {
961 t = t.mid( 1, t.length() - 2 );
962 }
963 else if ( t.endsWith( ']' ) )
964 {
965 t.chop( 1 );
966 }
967 t = t.trimmed();
968 t.replace( "]]"_L1, "]"_L1 );
969 return t;
970}
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents an array literal expression.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptArrayLiteralExpression(const QList< QgsVbScriptExpression * > &elements)
Constructor for QgsVbScriptArrayLiteralExpression.
QList< QgsVbScriptExpression * > elements() const
Returns the list of element expressions.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
Represents a variable reassignment statement.
bool isSet() const
Returns true if the assignment uses the Set keyword.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
QgsVbScriptExpression * target() const
Returns the target expression being assigned to.
QgsVbScriptAssignmentStatement(QgsVbScriptExpression *target, QgsVbScriptExpression *value, bool isSet=false)
Constructor for QgsVbScriptAssignmentStatement.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptExpression * value() const
Returns the assigned value expression.
Represents a binary operation expression.
QgsVbScriptBinaryOperatorExpression(BinaryOperator binaryOperator, QgsVbScriptExpression *leftOperand, QgsVbScriptExpression *rightOperand)
Constructor for QgsVbScriptBinaryOperatorExpression.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
BinaryOperator
Binary operator enumeration.
@ GreaterThanOrEqual
Greater than or equal to >=.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptExpression * leftOperand() const
Returns the left-hand operand expression.
int precedence() const
Returns operator precedence value.
static const char * BINARY_OPERATOR_TEXT[]
Definition qgsvbscript.h:31
QString text() const
Returns text representation of the operator.
BinaryOperator binaryOperator() const
Returns the binary operator.
QgsVbScriptExpression * rightOperand() const
Returns the right-hand operand expression.
bool leftAssociative() const
Returns true if operator is left-associative.
Represents a sequence of statements.
~QgsVbScriptBlockStatement() override
QString dump() const override
Returns the formatted script representation of the node.
QList< QgsVbScriptStatement * > statements() const
Returns the list of statements contained within this block.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptBlockStatement()=default
void appendStatement(QgsVbScriptStatement *statement)
Appends a statement to the block.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
Represents an Exit loop or Exit Sub/Function statement.
QgsVbScriptExitStatement(ExitType exitType)
Constructor for QgsVbScriptExitStatement.
QString dump() const override
Returns the formatted script representation of the node.
ExitType exitType() const
Returns the exit target scope.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
ExitType
Object to exit from.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
Represents an expression evaluated as a statement.
QString dump() const override
Returns the formatted script representation of the node.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptExpressionStatement(QgsVbScriptExpression *expression)
Constructor for QgsVbScriptExpressionStatement.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
QgsVbScriptExpression * expression() const
Returns the underlying expression.
Base class for expression nodes in a VBScript script.
Represents an ESRI specific node for a reference to an attribute field enclosed in square brackets.
QgsVbScriptFieldReferenceExpression(const QString &fieldName)
Constructor for QgsVbScriptFieldReferenceExpression.
QString fieldName() const
Returns the unescaped field name.
QString dump() const override
Returns the formatted script representation of the node.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
Represents a for ... each loop.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
QString variableName() const
Returns the loop variable identifier name.
QgsVbScriptBlockStatement * body() const
Returns the loop body block statement.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptForEachStatement(const QString &variableName, QgsVbScriptExpression *collection, QgsVbScriptBlockStatement *body)
Constructor for QgsVbScriptForEachStatement.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptExpression * collection() const
Returns the collection expression being iterated over.
Represents a standard counter-based for loop.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptExpression * startExpression() const
Returns the initial counter expression.
QgsVbScriptExpression * stepExpression() const
Returns the step increment expression, or nullptr if default.
QgsVbScriptBlockStatement * body() const
Returns the loop body block statement.
QgsVbScriptForStatement(const QString &variableName, QgsVbScriptExpression *startExpression, QgsVbScriptExpression *endExpression, QgsVbScriptExpression *stepExpression, QgsVbScriptBlockStatement *body)
Constructor for QgsVbScriptForStatement.
QgsVbScriptExpression * endExpression() const
Returns the terminal counter expression.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
QString variableName() const
Returns the loop variable identifier name.
Represents a function call expression.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptFunctionCallExpression(QgsVbScriptExpression *targetExpression, const QList< QgsVbScriptExpression * > &arguments)
Constructor for QgsVbScriptFunctionCallExpression.
QList< QgsVbScriptExpression * > arguments() const
Returns the list of argument expressions.
QgsVbScriptExpression * targetExpression() const
Returns the callee expression.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
Represents a function declaration statement.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
QStringList parameters() const
Returns the parameter names.
QgsVbScriptBlockStatement * body() const
Returns the function body block.
QString functionName() const
Returns the function name.
QgsVbScriptFunctionDeclarationStatement(const QString &functionName, const QStringList &parameters, QgsVbScriptBlockStatement *body, bool isSub=false)
Constructor for QgsVbScriptFunctionDeclarationStatement.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
bool isSub() const
Returns true if this is a Sub procedure rather than a Function.
QString dump() const override
Returns the formatted script representation of the node.
Represents an if statement.
QgsVbScriptBlockStatement * falseBlock() const
Returns the statement block executed when the condition evaluates to false, or nullptr if none.
QgsVbScriptIfStatement(QgsVbScriptExpression *condition, QgsVbScriptBlockStatement *trueBlock, QgsVbScriptBlockStatement *falseBlock=nullptr, bool isInline=false)
Constructor for QgsVbScriptIfStatement.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptExpression * condition() const
Returns the condition expression.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
bool isInline() const
Returns true if this statement was declared as a single-line inline If.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptBlockStatement * trueBlock() const
Returns the statement block executed when the condition evaluates to true.
Represents an array or dictionary index subscript expression.
QgsVbScriptIndexExpression(QgsVbScriptExpression *containerExpression, QgsVbScriptExpression *indexExpression)
Constructor for QgsVbScriptIndexExpression.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptExpression * indexExpression() const
Returns the index expression.
QgsVbScriptExpression * containerExpression() const
Returns the container expression being indexed.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
Represents a literal value (number, text string, boolean, or null).
QVariant value() const
Returns the literal value.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptLiteralExpression(const QVariant &value)
Constructor for QgsVbScriptLiteralExpression.
QString valueAsString() const
Returns a string representation of the literal value.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
Represents a member access expression using bracket notation.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
QgsVbScriptExpression * containerExpression() const
Returns the container expression being accessed.
QgsVbScriptMemberAccessExpression(QgsVbScriptExpression *containerExpression, const QString &memberName)
Constructor for QgsVbScriptMemberAccessExpression.
QString memberName() const
Returns the member attribute name.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QString dump() const override
Returns the formatted script representation of the node.
Represents an abstract node for a VBScript script.
Definition qgsvbscript.h:58
@ IfStatement
If statement.
virtual void accept(QgsVbScriptVisitor &visitor) const =0
Accepts a visitor to traverse the node.
void visit(const QgsVbScriptBlockStatement &node) override
Visit a QgsVbScriptBlockStatement node.
Base class for statement nodes in a VBScript script.
Represents a unary operation expression.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QString dump() const override
Returns the formatted script representation of the node.
QgsVbScriptUnaryOperatorExpression(UnaryOperator unaryOperator, QgsVbScriptExpression *operand)
Constructor for QgsVbScriptUnaryOperatorExpression.
UnaryOperator
Unary operator enumeration.
QgsVbScriptExpression * operand() const
Returns the operand expression.
QString text() const
Returns text representation of the operator.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
UnaryOperator unaryOperator() const
Returns the unary operator.
static const char * UNARY_OPERATOR_TEXT[]
Definition qgsvbscript.h:33
Represents a variable declaration statement.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
QString dump() const override
Returns the formatted script representation of the node.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QStringList variableNames() const
Returns the declared variable names.
QgsVbScriptVariableDeclarationStatement(const QString &variableName)
Constructor for QgsVbScriptVariableDeclarationStatement.
Represents a reference to a variable identifier.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
QString dump() const override
Returns the formatted script representation of the node.
QString name() const
Returns the variable identifier name.
QgsVbScriptVariableReferenceExpression(const QString &name)
Constructor for QgsVbScriptVariableReferenceExpression.
Abstract visitor interface for traversing VBScript abstract syntax trees.
virtual void visit(const QgsVbScriptBlockStatement &node)=0
Visit a QgsVbScriptBlockStatement node.
Represents a while loop statement.
LoopType loopType() const
Returns the loop variant type.
QString dump() const override
Returns the formatted script representation of the node.
LoopType
Type of while or do loop.
QgsVbScriptExpression * condition() const
Returns the loop condition expression.
QgsVbScriptBlockStatement * body() const
Returns the loop body block statement.
QgsVbScriptNode * clone() const override
Creates a copy of the node.
void accept(QgsVbScriptVisitor &visitor) const override
Accepts a visitor to traverse the node.
QgsVbScriptWhileStatement(LoopType loopType, QgsVbScriptExpression *condition, QgsVbScriptBlockStatement *body)
Constructor for QgsVbScriptWhileStatement.
static QString extractFieldReference(const QString &text)
Strips enclosing square brackets from text and unescapes double right brackets.
QgsVbScript & operator=(const QgsVbScript &other)
QString mScript
QString script() const
Returns the original script text.
std::unique_ptr< QgsVbScriptBlockStatement > mRootBlock
void acceptVisitor(QgsVbScriptVisitor &visitor) const
Accepts a visitor to traverse the script abstract syntax tree.
static QString quotedFieldReference(const QString &fieldName)
Returns a field name enclosed in square brackets with internal right brackets escaped.
QString parserErrorString() const
Returns parser error description.
const QgsVbScriptBlockStatement * rootBlock() const
Returns the root block statement node, or nullptr if parsing failed.
QString mParserErrorString
bool hasParserError() const
Returns true if a parsing error occurred.
static QString quotedString(const QString &text)
Returns a properly quoted string literal (enclosed in double quotes with escape sequences).
QString dump() const
Dumps the abstract syntax tree back into an Arcade script string.
QgsVbScript(const QString &scriptText=QString())
Creates a new script from scriptText.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:7718
QgsVbScriptBlockStatement * parseVbScript(const QString &scriptText, QString &parserErrorMessage)