axmol/extensions/Particle3D/PU/CCPUScriptParser.cpp

533 lines
18 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
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.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PUScriptParser::PUScriptParser() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PUScriptParser::~PUScriptParser() {}
void traceScriptParserCell(PUConcreteNodeList& nodes, int level)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (const auto& node : nodes)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
printf("%s,##%d\n", node->token.c_str(), level);
if (node->children.size() != 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
traceScriptParserCell(node->children, level + 1);
2019-11-23 20:27:39 +08:00
}
}
}
void traceScriptParser(PUConcreteNodeList& nodes)
{
2021-12-25 10:04:45 +08:00
traceScriptParserCell(nodes, 1);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void PUScriptParser::parse(PUConcreteNodeList& nodes, const PUScriptTokenList& tokens)
2019-11-23 20:27:39 +08:00
{
// MEMCATEGORY_GENERAL because SharedPtr can only free using that category
2021-12-25 10:04:45 +08:00
enum
{
READY,
OBJECT
};
2019-11-23 20:27:39 +08:00
unsigned int state = READY;
2021-12-25 10:04:45 +08:00
PUConcreteNode* parent = 0;
2019-11-23 20:27:39 +08:00
PUConcreteNode* node;
2021-12-25 10:04:45 +08:00
PUScriptToken* token = 0;
2019-11-23 20:27:39 +08:00
PUScriptTokenList::const_iterator i = tokens.begin(), end = tokens.end();
2021-12-25 10:04:45 +08:00
// int kkkk = 0;
while (i != end)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// kkkk ++;
2019-11-23 20:27:39 +08:00
token = (*i);
2021-12-25 10:04:45 +08:00
switch (state)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case READY:
if (token->type == TID_WORD)
{
if (token->lexeme == "import")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
node = new 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))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
printf("Except,expected import target at line :%d,ScriptParser::parse", node->line);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
PUConcreteNode* temp = new 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);
2019-11-23 20:27:39 +08:00
else
2021-12-25 10:04:45 +08:00
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))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
printf("expected import source at line :%d,ScriptParser::parse", node->line);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
temp = new 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);
2019-11-23 20:27:39 +08:00
// Consume all the newlines
i = skipNewlines(i, end);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Insert the node
2021-12-25 10:04:45 +08:00
if (parent)
2019-11-23 20:27:39 +08:00
{
node->parent = parent;
parent->children.push_back(node);
}
else
{
node->parent = 0;
nodes.push_back(node);
}
node = nullptr;
}
2021-12-25 10:04:45 +08:00
else if (token->lexeme == "set")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
node = new PUConcreteNode;
2019-11-23 20:27:39 +08:00
node->token = token->lexeme;
2021-12-25 10:04:45 +08:00
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");
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
PUConcreteNode* temp = new 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))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
printf("expected variable value at line %d ScriptParser::parse\n", node->line);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
temp = new 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);
2019-11-23 20:27:39 +08:00
// Insert the node
2021-12-25 10:04:45 +08:00
if (parent)
2019-11-23 20:27:39 +08:00
{
node->parent = parent;
parent->children.push_back(node);
}
else
{
node->parent = 0;
nodes.push_back(node);
}
node = nullptr;
}
2021-12-25 10:04:45 +08:00
else
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
node = new PUConcreteNode();
2019-11-23 20:27:39 +08:00
node->file = token->file;
node->line = token->line;
2021-12-25 10:04:45 +08:00
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;
2019-11-23 20:27:39 +08:00
// Insert the node
2021-12-25 10:04:45 +08:00
if (parent)
2019-11-23 20:27:39 +08:00
{
node->parent = parent;
parent->children.push_back(node);
}
else
{
node->parent = 0;
nodes.push_back(node);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Set the parent
parent = node;
2021-12-25 10:04:45 +08:00
// Switch states
state = OBJECT;
2019-11-23 20:27:39 +08:00
node = nullptr;
}
2021-12-25 10:04:45 +08:00
}
else if (token->type == TID_RBRACKET)
{
// Go up one level if we can
if (parent)
parent = parent->parent;
node = new 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)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
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)
2019-11-23 20:27:39 +08:00
parent = parent->parent;
state = READY;
}
2021-12-25 10:04:45 +08:00
}
else if (token->type == TID_COLON)
{
node = new 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))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
printf("expected object identifier at line %d ScriptParser::parse\n", node->line);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
while (j != end && ((*j)->type == TID_WORD || (*j)->type == TID_QUOTE))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PUConcreteNode* tempNode = new 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;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// Move it backwards once, since the end of the loop moves it forwards again anyway
--j;
i = j;
// Insert the node
if (parent)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
node->parent = parent;
parent->children.push_back(node);
}
else
{
node->parent = 0;
nodes.push_back(node);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
node = nullptr;
}
else if (token->type == TID_LBRACKET)
{
node = new 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 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 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 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 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;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
++i;
}
2021-12-25 10:04:45 +08:00
// traceScriptParser(nodes);//
// printf("kkkk:%d\n",kkkk);//
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void PUScriptParser::parseChunk(PUConcreteNodeList& nodes, const PUScriptTokenList& tokens)
2019-11-23 20:27:39 +08:00
{
PUConcreteNode* node = nullptr;
2021-12-25 10:04:45 +08:00
PUScriptToken* token = 0;
for (PUScriptTokenList::const_iterator i = tokens.begin(); i != tokens.end(); ++i)
2019-11-23 20:27:39 +08:00
{
token = *i;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
node = nullptr;
2021-12-25 10:04:45 +08:00
switch (token->type)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case TID_VARIABLE:
node = new 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 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 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);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
if (node != nullptr)
2019-11-23 20:27:39 +08:00
nodes.push_back(node);
}
}
2021-12-25 10:04:45 +08:00
PUScriptToken* PUScriptParser::getToken(PUScriptTokenList::iterator i, PUScriptTokenList::iterator end, int offset)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
PUScriptToken* token = 0;
2019-11-23 20:27:39 +08:00
PUScriptTokenList::iterator iter = i + offset;
2021-12-25 10:04:45 +08:00
if (iter != end)
2019-11-23 20:27:39 +08:00
token = (*i);
return token;
}
2021-12-25 10:04:45 +08:00
PUScriptTokenList::const_iterator PUScriptParser::skipNewlines(PUScriptTokenList::const_iterator i,
PUScriptTokenList::const_iterator end)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
while (i != end && (*i)->type == TID_NEWLINE)
2019-11-23 20:27:39 +08:00
++i;
return i;
}
PUConcreteNode::~PUConcreteNode()
{
2021-12-25 10:04:45 +08:00
for (auto iter : children)
{
2019-11-23 20:27:39 +08:00
delete iter;
}
}
NS_AX_END