as_tokenizer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 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()
  38. {
  39. engine = 0;
  40. // Initialize the keyword map
  41. for( asUINT n = 0; n < numTokenWords; n++ )
  42. {
  43. if( (tokenWords[n].word[0] >= 'a' && tokenWords[n].word[0] <= 'z') ||
  44. (tokenWords[n].word[0] >= 'A' && tokenWords[n].word[0] <= 'Z') )
  45. alphaKeywordMap.Insert(asCStringPointer(tokenWords[n].word, strlen(tokenWords[n].word)), tokenWords[n].tokenType);
  46. else
  47. nonAlphaKeywordMap.Insert(asCStringPointer(tokenWords[n].word, strlen(tokenWords[n].word)), tokenWords[n].tokenType);
  48. }
  49. }
  50. asCTokenizer::~asCTokenizer()
  51. {
  52. }
  53. // static
  54. const char *asCTokenizer::GetDefinition(int tokenType)
  55. {
  56. if( tokenType == ttUnrecognizedToken ) return "<unrecognized token>";
  57. if( tokenType == ttEnd ) return "<end of file>";
  58. if( tokenType == ttWhiteSpace ) return "<white space>";
  59. if( tokenType == ttOnelineComment ) return "<one line comment>";
  60. if( tokenType == ttMultilineComment ) return "<multiple lines comment>";
  61. if( tokenType == ttIdentifier ) return "<identifier>";
  62. if( tokenType == ttIntConstant ) return "<integer constant>";
  63. if( tokenType == ttFloatConstant ) return "<float constant>";
  64. if( tokenType == ttDoubleConstant ) return "<double constant>";
  65. if( tokenType == ttStringConstant ) return "<string constant>";
  66. if( tokenType == ttMultilineStringConstant ) return "<multiline string constant>";
  67. if( tokenType == ttNonTerminatedStringConstant ) return "<nonterminated string constant>";
  68. if( tokenType == ttBitsConstant ) return "<bits constant>";
  69. if( tokenType == ttHeredocStringConstant ) return "<heredoc string constant>";
  70. for( asUINT n = 0; n < numTokenWords; n++ )
  71. if( tokenWords[n].tokenType == tokenType )
  72. return tokenWords[n].word;
  73. return 0;
  74. }
  75. bool asCTokenizer::IsDigitInRadix(char ch, int radix) const
  76. {
  77. if( ch >= '0' && ch <= '9' ) return (ch -= '0') < radix;
  78. if( ch >= 'A' && ch <= 'Z' ) return (ch -= 'A'-10) < radix;
  79. if( ch >= 'a' && ch <= 'z' ) return (ch -= 'a'-10) < radix;
  80. return false;
  81. }
  82. eTokenType asCTokenizer::GetToken(const char *source, size_t sourceLength, size_t *tokenLength, asETokenClass *tc) const
  83. {
  84. asASSERT(source != 0);
  85. asASSERT(tokenLength != 0);
  86. eTokenType tokenType;
  87. size_t tlen;
  88. asETokenClass t = ParseToken(source, sourceLength, tlen, tokenType);
  89. if( tc ) *tc = t;
  90. if( tokenLength ) *tokenLength = tlen;
  91. return tokenType;
  92. }
  93. asETokenClass asCTokenizer::ParseToken(const char *source, size_t sourceLength, size_t &tokenLength, eTokenType &tokenType) const
  94. {
  95. if( IsWhiteSpace(source, sourceLength, tokenLength, tokenType) ) return asTC_WHITESPACE;
  96. if( IsComment(source, sourceLength, tokenLength, tokenType) ) return asTC_COMMENT;
  97. if( IsConstant(source, sourceLength, tokenLength, tokenType) ) return asTC_VALUE;
  98. if( IsIdentifier(source, sourceLength, tokenLength, tokenType) ) return asTC_IDENTIFIER;
  99. if( IsKeyWord(source, sourceLength, tokenLength, tokenType) ) return asTC_KEYWORD;
  100. // If none of the above this is an unrecognized token
  101. // We can find the length of the token by advancing
  102. // one step and trying to identify a token there
  103. tokenType = ttUnrecognizedToken;
  104. tokenLength = 1;
  105. return asTC_UNKNOWN;
  106. }
  107. bool asCTokenizer::IsWhiteSpace(const char *source, size_t sourceLength, size_t &tokenLength, eTokenType &tokenType) const
  108. {
  109. // Treat UTF8 byte-order-mark (EF BB BF) as whitespace
  110. if( sourceLength >= 3 &&
  111. asBYTE(source[0]) == 0xEFu &&
  112. asBYTE(source[1]) == 0xBBu &&
  113. asBYTE(source[2]) == 0xBFu )
  114. {
  115. tokenType = ttWhiteSpace;
  116. tokenLength = 3;
  117. return true;
  118. }
  119. // Group all other white space characters into one
  120. size_t n;
  121. int numWsChars = (int)strlen(whiteSpace);
  122. for( n = 0; n < sourceLength; n++ )
  123. {
  124. bool isWhiteSpace = false;
  125. for( int w = 0; w < numWsChars; w++ )
  126. {
  127. if( source[n] == whiteSpace[w] )
  128. {
  129. isWhiteSpace = true;
  130. break;
  131. }
  132. }
  133. if( !isWhiteSpace ) break;
  134. }
  135. if( n > 0 )
  136. {
  137. tokenType = ttWhiteSpace;
  138. tokenLength = n;
  139. return true;
  140. }
  141. return false;
  142. }
  143. bool asCTokenizer::IsComment(const char *source, size_t sourceLength, size_t &tokenLength, eTokenType &tokenType) const
  144. {
  145. if( sourceLength < 2 )
  146. return false;
  147. if( source[0] != '/' )
  148. return false;
  149. if( source[1] == '/' )
  150. {
  151. // One-line comment
  152. // Find the length
  153. size_t n;
  154. for( n = 2; n < sourceLength; n++ )
  155. {
  156. if( source[n] == '\n' )
  157. break;
  158. }
  159. tokenType = ttOnelineComment;
  160. tokenLength = n+1;
  161. return true;
  162. }
  163. if( source[1] == '*' )
  164. {
  165. // Multi-line comment
  166. // Find the length
  167. size_t n;
  168. for( n = 2; n < sourceLength-1; )
  169. {
  170. if( source[n++] == '*' && source[n] == '/' )
  171. break;
  172. }
  173. tokenType = ttMultilineComment;
  174. tokenLength = n+1;
  175. return true;
  176. }
  177. return false;
  178. }
  179. bool asCTokenizer::IsConstant(const char *source, size_t sourceLength, size_t &tokenLength, eTokenType &tokenType) const
  180. {
  181. // Starting with number
  182. if( (source[0] >= '0' && source[0] <= '9') || (source[0] == '.' && sourceLength > 1 && source[1] >= '0' && source[1] <= '9') )
  183. {
  184. // Is it a based number?
  185. if( source[0] == '0' && sourceLength > 1 )
  186. {
  187. // Determine the radix for the constant
  188. int radix = 0;
  189. switch( source[1] )
  190. {
  191. case 'b': case 'B': radix = 2; break;
  192. case 'o': case 'O': radix = 8; break;
  193. case 'd': case 'D': radix = 10; break;
  194. case 'x': case 'X': radix = 16; break;
  195. }
  196. if( radix )
  197. {
  198. size_t n;
  199. for( n = 2; n < sourceLength; n++ )
  200. if( !IsDigitInRadix(source[n], radix) )
  201. break;
  202. tokenType = ttBitsConstant;
  203. tokenLength = n;
  204. return true;
  205. }
  206. }
  207. size_t n;
  208. for( n = 0; n < sourceLength; n++ )
  209. {
  210. if( source[n] < '0' || source[n] > '9' )
  211. break;
  212. }
  213. if( n < sourceLength && source[n] == '.' )
  214. {
  215. n++;
  216. for( ; n < sourceLength; n++ )
  217. {
  218. if( source[n] < '0' || source[n] > '9' )
  219. break;
  220. }
  221. if( n < sourceLength && (source[n] == 'e' || source[n] == 'E') )
  222. {
  223. n++;
  224. if( n < sourceLength && (source[n] == '-' || source[n] == '+') )
  225. n++;
  226. for( ; n < sourceLength; n++ )
  227. {
  228. if( source[n] < '0' || source[n] > '9' )
  229. break;
  230. }
  231. }
  232. if( n < sourceLength && (source[n] == 'f' || source[n] == 'F') )
  233. {
  234. tokenType = ttFloatConstant;
  235. tokenLength = n + 1;
  236. }
  237. else
  238. {
  239. #ifdef AS_USE_DOUBLE_AS_FLOAT
  240. tokenType = ttFloatConstant;
  241. #else
  242. tokenType = ttDoubleConstant;
  243. #endif
  244. tokenLength = n;
  245. }
  246. return true;
  247. }
  248. tokenType = ttIntConstant;
  249. tokenLength = n;
  250. return true;
  251. }
  252. // String constant between double or single quotes
  253. if( source[0] == '"' || source[0] == '\'' )
  254. {
  255. // Is it a normal string constant or a heredoc string constant?
  256. if( sourceLength >= 6 && source[0] == '"' && source[1] == '"' && source[2] == '"' )
  257. {
  258. // Heredoc string constant (spans multiple lines, no escape sequences)
  259. // Find the length
  260. size_t n;
  261. for( n = 3; n < sourceLength-2; n++ )
  262. {
  263. if( source[n] == '"' && source[n+1] == '"' && source[n+2] == '"' )
  264. break;
  265. }
  266. tokenType = ttHeredocStringConstant;
  267. tokenLength = n+3;
  268. }
  269. else
  270. {
  271. // Normal string constant
  272. tokenType = ttStringConstant;
  273. char quote = source[0];
  274. bool evenSlashes = true;
  275. size_t n;
  276. for( n = 1; n < sourceLength; n++ )
  277. {
  278. #ifdef AS_DOUBLEBYTE_CHARSET
  279. // Double-byte characters are only allowed for ASCII
  280. if( (source[n] & 0x80) && engine->ep.scanner == 0 )
  281. {
  282. // This is a leading character in a double byte character,
  283. // include both in the string and continue processing.
  284. n++;
  285. continue;
  286. }
  287. #endif
  288. if( source[n] == '\n' )
  289. tokenType = ttMultilineStringConstant;
  290. if( source[n] == quote && evenSlashes )
  291. {
  292. tokenLength = n+1;
  293. return true;
  294. }
  295. if( source[n] == '\\' ) evenSlashes = !evenSlashes; else evenSlashes = true;
  296. }
  297. tokenType = ttNonTerminatedStringConstant;
  298. tokenLength = n;
  299. }
  300. return true;
  301. }
  302. return false;
  303. }
  304. bool asCTokenizer::IsIdentifier(const char *source, size_t sourceLength, size_t &tokenLength, eTokenType &tokenType) const
  305. {
  306. // Starting with letter or underscore
  307. if( (source[0] >= 'a' && source[0] <= 'z') ||
  308. (source[0] >= 'A' && source[0] <= 'Z') ||
  309. source[0] == '_' )
  310. {
  311. tokenType = ttIdentifier;
  312. tokenLength = 1;
  313. for( size_t n = 1; n < sourceLength; n++ )
  314. {
  315. if( (source[n] >= 'a' && source[n] <= 'z') ||
  316. (source[n] >= 'A' && source[n] <= 'Z') ||
  317. (source[n] >= '0' && source[n] <= '9') ||
  318. source[n] == '_' )
  319. tokenLength++;
  320. else
  321. break;
  322. }
  323. // Make sure the identifier isn't a reserved keyword
  324. if( alphaKeywordMap.MoveTo(0, asCStringPointer(source, tokenLength)) )
  325. return false;
  326. return true;
  327. }
  328. return false;
  329. }
  330. bool asCTokenizer::IsKeyWord(const char *source, size_t sourceLength, size_t &tokenLength, eTokenType &tokenType) const
  331. {
  332. // TODO: optimize: This can probably be optimized further with a specialized algorithm
  333. // As most keywords are shorter, then we should start from the shortest
  334. // to the longest. Only for some of the keywords is it necessary to look
  335. // for a longer part, e.g. ! and !is.
  336. //
  337. // We can use a map that separates the tokens based on the first character.
  338. // The highest number of possible tokens would then be 11 for the letter 'i'.
  339. // Choose the best map
  340. const asCMap<asCStringPointer,eTokenType> *map;
  341. int maxLength;
  342. // Optimization for large array init-lists
  343. // This makes a significant improvement when parsing
  344. // very long initialization lists, yet doesn't cause
  345. // a noticeable impact in other situations.
  346. if (source[0] == ',')
  347. {
  348. tokenType = ttListSeparator;
  349. tokenLength = 1;
  350. return true;
  351. }
  352. if( (source[0] >= 'a' && source[0] <= 'z') ||
  353. (source[0] >= 'A' && source[0] <= 'Z') )
  354. {
  355. map = &alphaKeywordMap;
  356. // 'interface' is the longest alpha keyword
  357. maxLength = sourceLength > 9 ? 9 : int(sourceLength);
  358. }
  359. else
  360. {
  361. map = &nonAlphaKeywordMap;
  362. // '>>>=' is the longest non-alpha keyword
  363. maxLength = sourceLength > 4 ? 4 : int(sourceLength);
  364. }
  365. // Find the longest keyword that matches the start of the source string
  366. while( maxLength > 0 )
  367. {
  368. asSMapNode<asCStringPointer, eTokenType> *cursor;
  369. if( map->MoveTo(&cursor, asCStringPointer(source, maxLength)) )
  370. {
  371. // Tokens that end with a character that can be part of an
  372. // identifier require an extra verification to guarantee that
  373. // we don't split an identifier token, e.g. the "!is" token
  374. // and the tokens "!" and "isTrue" in the "!isTrue" expression.
  375. if( maxLength < int(sourceLength) &&
  376. ((source[maxLength-1] >= 'a' && source[maxLength-1] <= 'z') ||
  377. (source[maxLength-1] >= 'A' && source[maxLength-1] <= 'Z')) &&
  378. ((source[maxLength] >= 'a' && source[maxLength] <= 'z') ||
  379. (source[maxLength] >= 'A' && source[maxLength] <= 'Z') ||
  380. (source[maxLength] >= '0' && source[maxLength] <= '9') ||
  381. (source[maxLength] == '_')) )
  382. {
  383. // The token doesn't really match, even though
  384. // the start of the source matches the token
  385. maxLength--;
  386. continue;
  387. }
  388. tokenType = cursor->value;
  389. tokenLength = maxLength;
  390. return true;
  391. }
  392. maxLength--;
  393. }
  394. return false;
  395. }
  396. END_AS_NAMESPACE