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.

362 lines
8.8 KiB

  1. /*****************************************************************************
  2. *
  3. * tok.h
  4. *
  5. *****************************************************************************/
  6. /*****************************************************************************
  7. *
  8. * Tokens
  9. *
  10. * A TOK records a block of characters.
  11. *
  12. * itch = hold-relative offset to beginning of value (if unsnapped)
  13. * ptch -> beginning of value (if snapped)
  14. * ctch = number of tchar's in value
  15. *
  16. * A UTok is an unsnapped token. An STok is a snapped token.
  17. *
  18. *****************************************************************************/
  19. typedef UINT TSFL; /* Token state flags */
  20. #define tsflClosed 1 /* ctch can be used */
  21. #define tsflHeap 2 /* ptch points into process heap */
  22. #define tsflStatic 4 /* ptch points into process static data */
  23. #define tsflScratch 8 /* token is modifiable */
  24. typedef struct TOKEN {
  25. D(SIG sig;)
  26. union {
  27. PTCH ptch;
  28. ITCH itch;
  29. } u;
  30. CTCH ctch;
  31. D(TSFL tsfl;)
  32. } TOK, *PTOK, **PPTOK;
  33. typedef CONST TOK *PCTOK;
  34. typedef int IPTOK, ITOK;
  35. typedef unsigned CTOK;
  36. #define sigUPtok sigABCD('U', 'T', 'o', 'k')
  37. #define sigSPtok sigABCD('S', 'T', 'o', 'k')
  38. #define AssertUPtok(ptok) AssertPNm(ptok, UPtok)
  39. #define AssertSPtok(ptok) AssertPNm(ptok, SPtok)
  40. #define StrMagic(tch) { tchMagic, tch }
  41. #define comma ,
  42. #define DeclareStaticTok(nm, cch, str) \
  43. static TCH rgtch##nm[cch] = str; \
  44. TOK nm = { D(sigSPtok comma) rgtch##nm, cch, D(tsflClosed|tsflStatic) }
  45. #define ctokGrow 256 /* Growth rate of token buffer */
  46. extern PTOK rgtokArgv; /* The token pool */
  47. /*****************************************************************************
  48. *
  49. * Meta-function
  50. *
  51. * fXxPtok(ptok) defines an inline function which returns nonzero
  52. * if the corresponding bit is set. Meaningful only in DEBUG,
  53. * because the information is not tracked in retail.
  54. *
  55. *****************************************************************************/
  56. #ifdef DEBUG
  57. #define fXxPtokX(xx) \
  58. INLINE F f##xx##Ptok(PCTOK ptok) { return ptok->tsfl & tsfl##xx; }
  59. #define fXxPtok(xx) fXxPtokX(xx)
  60. fXxPtok(Closed)
  61. fXxPtok(Heap)
  62. fXxPtok(Static)
  63. fXxPtok(Scratch)
  64. #undef fXxPtok
  65. #undef fXxPtokX
  66. #endif
  67. /*****************************************************************************
  68. *
  69. * ptchPtok
  70. *
  71. * Returns a pointer to the first character in the ptok.
  72. * The token must be snapped.
  73. *
  74. *****************************************************************************/
  75. INLINE PTCH
  76. ptchPtok(PCTOK ptok)
  77. {
  78. AssertSPtok(ptok);
  79. return ptok->u.ptch;
  80. }
  81. /*****************************************************************************
  82. *
  83. * itchPtok
  84. *
  85. * Returns the index of the first character in the ptok.
  86. * The token must not be snapped.
  87. *
  88. *****************************************************************************/
  89. INLINE ITCH
  90. itchPtok(PCTOK ptok)
  91. {
  92. AssertUPtok(ptok);
  93. return ptok->u.itch;
  94. }
  95. /*****************************************************************************
  96. *
  97. * SetPtokItch
  98. *
  99. * Set the itch for a ptok.
  100. * The token must not be snapped.
  101. *
  102. *****************************************************************************/
  103. INLINE void
  104. SetPtokItch(PTOK ptok, ITCH itch)
  105. {
  106. AssertUPtok(ptok);
  107. ptok->u.itch = itch;
  108. }
  109. /*****************************************************************************
  110. *
  111. * SetPtokCtch
  112. *
  113. * Set the ctch for a ptok.
  114. * This closes the token.
  115. *
  116. *****************************************************************************/
  117. INLINE void
  118. SetPtokCtch(PTOK ptok, CTCH ctch)
  119. {
  120. AssertUPtok(ptok);
  121. Assert(!fClosedPtok(ptok));
  122. ptok->ctch = ctch;
  123. #ifdef DEBUG
  124. ptok->tsfl |= tsflClosed;
  125. #endif
  126. }
  127. /*****************************************************************************
  128. *
  129. * SetPtokPtch
  130. *
  131. * Set the ptch for a ptok.
  132. * This snaps the token.
  133. *
  134. *****************************************************************************/
  135. INLINE void
  136. SetPtokPtch(PTOK ptok, PTCH ptch)
  137. {
  138. AssertUPtok(ptok);
  139. ptok->u.ptch = ptch;
  140. D(ptok->sig = sigSPtok);
  141. }
  142. /*****************************************************************************
  143. *
  144. * ctchUPtok
  145. *
  146. * Returns the number of characters in the token.
  147. * The token must not be snapped.
  148. *
  149. *****************************************************************************/
  150. INLINE CTCH
  151. ctchUPtok(PCTOK ptok)
  152. {
  153. AssertUPtok(ptok);
  154. Assert(fClosedPtok(ptok));
  155. return ptok->ctch;
  156. }
  157. /*****************************************************************************
  158. *
  159. * ctchSPtok
  160. *
  161. * Returns the number of characters in the token.
  162. * The token must be snapped.
  163. *
  164. *****************************************************************************/
  165. INLINE CTCH
  166. ctchSPtok(PCTOK ptok)
  167. {
  168. AssertSPtok(ptok);
  169. Assert(fClosedPtok(ptok));
  170. return ptok->ctch;
  171. }
  172. /*****************************************************************************
  173. *
  174. * fNullPtok
  175. *
  176. * Returns nonzero if the token is empty.
  177. * The token must be snapped.
  178. *
  179. *****************************************************************************/
  180. INLINE F
  181. fNullPtok(PCTOK ptok)
  182. {
  183. return ctchSPtok(ptok) == 0;
  184. }
  185. /*****************************************************************************
  186. *
  187. * ptchMaxPtok
  188. *
  189. * Returns a pointer to one past the last character in the token.
  190. * The token must be snapped.
  191. *
  192. *****************************************************************************/
  193. INLINE PTCH
  194. ptchMaxPtok(PCTOK ptok)
  195. {
  196. AssertSPtok(ptok);
  197. return ptchPtok(ptok) + ctchSPtok(ptok);
  198. }
  199. /*****************************************************************************
  200. *
  201. * EatHeadPtokCtch
  202. *
  203. * Delete ctch characters from the beginning of the token.
  204. * A negative number regurgitates characters.
  205. *
  206. * The token must be snapped.
  207. *
  208. * NOTE! This modifies the token.
  209. *
  210. *****************************************************************************/
  211. INLINE void
  212. EatHeadPtokCtch(PTOK ptok, CTCH ctch)
  213. {
  214. AssertSPtok(ptok);
  215. Assert(ctch <= ctchSPtok(ptok));
  216. Assert(fScratchPtok(ptok));
  217. ptok->u.ptch += ctch;
  218. ptok->ctch -= ctch;
  219. }
  220. /*****************************************************************************
  221. *
  222. * EatTailPtokCtch
  223. *
  224. * Delete ctch characters from the end of the token.
  225. *
  226. * The token must be snapped.
  227. *
  228. * NOTE! This modifies the token.
  229. *
  230. *****************************************************************************/
  231. INLINE void
  232. EatTailPtokCtch(PTOK ptok, CTCH ctch)
  233. {
  234. AssertSPtok(ptok);
  235. Assert(ctch <= ctchSPtok(ptok));
  236. Assert(fScratchPtok(ptok));
  237. ptok->ctch -= ctch;
  238. }
  239. /*****************************************************************************
  240. *
  241. * EatTailUPtokCtch
  242. *
  243. * Delete ctch characters from the end of the token.
  244. *
  245. * The token must not be snapped.
  246. *
  247. * NOTE! This modifies the token.
  248. *
  249. *****************************************************************************/
  250. INLINE void
  251. EatTailUPtokCtch(PTOK ptok, CTCH ctch)
  252. {
  253. AssertUPtok(ptok);
  254. Assert(ctch <= ctchUPtok(ptok));
  255. Assert(fScratchPtok(ptok));
  256. ptok->ctch -= ctch;
  257. }
  258. /*****************************************************************************
  259. *
  260. * SetStaticPtokPtchCtch
  261. *
  262. * Initialize everything for a static token.
  263. *
  264. *****************************************************************************/
  265. INLINE void
  266. SetStaticPtokPtchCtch(PTOK ptok, PCTCH ptch, CTCH ctch)
  267. {
  268. D(ptok->sig = sigUPtok);
  269. D(ptok->tsfl = tsflClosed | tsflStatic);
  270. SetPtokPtch(ptok, (PTCH)ptch);
  271. ptok->ctch = ctch;
  272. }
  273. /*****************************************************************************
  274. *
  275. * DupStaticPtokPtok
  276. *
  277. * Copy a snapped token into a static one.
  278. *
  279. *****************************************************************************/
  280. INLINE void
  281. DupStaticPtokPtok(PTOK ptokDst, PCTOK ptokSrc)
  282. {
  283. AssertSPtok(ptokSrc);
  284. SetStaticPtokPtchCtch(ptokDst, ptchPtok(ptokSrc), ctchSPtok(ptokSrc));
  285. }
  286. /*****************************************************************************
  287. *
  288. * Token Types
  289. *
  290. *****************************************************************************/
  291. typedef enum TYP {
  292. typQuo, /* Quoted string (quotes stripped) or comment */
  293. typId, /* Identifier */
  294. typMagic, /* Magic */
  295. typPunc, /* Punctuation */
  296. } TYP;
  297. /*****************************************************************************
  298. *
  299. * token.c
  300. *
  301. *****************************************************************************/
  302. TYP STDCALL typGetPtok(PTOK ptok);
  303. /*****************************************************************************
  304. *
  305. * xtoken.c
  306. *
  307. *****************************************************************************/
  308. extern PTOK ptokTop, ptokMax;
  309. #define itokTop() ((ITOK)(ptokTop - rgtokArgv))
  310. extern CTOK ctokArg;
  311. extern F g_fTrace;
  312. TYP STDCALL typXtokPtok(PTOK ptok);
  313. extern TOK tokTraceLpar, tokRparColonSpace, tokEol;
  314. extern TOK tokEof, tokEoi;