as_tokenizer.cpp 12 KB

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