CCPUScriptParser.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUScriptParser.h"
  23. NS_CC_BEGIN
  24. PUScriptParser::PUScriptParser()
  25. {
  26. }
  27. PUScriptParser::~PUScriptParser()
  28. {
  29. }
  30. void traceScriptParserCell(PUConcreteNodeList& nodes,int level)
  31. {
  32. for(const auto& node : nodes)
  33. {
  34. printf("%s,##%d\n",node->token.c_str(),level);
  35. if(node->children.size() != 0)
  36. {
  37. traceScriptParserCell(node->children,level+1);
  38. }
  39. }
  40. }
  41. void traceScriptParser(PUConcreteNodeList& nodes)
  42. {
  43. traceScriptParserCell(nodes,1);
  44. }
  45. void PUScriptParser::parse(PUConcreteNodeList& nodes,const PUScriptTokenList& tokens)
  46. {
  47. // MEMCATEGORY_GENERAL because SharedPtr can only free using that category
  48. enum{READY, OBJECT};
  49. unsigned int state = READY;
  50. PUConcreteNode *parent = 0;
  51. PUConcreteNode* node;
  52. PUScriptToken *token = 0;
  53. PUScriptTokenList::const_iterator i = tokens.begin(), end = tokens.end();
  54. // int kkkk = 0;
  55. while(i != end)
  56. {
  57. // kkkk ++;
  58. token = (*i);
  59. switch(state)
  60. {
  61. case READY:
  62. if(token->type == TID_WORD)
  63. {
  64. if(token->lexeme == "import")
  65. {
  66. node = new (std::nothrow) PUConcreteNode;
  67. node->token = token->lexeme;
  68. node->file = token->file;
  69. node->line = token->line;
  70. node->type = CNT_IMPORT;
  71. // The next token is the target
  72. ++i;
  73. if(i == end || ((*i)->type != TID_WORD && (*i)->type != TID_QUOTE))
  74. {
  75. printf("Except,expected import target at line :%d,ScriptParser::parse",node->line);
  76. }
  77. PUConcreteNode* temp = new (std::nothrow) PUConcreteNode;
  78. temp->parent = node;
  79. temp->file = (*i)->file;
  80. temp->line = (*i)->line;
  81. temp->type = (*i)->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
  82. if(temp->type == CNT_QUOTE)
  83. temp->token = (*i)->lexeme.substr(1, token->lexeme.size() - 2);
  84. else
  85. temp->token = (*i)->lexeme;
  86. node->children.push_back(temp);
  87. // The second-next token is the source
  88. ++i;
  89. ++i;
  90. if(i == end || ((*i)->type != TID_WORD && (*i)->type != TID_QUOTE))
  91. {
  92. printf("expected import source at line :%d,ScriptParser::parse",node->line);
  93. }
  94. temp = new (std::nothrow) PUConcreteNode;
  95. temp->parent = node;
  96. temp->file = (*i)->file;
  97. temp->line = (*i)->line;
  98. temp->type = (*i)->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
  99. if(temp->type == CNT_QUOTE)
  100. temp->token = (*i)->lexeme.substr(1, (*i)->lexeme.size() - 2);
  101. else
  102. temp->token = (*i)->lexeme;
  103. node->children.push_back(temp);
  104. // Consume all the newlines
  105. i = skipNewlines(i, end);
  106. // Insert the node
  107. if(parent)
  108. {
  109. node->parent = parent;
  110. parent->children.push_back(node);
  111. }
  112. else
  113. {
  114. node->parent = 0;
  115. nodes.push_back(node);
  116. }
  117. node = nullptr;
  118. }
  119. else if(token->lexeme == "set")
  120. {
  121. node = new (std::nothrow) PUConcreteNode;
  122. node->token = token->lexeme;
  123. node->file = token->file;
  124. node->line = token->line;
  125. node->type = CNT_VARIABLE_ASSIGN;
  126. // The next token is the variable
  127. ++i;
  128. if(i == end || (*i)->type != TID_VARIABLE)
  129. {
  130. printf("Exception");
  131. }
  132. PUConcreteNode* temp = new (std::nothrow) PUConcreteNode;
  133. temp->parent = node;
  134. temp->file = (*i)->file;
  135. temp->line = (*i)->line;
  136. temp->type = CNT_VARIABLE;
  137. temp->token = (*i)->lexeme;
  138. node->children.push_back(temp);
  139. // The next token is the assignment
  140. ++i;
  141. if(i == end || ((*i)->type != TID_WORD && (*i)->type != TID_QUOTE))
  142. {
  143. printf("expected variable value at line %d ScriptParser::parse\n",node->line);
  144. }
  145. temp = new (std::nothrow) PUConcreteNode;
  146. temp->parent = node;
  147. temp->file = (*i)->file;
  148. temp->line = (*i)->line;
  149. temp->type = (*i)->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
  150. if(temp->type == CNT_QUOTE)
  151. temp->token = (*i)->lexeme.substr(1, (*i)->lexeme.size() - 2);
  152. else
  153. temp->token = (*i)->lexeme;
  154. node->children.push_back(temp);
  155. // Consume all the newlines
  156. i = skipNewlines(i, end);
  157. // Insert the node
  158. if(parent)
  159. {
  160. node->parent = parent;
  161. parent->children.push_back(node);
  162. }
  163. else
  164. {
  165. node->parent = 0;
  166. nodes.push_back(node);
  167. }
  168. node = nullptr;
  169. }
  170. else
  171. {
  172. node = new (std::nothrow) PUConcreteNode();
  173. node->file = token->file;
  174. node->line = token->line;
  175. node->type = token->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
  176. if(node->type == CNT_QUOTE)
  177. node->token = token->lexeme.substr(1, token->lexeme.size() - 2);
  178. else
  179. node->token = token->lexeme;
  180. // Insert the node
  181. if(parent)
  182. {
  183. node->parent = parent;
  184. parent->children.push_back(node);
  185. }
  186. else
  187. {
  188. node->parent = 0;
  189. nodes.push_back(node);
  190. }
  191. // Set the parent
  192. parent = node;
  193. // Switch states
  194. state = OBJECT;
  195. node = nullptr;
  196. }
  197. }
  198. else if(token->type == TID_RBRACKET)
  199. {
  200. // Go up one level if we can
  201. if(parent)
  202. parent = parent->parent;
  203. node = new (std::nothrow) PUConcreteNode();
  204. node->token = token->lexeme;
  205. node->file = token->file;
  206. node->line = token->line;
  207. node->type = CNT_RBRACE;
  208. // Consume all the newlines
  209. i = skipNewlines(i, end);
  210. // Insert the node
  211. if(parent)
  212. {
  213. node->parent = parent;
  214. parent->children.push_back(node);
  215. }
  216. else
  217. {
  218. node->parent = 0;
  219. nodes.push_back(node);
  220. }
  221. // Move up another level
  222. if(parent)
  223. parent = parent->parent;
  224. node = nullptr;
  225. }
  226. break;
  227. case OBJECT:
  228. if(token->type == TID_NEWLINE)
  229. {
  230. // Look ahead to the next non-newline token and if it isn't an {, this was a property
  231. PUScriptTokenList::const_iterator next = skipNewlines(i, end);
  232. if(next == end || (*next)->type != TID_LBRACKET)
  233. {
  234. // Ended a property here
  235. if(parent)
  236. parent = parent->parent;
  237. state = READY;
  238. }
  239. }
  240. else if(token->type == TID_COLON)
  241. {
  242. node = new (std::nothrow) PUConcreteNode();
  243. node->token = token->lexeme;
  244. node->file = token->file;
  245. node->line = token->line;
  246. node->type = CNT_COLON;
  247. // The following token are the parent objects (base classes).
  248. // Require at least one of them.
  249. PUScriptTokenList::const_iterator j = i + 1;
  250. j = skipNewlines(j, end);
  251. if(j == end || ((*j)->type != TID_WORD && (*j)->type != TID_QUOTE)) {
  252. printf("expected object identifier at line %d ScriptParser::parse\n",node->line);
  253. }
  254. while(j != end && ((*j)->type == TID_WORD || (*j)->type == TID_QUOTE))
  255. {
  256. PUConcreteNode* tempNode = new (std::nothrow) PUConcreteNode;
  257. tempNode->token = (*j)->lexeme;
  258. tempNode->file = (*j)->file;
  259. tempNode->line = (*j)->line;
  260. tempNode->type = (*j)->type == TID_WORD ? CNT_WORD : CNT_QUOTE;
  261. tempNode->parent = node;
  262. node->children.push_back(tempNode);
  263. ++j;
  264. }
  265. // Move it backwards once, since the end of the loop moves it forwards again anyway
  266. --j;
  267. i = j;
  268. // Insert the node
  269. if(parent)
  270. {
  271. node->parent = parent;
  272. parent->children.push_back(node);
  273. }
  274. else
  275. {
  276. node->parent = 0;
  277. nodes.push_back(node);
  278. }
  279. node = nullptr;
  280. }
  281. else if(token->type == TID_LBRACKET)
  282. {
  283. node = new (std::nothrow) PUConcreteNode;
  284. node->token = token->lexeme;
  285. node->file = token->file;
  286. node->line = token->line;
  287. node->type = CNT_LBRACE;
  288. // Consume all the newlines
  289. i = skipNewlines(i, end);
  290. // Insert the node
  291. if(parent)
  292. {
  293. node->parent = parent;
  294. parent->children.push_back(node);
  295. }
  296. else
  297. {
  298. node->parent = 0;
  299. nodes.push_back(node);
  300. }
  301. // Set the parent
  302. parent = node;
  303. // Change the state
  304. state = READY;
  305. node = nullptr;
  306. }
  307. else if(token->type == TID_RBRACKET)
  308. {
  309. // Go up one level if we can
  310. if(parent)
  311. parent = parent->parent;
  312. // If the parent is currently a { then go up again
  313. if(parent && parent->type == CNT_LBRACE && parent->parent)
  314. parent = parent->parent;
  315. node = new (std::nothrow) PUConcreteNode;
  316. node->token = token->lexeme;
  317. node->file = token->file;
  318. node->line = token->line;
  319. node->type = CNT_RBRACE;
  320. // Consume all the newlines
  321. i = skipNewlines(i, end);
  322. // Insert the node
  323. if(parent)
  324. {
  325. node->parent = parent;
  326. parent->children.push_back(node);
  327. }
  328. else
  329. {
  330. node->parent = 0;
  331. nodes.push_back(node);
  332. }
  333. // Move up another level
  334. if(parent)
  335. parent = parent->parent;
  336. node = nullptr;
  337. state = READY;
  338. }
  339. else if(token->type == TID_VARIABLE)
  340. {
  341. node = new (std::nothrow) PUConcreteNode;
  342. node->token = token->lexeme;
  343. node->file = token->file;
  344. node->line = token->line;
  345. node->type = CNT_VARIABLE;
  346. // Insert the node
  347. if(parent)
  348. {
  349. node->parent = parent;
  350. parent->children.push_back(node);
  351. }
  352. else
  353. {
  354. node->parent = 0;
  355. nodes.push_back(node);
  356. }
  357. node = nullptr;
  358. }
  359. else if(token->type == TID_QUOTE)
  360. {
  361. node = new (std::nothrow) PUConcreteNode;
  362. node->token = token->lexeme.substr(1, token->lexeme.size() - 2);
  363. node->file = token->file;
  364. node->line = token->line;
  365. node->type = CNT_QUOTE;
  366. // Insert the node
  367. if(parent)
  368. {
  369. node->parent = parent;
  370. parent->children.push_back(node);
  371. }
  372. else
  373. {
  374. node->parent = 0;
  375. nodes.push_back(node);
  376. }
  377. node = nullptr;
  378. }
  379. else if(token->type == TID_WORD)
  380. {
  381. node = new (std::nothrow) PUConcreteNode;
  382. node->token = token->lexeme;
  383. node->file = token->file;
  384. node->line = token->line;
  385. node->type = CNT_WORD;
  386. // Insert the node
  387. if(parent)
  388. {
  389. node->parent = parent;
  390. parent->children.push_back(node);
  391. }
  392. else
  393. {
  394. node->parent = 0;
  395. nodes.push_back(node);
  396. }
  397. node = nullptr;
  398. }
  399. break;
  400. }
  401. ++i;
  402. }
  403. // traceScriptParser(nodes);//
  404. // printf("kkkk:%d\n",kkkk);//
  405. }
  406. void PUScriptParser::parseChunk(PUConcreteNodeList& nodes, const PUScriptTokenList &tokens)
  407. {
  408. PUConcreteNode* node = nullptr;
  409. PUScriptToken *token = 0;
  410. for(PUScriptTokenList::const_iterator i = tokens.begin(); i != tokens.end(); ++i)
  411. {
  412. token = *i;
  413. node = nullptr;
  414. switch(token->type)
  415. {
  416. case TID_VARIABLE:
  417. node = new (std::nothrow) PUConcreteNode;
  418. node->file = token->file;
  419. node->line = token->line;
  420. node->parent = 0;
  421. node->token = token->lexeme;
  422. node->type = CNT_VARIABLE;
  423. break;
  424. case TID_WORD:
  425. node = new (std::nothrow) PUConcreteNode;
  426. node->file = token->file;
  427. node->line = token->line;
  428. node->parent = 0;
  429. node->token = token->lexeme;
  430. node->type = CNT_WORD;
  431. break;
  432. case TID_QUOTE:
  433. node = new (std::nothrow) PUConcreteNode;
  434. node->file = token->file;
  435. node->line = token->line;
  436. node->parent = 0;
  437. node->token = token->lexeme.substr(1, token->lexeme.size() - 2);
  438. node->type = CNT_QUOTE;
  439. default:
  440. printf("unexpected token,%s,%d\n",token->lexeme.c_str(),token->line);
  441. }
  442. if(node != nullptr)
  443. nodes.push_back(node);
  444. }
  445. }
  446. PUScriptToken *PUScriptParser::getToken(PUScriptTokenList::iterator i, PUScriptTokenList::iterator end, int offset)
  447. {
  448. PUScriptToken *token = 0;
  449. PUScriptTokenList::iterator iter = i + offset;
  450. if(iter != end)
  451. token = (*i);
  452. return token;
  453. }
  454. PUScriptTokenList::const_iterator PUScriptParser::skipNewlines(PUScriptTokenList::const_iterator i, PUScriptTokenList::const_iterator end)
  455. {
  456. while(i != end && (*i)->type == TID_NEWLINE)
  457. ++i;
  458. return i;
  459. }
  460. PUConcreteNode::~PUConcreteNode()
  461. {
  462. for (auto iter : children){
  463. delete iter;
  464. }
  465. }
  466. NS_CC_END