comment.c
上传用户:upcnvip
上传日期:2007-01-06
资源大小:474k
文件大小:9k
源码类别:

编译器/解释器

开发平台:

C/C++

  1. /* "p2c", a Pascal to C translator.
  2.    Copyright (C) 1989 David Gillespie.
  3.    Author's address: daveg@csvax.caltech.edu; 256-80 Caltech/Pasadena CA 91125.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation (any version).
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING.  If not, write to
  13. the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. #define PROTO_COMMENT_C
  15. #include "trans.h"
  16. Static int cmttablesize;
  17. Static uchar *cmttable;
  18. Static int grabbed_comment;
  19. /* Special comment forms:
  20.    010101...      Blank line(s), one 01 char per blank line
  21.    02text...          Additional line for previous comment
  22.    03text...          Additional comment line, absolutely indented
  23.    04text... Note or warning line, unindented
  24. */
  25. void setup_comment()
  26. {
  27.     curcomments = NULL;
  28.     cmttablesize = 200;
  29.     cmttable = ALLOC(cmttablesize, uchar, misc);
  30.     grabbed_comment = 0;
  31. }
  32. int commentlen(cmt)
  33. Strlist *cmt;
  34. {
  35.     if (cmt)
  36. if (*(cmt->s))
  37.     return strlen(cmt->s) + 4;
  38. else
  39.     return 5;
  40.     else
  41. return 0;
  42. }
  43. int commentvisible(cmt)
  44. Strlist *cmt;
  45. {
  46.     return (cmt &&
  47.     getcommentkind(cmt) != CMT_DONE &&
  48.     eatcomments != 1 && eatcomments != 2);
  49. }
  50. /* If preceding statement's POST comments include blank lines,
  51.    steal all comments after longest stretch of blank lines as
  52.    PRE comments for the next statement. */
  53. void steal_comments(olds, news, always)
  54. long olds, news;
  55. int always;
  56. {
  57.     Strlist *cmt, *cmtfirst = NULL, *cmtblank = NULL;
  58.     int len, longest;
  59.     for (cmt = curcomments; cmt; cmt = cmt->next) {
  60. if ((cmt->value & CMT_MASK) == olds &&
  61.     getcommentkind(cmt) == CMT_POST) {
  62.     if (!cmtfirst)
  63. cmtfirst = cmt;
  64. } else {
  65.     cmtfirst = NULL;
  66. }
  67.     }
  68.     if (cmtfirst) {
  69. if (!always) {
  70.     longest = 0;
  71.     for (cmt = cmtfirst; cmt; cmt = cmt->next) {
  72. if (cmt->s[0] == '01') {   /* blank line(s) */
  73.     len = strlen(cmt->s);
  74.     if (len > longest) {
  75. longest = len;
  76. cmtblank = cmt;
  77.     }
  78. }
  79.     }
  80.     if (longest > 0) {
  81. if (blankafter)
  82.     cmtfirst = cmtblank->next;
  83. else
  84.     cmtfirst = cmtblank;
  85.     } else if (commentafter == 1)
  86. cmtfirst = NULL;
  87. }
  88. changecomments(cmtfirst, CMT_POST, olds, CMT_PRE, news);
  89.     }
  90. }
  91. Strlist *fixbeginendcomment(cmt)
  92. Strlist *cmt;
  93. {
  94.     char *cp, *cp2;
  95.     if (!cmt)
  96. return NULL;
  97.     cp = cmt->s;
  98.     while (isspace(*cp))
  99. cp++;
  100.     if (!strcincmp(cp, "procedure ", 10)) {    /* remove "PROCEDURE" keyword */
  101. strcpy(cp, cp+10);
  102.     } else if (!strcincmp(cp, "function ", 9)) {
  103. strcpy(cp, cp+9);
  104.     }
  105.     while (isspace(*cp))
  106. cp++;
  107.     if (!*cp)
  108. return NULL;
  109.     if (getcommentkind(cmt) == CMT_ONBEGIN) {
  110. cp2 = curctx->sym->name;
  111. while (*cp2) {
  112.     if (toupper(*cp2++) != toupper(*cp++))
  113. break;
  114. }
  115. while (isspace(*cp))
  116.     cp++;
  117. if (!*cp2 && !*cp)
  118.     return NULL;     /* eliminate function-begin comment */
  119.     }
  120.     return cmt;
  121. }
  122. Static void attach_mark(sp)
  123. Stmt *sp;
  124. {
  125.     long serial;
  126.     while (sp) {
  127. serial = sp->serial;
  128. if (serial >= 0 && serial < cmttablesize) {
  129.     cmttable[serial]++;
  130.     if (sp->kind == SK_IF && serial+1 < cmttablesize)
  131. cmttable[serial+1]++;   /* the "else" branch */
  132. }
  133. attach_mark(sp->stm1);
  134. attach_mark(sp->stm2);
  135. sp = sp->next;
  136.     }
  137. }
  138. void attach_comments(sbase)
  139. Stmt *sbase;
  140. {
  141.     Strlist *cmt;
  142.     long serial, i, j;
  143.     int kind;
  144.     if (spitorphancomments)
  145. return;
  146.     if (serialcount >= cmttablesize) {
  147. cmttablesize = serialcount + 100;
  148. cmttable = REALLOC(cmttable, cmttablesize, uchar);
  149.     }
  150.     for (i = 0; i < cmttablesize; i++)
  151. cmttable[i] = 0;
  152.     attach_mark(sbase);
  153.     for (cmt = curcomments; cmt; cmt = cmt->next) {
  154. serial = cmt->value & CMT_MASK;
  155. kind = getcommentkind(cmt);
  156. if (serial < 0 || serial >= cmttablesize || cmttable[serial])
  157.     continue;
  158. i = 0;
  159. j = 0;
  160. do {
  161.     if (commentafter == 1) {
  162. j++;
  163. if (j % 3 == 0)
  164.     i++;
  165.     } else if (commentafter == 0) {
  166. i++;
  167. if (i % 3 == 0)
  168.     j++;
  169.     } else {
  170. i++;
  171. j++;
  172.     }
  173.     if (serial+i < cmttablesize && cmttable[serial+i]) {
  174. setcommentkind(cmt, CMT_PRE);
  175. cmt->value += i;
  176. break;
  177.     }
  178.     if (serial-j > 0 && cmttable[serial-j]) {
  179. setcommentkind(cmt, CMT_POST);
  180. cmt->value -= j;
  181. break;
  182.     }
  183. } while (serial+i < cmttablesize || serial-j > 0);
  184.     }
  185. }
  186. void setcommentkind(cmt, kind)
  187. Strlist *cmt;
  188. int kind;
  189. {
  190.     cmt->value = (cmt->value & CMT_MASK) | (kind << CMT_SHIFT);
  191. }
  192. void commentline(kind)
  193. int kind;
  194. {
  195.     char *cp;
  196.     Strlist *sl;
  197.     if (grabbed_comment) {
  198. grabbed_comment = 0;
  199. return;
  200.     }
  201.     if (blockkind == TOK_IMPORT || skipping_module)
  202. return;
  203.     if (eatcomments == 1)
  204. return;
  205.     for (cp = curtokbuf; (cp = my_strchr(cp, '*')) != NULL; ) {
  206. if (*++cp == '/') {
  207.     cp[-1] = '%';
  208.     note("Changed "* /" to "% /" in comment [140]");
  209. }
  210.     }
  211.     sl = strlist_append(&curcomments, curtokbuf);
  212.     sl->value = curserial;
  213.     setcommentkind(sl, kind);
  214. }
  215. void addnote(msg, serial)
  216. char *msg;
  217. long serial;
  218. {
  219.     int len1, len2, xextra, extra;
  220.     int defer = (notephase > 0 && spitcomments == 0);
  221.     Strlist *sl, *base = NULL, **pbase = (defer) ? &curcomments : &base;
  222.     char *prefix;
  223.     if (defer && (outf != stdout || !quietmode))
  224. printf("%s, line %d: %sn", infname, inf_lnum, msg);
  225.     else if (outf != stdout)
  226. printf("%s, line %d/%d: %sn", infname, inf_lnum, outf_lnum, msg);
  227.     if (verbose)
  228. fprintf(logf, "%s, %d/%d: %sn", infname, inf_lnum, outf_lnum, msg);
  229.     if (notephase == 2 || regression)
  230. prefix = format_s("04 p2c: %s:", infname);
  231.     else
  232. prefix = format_sd("04 p2c: %s, line %d:", infname, inf_lnum);
  233.     len1 = strlen(prefix);
  234.     len2 = strlen(msg) + 2;
  235.     if (len1 + len2 < linewidth-4) {
  236. msg = format_ss("%s %s ", prefix, msg);
  237.     } else {
  238. extra = xextra = 0;
  239. while (len2 - extra > linewidth-6) {
  240.     while (extra < len2 && !isspace(msg[extra]))
  241. extra++;
  242.     xextra = extra;
  243.     while (extra < len2 && isspace(msg[extra]))
  244. extra++;
  245. }
  246. prefix = format_sds("%s %.*s", prefix, xextra, msg);
  247. msg += extra;
  248. sl = strlist_append(pbase, prefix);
  249. sl->value = serial;
  250. setcommentkind(sl, CMT_POST);
  251. msg = format_s("03 * %s ", msg);
  252.     }
  253.     sl = strlist_append(pbase, msg);
  254.     sl->value = serial;
  255.     setcommentkind(sl, CMT_POST);
  256.     outputmode++;
  257.     outcomments(base);
  258.     outputmode--;
  259. }
  260. /* Grab a comment off the end of the current line */
  261. Strlist *grabcomment(kind)
  262. int kind;
  263. {
  264.     char *cp, *cp2;
  265.     Strlist *cmt, *savecmt;
  266.     if (grabbed_comment || spitcomments == 1)
  267. return NULL;
  268.     cp = inbufptr;
  269.     while (isspace(*cp))
  270. cp++;
  271.     if (*cp == ';' || *cp == ',' || *cp == '.')
  272. cp++;
  273.     while (isspace(*cp))
  274. cp++;
  275.     cp2 = curtokbuf;
  276.     if (*cp == '{') {
  277. cp++;
  278. while (*cp && *cp != '}')
  279.     *cp2++ = *cp++;
  280. if (!*cp)
  281.     return NULL;
  282. cp++;
  283.     } else if (*cp == '(' && cp[1] == '*') {
  284. cp += 2;
  285. while (*cp && (*cp != '*' || cp[1] != ')'))
  286.     *cp2++ = *cp++;
  287. if (!*cp)
  288.     return NULL;
  289. cp += 2;
  290.     } else
  291. return NULL;
  292.     while (isspace(*cp))
  293. cp++;
  294.     if (*cp)
  295. return NULL;
  296.     *cp2 = 0;
  297.     savecmt = curcomments;
  298.     curcomments = NULL;
  299.     commentline(kind);
  300.     cmt = curcomments;
  301.     curcomments = savecmt;
  302.     grabbed_comment = 1;
  303.     if (cmtdebug > 1)
  304. fprintf(outf, "Grabbed comment [%d] "%s"n", cmt->value & CMT_MASK, cmt->s);
  305.     return cmt;
  306. }
  307. int matchcomment(cmt, kind, stamp)
  308. Strlist *cmt;
  309. int kind, stamp;
  310. {
  311.     if (spitcomments == 1 && (cmt->value & CMT_MASK) != 10000 &&
  312. *cmt->s != '01' && (kind >= 0 || stamp >= 0))
  313. return 0;
  314.     if (!cmt || getcommentkind(cmt) == CMT_DONE)
  315. return 0;
  316.     if (stamp >= 0 && (cmt->value & CMT_MASK) != stamp)
  317. return 0;
  318.     if (kind >= 0) {
  319. if (kind & CMT_NOT) {
  320.     if (getcommentkind(cmt) == kind - CMT_NOT)
  321. return 0;
  322. } else {
  323.     if (getcommentkind(cmt) != kind)
  324. return 0;
  325. }
  326.     }
  327.     return 1;
  328. }
  329. Strlist *findcomment(cmt, kind, stamp)
  330. Strlist *cmt;
  331. int kind, stamp;
  332. {
  333.     while (cmt && !matchcomment(cmt, kind, stamp))
  334. cmt = cmt->next;
  335.     if (cmt && cmtdebug > 1)
  336. fprintf(outf, "Found comment [%d] "%s"n", cmt->value & CMT_MASK, cmt->s);
  337.     return cmt;
  338. }
  339. Strlist *extractcomment(cmt, kind, stamp)
  340. Strlist **cmt;
  341. int kind, stamp;
  342. {
  343.     Strlist *base, **last, *sl;
  344.     last = &base;
  345.     while ((sl = *cmt)) {
  346. if (matchcomment(sl, kind, stamp)) {
  347.     if (cmtdebug > 1)
  348. fprintf(outf, "Extracted comment [%d] "%s"n",
  349.         sl->value & CMT_MASK, sl->s);
  350.     *cmt = sl->next;
  351.     *last = sl;
  352.     last = &sl->next;
  353. } else
  354.     cmt = &sl->next;
  355.     }
  356.     *last = NULL;
  357.     return base;
  358. }
  359. void changecomments(cmt, okind, ostamp, kind, stamp)
  360. Strlist *cmt;
  361. int okind, ostamp, kind, stamp;
  362. {
  363.     while (cmt) {
  364. if (matchcomment(cmt, okind, ostamp)) {
  365.     if (cmtdebug > 1)
  366. fprintf(outf, "Changed comment [%s:%d] "%s" ",
  367. CMT_NAMES[getcommentkind(cmt)],
  368. cmt->value & CMT_MASK, cmt->s);
  369.     if (kind >= 0)
  370. setcommentkind(cmt, kind);
  371.     if (stamp >= 0)
  372. cmt->value = (cmt->value & ~CMT_MASK) | stamp;
  373.     if (cmtdebug > 1)
  374. fprintf(outf, " to [%s:%d]n",
  375. CMT_NAMES[getcommentkind(cmt)], cmt->value & CMT_MASK);
  376. }
  377. cmt = cmt->next;
  378.     }
  379. }
  380. /* End. */