23#include <QRegularExpression>
26using namespace Qt::StringLiterals;
31const char *
QgsVbScriptBinaryOperatorExpression::BINARY_OPERATOR_TEXT[] = {
"+",
"-",
"*",
"/",
"\\",
"Mod",
"^",
"&",
"=",
"<>",
"<",
">",
"<=",
">=",
"Is",
"And",
"Or",
"Xor",
"Eqv",
"Imp" };
45 qDeleteAll( mStatements );
50 mStatements.append( statement );
56 parts.reserve( mStatements.size() );
59 parts << statement->dump();
61 return parts.join(
'\n' );
66 QList<QgsVbScriptStatement *> clonedList;
67 clonedList.reserve( mStatements.size() );
77 visitor.
visit( *
this );
85 : mVariableNames( { variableName } )
94 return u
"Dim %1"_s.arg( mVariableNames.join(
", "_L1 ) );
104 visitor.
visit( *
this );
119 return u
"%1%2 = %3"_s.arg( mIsSet ?
"Set "_L1 :
""_L1, mTarget ? mTarget->dump() : QString(), mValue ? mValue->dump() : QString() );
131 visitor.
visit( *
this );
147 const QString bodyDump = mBody ? mBody->dump() : QString();
148 const QString keyword = mIsSub ? u
"Sub"_s : u
"Function"_s;
150 return u
"%1 %2(%3)\n%4\nEnd %1"_s.arg( keyword, mFunctionName, mParameters.join(
", "_L1 ), bodyDump );
160 visitor.
visit( *
this );
173 return mExpression ? mExpression->dump() : QString();
183 visitor.
visit( *
this );
199 switch ( mValue.userType() )
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:
207 case QMetaType::Type::QString:
208 return mValue.toString();
209 case QMetaType::Type::Bool:
210 return mValue.toBool() ? u
"True"_s : u
"False"_s;
212 return mValue.toString();
221 if ( mValue.userType() == QMetaType::Type::QString )
234 visitor.
visit( *
this );
257 visitor.
visit( *
this );
271 return u
"%1.%2"_s.arg( mContainerExpression ? mContainerExpression->dump() : QString(), mMemberName );
282 visitor.
visit( *
this );
302 switch ( mBinaryOperator )
350 const QString leftDump = leftBinary && ( leftBinary->
precedence() <
precedence() ) ? u
"(%1)"_s.arg( mLeftOperand->dump() ) : mLeftOperand->
dump();
352 const QString rightDump = rightBinary && ( rightBinary->
precedence() <=
precedence() ) ? u
"(%1)"_s.arg( mRightOperand->dump() ) : mRightOperand->
dump();
354 return u
"%1 %2 %3"_s.arg( leftDump,
text(), rightDump );
366 visitor.
visit( *
this );
386 return u
"Not %1"_s.arg( mOperand ? mOperand->dump() : QString() );
388 return u
"%1%2"_s.arg(
text(), mOperand ? mOperand->dump() : QString() );
398 visitor.
visit( *
this );
412 qDeleteAll( mArguments );
417 QStringList argumentStrings;
418 argumentStrings.reserve( mArguments.size() );
421 argumentStrings.append( argument->dump() );
423 return u
"%1(%2)"_s.arg( mTargetExpression ? mTargetExpression->dump() : QString(), argumentStrings.join(
", "_L1 ) );
428 QList<QgsVbScriptExpression *> clonedArguments;
429 clonedArguments.reserve( mArguments.size() );
439 visitor.
visit( *
this );
453 return u
"%1[%2]"_s.arg( mContainerExpression ? mContainerExpression->dump() : QString(), mIndexExpression ? mIndexExpression->dump() : QString() );
465 visitor.
visit( *
this );
478 qDeleteAll( mElements );
483 QStringList elementStrings;
484 elementStrings.reserve( mElements.size() );
487 elementStrings.append( element->dump() );
489 return u
"Array(%1)"_s.arg( elementStrings.join(
", "_L1 ) );
494 QList<QgsVbScriptExpression *> clonedElements;
495 clonedElements.reserve( mElements.size() );
505 visitor.
visit( *
this );
524 QString result = u
"If %1 Then %2"_s.arg( mCondition ? mCondition->dump() : QString(), mTrueBlock ? mTrueBlock->dump() : QString() );
526 result += u
" Else %1"_s.arg( mFalseBlock->dump() );
530 QString result = u
"If %1 Then\n%2"_s.arg( mCondition ? mCondition->dump() : QString(), mTrueBlock ? mTrueBlock->dump() : QString() );
533 if ( mFalseBlock->count() == 1 && mFalseBlock->statements().at( 0 )->nodeType() ==
NodeType::IfStatement )
536 if ( elseIfStatement && !elseIfStatement->
isInline() )
538 result += u
"\nElse%1"_s.arg( elseIfStatement->
dump() );
542 result += u
"\nElse\n%1"_s.arg( mFalseBlock->dump() );
544 result +=
"\nEnd If"_L1;
558 visitor.
visit( *
this );
578 if ( mStepExpression )
579 stepString = u
" Step %1"_s.arg( mStepExpression->dump() );
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() );
596 visitor.
visit( *
this );
611 return u
"For Each %1 In %2\n%3\nNext"_s.arg( mVariableName, mCollection ? mCollection->dump() : QString(), mBody ? mBody->dump() : QString() );
623 visitor.
visit( *
this );
638 const QString bodyDump = mBody ? mBody->dump() : QString();
639 const QString conditionDump = mCondition ? mCondition->dump() : QString();
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 );
666 visitor.
visit( *
this );
685 return u
"Exit For"_s;
687 return u
"Exit Function"_s;
689 return u
"Exit Sub"_s;
701 visitor.
visit( *
this );
724 visitor.
visit( *
this );
734 const QList<QgsVbScriptStatement *> statements = node.
statements();
737 statement->accept( *
this );
794 const QList<QgsVbScriptExpression *> arguments = node.
arguments();
797 argument->accept( *
this );
811 const QList<QgsVbScriptExpression *> elements = node.
elements();
814 element->accept( *
this );
869 if ( !scriptText.isEmpty() )
871 mRootBlock.reset( parseVbScript( scriptText, mParserErrorString ) );
881 mRootBlock.reset( dynamic_cast<QgsVbScriptBlockStatement *>( other.mRootBlock->clone() ) );
887 if (
this != &other )
944 QString escaped = text;
945 escaped.replace(
'\"'_L1,
"\"\""_L1 );
946 return u
"\"%1\""_s.arg( escaped );
951 QString escaped = fieldName;
952 escaped.replace(
']',
"]]"_L1 );
953 return u
"[%1]"_s.arg( escaped );
958 QString t = text.trimmed();
959 if ( t.startsWith(
'[' ) && t.endsWith(
']' ) && t.length() >= 2 )
961 t = t.mid( 1, t.length() - 2 );
963 else if ( t.endsWith(
']' ) )
968 t.replace(
"]]"_L1,
"]"_L1 );
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.
~QgsVbScriptArrayLiteralExpression() override
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.
@ NotEqual
Not equal test !=.
@ IntegerDivide
Integer division.
@ GreaterThanOrEqual
Greater than or equal to >=.
@ LessThanOrEqual
Less than or equal to <=.
@ Concat
String concatenation &.
@ Multiply
Multiplication *.
@ GreaterThan
Greater than >.
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[]
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.
@ ExitFunction
Exit Function.
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.
~QgsVbScriptFunctionCallExpression() override
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 ¶meters, 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.
@ 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[]
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.
@ DoLoopUntil
Do ... Loop Until.
@ DoWhile
Do While ... Loop.
@ WhileWend
While ... Wend loop.
@ DoLoopWhile
Do ... Loop While.
@ DoUntil
Do Until ... 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 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.
QgsVbScriptBlockStatement * parseVbScript(const QString &scriptText, QString &parserErrorMessage)