tokens.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <stdio.h>
  4. #include "cpp.h"
  5. static char wbuf[2*OBS];
  6. static char *wbp = wbuf;
  7. /*
  8. * 1 for tokens that don't need whitespace when they get inserted
  9. * by macro expansion
  10. */
  11. static char wstab[] = {
  12. 0, /* END */
  13. 0, /* UNCLASS */
  14. 0, /* NAME */
  15. 0, /* NUMBER */
  16. 0, /* STRING */
  17. 0, /* CCON */
  18. 1, /* NL */
  19. 0, /* WS */
  20. 0, /* DSHARP */
  21. 0, /* EQ */
  22. 0, /* NEQ */
  23. 0, /* LEQ */
  24. 0, /* GEQ */
  25. 0, /* LSH */
  26. 0, /* RSH */
  27. 0, /* LAND */
  28. 0, /* LOR */
  29. 0, /* PPLUS */
  30. 0, /* MMINUS */
  31. 0, /* ARROW */
  32. 1, /* SBRA */
  33. 1, /* SKET */
  34. 1, /* LP */
  35. 1, /* RP */
  36. 0, /* DOT */
  37. 0, /* AND */
  38. 0, /* STAR */
  39. 0, /* PLUS */
  40. 0, /* MINUS */
  41. 0, /* TILDE */
  42. 0, /* NOT */
  43. 0, /* SLASH */
  44. 0, /* PCT */
  45. 0, /* LT */
  46. 0, /* GT */
  47. 0, /* CIRC */
  48. 0, /* OR */
  49. 0, /* QUEST */
  50. 0, /* COLON */
  51. 0, /* ASGN */
  52. 1, /* COMMA */
  53. 0, /* SHARP */
  54. 1, /* SEMIC */
  55. 1, /* CBRA */
  56. 1, /* CKET */
  57. 0, /* ASPLUS */
  58. 0, /* ASMINUS */
  59. 0, /* ASSTAR */
  60. 0, /* ASSLASH */
  61. 0, /* ASPCT */
  62. 0, /* ASCIRC */
  63. 0, /* ASLSH */
  64. 0, /* ASRSH */
  65. 0, /* ASOR */
  66. 0, /* ASAND */
  67. 0, /* ELLIPS */
  68. 0, /* DSHARP1 */
  69. 0, /* NAME1 */
  70. 0, /* DEFINED */
  71. 0, /* UMINUS */
  72. };
  73. void
  74. maketokenrow(int size, Tokenrow *trp)
  75. {
  76. trp->max = size;
  77. if (size>0)
  78. trp->bp = (Token *)domalloc(size*sizeof(Token));
  79. else
  80. trp->bp = NULL;
  81. trp->tp = trp->bp;
  82. trp->lp = trp->bp;
  83. }
  84. Token *
  85. growtokenrow(Tokenrow *trp)
  86. {
  87. int ncur = trp->tp - trp->bp;
  88. int nlast = trp->lp - trp->bp;
  89. trp->max = 3*trp->max/2 + 1;
  90. trp->bp = (Token *)realloc(trp->bp, trp->max*sizeof(Token));
  91. trp->lp = &trp->bp[nlast];
  92. trp->tp = &trp->bp[ncur];
  93. return trp->lp;
  94. }
  95. /*
  96. * Compare a row of tokens, ignoring the content of WS; return !=0 if different
  97. */
  98. int
  99. comparetokens(Tokenrow *tr1, Tokenrow *tr2)
  100. {
  101. Token *tp1, *tp2;
  102. tp1 = tr1->tp;
  103. tp2 = tr2->tp;
  104. if (tr1->lp-tp1 != tr2->lp-tp2)
  105. return 1;
  106. for (; tp1<tr1->lp ; tp1++, tp2++) {
  107. if (tp1->type != tp2->type
  108. || (tp1->wslen==0) != (tp2->wslen==0)
  109. || tp1->len != tp2->len
  110. || strncmp((char*)tp1->t, (char*)tp2->t, tp1->len)!=0)
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. /*
  116. * replace ntok tokens starting at dtr->tp with the contents of str.
  117. * tp ends up pointing just beyond the replacement.
  118. * Canonical whitespace is assured on each side.
  119. */
  120. void
  121. insertrow(Tokenrow *dtr, int ntok, Tokenrow *str)
  122. {
  123. int nrtok = rowlen(str);
  124. dtr->tp += ntok;
  125. adjustrow(dtr, nrtok-ntok);
  126. dtr->tp -= ntok;
  127. movetokenrow(dtr, str);
  128. makespace(dtr);
  129. dtr->tp += nrtok;
  130. makespace(dtr);
  131. }
  132. /*
  133. * make sure there is WS before trp->tp, if tokens might merge in the output
  134. */
  135. void
  136. makespace(Tokenrow *trp)
  137. {
  138. uchar *tt;
  139. Token *tp = trp->tp;
  140. if (tp >= trp->lp)
  141. return;
  142. if (tp->wslen) {
  143. if (tp->flag&XPWS
  144. && (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])) {
  145. tp->wslen = 0;
  146. return;
  147. }
  148. tp->t[-1] = ' ';
  149. return;
  150. }
  151. if (wstab[tp->type] || trp->tp>trp->bp && wstab[(tp-1)->type])
  152. return;
  153. tt = newstring(tp->t, tp->len, 1);
  154. *tt++ = ' ';
  155. tp->t = tt;
  156. tp->wslen = 1;
  157. tp->flag |= XPWS;
  158. }
  159. /*
  160. * Copy an entire tokenrow into another, at tp.
  161. * It is assumed that there is enough space.
  162. * Not strictly conforming.
  163. */
  164. void
  165. movetokenrow(Tokenrow *dtr, Tokenrow *str)
  166. {
  167. int nby;
  168. /* nby = sizeof(Token) * (str->lp - str->bp); */
  169. nby = (char *)str->lp - (char *)str->bp;
  170. memmove(dtr->tp, str->bp, nby);
  171. }
  172. /*
  173. * Move the tokens in a row, starting at tr->tp, rightward by nt tokens;
  174. * nt may be negative (left move).
  175. * The row may need to be grown.
  176. * Non-strictly conforming because of the (char *), but easily fixed
  177. */
  178. void
  179. adjustrow(Tokenrow *trp, int nt)
  180. {
  181. int nby, size;
  182. if (nt==0)
  183. return;
  184. size = (trp->lp - trp->bp) + nt;
  185. while (size > trp->max)
  186. growtokenrow(trp);
  187. /* nby = sizeof(Token) * (trp->lp - trp->tp); */
  188. nby = (char *)trp->lp - (char *)trp->tp;
  189. if (nby)
  190. memmove(trp->tp+nt, trp->tp, nby);
  191. trp->lp += nt;
  192. }
  193. /*
  194. * Copy a row of tokens into the destination holder, allocating
  195. * the space for the contents. Return the destination.
  196. */
  197. Tokenrow *
  198. copytokenrow(Tokenrow *dtr, Tokenrow *str)
  199. {
  200. int len = rowlen(str);
  201. maketokenrow(len, dtr);
  202. movetokenrow(dtr, str);
  203. dtr->lp += len;
  204. return dtr;
  205. }
  206. /*
  207. * Produce a copy of a row of tokens. Start at trp->tp.
  208. * The value strings are copied as well. The first token
  209. * has WS available.
  210. */
  211. Tokenrow *
  212. normtokenrow(Tokenrow *trp)
  213. {
  214. Token *tp;
  215. Tokenrow *ntrp = new(Tokenrow);
  216. int len;
  217. len = trp->lp - trp->tp;
  218. if (len<=0)
  219. len = 1;
  220. maketokenrow(len, ntrp);
  221. for (tp=trp->tp; tp < trp->lp; tp++) {
  222. *ntrp->lp = *tp;
  223. if (tp->len) {
  224. ntrp->lp->t = newstring(tp->t, tp->len, 1);
  225. *ntrp->lp->t++ = ' ';
  226. if (tp->wslen)
  227. ntrp->lp->wslen = 1;
  228. }
  229. ntrp->lp++;
  230. }
  231. if (ntrp->lp > ntrp->bp)
  232. ntrp->bp->wslen = 0;
  233. return ntrp;
  234. }
  235. /*
  236. * Debugging
  237. */
  238. void
  239. peektokens(Tokenrow *trp, char *str)
  240. {
  241. Token *tp;
  242. int c;
  243. tp = trp->tp;
  244. flushout();
  245. if (str)
  246. fprintf(stderr, "%s ", str);
  247. if (tp<trp->bp || tp>trp->lp)
  248. fprintf(stderr, "(tp offset %d) ", tp-trp->bp);
  249. for (tp=trp->bp; tp<trp->lp && tp<trp->bp+32; tp++) {
  250. if (tp->type!=NL) {
  251. c = tp->t[tp->len];
  252. tp->t[tp->len] = 0;
  253. fprintf(stderr, "%s", tp->t, tp->len);
  254. tp->t[tp->len] = c;
  255. }
  256. if (tp->type==NAME) {
  257. fprintf(stderr, tp==trp->tp?"{*":"{");
  258. prhideset(tp->hideset);
  259. fprintf(stderr, "} ");
  260. } else
  261. fprintf(stderr, tp==trp->tp?"{%x*} ":"{%x} ", tp->type);
  262. }
  263. fprintf(stderr, "\n");
  264. fflush(stderr);
  265. }
  266. void
  267. puttokens(Tokenrow *trp)
  268. {
  269. Token *tp;
  270. int len;
  271. uchar *p;
  272. if (verbose)
  273. peektokens(trp, "");
  274. tp = trp->bp;
  275. for (; tp<trp->lp; tp++) {
  276. len = tp->len+tp->wslen;
  277. p = tp->t-tp->wslen;
  278. while (tp<trp->lp-1 && p+len == (tp+1)->t - (tp+1)->wslen) {
  279. tp++;
  280. len += tp->wslen+tp->len;
  281. }
  282. if (Mflag==0) {
  283. if (len>OBS/2) { /* handle giant token */
  284. if (wbp > wbuf)
  285. write(1, wbuf, wbp-wbuf);
  286. write(1, p, len);
  287. wbp = wbuf;
  288. } else {
  289. memcpy(wbp, p, len);
  290. wbp += len;
  291. }
  292. }
  293. if (wbp >= &wbuf[OBS]) {
  294. write(1, wbuf, OBS);
  295. if (wbp > &wbuf[OBS])
  296. memcpy(wbuf, wbuf+OBS, wbp - &wbuf[OBS]);
  297. wbp -= OBS;
  298. }
  299. }
  300. trp->tp = tp;
  301. if (cursource->fd==0)
  302. flushout();
  303. }
  304. void
  305. flushout(void)
  306. {
  307. if (wbp>wbuf) {
  308. write(1, wbuf, wbp-wbuf);
  309. wbp = wbuf;
  310. }
  311. }
  312. /*
  313. * turn a row into just a newline
  314. */
  315. void
  316. setempty(Tokenrow *trp)
  317. {
  318. trp->tp = trp->bp;
  319. trp->lp = trp->bp+1;
  320. *trp->bp = nltoken;
  321. }
  322. /*
  323. * generate a number
  324. */
  325. char *
  326. outnum(char *p, int n)
  327. {
  328. if (n>=10)
  329. p = outnum(p, n/10);
  330. *p++ = n%10 + '0';
  331. return p;
  332. }
  333. /*
  334. * allocate and initialize a new string from s, of length l, at offset o
  335. * Null terminated.
  336. */
  337. uchar *
  338. newstring(uchar *s, int l, int o)
  339. {
  340. uchar *ns = (uchar *)domalloc(l+o+1);
  341. ns[l+o] = '\0';
  342. return (uchar*)strncpy((char*)ns+o, (char*)s, l) - o;
  343. }