tokenizer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef _TOKENIZER_H_
  23. #define _TOKENIZER_H_
  24. //Includes
  25. #ifndef _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. #ifndef _STREAM_H_
  29. #include "core/stream/stream.h"
  30. #endif
  31. #ifndef _TVECTOR_H_
  32. #include "core/util/tVector.h"
  33. #endif
  34. class SizedStream;
  35. class Tokenizer
  36. {
  37. public:
  38. enum
  39. {
  40. MaxTokenSize = 1023
  41. };
  42. private:
  43. char mFileName[1024];
  44. char* mpBuffer;
  45. U32 mBufferSize;
  46. S32 mCurrPos;
  47. S32 mStartPos;
  48. bool mTokenIsQuoted;
  49. char mCurrTokenBuffer[MaxTokenSize + 1];
  50. bool mTokenIsCurrent;
  51. char* mSingleTokens;
  52. Vector<U32> mLinePositions;
  53. public:
  54. Tokenizer();
  55. ~Tokenizer();
  56. bool openFile(const char* pFileName);
  57. bool openFile(Stream* pStream);
  58. void setBuffer(const char* buffer, U32 bufferSize);
  59. void setSingleTokens(const char* singleTokens);
  60. void buildLinePositions();
  61. bool advanceToken(const bool crossLine, const bool assertAvailable = false);
  62. bool regressToken(const bool crossLine);
  63. bool tokenAvailable();
  64. const char* getToken() const;
  65. const char* getNextToken();
  66. bool tokenICmp(const char* pCmp) const;
  67. bool tokenIsQuoted() const { return mTokenIsQuoted; }
  68. bool findToken(const char* pCmp);
  69. bool findToken(U32 start, const char* pCmp);
  70. const char* getFileName() const { return mFileName; }
  71. U32 getLinePosition(const U32 pos, U32 lowIndex = 0, S32 highIndex = -1);
  72. U32 getCurrentLine();
  73. U32 getTokenLineOffset();
  74. U32 getCurrentPos() const { return mCurrPos; }
  75. bool setCurrentPos(U32 pos);
  76. // Resets our active data but leaves the buffer intact
  77. bool reset();
  78. // Clears the buffer and resets the active data
  79. bool clear();
  80. bool endOfFile();
  81. };
  82. #endif //_TOKENIZER_H_