PARSE.CPP 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "qe3.h"
  23. char token[MAXTOKEN];
  24. bool unget;
  25. const char *script_p;
  26. int scriptline;
  27. void StartTokenParsing (const char *data)
  28. {
  29. scriptline = 1;
  30. script_p = data;
  31. unget = false;
  32. }
  33. bool WINAPI GetToken (bool crossline)
  34. {
  35. char *token_p;
  36. if (unget) // is a token allready waiting?
  37. {
  38. unget = false;
  39. return true;
  40. }
  41. //
  42. // skip space
  43. //
  44. skipspace:
  45. while (*script_p <= 32)
  46. {
  47. if (!*script_p)
  48. {
  49. if (!crossline)
  50. common->Printf("Warning: Line %i is incomplete [01]\n",scriptline);
  51. return false;
  52. }
  53. if (*script_p++ == '\n')
  54. {
  55. if (!crossline)
  56. common->Printf("Warning: Line %i is incomplete [02]\n",scriptline);
  57. scriptline++;
  58. }
  59. }
  60. if (script_p[0] == '/' && script_p[1] == '/') // comment field
  61. {
  62. if (!crossline)
  63. common->Printf("Warning: Line %i is incomplete [03]\n",scriptline);
  64. while (*script_p++ != '\n')
  65. if (!*script_p)
  66. {
  67. if (!crossline)
  68. common->Printf("Warning: Line %i is incomplete [04]\n",scriptline);
  69. return false;
  70. }
  71. goto skipspace;
  72. }
  73. //
  74. // copy token
  75. //
  76. token_p = token;
  77. if (*script_p == '"')
  78. {
  79. script_p++;
  80. //if (*script_p == '"') // handle double quotes i suspect they are put in by other editors cccasionally
  81. // script_p++;
  82. while ( *script_p != '"' )
  83. {
  84. if (!*script_p)
  85. Error ("EOF inside quoted token");
  86. *token_p++ = *script_p++;
  87. if (token_p == &token[MAXTOKEN])
  88. Error ("Token too large on line %i",scriptline);
  89. }
  90. script_p++;
  91. //if (*script_p == '"') // handle double quotes i suspect they are put in by other editors cccasionally
  92. // script_p++;
  93. }
  94. else while ( *script_p > 32 )
  95. {
  96. *token_p++ = *script_p++;
  97. if (token_p == &token[MAXTOKEN])
  98. Error ("Token too large on line %i",scriptline);
  99. }
  100. *token_p = 0;
  101. return true;
  102. }
  103. void WINAPI UngetToken (void)
  104. {
  105. unget = true;
  106. }
  107. /*
  108. ==============
  109. TokenAvailable
  110. Returns true if there is another token on the line
  111. ==============
  112. */
  113. bool TokenAvailable (void)
  114. {
  115. const char *search_p;
  116. search_p = script_p;
  117. while ( *search_p <= 32)
  118. {
  119. if (*search_p == '\n')
  120. return false;
  121. if (*search_p == 0)
  122. return false;
  123. search_p++;
  124. }
  125. if (*search_p == ';')
  126. return false;
  127. return true;
  128. }