tokenizer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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);
  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);
  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
  315. {
  316. // If this is the first non-token character then store the
  317. // beginning of the token
  318. if (currPosition == 0)
  319. mStartPos = mCurrPos;
  320. mCurrTokenBuffer[currPosition++] = c;
  321. mCurrPos++;
  322. }
  323. break;
  324. }
  325. }
  326. if (cont == false)
  327. break;
  328. }
  329. mCurrTokenBuffer[currPosition] = '\0';
  330. if (assertAvail == true)
  331. AssertISV(currPosition != 0, avar("Error parsing: %s at or around line: %d", getFileName(), getCurrentLine()));
  332. if (mCurrPos == mBufferSize)
  333. return false;
  334. return true;
  335. }
  336. bool Tokenizer::regressToken(const bool crossLine)
  337. {
  338. if (mTokenIsCurrent == true)
  339. {
  340. AssertFatal(mCurrTokenBuffer[0] != '\0', "No token, but marked as current?");
  341. mTokenIsCurrent = false;
  342. return true;
  343. }
  344. U32 currPosition = 0;
  345. mCurrTokenBuffer[0] = '\0';
  346. mTokenIsQuoted = false;
  347. // Store the beginning of the previous advance
  348. // and the beginning of the current advance
  349. mCurrPos = mStartPos;
  350. // Back up to the first character of the previous token
  351. mStartPos--;
  352. while (mStartPos > 0)
  353. {
  354. char c = mpBuffer[mStartPos];
  355. bool cont = true;
  356. if (mSingleTokens && dStrchr(mSingleTokens, c))
  357. {
  358. if (currPosition == 0)
  359. {
  360. mCurrTokenBuffer[currPosition++] = c;
  361. mStartPos--;
  362. cont = false;
  363. break;
  364. }
  365. else
  366. {
  367. // End of token
  368. cont = false;
  369. }
  370. }
  371. else
  372. {
  373. switch (c)
  374. {
  375. case ' ':
  376. case '\t':
  377. if (currPosition == 0)
  378. {
  379. // Token hasn't started yet...
  380. mStartPos--;
  381. }
  382. else
  383. {
  384. // End of token
  385. mStartPos--;
  386. cont = false;
  387. }
  388. break;
  389. case '\r':
  390. case '\n':
  391. if (crossLine == true && currPosition == 0)
  392. {
  393. // Windows line ending
  394. if (mStartPos > 0 && mpBuffer[mStartPos] == '\r' && mpBuffer[mStartPos - 1] == '\n')
  395. mStartPos -= 2;
  396. // Not sure if this ever happens but just in case
  397. else if (mStartPos > 0 && mpBuffer[mStartPos] == '\n' && mpBuffer[mStartPos - 1] == '\r')
  398. mStartPos -= 2;
  399. // Unix line endings should only have a single line break character
  400. else
  401. mStartPos--;
  402. }
  403. else
  404. {
  405. cont = false;
  406. break;
  407. }
  408. break;
  409. default:
  410. if (c == '\"' || c == '\'')
  411. {
  412. // Quoted token
  413. U32 endLine = getCurrentLine();
  414. mStartPos--;
  415. while (mpBuffer[mStartPos] != c)
  416. {
  417. AssertISV(mStartPos < 0,
  418. avar("Beginning of file reached before finding begin quote. Quote ended: (%s: %d)",
  419. getFileName(), endLine));
  420. mCurrTokenBuffer[currPosition++] = mpBuffer[mStartPos--];
  421. }
  422. mTokenIsQuoted = true;
  423. mStartPos--;
  424. cont = false;
  425. }
  426. else if (c == '/' && mStartPos > 0 && mpBuffer[mStartPos - 1] == '/')
  427. {
  428. // Line quote...
  429. // Clear out anything saved already
  430. currPosition = 0;
  431. mStartPos -= 2;
  432. }
  433. else
  434. {
  435. mCurrTokenBuffer[currPosition++] = c;
  436. mStartPos--;
  437. }
  438. break;
  439. }
  440. }
  441. if (cont == false)
  442. break;
  443. }
  444. mCurrTokenBuffer[currPosition] = '\0';
  445. // Reveres the token
  446. for (U32 i = 0; i < currPosition / 2; i++)
  447. {
  448. char c = mCurrTokenBuffer[i];
  449. mCurrTokenBuffer[i] = mCurrTokenBuffer[currPosition - i - 1];
  450. mCurrTokenBuffer[currPosition - i - 1] = c;
  451. }
  452. mStartPos++;
  453. if (mStartPos == mCurrPos)
  454. return false;
  455. return true;
  456. }
  457. bool Tokenizer::tokenAvailable()
  458. {
  459. // Note: this implies that when advanceToken(false) fails, it must cap the
  460. // token buffer.
  461. //
  462. return mCurrTokenBuffer[0] != '\0';
  463. }
  464. const char* Tokenizer::getToken() const
  465. {
  466. return mCurrTokenBuffer;
  467. }
  468. const char* Tokenizer::getNextToken()
  469. {
  470. advanceToken(true);
  471. return getToken();
  472. }
  473. bool Tokenizer::tokenICmp(const char* pCmp) const
  474. {
  475. return dStricmp(mCurrTokenBuffer, pCmp) == 0;
  476. }
  477. bool Tokenizer::findToken(U32 start, const char* pCmp)
  478. {
  479. // Move to the start
  480. setCurrentPos(start);
  481. // In case the first token is what we are looking for
  482. if (tokenICmp(pCmp))
  483. return true;
  484. // Loop through the file and see if the token exists
  485. while (advanceToken(true))
  486. {
  487. if (tokenICmp(pCmp))
  488. return true;
  489. }
  490. return false;
  491. }
  492. bool Tokenizer::findToken(const char* pCmp)
  493. {
  494. return findToken(0, pCmp);
  495. }
  496. bool Tokenizer::endOfFile()
  497. {
  498. if (mCurrPos < mBufferSize)
  499. return false;
  500. else
  501. return true;
  502. }