CCPUScriptLexer.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "CCPUScriptLexer.h"
  23. NS_CC_BEGIN
  24. PUScriptLexer::PUScriptLexer()
  25. {
  26. }
  27. PUScriptLexer::~PUScriptLexer()
  28. {
  29. }
  30. void PUScriptLexer::openLexer(const std::string &str,const std::string &source,PUScriptTokenList& tokens)
  31. {
  32. enum{ READY = 0, COMMENT, MULTICOMMENT, WORD, QUOTE, VAR, POSSIBLECOMMENT };
  33. const wchar_t varopener = '$', quote = '\"', slash = '/', backslash = '\\', openbrace = '{', closebrace = '}', colon = ':', star = '*', cr = '\r', lf = '\n';
  34. char c = 0, lastc = 0;
  35. std::string lexeme;
  36. unsigned int line = 1, state = READY, lastQuote = 0;
  37. // ScriptTokenListPtr tokens(OGRE_NEW_T(ScriptTokenList, MEMCATEGORY_GENERAL)(), SPFM_DELETE_T);
  38. //
  39. // Iterate over the input
  40. std::string::const_iterator i = str.begin(), end = str.end();
  41. while(i != end)
  42. {
  43. lastc = c;
  44. c = *i;
  45. if(c == quote)
  46. lastQuote = line;
  47. switch(state)
  48. {
  49. case READY:
  50. if(c == slash && lastc == slash)
  51. {
  52. // Comment start, clear out the lexeme
  53. lexeme = "";
  54. state = COMMENT;
  55. }
  56. else if(c == star && lastc == slash)
  57. {
  58. lexeme = "";
  59. state = MULTICOMMENT;
  60. }
  61. else if(c == quote)
  62. {
  63. // Clear out the lexeme ready to be filled with quotes!
  64. lexeme = c;
  65. state = QUOTE;
  66. }
  67. else if(c == varopener)
  68. {
  69. // Set up to read in a variable
  70. lexeme = c;
  71. state = VAR;
  72. }
  73. else if(isNewline(c))
  74. {
  75. lexeme = c;
  76. setToken(lexeme, line, source, &tokens);
  77. }
  78. else if(!isWhitespace(c))
  79. {
  80. lexeme = c;
  81. if(c == slash)
  82. state = POSSIBLECOMMENT;
  83. else
  84. state = WORD;
  85. }
  86. break;
  87. case COMMENT:
  88. // This newline happens to be ignored automatically
  89. if(isNewline(c))
  90. state = READY;
  91. break;
  92. case MULTICOMMENT:
  93. if(c == slash && lastc == star)
  94. state = READY;
  95. break;
  96. case POSSIBLECOMMENT:
  97. if(c == slash && lastc == slash)
  98. {
  99. lexeme = "";
  100. state = COMMENT;
  101. break;
  102. }
  103. else if(c == star && lastc == slash)
  104. {
  105. lexeme = "";
  106. state = MULTICOMMENT;
  107. break;
  108. }
  109. else
  110. {
  111. state = WORD;
  112. }
  113. case WORD:
  114. if(isNewline(c))
  115. {
  116. setToken(lexeme, line, source, &tokens);
  117. lexeme = c;
  118. setToken(lexeme, line, source, &tokens);
  119. state = READY;
  120. }
  121. else if(isWhitespace(c))
  122. {
  123. setToken(lexeme, line, source, &tokens);
  124. state = READY;
  125. }
  126. else if(c == openbrace || c == closebrace || c == colon)
  127. {
  128. setToken(lexeme, line, source, &tokens);
  129. lexeme = c;
  130. setToken(lexeme, line, source, &tokens);
  131. state = READY;
  132. }
  133. else
  134. {
  135. lexeme += c;
  136. }
  137. break;
  138. case QUOTE:
  139. if(c != backslash)
  140. {
  141. // Allow embedded quotes with escaping
  142. if(c == quote && lastc == backslash)
  143. {
  144. lexeme += c;
  145. }
  146. else if(c == quote)
  147. {
  148. lexeme += c;
  149. setToken(lexeme, line, source, &tokens);
  150. state = READY;
  151. }
  152. else
  153. {
  154. // Backtrack here and allow a backslash normally within the quote
  155. if(lastc == backslash)
  156. lexeme = lexeme + "\\" + c;
  157. else
  158. lexeme += c;
  159. }
  160. }
  161. break;
  162. case VAR:
  163. if(isNewline(c))
  164. {
  165. setToken(lexeme, line, source, &tokens);
  166. lexeme = c;
  167. setToken(lexeme, line, source, &tokens);
  168. state = READY;
  169. }
  170. else if(isWhitespace(c))
  171. {
  172. setToken(lexeme, line, source, &tokens);
  173. state = READY;
  174. }
  175. else if(c == openbrace || c == closebrace || c == colon)
  176. {
  177. setToken(lexeme, line, source, &tokens);
  178. lexeme = c;
  179. setToken(lexeme, line, source, &tokens);
  180. state = READY;
  181. }
  182. else
  183. {
  184. lexeme += c;
  185. }
  186. break;
  187. }
  188. // Separate check for newlines just to track line numbers
  189. if(c == cr || (c == lf && lastc != cr))
  190. line++;
  191. ++i;
  192. }
  193. // Check for valid exit states
  194. if(state == WORD || state == VAR)
  195. {
  196. if(!lexeme.empty())
  197. setToken(lexeme, line, source, &tokens);
  198. }
  199. else
  200. {
  201. if(state == QUOTE)
  202. {
  203. printf("Exception\n");
  204. // OGRE_EXCEPT(Exception::ERR_INVALID_STATE,
  205. // Ogre::String("no matching \" found for \" at line ") +
  206. // Ogre::StringConverter::toString(lastQuote),
  207. // "ScriptLexer::tokenize");
  208. }
  209. }
  210. }
  211. void PUScriptLexer::setToken(const std::string &lexeme, int line, const std::string &source, PUScriptTokenList *tokens)
  212. {
  213. const char openBracket = '{', closeBracket = '}', colon = ':',
  214. quote = '\"', var = '$';
  215. PUScriptToken* token = new (std::nothrow) PUScriptToken;
  216. token->lexeme = lexeme;
  217. token->line = line;
  218. token->file = source;
  219. bool ignore = false;
  220. // Check the user token map first
  221. if(lexeme.size() == 1 && isNewline(lexeme[0]))
  222. {
  223. token->type = TID_NEWLINE;
  224. if(!tokens->empty() && tokens->back()->type == TID_NEWLINE){
  225. ignore = true;
  226. delete token;
  227. }
  228. }
  229. else if(lexeme.size() == 1 && lexeme[0] == openBracket)
  230. token->type = TID_LBRACKET;
  231. else if(lexeme.size() == 1 && lexeme[0] == closeBracket)
  232. token->type = TID_RBRACKET;
  233. else if(lexeme.size() == 1 && lexeme[0] == colon)
  234. token->type = TID_COLON;
  235. else if(lexeme[0] == var)
  236. token->type = TID_VARIABLE;
  237. else
  238. {
  239. // This is either a non-zero length phrase or quoted phrase
  240. if(lexeme.size() >= 2 && lexeme[0] == quote && lexeme[lexeme.size() - 1] == quote)
  241. {
  242. token->type = TID_QUOTE;
  243. }
  244. else
  245. {
  246. token->type = TID_WORD;
  247. }
  248. }
  249. if(!ignore)
  250. tokens->push_back(token);
  251. }
  252. bool PUScriptLexer::isWhitespace(char c) const
  253. {
  254. return c == ' ' || c == '\r' || c == '\t';
  255. }
  256. bool PUScriptLexer::isNewline(char c) const
  257. {
  258. return c == '\n' || c == '\r';
  259. }
  260. NS_CC_END