as_tokenizer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2010 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_tokenizer.cpp
  25. //
  26. // This class identifies tokens from the script code
  27. //
  28. #include "as_config.h"
  29. #include "as_scriptengine.h"
  30. #include "as_tokenizer.h"
  31. #include "as_tokendef.h"
  32. #if !defined(AS_NO_MEMORY_H)
  33. #include <memory.h>
  34. #endif
  35. #include <string.h> // strcmp()
  36. BEGIN_AS_NAMESPACE
  37. asCTokenizer::asCTokenizer(const asCScriptEngine *e)
  38. {
  39. engine = e;
  40. }
  41. asCTokenizer::~asCTokenizer()
  42. {
  43. }
  44. const char *asGetTokenDefinition(int tokenType)
  45. {
  46. if( tokenType == ttUnrecognizedToken ) return "<unrecognized token>";
  47. if( tokenType == ttEnd ) return "<end of file>";
  48. if( tokenType == ttWhiteSpace ) return "<white space>";
  49. if( tokenType == ttOnelineComment ) return "<one line comment>";
  50. if( tokenType == ttMultilineComment ) return "<multiple lines comment>";
  51. if( tokenType == ttIdentifier ) return "<identifier>";
  52. if( tokenType == ttIntConstant ) return "<integer constant>";
  53. if( tokenType == ttFloatConstant ) return "<float constant>";
  54. if( tokenType == ttDoubleConstant ) return "<double constant>";
  55. if( tokenType == ttStringConstant ) return "<string constant>";
  56. if( tokenType == ttMultilineStringConstant ) return "<multiline string constant>";
  57. if( tokenType == ttNonTerminatedStringConstant ) return "<nonterminated string constant>";
  58. if( tokenType == ttBitsConstant ) return "<bits constant>";
  59. if( tokenType == ttHeredocStringConstant ) return "<heredoc string constant>";
  60. for( asUINT n = 0; n < numTokenWords; n++ )
  61. if( tokenWords[n].tokenType == tokenType )
  62. return tokenWords[n].word;
  63. return 0;
  64. }
  65. eTokenType asCTokenizer::GetToken(const char *source, size_t sourceLength, size_t *tokenLength, asETokenClass *tc)
  66. {
  67. asASSERT(source != 0);
  68. asASSERT(tokenLength != 0);
  69. this->source = source;
  70. this->sourceLength = sourceLength;
  71. asETokenClass t = ParseToken();
  72. if( tc ) *tc = t;
  73. // Copy the output to the token
  74. *tokenLength = this->tokenLength;
  75. return tokenType;
  76. }
  77. asETokenClass asCTokenizer::ParseToken()
  78. {
  79. if( IsWhiteSpace() ) return asTC_WHITESPACE;
  80. if( IsComment() ) return asTC_COMMENT;
  81. if( IsConstant() ) return asTC_VALUE;
  82. if( IsIdentifier() ) return asTC_IDENTIFIER;
  83. if( IsKeyWord() ) return asTC_KEYWORD;
  84. // If none of the above this is an unrecognized token
  85. // We can find the length of the token by advancing
  86. // one step and trying to identify a token there
  87. tokenType = ttUnrecognizedToken;
  88. tokenLength = 1;
  89. return asTC_UNKNOWN;
  90. }
  91. bool asCTokenizer::IsWhiteSpace()
  92. {
  93. // Treat UTF8 byte-order-mark (EF BB BF) as whitespace
  94. if( sourceLength >= 3 &&
  95. asBYTE(source[0]) == 0xEFu &&
  96. asBYTE(source[1]) == 0xBBu &&
  97. asBYTE(source[2]) == 0xBFu )
  98. {
  99. tokenType = ttWhiteSpace;
  100. tokenLength = 3;
  101. return true;
  102. }
  103. // Group all other white space characters into one
  104. size_t n;
  105. int numWsChars = (int)strlen(whiteSpace);
  106. for( n = 0; n < sourceLength; n++ )
  107. {
  108. bool isWhiteSpace = false;
  109. for( int w = 0; w < numWsChars; w++ )
  110. {
  111. if( source[n] == whiteSpace[w] )
  112. {
  113. isWhiteSpace = true;
  114. break;
  115. }
  116. }
  117. if( !isWhiteSpace ) break;
  118. }
  119. if( n > 0 )
  120. {
  121. tokenType = ttWhiteSpace;
  122. tokenLength = n;
  123. return true;
  124. }
  125. return false;
  126. }
  127. bool asCTokenizer::IsComment()
  128. {
  129. if( sourceLength < 2 )
  130. return false;
  131. if( source[0] != '/' )
  132. return false;
  133. if( source[1] == '/' )
  134. {
  135. // One-line comment
  136. // Find the length
  137. size_t n;
  138. for( n = 2; n < sourceLength; n++ )
  139. {
  140. if( source[n] == '\n' )
  141. break;
  142. }
  143. tokenType = ttOnelineComment;
  144. tokenLength = n+1;
  145. return true;
  146. }
  147. if( source[1] == '*' )
  148. {
  149. // Multi-line comment
  150. // Find the length
  151. size_t n;
  152. for( n = 2; n < sourceLength-1; )
  153. {
  154. if( source[n++] == '*' && source[n] == '/' )
  155. break;
  156. }
  157. tokenType = ttMultilineComment;
  158. tokenLength = n+1;
  159. return true;
  160. }
  161. return false;
  162. }
  163. bool asCTokenizer::IsConstant()
  164. {
  165. // Starting with number
  166. if( (source[0] >= '0' && source[0] <= '9') || (source[0] == '.' && sourceLength > 1 && source[1] >= '0' && source[1] <= '9') )
  167. {
  168. // Is it a hexadecimal number?
  169. if( source[0] == '0' && sourceLength > 1 && (source[1] == 'x' || source[1] == 'X') )
  170. {
  171. size_t n;
  172. for( n = 2; n < sourceLength; n++ )
  173. {
  174. if( !(source[n] >= '0' && source[n] <= '9') &&
  175. !(source[n] >= 'a' && source[n] <= 'f') &&
  176. !(source[n] >= 'A' && source[n] <= 'F') )
  177. break;
  178. }
  179. tokenType = ttBitsConstant;
  180. tokenLength = n;
  181. return true;
  182. }
  183. size_t n;
  184. for( n = 0; n < sourceLength; n++ )
  185. {
  186. if( source[n] < '0' || source[n] > '9' )
  187. break;
  188. }
  189. if( n < sourceLength && source[n] == '.' )
  190. {
  191. n++;
  192. for( ; n < sourceLength; n++ )
  193. {
  194. if( source[n] < '0' || source[n] > '9' )
  195. break;
  196. }
  197. if( n < sourceLength && (source[n] == 'e' || source[n] == 'E') )
  198. {
  199. n++;
  200. if( n < sourceLength && (source[n] == '-' || source[n] == '+') )
  201. n++;
  202. for( ; n < sourceLength; n++ )
  203. {
  204. if( source[n] < '0' || source[n] > '9' )
  205. break;
  206. }
  207. }
  208. if( n < sourceLength && (source[n] == 'f' || source[n] == 'F') )
  209. {
  210. tokenType = ttFloatConstant;
  211. tokenLength = n + 1;
  212. }
  213. else
  214. {
  215. #ifdef AS_USE_DOUBLE_AS_FLOAT
  216. tokenType = ttFloatConstant;
  217. #else
  218. tokenType = ttDoubleConstant;
  219. #endif
  220. tokenLength = n;
  221. }
  222. return true;
  223. }
  224. tokenType = ttIntConstant;
  225. tokenLength = n;
  226. return true;
  227. }
  228. // String constant between double or single quotes
  229. if( source[0] == '"' || source[0] == '\'' )
  230. {
  231. // Is it a normal string constant or a heredoc string constant?
  232. if( sourceLength >= 6 && source[0] == '"' && source[1] == '"' && source[2] == '"' )
  233. {
  234. // Heredoc string constant (spans multiple lines, no escape sequences)
  235. // Find the length
  236. size_t n;
  237. for( n = 3; n < sourceLength-2; n++ )
  238. {
  239. if( source[n] == '"' && source[n+1] == '"' && source[n+2] == '"' )
  240. break;
  241. }
  242. tokenType = ttHeredocStringConstant;
  243. tokenLength = n+3;
  244. }
  245. else
  246. {
  247. // Normal string constant
  248. tokenType = ttStringConstant;
  249. char quote = source[0];
  250. bool evenSlashes = true;
  251. size_t n;
  252. for( n = 1; n < sourceLength; n++ )
  253. {
  254. #ifdef AS_DOUBLEBYTE_CHARSET
  255. // Double-byte characters are only allowed for ASCII
  256. if( (source[n] & 0x80) && engine->ep.scanner == 0 )
  257. {
  258. // This is a leading character in a double byte character,
  259. // include both in the string and continue processing.
  260. n++;
  261. continue;
  262. }
  263. #endif
  264. if( source[n] == '\n' )
  265. tokenType = ttMultilineStringConstant;
  266. if( source[n] == quote && evenSlashes )
  267. {
  268. tokenLength = n+1;
  269. return true;
  270. }
  271. if( source[n] == '\\' ) evenSlashes = !evenSlashes; else evenSlashes = true;
  272. }
  273. tokenType = ttNonTerminatedStringConstant;
  274. tokenLength = n;
  275. }
  276. return true;
  277. }
  278. return false;
  279. }
  280. bool asCTokenizer::IsIdentifier()
  281. {
  282. // Starting with letter or underscore
  283. if( (source[0] >= 'a' && source[0] <= 'z') ||
  284. (source[0] >= 'A' && source[0] <= 'Z') ||
  285. source[0] == '_' )
  286. {
  287. tokenType = ttIdentifier;
  288. tokenLength = 1;
  289. for( size_t n = 1; n < sourceLength; n++ )
  290. {
  291. if( (source[n] >= 'a' && source[n] <= 'z') ||
  292. (source[n] >= 'A' && source[n] <= 'Z') ||
  293. (source[n] >= '0' && source[n] <= '9') ||
  294. source[n] == '_' )
  295. tokenLength++;
  296. else
  297. break;
  298. }
  299. // Make sure the identifier isn't a reserved keyword
  300. if( tokenLength > 50 ) return true;
  301. char test[51];
  302. memcpy(test, source, tokenLength);
  303. test[tokenLength] = 0;
  304. for( asUINT i = 0; i < numTokenWords; i++ )
  305. {
  306. if( strcmp(test, tokenWords[i].word) == 0 )
  307. return false;
  308. }
  309. return true;
  310. }
  311. return false;
  312. }
  313. bool asCTokenizer::IsKeyWord()
  314. {
  315. // Fill the list with all possible keywords
  316. // Check each character against all the keywords in the list,
  317. // remove keywords that don't match. When only one remains and
  318. // it matches the source completely we have found a match.
  319. int words[numTokenWords];
  320. asUINT n;
  321. for( n = 0; n < numTokenWords; n++ )
  322. words[n] = n;
  323. int numWords = numTokenWords;
  324. int lastPossible = -1;
  325. n = 0;
  326. while( n < sourceLength && numWords >= 0 )
  327. {
  328. for( int i = 0; i < numWords; i++ )
  329. {
  330. if( tokenWords[words[i]].word[n] == '\0' )
  331. {
  332. // tokens that end with a character that can be part of an
  333. // identifier require an extra verification to guarantee that
  334. // we don't split an identifier token, e.g. the "!is" token
  335. // and the "!isTrue" expression.
  336. if( ((tokenWords[words[i]].word[n-1] >= 'a' && tokenWords[words[i]].word[n-1] <= 'z') ||
  337. (tokenWords[words[i]].word[n-1] >= 'A' && tokenWords[words[i]].word[n-1] <= 'Z')) &&
  338. ((source[n] >= 'a' && source[n] <= 'z') ||
  339. (source[n] >= 'A' && source[n] <= 'Z') ||
  340. (source[n] >= '0' && source[n] <= '9') ||
  341. (source[n] == '_')) )
  342. {
  343. // The token doesn't really match, even though
  344. // the start of the source matches the token
  345. words[i--] = words[--numWords];
  346. }
  347. else if( numWords > 1 )
  348. {
  349. // It's possible that a longer token matches, so let's
  350. // remember this match and continue searching
  351. lastPossible = words[i];
  352. words[i--] = words[--numWords];
  353. continue;
  354. }
  355. else
  356. {
  357. // Only one token matches, so we return it
  358. tokenType = tokenWords[words[i]].tokenType;
  359. tokenLength = n;
  360. return true;
  361. }
  362. }
  363. else if( tokenWords[words[i]].word[n] != source[n] )
  364. {
  365. // The token doesn't match
  366. words[i--] = words[--numWords];
  367. }
  368. }
  369. n++;
  370. }
  371. // The source length ended or there where no more matchable words
  372. if( numWords )
  373. {
  374. // If any of the tokenWords also end at this
  375. // position then we have found the matching token
  376. for( int i = 0; i < numWords; i++ )
  377. {
  378. if( tokenWords[words[i]].word[n] == '\0' )
  379. {
  380. tokenType = tokenWords[words[i]].tokenType;
  381. tokenLength = n;
  382. return true;
  383. }
  384. }
  385. }
  386. // It is still possible that a shorter token was found
  387. if( lastPossible > -1 )
  388. {
  389. tokenType = tokenWords[lastPossible].tokenType;
  390. tokenLength = strlen(tokenWords[lastPossible].word);
  391. return true;
  392. }
  393. return false;
  394. }
  395. END_AS_NAMESPACE