Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.8 KiB

  1. /*****************************************************************************
  2. *
  3. * token.c
  4. *
  5. * Tokenization.
  6. *
  7. * The tokenizer always returns unsnapped tokens.
  8. *
  9. * We avoid the traditional tokenizer problems of ``giant comment'' and
  10. * ``giant string'' by using a dynamic token buffer.
  11. *
  12. * All tokens are stacked into the token buffer. If you need the token
  13. * to be persistent, you have to save it somewhere else.
  14. *
  15. *****************************************************************************/
  16. #include "m4.h"
  17. /*****************************************************************************
  18. *
  19. * typGetComTch
  20. *
  21. * Scan and consume a comment token, returning typQuo
  22. * because comments and quotes are essentially the same thing.
  23. * tch contains the open-comment.
  24. *
  25. * Comments do not nest.
  26. *
  27. *****************************************************************************/
  28. TYP STDCALL
  29. typGetComTch(TCH tch)
  30. {
  31. AddArgTch(tch); /* Save the comment start */
  32. do {
  33. tch = tchGet();
  34. AddArgTch(tch);
  35. if (tch == tchMagic) {
  36. /* Ooh, regurgitating a magic token - these consist of two bytes */
  37. tch = tchGet();
  38. if (tch == tchEof) {
  39. Die("EOF in comment");
  40. }
  41. AddArgTch(tch);
  42. }
  43. } while (!fRcomTch(tch));
  44. return typQuo;
  45. }
  46. /*****************************************************************************
  47. *
  48. * typGetQuoTch
  49. *
  50. * Scan and consume a quote token, returning typQuo.
  51. * tch contains the open-quote.
  52. *
  53. *****************************************************************************/
  54. TYP STDCALL
  55. typGetQuoTch(TCH tch)
  56. {
  57. int iDepth = 1;
  58. for (;;) {
  59. tch = tchGet();
  60. if (tch == tchMagic) {
  61. /* SOMEDAY -- Should unget so that Die won't see past EOF */
  62. /* Ooh, regurgitating a magic token - these consist of two bytes */
  63. tch = tchGet();
  64. if (tch == tchEof) {
  65. Die("EOF in quote");
  66. }
  67. AddArgTch(tchMagic); /* Add the magic prefix */
  68. /* Fallthrough will add tch */
  69. } else if (fLquoTch(tch)) {
  70. ++iDepth;
  71. } else if (fRquoTch(tch)) {
  72. if (--iDepth == 0) {
  73. break; /* Final Rquo found */
  74. }
  75. }
  76. AddArgTch(tch);
  77. }
  78. return typQuo;
  79. }
  80. /*****************************************************************************
  81. *
  82. * typGetIdentTch
  83. *
  84. * Scan and consume an identifier token, returning typId.
  85. * tch contains the first character of the identifier.
  86. *
  87. *****************************************************************************/
  88. TYP STDCALL
  89. typGetIdentTch(TCH tch)
  90. {
  91. do {
  92. AddArgTch(tch);
  93. tch = tchGet();
  94. } while (fIdentTch(tch));
  95. UngetTch(tch);
  96. return typId;
  97. }
  98. /*****************************************************************************
  99. *
  100. * typGetMagicTch
  101. *
  102. * Scan and consume a magic token, returning the token type.
  103. * Magics are out-of-band gizmos that get inserted into the
  104. * input stream via the tchMagic escape.
  105. *
  106. *****************************************************************************/
  107. TYP STDCALL
  108. typGetMagicTch(TCH tch)
  109. {
  110. AddArgTch(tch);
  111. tch = tchGet();
  112. Assert(fValidMagicTch(tch));
  113. AddArgTch(tch);
  114. return typMagic;
  115. }
  116. /*****************************************************************************
  117. *
  118. * typGetPuncTch
  119. *
  120. * Scan and consume a punctuation token, returning the token type.
  121. *
  122. * It is here that comments are recognized.
  123. *
  124. *
  125. * LATER - It is here where consecutive typPunc's are coalesced.
  126. * This would speed up top-level scanning.
  127. * Be careful not to coalesce a comma!
  128. * Lparen is okay because xtok handles that one.
  129. * Whitespace is also okay because xtok handles those too.
  130. *
  131. *****************************************************************************/
  132. TYP STDCALL
  133. typGetPuncTch(TCH tch)
  134. {
  135. AddArgTch(tch);
  136. return typPunc;
  137. }
  138. /*****************************************************************************
  139. *
  140. * typGetPtok
  141. *
  142. * Scan and consume a snapped token, returning the token type.
  143. *
  144. *****************************************************************************/
  145. TYP STDCALL
  146. typGetPtok(PTOK ptok)
  147. {
  148. TCH tch;
  149. TYP typ;
  150. OpenArgPtok(ptok);
  151. tch = tchGet();
  152. if (fInitialIdentTch(tch)) {
  153. typ = typGetIdentTch(tch);
  154. } else if (fLcomTch(tch)) {
  155. typ = typGetComTch(tch);
  156. } else if (fLquoTch(tch)) {
  157. typ = typGetQuoTch(tch);
  158. } else if (fMagicTch(tch)) {
  159. typ = typGetMagicTch(tch);
  160. } else {
  161. typ = typGetPuncTch(tch);
  162. }
  163. CloseArgPtok(ptok);
  164. SnapArgPtok(ptok);
  165. return typ;
  166. }