123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- /****************************************************************************
- Copyright (C) 2013 Henry van Merode. All rights reserved.
- Copyright (c) 2015-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
-
- http://www.cocos2d-x.org
-
- 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.
- ****************************************************************************/
- #include "CCPUScriptParser.h"
- NS_CC_BEGIN
- PUScriptParser::PUScriptParser()
- {
-
- }
- PUScriptParser::~PUScriptParser()
- {
-
- }
- void traceScriptParserCell(PUConcreteNodeList& nodes,int level)
- {
- for(const auto& node : nodes)
- {
- printf("%s,##%d\n",node->token.c_str(),level);
- if(node->children.size() != 0)
- {
- traceScriptParserCell(node->children,level+1);
- }
- }
- }
- void traceScriptParser(PUConcreteNodeList& nodes)
- {
-
- traceScriptParserCell(nodes,1);
-
- }
- void PUScriptParser::parse(PUConcreteNodeList& nodes,const PUScriptTokenList& tokens)
- {
- // MEMCATEGORY_GENERAL because SharedPtr can only free using that category
-
- enum{READY, OBJECT};
- unsigned int state = READY;
-
- PUConcreteNode *parent = 0;
- PUConcreteNode* node;
- PUScriptToken *token = 0;
- PUScriptTokenList::const_iterator i = tokens.begin(), end = tokens.end();
- // int kkkk = 0;
- while(i != end)
- {
-
- // kkkk ++;
- token = (*i);
-
- switch(state)
- {
- case READY:
- if(token->type == TID_WORD)
- {
- if(token->lexeme == "import")
- {
- node = new (std::nothrow) PUConcreteNode;
-
- node->token = token->lexeme;
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_IMPORT;
-
- // The next token is the target
- ++i;
- if(i == end || ((*i)->type != TID_WORD && (*i)->type != TID_QUOTE))
- {
- printf("Except,expected import target at line :%d,ScriptParser::parse",node->line);
- }
- PUConcreteNode* temp = new (std::nothrow) PUConcreteNode;
- temp->parent = node;
- temp->file = (*i)->file;
- temp->line = (*i)->line;
- temp->type = (*i)->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
- if(temp->type == CNT_QUOTE)
- temp->token = (*i)->lexeme.substr(1, token->lexeme.size() - 2);
- else
- temp->token = (*i)->lexeme;
- node->children.push_back(temp);
-
- // The second-next token is the source
- ++i;
- ++i;
- if(i == end || ((*i)->type != TID_WORD && (*i)->type != TID_QUOTE))
- {
- printf("expected import source at line :%d,ScriptParser::parse",node->line);
- }
- temp = new (std::nothrow) PUConcreteNode;
- temp->parent = node;
- temp->file = (*i)->file;
- temp->line = (*i)->line;
- temp->type = (*i)->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
- if(temp->type == CNT_QUOTE)
- temp->token = (*i)->lexeme.substr(1, (*i)->lexeme.size() - 2);
- else
- temp->token = (*i)->lexeme;
-
- node->children.push_back(temp);
-
- // Consume all the newlines
- i = skipNewlines(i, end);
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
- node = nullptr;
- }
- else if(token->lexeme == "set")
- {
- node = new (std::nothrow) PUConcreteNode;
- node->token = token->lexeme;
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_VARIABLE_ASSIGN;
-
- // The next token is the variable
- ++i;
- if(i == end || (*i)->type != TID_VARIABLE)
- {
- printf("Exception");
- }
- PUConcreteNode* temp = new (std::nothrow) PUConcreteNode;
- temp->parent = node;
- temp->file = (*i)->file;
- temp->line = (*i)->line;
- temp->type = CNT_VARIABLE;
- temp->token = (*i)->lexeme;
- node->children.push_back(temp);
-
- // The next token is the assignment
- ++i;
- if(i == end || ((*i)->type != TID_WORD && (*i)->type != TID_QUOTE))
- {
- printf("expected variable value at line %d ScriptParser::parse\n",node->line);
- }
- temp = new (std::nothrow) PUConcreteNode;
- temp->parent = node;
- temp->file = (*i)->file;
- temp->line = (*i)->line;
- temp->type = (*i)->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
- if(temp->type == CNT_QUOTE)
- temp->token = (*i)->lexeme.substr(1, (*i)->lexeme.size() - 2);
- else
- temp->token = (*i)->lexeme;
- node->children.push_back(temp);
-
- // Consume all the newlines
- i = skipNewlines(i, end);
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
- node = nullptr;
- }
- else
- {
- node = new (std::nothrow) PUConcreteNode();
- node->file = token->file;
- node->line = token->line;
- node->type = token->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
- if(node->type == CNT_QUOTE)
- node->token = token->lexeme.substr(1, token->lexeme.size() - 2);
- else
- node->token = token->lexeme;
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
-
- // Set the parent
- parent = node;
-
- // Switch states
- state = OBJECT;
-
- node = nullptr;
- }
- }
- else if(token->type == TID_RBRACKET)
- {
- // Go up one level if we can
- if(parent)
- parent = parent->parent;
-
- node = new (std::nothrow) PUConcreteNode();
- node->token = token->lexeme;
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_RBRACE;
-
- // Consume all the newlines
- i = skipNewlines(i, end);
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
-
- // Move up another level
- if(parent)
- parent = parent->parent;
-
- node = nullptr;
- }
- break;
- case OBJECT:
- if(token->type == TID_NEWLINE)
- {
- // Look ahead to the next non-newline token and if it isn't an {, this was a property
- PUScriptTokenList::const_iterator next = skipNewlines(i, end);
- if(next == end || (*next)->type != TID_LBRACKET)
- {
- // Ended a property here
- if(parent)
- parent = parent->parent;
- state = READY;
- }
- }
- else if(token->type == TID_COLON)
- {
- node = new (std::nothrow) PUConcreteNode();
- node->token = token->lexeme;
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_COLON;
-
- // The following token are the parent objects (base classes).
- // Require at least one of them.
-
- PUScriptTokenList::const_iterator j = i + 1;
- j = skipNewlines(j, end);
- if(j == end || ((*j)->type != TID_WORD && (*j)->type != TID_QUOTE)) {
-
- printf("expected object identifier at line %d ScriptParser::parse\n",node->line);
- }
-
- while(j != end && ((*j)->type == TID_WORD || (*j)->type == TID_QUOTE))
- {
- PUConcreteNode* tempNode = new (std::nothrow) PUConcreteNode;
- tempNode->token = (*j)->lexeme;
- tempNode->file = (*j)->file;
- tempNode->line = (*j)->line;
- tempNode->type = (*j)->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
- tempNode->parent = node;
- node->children.push_back(tempNode);
- ++j;
- }
-
- // Move it backwards once, since the end of the loop moves it forwards again anyway
- --j;
- i = j;
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
- node = nullptr;
- }
- else if(token->type == TID_LBRACKET)
- {
- node = new (std::nothrow) PUConcreteNode;
- node->token = token->lexeme;
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_LBRACE;
-
- // Consume all the newlines
- i = skipNewlines(i, end);
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
-
- // Set the parent
- parent = node;
-
- // Change the state
- state = READY;
-
- node = nullptr;
- }
- else if(token->type == TID_RBRACKET)
- {
- // Go up one level if we can
- if(parent)
- parent = parent->parent;
-
- // If the parent is currently a { then go up again
- if(parent && parent->type == CNT_LBRACE && parent->parent)
- parent = parent->parent;
-
- node = new (std::nothrow) PUConcreteNode;
- node->token = token->lexeme;
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_RBRACE;
-
- // Consume all the newlines
- i = skipNewlines(i, end);
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
-
- // Move up another level
- if(parent)
- parent = parent->parent;
-
- node = nullptr;
- state = READY;
- }
- else if(token->type == TID_VARIABLE)
- {
- node = new (std::nothrow) PUConcreteNode;
- node->token = token->lexeme;
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_VARIABLE;
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
- node = nullptr;
- }
- else if(token->type == TID_QUOTE)
- {
- node = new (std::nothrow) PUConcreteNode;
- node->token = token->lexeme.substr(1, token->lexeme.size() - 2);
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_QUOTE;
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
- node = nullptr;
- }
- else if(token->type == TID_WORD)
- {
- node = new (std::nothrow) PUConcreteNode;
- node->token = token->lexeme;
- node->file = token->file;
- node->line = token->line;
- node->type = CNT_WORD;
-
- // Insert the node
- if(parent)
- {
- node->parent = parent;
- parent->children.push_back(node);
- }
- else
- {
- node->parent = 0;
- nodes.push_back(node);
- }
- node = nullptr;
- }
- break;
- }
-
- ++i;
- }
-
-
- // traceScriptParser(nodes);//
- // printf("kkkk:%d\n",kkkk);//
-
-
- }
- void PUScriptParser::parseChunk(PUConcreteNodeList& nodes, const PUScriptTokenList &tokens)
- {
- PUConcreteNode* node = nullptr;
- PUScriptToken *token = 0;
- for(PUScriptTokenList::const_iterator i = tokens.begin(); i != tokens.end(); ++i)
- {
- token = *i;
-
- node = nullptr;
- switch(token->type)
- {
- case TID_VARIABLE:
- node = new (std::nothrow) PUConcreteNode;
- node->file = token->file;
- node->line = token->line;
- node->parent = 0;
- node->token = token->lexeme;
- node->type = CNT_VARIABLE;
- break;
- case TID_WORD:
- node = new (std::nothrow) PUConcreteNode;
- node->file = token->file;
- node->line = token->line;
- node->parent = 0;
- node->token = token->lexeme;
- node->type = CNT_WORD;
- break;
- case TID_QUOTE:
- node = new (std::nothrow) PUConcreteNode;
- node->file = token->file;
- node->line = token->line;
- node->parent = 0;
- node->token = token->lexeme.substr(1, token->lexeme.size() - 2);
- node->type = CNT_QUOTE;
- default:
- printf("unexpected token,%s,%d\n",token->lexeme.c_str(),token->line);
- }
-
- if(node != nullptr)
- nodes.push_back(node);
- }
- }
- PUScriptToken *PUScriptParser::getToken(PUScriptTokenList::iterator i, PUScriptTokenList::iterator end, int offset)
- {
- PUScriptToken *token = 0;
- PUScriptTokenList::iterator iter = i + offset;
- if(iter != end)
- token = (*i);
- return token;
- }
- PUScriptTokenList::const_iterator PUScriptParser::skipNewlines(PUScriptTokenList::const_iterator i, PUScriptTokenList::const_iterator end)
- {
- while(i != end && (*i)->type == TID_NEWLINE)
- ++i;
- return i;
- }
- PUConcreteNode::~PUConcreteNode()
- {
- for (auto iter : children){
- delete iter;
- }
- }
- NS_CC_END
|