tokenizer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/tokenizer.h"
  23. #include "platform/platform.h"
  24. #include "core/stream/fileStream.h"
  25. #include "core/strings/stringFunctions.h"
  26. #include "core/util/safeDelete.h"
  27. Tokenizer::Tokenizer()
  28. {
  29. dMemset(mFileName, 0, sizeof(mFileName));
  30. mpBuffer = NULL;
  31. mBufferSize = 0;
  32. mStartPos = 0;
  33. mCurrPos = 0;
  34. mTokenIsQuoted = false;
  35. dMemset(mCurrTokenBuffer, 0, sizeof(mCurrTokenBuffer));
  36. mTokenIsCurrent = false;
  37. mSingleTokens = NULL;
  38. VECTOR_SET_ASSOCIATION(mLinePositions);
  39. }
  40. Tokenizer::~Tokenizer()
  41. {
  42. clear();
  43. }
  44. bool Tokenizer::openFile(const char* pFileName)
  45. {
  46. AssertFatal(mFileName[0] == '\0', "Reuse of Tokenizers not allowed!");
  47. FileStream* pStream = new FileStream;
  48. if (pStream->open(pFileName, Torque::FS::File::Read) == false)
  49. {
  50. delete pStream;
  51. return false;
  52. }
  53. dStrcpy(mFileName, pFileName, 1024);
  54. mBufferSize = pStream->getStreamSize();
  55. mpBuffer = new char[mBufferSize];
  56. pStream->read(mBufferSize, mpBuffer);
  57. pStream->close();
  58. delete pStream;
  59. reset();
  60. buildLinePositions();
  61. return true;
  62. }
  63. bool Tokenizer::openFile(Stream* pStream)
  64. {
  65. mBufferSize = pStream->getStreamSize();
  66. mpBuffer = new char[mBufferSize];
  67. pStream->read(mBufferSize, mpBuffer);
  68. reset();
  69. buildLinePositions();
  70. return true;
  71. }
  72. void Tokenizer::setBuffer(const char* buffer, U32 bufferSize)
  73. {
  74. if (mpBuffer)
  75. {
  76. SAFE_DELETE_ARRAY(mpBuffer);
  77. mBufferSize = 0;
  78. }
  79. mBufferSize = bufferSize;
  80. mpBuffer = new char[mBufferSize + 1];
  81. dStrcpy(mpBuffer, buffer, mBufferSize + 1);
  82. reset();
  83. buildLinePositions();
  84. }
  85. void Tokenizer::setSingleTokens(const char* singleTokens)
  86. {
  87. if (mSingleTokens)
  88. SAFE_DELETE(mSingleTokens);
  89. if (singleTokens)
  90. mSingleTokens = dStrdup(singleTokens);
  91. }
  92. bool Tokenizer::reset()
  93. {
  94. mStartPos = 0;
  95. mCurrPos = 0;
  96. mTokenIsQuoted = false;
  97. dMemset(mCurrTokenBuffer, 0, sizeof(mCurrTokenBuffer));
  98. mTokenIsCurrent = false;
  99. return true;
  100. }
  101. bool Tokenizer::clear()
  102. {
  103. // Delete our buffer
  104. if (mpBuffer)
  105. SAFE_DELETE_ARRAY(mpBuffer);
  106. // Reset the buffer size
  107. mBufferSize = 0;
  108. // Reset our active data
  109. reset();
  110. // Clear our line positions
  111. mLinePositions.clear();
  112. // Reset our file name
  113. dMemset(mFileName, 0, 1024);
  114. // Wipe the single tokens
  115. setSingleTokens(NULL);
  116. return true;
  117. }
  118. bool Tokenizer::setCurrentPos(U32 pos)
  119. {
  120. mCurrPos = pos;
  121. mTokenIsCurrent = false;
  122. return advanceToken(true);
  123. }
  124. void Tokenizer::buildLinePositions()
  125. {
  126. if (mBufferSize == 0)
  127. return;
  128. // We can safely assume that the first line is at position 0
  129. mLinePositions.push_back(0);
  130. U32 currPos = 0;
  131. while (currPos + 1 < mBufferSize)
  132. {
  133. // Windows line ending
  134. if (mpBuffer[currPos] == '\r' && mpBuffer[currPos + 1] == '\n')
  135. {
  136. currPos += 2;
  137. mLinePositions.push_back(currPos);
  138. }
  139. // Not sure if this ever happens but just in case
  140. else if (mpBuffer[currPos] == '\n' && mpBuffer[currPos + 1] == '\r')
  141. {
  142. currPos += 2;
  143. mLinePositions.push_back(currPos);
  144. }
  145. // Unix line endings should only have a single line break character
  146. else if (mpBuffer[currPos] == '\n' || mpBuffer[currPos] == '\r')
  147. {
  148. currPos++;
  149. mLinePositions.push_back(currPos);
  150. }
  151. else
  152. currPos++;
  153. }
  154. }
  155. U32 Tokenizer::getLinePosition(const U32 pos, U32 lowIndex, S32 highIndex)
  156. {
  157. // If we have one or less lines then
  158. // the result is easy
  159. if (mLinePositions.size() <= 1)
  160. return 0;
  161. // Now that we know we have at least one position
  162. // we can do a quick test against the last line
  163. if (pos >= mLinePositions.last())
  164. return mLinePositions.size() - 1;
  165. // If this is the beginning of the search
  166. // set a good starting point (the middle)
  167. if (highIndex < 0)
  168. highIndex = mLinePositions.size() - 1;
  169. // Just in case bad values got handed in
  170. if (lowIndex > highIndex)
  171. lowIndex = highIndex;
  172. // Compute our test index (middle)
  173. U32 testIndex = (lowIndex + highIndex) / 2;
  174. // Make sure that our test indices are valid
  175. if (testIndex >= mLinePositions.size() ||
  176. testIndex + 1 >= mLinePositions.size())
  177. return mLinePositions.size() - 1;
  178. // See if we are already at the right line
  179. if (pos >= mLinePositions[testIndex] && pos < mLinePositions[testIndex + 1])
  180. return testIndex;
  181. if (pos < mLinePositions[testIndex])
  182. highIndex = testIndex;
  183. else
  184. lowIndex = testIndex;
  185. return getLinePosition(pos, lowIndex, highIndex);
  186. }
  187. U32 Tokenizer::getCurrentLine()
  188. {
  189. // Binary search for the line number whose
  190. // position is equal to or lower than the
  191. // current position
  192. return getLinePosition(mStartPos);
  193. }
  194. U32 Tokenizer::getTokenLineOffset()
  195. {
  196. U32 lineNumber = getCurrentLine();
  197. if (lineNumber >= mLinePositions.size())
  198. return 0;
  199. U32 linePosition = mLinePositions[lineNumber];
  200. if (linePosition >= mStartPos)
  201. return 0;
  202. return mStartPos - linePosition;
  203. }
  204. bool Tokenizer::advanceToken(const bool crossLine, const bool assertAvail)
  205. {
  206. if (mTokenIsCurrent == true)
  207. {
  208. AssertFatal(mCurrTokenBuffer[0] != '\0', "No token, but marked as current?");
  209. mTokenIsCurrent = false;
  210. return true;
  211. }
  212. U32 currPosition = 0;
  213. mCurrTokenBuffer[0] = '\0';
  214. mTokenIsQuoted = false;
  215. // Store the beginning of the previous advance
  216. // and the beginning of the current advance
  217. mStartPos = mCurrPos;
  218. while (mCurrPos < mBufferSize)
  219. {
  220. char c = mpBuffer[mCurrPos];
  221. bool cont = true;
  222. if (mSingleTokens && dStrchr(mSingleTokens, c))
  223. {
  224. if (currPosition == 0)
  225. {
  226. mCurrTokenBuffer[currPosition++] = c;
  227. mCurrPos++;
  228. cont = false;
  229. break;
  230. }
  231. else
  232. {
  233. // End of token
  234. cont = false;
  235. }
  236. }
  237. else
  238. {
  239. switch (c)
  240. {
  241. case ' ':
  242. case '\t':
  243. if (currPosition == 0)
  244. {
  245. // Token hasn't started yet...
  246. mCurrPos++;
  247. }
  248. else
  249. {
  250. // End of token
  251. mCurrPos++;
  252. cont = false;
  253. }
  254. break;
  255. case '\r':
  256. case '\n':
  257. if (crossLine == true)
  258. {
  259. // Windows line ending
  260. if (mpBuffer[mCurrPos] == '\r' && mpBuffer[mCurrPos + 1] == '\n')
  261. mCurrPos += 2;
  262. // Not sure if this ever happens but just in case
  263. else if (mpBuffer[mCurrPos] == '\n' && mpBuffer[mCurrPos + 1] == '\r')
  264. mCurrPos += 2;
  265. // Unix line endings should only have a single line break character
  266. else
  267. mCurrPos++;
  268. }
  269. else
  270. {
  271. cont = false;
  272. break;
  273. }
  274. break;
  275. default:
  276. if (c == '\"' || c == '\'')
  277. {
  278. // Quoted token
  279. U32 startLine = getCurrentLine();
  280. mCurrPos++;
  281. // Store the beginning of the token
  282. mStartPos = mCurrPos;
  283. while (mpBuffer[mCurrPos] != c)
  284. {
  285. AssertISV(mCurrPos < mBufferSize,
  286. avar("End of file before quote closed. Quote started: (%s: %d)",
  287. getFileName(), startLine));
  288. AssertISV((mpBuffer[mCurrPos] != '\n' && mpBuffer[mCurrPos] != '\r'),
  289. avar("End of line reached before end of quote. Quote started: (%s: %d)",
  290. getFileName(), startLine));
  291. mCurrTokenBuffer[currPosition++] = mpBuffer[mCurrPos++];
  292. }
  293. mTokenIsQuoted = true;
  294. mCurrPos++;
  295. cont = false;
  296. }
  297. else if (c == '/' && mpBuffer[mCurrPos+1] == '/')
  298. {
  299. // Line quote...
  300. if (currPosition == 0)
  301. {
  302. // continue to end of line, then let crossLine determine on the next pass
  303. while (mCurrPos < mBufferSize && (mpBuffer[mCurrPos] != '\n' && mpBuffer[mCurrPos] != '\r'))
  304. mCurrPos++;
  305. }
  306. else
  307. {
  308. // This is the end of the token. Continue to EOL
  309. while (mCurrPos < mBufferSize && (mpBuffer[mCurrPos] != '\n' && mpBuffer[mCurrPos] != '\r'))
  310. mCurrPos++;
  311. cont = false;
  312. }
  313. }
  314. else if (c == '/' && mpBuffer[mCurrPos+1] == '*')
  315. {
  316. // Block quote...
  317. if (currPosition == 0)
  318. {
  319. // continue to end of block, then let crossLine determine on the next pass
  320. while (mCurrPos < mBufferSize - 1 && (mpBuffer[mCurrPos] != '*' || mpBuffer[mCurrPos + 1] != '/'))
  321. mCurrPos++;
  322. if (mCurrPos < mBufferSize - 1)
  323. mCurrPos += 2;
  324. }
  325. else
  326. {
  327. // This is the end of the token. Continue to EOL
  328. while (mCurrPos < mBufferSize - 1 && (mpBuffer[mCurrPos] != '*' || mpBuffer[mCurrPos + 1] != '/'))
  329. mCurrPos++;
  330. if (mCurrPos < mBufferSize - 1)
  331. mCurrPos += 2;
  332. cont = false;
  333. }
  334. }
  335. else
  336. {
  337. // If this is the first non-token character then store the
  338. // beginning of the token
  339. if (currPosition == 0)
  340. mStartPos = mCurrPos;
  341. mCurrTokenBuffer[currPosition++] = c;
  342. mCurrPos++;
  343. }
  344. break;
  345. }
  346. }
  347. if (cont == false)
  348. break;
  349. }
  350. mCurrTokenBuffer[currPosition] = '\0';
  351. if (assertAvail == true)
  352. AssertISV(currPosition != 0, avar("Error parsing: %s at or around line: %d", getFileName(), getCurrentLine()));
  353. if (mCurrPos == mBufferSize)
  354. return false;
  355. return true;
  356. }
  357. bool Tokenizer::regressToken(const bool crossLine)
  358. {
  359. if (mTokenIsCurrent == true)
  360. {
  361. AssertFatal(mCurrTokenBuffer[0] != '\0', "No token, but marked as current?");
  362. mTokenIsCurrent = false;
  363. return true;
  364. }
  365. U32 currPosition = 0;
  366. mCurrTokenBuffer[0] = '\0';
  367. mTokenIsQuoted = false;
  368. // Store the beginning of the previous advance
  369. // and the beginning of the current advance
  370. mCurrPos = mStartPos;
  371. // Back up to the first character of the previous token
  372. mStartPos--;
  373. while (mStartPos > 0)
  374. {
  375. char c = mpBuffer[mStartPos];
  376. bool cont = true;
  377. if (mSingleTokens && dStrchr(mSingleTokens, c))
  378. {
  379. if (currPosition == 0)
  380. {
  381. mCurrTokenBuffer[currPosition++] = c;
  382. mStartPos--;
  383. cont = false;
  384. break;
  385. }
  386. else
  387. {
  388. // End of token
  389. cont = false;
  390. }
  391. }
  392. else
  393. {
  394. switch (c)
  395. {
  396. case ' ':
  397. case '\t':
  398. if (currPosition == 0)
  399. {
  400. // Token hasn't started yet...
  401. mStartPos--;
  402. }
  403. else
  404. {
  405. // End of token
  406. mStartPos--;
  407. cont = false;
  408. }
  409. break;
  410. case '\r':
  411. case '\n':
  412. if (crossLine == true && currPosition == 0)
  413. {
  414. // Windows line ending
  415. if (mStartPos > 0 && mpBuffer[mStartPos] == '\r' && mpBuffer[mStartPos - 1] == '\n')
  416. mStartPos -= 2;
  417. // Not sure if this ever happens but just in case
  418. else if (mStartPos > 0 && mpBuffer[mStartPos] == '\n' && mpBuffer[mStartPos - 1] == '\r')
  419. mStartPos -= 2;
  420. // Unix line endings should only have a single line break character
  421. else
  422. mStartPos--;
  423. }
  424. else
  425. {
  426. cont = false;
  427. break;
  428. }
  429. break;
  430. default:
  431. if (c == '\"' || c == '\'')
  432. {
  433. // Quoted token
  434. U32 endLine = getCurrentLine();
  435. mStartPos--;
  436. while (mpBuffer[mStartPos] != c)
  437. {
  438. AssertISV(mStartPos < 0,
  439. avar("Beginning of file reached before finding begin quote. Quote ended: (%s: %d)",
  440. getFileName(), endLine));
  441. mCurrTokenBuffer[currPosition++] = mpBuffer[mStartPos--];
  442. }
  443. mTokenIsQuoted = true;
  444. mStartPos--;
  445. cont = false;
  446. }
  447. else if (c == '/' && mStartPos > 0 && mpBuffer[mStartPos - 1] == '/')
  448. {
  449. // Line quote...
  450. // Clear out anything saved already
  451. currPosition = 0;
  452. mStartPos -= 2;
  453. }
  454. else
  455. {
  456. mCurrTokenBuffer[currPosition++] = c;
  457. mStartPos--;
  458. }
  459. break;
  460. }
  461. }
  462. if (cont == false)
  463. break;
  464. }
  465. mCurrTokenBuffer[currPosition] = '\0';
  466. // Reveres the token
  467. for (U32 i = 0; i < currPosition / 2; i++)
  468. {
  469. char c = mCurrTokenBuffer[i];
  470. mCurrTokenBuffer[i] = mCurrTokenBuffer[currPosition - i - 1];
  471. mCurrTokenBuffer[currPosition - i - 1] = c;
  472. }
  473. mStartPos++;
  474. if (mStartPos == mCurrPos)
  475. return false;
  476. return true;
  477. }
  478. bool Tokenizer::tokenAvailable()
  479. {
  480. // Note: this implies that when advanceToken(false) fails, it must cap the
  481. // token buffer.
  482. //
  483. return mCurrTokenBuffer[0] != '\0';
  484. }
  485. const char* Tokenizer::getToken() const
  486. {
  487. return mCurrTokenBuffer;
  488. }
  489. const char* Tokenizer::getNextToken()
  490. {
  491. advanceToken(true);
  492. return getToken();
  493. }
  494. bool Tokenizer::tokenICmp(const char* pCmp) const
  495. {
  496. return dStricmp(mCurrTokenBuffer, pCmp) == 0;
  497. }
  498. bool Tokenizer::findToken(U32 start, const char* pCmp)
  499. {
  500. // Move to the start
  501. setCurrentPos(start);
  502. // In case the first token is what we are looking for
  503. if (tokenICmp(pCmp))
  504. return true;
  505. // Loop through the file and see if the token exists
  506. while (advanceToken(true))
  507. {
  508. if (tokenICmp(pCmp))
  509. return true;
  510. }
  511. return false;
  512. }
  513. bool Tokenizer::findToken(const char* pCmp)
  514. {
  515. return findToken(0, pCmp);
  516. }
  517. bool Tokenizer::endOfFile()
  518. {
  519. if (mCurrPos < mBufferSize)
  520. return false;
  521. else
  522. return true;
  523. }