CCPUScriptLexer.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "CCPUScriptLexer.h"
  22. NS_CC_BEGIN
  23. PUScriptLexer::PUScriptLexer()
  24. {
  25. }
  26. PUScriptLexer::~PUScriptLexer()
  27. {
  28. }
  29. void PUScriptLexer::openLexer(const std::string &str,const std::string &source,PUScriptTokenList& tokens)
  30. {
  31. enum{ READY = 0, COMMENT, MULTICOMMENT, WORD, QUOTE, VAR, POSSIBLECOMMENT };
  32. const wchar_t varopener = '$', quote = '\"', slash = '/', backslash = '\\', openbrace = '{', closebrace = '}', colon = ':', star = '*', cr = '\r', lf = '\n';
  33. char c = 0, lastc = 0;
  34. std::string lexeme;
  35. unsigned int line = 1, state = READY, lastQuote = 0;
  36. // ScriptTokenListPtr tokens(OGRE_NEW_T(ScriptTokenList, MEMCATEGORY_GENERAL)(), SPFM_DELETE_T);
  37. //
  38. // Iterate over the input
  39. std::string::const_iterator i = str.begin(), end = str.end();
  40. while(i != end)
  41. {
  42. lastc = c;
  43. c = *i;
  44. if(c == quote)
  45. lastQuote = line;
  46. switch(state)
  47. {
  48. case READY:
  49. if(c == slash && lastc == slash)
  50. {
  51. // Comment start, clear out the lexeme
  52. lexeme = "";
  53. state = COMMENT;
  54. }
  55. else if(c == star && lastc == slash)
  56. {
  57. lexeme = "";
  58. state = MULTICOMMENT;
  59. }
  60. else if(c == quote)
  61. {
  62. // Clear out the lexeme ready to be filled with quotes!
  63. lexeme = c;
  64. state = QUOTE;
  65. }
  66. else if(c == varopener)
  67. {
  68. // Set up to read in a variable
  69. lexeme = c;
  70. state = VAR;
  71. }
  72. else if(isNewline(c))
  73. {
  74. lexeme = c;
  75. setToken(lexeme, line, source, &tokens);
  76. }
  77. else if(!isWhitespace(c))
  78. {
  79. lexeme = c;
  80. if(c == slash)
  81. state = POSSIBLECOMMENT;
  82. else
  83. state = WORD;
  84. }
  85. break;
  86. case COMMENT:
  87. // This newline happens to be ignored automatically
  88. if(isNewline(c))
  89. state = READY;
  90. break;
  91. case MULTICOMMENT:
  92. if(c == slash && lastc == star)
  93. state = READY;
  94. break;
  95. case POSSIBLECOMMENT:
  96. if(c == slash && lastc == slash)
  97. {
  98. lexeme = "";
  99. state = COMMENT;
  100. break;
  101. }
  102. else if(c == star && lastc == slash)
  103. {
  104. lexeme = "";
  105. state = MULTICOMMENT;
  106. break;
  107. }
  108. else
  109. {
  110. state = WORD;
  111. }
  112. case WORD:
  113. if(isNewline(c))
  114. {
  115. setToken(lexeme, line, source, &tokens);
  116. lexeme = c;
  117. setToken(lexeme, line, source, &tokens);
  118. state = READY;
  119. }
  120. else if(isWhitespace(c))
  121. {
  122. setToken(lexeme, line, source, &tokens);
  123. state = READY;
  124. }
  125. else if(c == openbrace || c == closebrace || c == colon)
  126. {
  127. setToken(lexeme, line, source, &tokens);
  128. lexeme = c;
  129. setToken(lexeme, line, source, &tokens);
  130. state = READY;
  131. }
  132. else
  133. {
  134. lexeme += c;
  135. }
  136. break;
  137. case QUOTE:
  138. if(c != backslash)
  139. {
  140. // Allow embedded quotes with escaping
  141. if(c == quote && lastc == backslash)
  142. {
  143. lexeme += c;
  144. }
  145. else if(c == quote)
  146. {
  147. lexeme += c;
  148. setToken(lexeme, line, source, &tokens);
  149. state = READY;
  150. }
  151. else
  152. {
  153. // Backtrack here and allow a backslash normally within the quote
  154. if(lastc == backslash)
  155. lexeme = lexeme + "\\" + c;
  156. else
  157. lexeme += c;
  158. }
  159. }
  160. break;
  161. case VAR:
  162. if(isNewline(c))
  163. {
  164. setToken(lexeme, line, source, &tokens);
  165. lexeme = c;
  166. setToken(lexeme, line, source, &tokens);
  167. state = READY;
  168. }
  169. else if(isWhitespace(c))
  170. {
  171. setToken(lexeme, line, source, &tokens);
  172. state = READY;
  173. }
  174. else if(c == openbrace || c == closebrace || c == colon)
  175. {
  176. setToken(lexeme, line, source, &tokens);
  177. lexeme = c;
  178. setToken(lexeme, line, source, &tokens);
  179. state = READY;
  180. }
  181. else
  182. {
  183. lexeme += c;
  184. }
  185. break;
  186. }
  187. // Separate check for newlines just to track line numbers
  188. if(c == cr || (c == lf && lastc != cr))
  189. line++;
  190. ++i;
  191. }
  192. // Check for valid exit states
  193. if(state == WORD || state == VAR)
  194. {
  195. if(!lexeme.empty())
  196. setToken(lexeme, line, source, &tokens);
  197. }
  198. else
  199. {
  200. if(state == QUOTE)
  201. {
  202. printf("Exception\n");
  203. // OGRE_EXCEPT(Exception::ERR_INVALID_STATE,
  204. // Ogre::String("no matching \" found for \" at line ") +
  205. // Ogre::StringConverter::toString(lastQuote),
  206. // "ScriptLexer::tokenize");
  207. }
  208. }
  209. }
  210. void PUScriptLexer::setToken(const std::string &lexeme, int line, const std::string &source, PUScriptTokenList *tokens)
  211. {
  212. const char openBracket = '{', closeBracket = '}', colon = ':',
  213. quote = '\"', var = '$';
  214. PUScriptToken* token = new (std::nothrow) PUScriptToken;
  215. token->lexeme = lexeme;
  216. token->line = line;
  217. token->file = source;
  218. bool ignore = false;
  219. // Check the user token map first
  220. if(lexeme.size() == 1 && isNewline(lexeme[0]))
  221. {
  222. token->type = TID_NEWLINE;
  223. if(!tokens->empty() && tokens->back()->type == TID_NEWLINE){
  224. ignore = true;
  225. delete token;
  226. }
  227. }
  228. else if(lexeme.size() == 1 && lexeme[0] == openBracket)
  229. token->type = TID_LBRACKET;
  230. else if(lexeme.size() == 1 && lexeme[0] == closeBracket)
  231. token->type = TID_RBRACKET;
  232. else if(lexeme.size() == 1 && lexeme[0] == colon)
  233. token->type = TID_COLON;
  234. else if(lexeme[0] == var)
  235. token->type = TID_VARIABLE;
  236. else
  237. {
  238. // This is either a non-zero length phrase or quoted phrase
  239. if(lexeme.size() >= 2 && lexeme[0] == quote && lexeme[lexeme.size() - 1] == quote)
  240. {
  241. token->type = TID_QUOTE;
  242. }
  243. else
  244. {
  245. token->type = TID_WORD;
  246. }
  247. }
  248. if(!ignore)
  249. tokens->push_back(token);
  250. }
  251. bool PUScriptLexer::isWhitespace(char c) const
  252. {
  253. return c == ' ' || c == '\r' || c == '\t';
  254. }
  255. bool PUScriptLexer::isNewline(char c) const
  256. {
  257. return c == '\n' || c == '\r';
  258. }
  259. NS_CC_END