| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- //
- // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #pragma once
- #include "JSAST.h"
- namespace AtomicEditor
- {
- class JSASTVisitor
- {
- public:
- virtual JSASTProgram* visit(JSASTProgram *program) = 0;
- virtual JSASTStatement* visit(JSASTBlockStatement *stmt) = 0;
- virtual JSASTStatement* visit(JSASTExpressionStatement *stmt) = 0;
- virtual JSASTStatement* visit(JSASTIfStatement *stmt) = 0;
- virtual JSASTStatement* visit(JSASTFunctionDeclaration *stmt) = 0;
- virtual JSASTStatement* visit(JSASTVariableDeclaration *stmt) = 0;
- virtual JSASTStatement* visit(JSASTReturnStatement *stmt) = 0;
- virtual JSASTStatement* visit(JSASTEmptyStatement *stmt) = 0;
- virtual JSASTStatement* visit(JSASTForStatement *stmt) = 0;
- virtual JSASTExpression* visit(JSASTAssignmentExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTLogicalExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTConditionalExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTThisExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTIdentifier* expr) = 0;
- virtual JSASTExpression* visit(JSASTCallExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTBinaryExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTMemberExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTLiteral* expr) = 0;
- virtual JSASTExpression* visit(JSASTArrayExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTObjectExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTFunctionExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTNewExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTUnaryExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTUpdateExpression* expr) = 0;
- virtual JSASTExpression* visit(JSASTVariableDeclarator* expr) = 0;
- virtual JSASTProperty* visit(JSASTProperty* property) = 0;
- virtual JSASTStatement* visit(JSASTLabeledStatement* property) = 0;
- virtual JSASTComment* visit(JSASTComment* expr) = 0;
- };
- class JSASTTraversalVisitor : public JSASTVisitor
- {
- public:
- virtual JSASTProgram* visit(JSASTProgram *program)
- {
- // this would cause recursion
- // program->Accept(this);
- unsigned count = program->GetCommentCount();
- for (unsigned i = 0; i < count; i++)
- {
- JSASTComment* cmt = program->GetComment(i);
- if (cmt)
- cmt->Accept(this);
- }
- count = program->GetStatementCount();
- for (unsigned i = 0; i < count; i++)
- {
- JSASTStatement* stmt = program->GetStatement(i);
- if (stmt)
- stmt->Accept(this);
- }
- return program;
- }
- virtual JSASTStatement* visit(JSASTForStatement *stmt)
- {
- if (stmt->GetInitVariable())
- stmt->GetInitVariable()->Accept(this);
- if (stmt->GetInitExpression())
- stmt->GetInitExpression()->Accept(this);
- if (stmt->GetTest())
- stmt->GetTest()->Accept(this);
- if (stmt->GetUpdate())
- stmt->GetUpdate()->Accept(this);
- if (stmt->GetBody())
- stmt->GetBody()->Accept(this);
- return stmt;
- }
- virtual JSASTStatement* visit(JSASTBlockStatement *block)
- {
- unsigned count = block->GetStatementCount();
- for (unsigned i = 0; i < count; i++)
- {
- JSASTStatement* stmt = block->GetStatement(i);
- if (stmt)
- stmt->Accept(this);
- }
- return block;
- }
- virtual JSASTStatement* visit(JSASTExpressionStatement *stmt)
- {
- if (stmt->GetExpression())
- stmt->GetExpression()->Accept(this);
- return stmt;
- }
- virtual JSASTStatement* visit(JSASTIfStatement *stmt)
- {
- if (stmt->GetTest())
- stmt->GetTest()->Accept(this);
- if (stmt->GetConsequent())
- stmt->GetConsequent()->Accept(this);
- if (stmt->GetAlternate())
- stmt->GetAlternate()->Accept(this);
- return stmt;
- }
- virtual JSASTStatement* visit(JSASTFunctionDeclaration *stmt)
- {
- if (stmt->GetID())
- stmt->GetID()->Accept(this);
- for (unsigned i = 0; i < stmt->GetParamsCount(); i++)
- stmt->GetParam(i)->Accept(this);
- JSASTBlockStatement* body = stmt->GetBodyStatement();
- if (body)
- body->Accept(this);
- return stmt;
- }
- virtual JSASTStatement* visit(JSASTVariableDeclaration *stmt)
- {
- for (unsigned i = 0; i < stmt->GetDeclarationsCount(); i++)
- stmt->GetDeclaration(i)->Accept(this);
- return stmt;
- }
- virtual JSASTStatement* visit(JSASTReturnStatement *stmt)
- {
- if (stmt->GetArgument())
- stmt->GetArgument()->Accept(this);
- return stmt;
- }
- virtual JSASTStatement* visit(JSASTEmptyStatement *stmt)
- {
- return stmt;
- }
- virtual JSASTExpression* visit(JSASTAssignmentExpression* expr)
- {
- if (expr->GetLeft())
- expr->GetLeft()->Accept(this);
- if (expr->GetRight())
- expr->GetRight()->Accept(this);
- return expr;
- }
- virtual JSASTExpression* visit(JSASTConditionalExpression *expr)
- {
- if (expr->GetTest())
- expr->GetTest()->Accept(this);
- if (expr->GetConsequent())
- expr->GetConsequent()->Accept(this);
- if (expr->GetAlternate())
- expr->GetAlternate()->Accept(this);
- return expr;
- }
- virtual JSASTExpression* visit(JSASTLogicalExpression* expr)
- {
- if (expr->GetLeft())
- expr->GetLeft()->Accept(this);
- if (expr->GetRight())
- expr->GetRight()->Accept(this);
- return expr;
- }
- virtual JSASTExpression* visit(JSASTIdentifier* expr)
- {
- return expr;
- }
- virtual JSASTExpression* visit(JSASTCallExpression* expr)
- {
- if (expr->GetCallee())
- expr->GetCallee()->Accept(this);
- for (unsigned i = 0; i < expr->GetArgumentCount(); i++)
- { if (expr->GetArgument(i))
- expr->GetArgument(i)->Accept(this);
- }
- return expr;
- }
- virtual JSASTExpression* visit(JSASTBinaryExpression* expr)
- {
- if (expr->GetLeft())
- expr->GetLeft()->Accept(this);
- if (expr->GetRight())
- expr->GetRight()->Accept(this);
- return expr;
- }
- virtual JSASTExpression* visit(JSASTMemberExpression* expr)
- {
- if (expr->GetObject())
- expr->GetObject()->Accept(this);
- if (expr->GetProperty())
- expr->GetProperty()->Accept(this);
- return expr;
- }
- virtual JSASTExpression* visit(JSASTLiteral* expr)
- {
- return expr;
- }
- virtual JSASTExpression* visit(JSASTArrayExpression* expr)
- {
- for (unsigned i = 0; i < expr->GetElementCount(); i++)
- {
- JSASTExpression* arrayElement = expr->GetElement(i);
- if (arrayElement)
- arrayElement->Accept(this);
- }
- return expr;
- }
- virtual JSASTExpression* visit(JSASTObjectExpression* expr)
- {
- for (unsigned i = 0; i < expr->GetPropertyCount(); i++)
- {
- JSASTProperty* property = expr->GetProperty(i);
- if (property)
- property->Accept(this);
- }
- return expr;
- }
- virtual JSASTExpression* visit(JSASTFunctionExpression* expr)
- {
- if (expr->GetID())
- expr->GetID()->Accept(this);
- for (unsigned i = 0; i < expr->GetParamsCount(); i++)
- expr->GetParam(i)->Accept(this);
- JSASTBlockStatement* body = expr->GetBodyStatement();
- if (body)
- body->Accept(this);
- return expr;
- }
- virtual JSASTExpression* visit(JSASTNewExpression* expr)
- {
- if (expr->GetCallee())
- expr->GetCallee()->Accept(this);
- for (unsigned i = 0; i < expr->GetArgumentCount(); i++)
- {
- if (expr->GetArgument(i))
- expr->GetArgument(i)->Accept(this);
- }
- return expr;
- }
- virtual JSASTExpression* visit(JSASTUnaryExpression* expr)
- {
- if (expr->GetArgument())
- expr->GetArgument()->Accept(this);
- return expr;
- }
- virtual JSASTExpression* visit(JSASTUpdateExpression* expr)
- {
- if (expr->GetArgument())
- expr->GetArgument()->Accept(this);
- return expr;
- }
- virtual JSASTExpression* visit(JSASTVariableDeclarator* expr)
- {
- if (expr->GetID())
- expr->GetID()->Accept(this);
- if (expr->GetInit())
- expr->GetInit()->Accept(this);
- return expr;
- }
- virtual JSASTComment* visit(JSASTComment* comment)
- {
- return comment;
- }
- virtual JSASTProperty* visit(JSASTProperty* property)
- {
- if (property->GetKey())
- property->GetKey()->Accept(this);
- if (property->GetValue())
- property->GetValue()->Accept(this);
- return property;
- }
- virtual JSASTStatement* visit(JSASTLabeledStatement* labeledStatement)
- {
- if (labeledStatement->GetLabel())
- labeledStatement->GetLabel()->Accept(this);
- if (labeledStatement->GetBody())
- labeledStatement->GetBody()->Accept(this);
- return labeledStatement;
- }
- virtual JSASTExpression* visit(JSASTThisExpression* expr)
- {
- return expr;
- }
- };
- }
|