gcsx_tokenize.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* GCSx
  2. ** TOKENIZE.H
  3. **
  4. ** Script tokenization (to feed to compiler)
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_TOKENIZE_H_
  24. #define __GCSx_TOKENIZE_H_
  25. class Tokenizer {
  26. private:
  27. const std::list<std::string>* source;
  28. std::list<std::string>::const_iterator row;
  29. int rowNum;
  30. int col;
  31. int rowLen;
  32. int atNewLine;
  33. int nextCloseBrace;
  34. int errorCount;
  35. int warningCount;
  36. int silent;
  37. int errRow;
  38. int errCol;
  39. char* errBuffer;
  40. struct Token {
  41. int type;
  42. std::string* text;
  43. int rowN;
  44. int colN;
  45. };
  46. // Used for both peeks and bookmarks
  47. int nextBookmarkName;
  48. int cacheRecord;
  49. int bookmarkNew;
  50. std::list<Token> cached;
  51. std::list<Token>::iterator cacheReplay;
  52. std::map<int, std::list<Token>::iterator> bookmarks;
  53. void deallocRange(std::list<Token>::iterator start, std::list<Token>::iterator end);
  54. struct TokenStr {
  55. int type;
  56. const char* text;
  57. };
  58. static const TokenStr tokenStrings[];
  59. static const char* debugText[];
  60. static std::map<std::string, int>* tokenLookup;
  61. static void initTokenLookups();
  62. // All other functions assert not at EOF
  63. // Can never reach EOF until after reading an EOL, however
  64. int atEOF();
  65. char getCharacter(); // Returns '\0' for EOL
  66. void moveNext();
  67. void nextLine(); // Assumes there IS a next line
  68. // If next char is boundary, will return blank
  69. // If reaches EOL, throws 1 and does not move position
  70. // Otherwise, boundary character is next to be retrieved
  71. std::string grabUntil(const char* boundaries) throw_int;
  72. // If next char is not in charset, will return blank
  73. // If reached EOL, ends there; EOL should then be retrieved via getCharacter()
  74. std::string grabWhile(const char* charset);
  75. std::string grabRestOfLine();
  76. public:
  77. Tokenizer(const std::list<std::string>* src);
  78. ~Tokenizer();
  79. static void destroyGlobals();
  80. // Returns true if a new token was retrieved
  81. // False = EOF (always a ENDLINE before this, though)
  82. // Discards comments
  83. // Lowercases everything but strings
  84. // Automatically ensures EOL before a }, and NOT before a { or after a { or }
  85. int nextToken(int& type, std::string& token);
  86. // Look at next token without retrieving it
  87. int peekToken(int& type, std::string& token);
  88. void skipToken();
  89. // Bookmark position within token stream; return to it or cancel bookmark
  90. // Reserved bookmarks:
  91. // 1- when checking an identifier for label status
  92. void bookmarkStore(int name = 0);
  93. void bookmarkReturn(int name = 0);
  94. void bookmarkCancel(int name = 0);
  95. // Creates a unique bookmark name over 1000
  96. int getBookmarkName();
  97. // For error outputting with char/line- outputs at location
  98. // of start of token last retrieved with nextToken OR peekToken
  99. void outputError(const char* text, ...);
  100. void outputWarning(const char* text, ...);
  101. void silentErrors(int newSilent = 1);
  102. void resetErrors();
  103. int numErrors() { return errorCount; }
  104. int numWarnings() { return warningCount; }
  105. };
  106. #endif