scan.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. /* PREPROCESSOR: SCANNER FOR THE ACTUAL PARAMETERS OF MACROS */
  7. #include "nopp.h"
  8. #ifndef NOPP
  9. /* This file contains the function getactuals() which scans an actual
  10. parameter list and splits it up into a list of strings, each one
  11. representing an actual parameter.
  12. */
  13. #include "lapbuf.h" /* UF */
  14. #include "nparams.h" /* UF */
  15. #include "input.h"
  16. #include "class.h"
  17. #include "idf.h"
  18. #include "macro.h"
  19. #include "interface.h"
  20. #include "file_info.h"
  21. #define EOS '\0'
  22. #define overflow() (fatal("actual parameter buffer overflow"))
  23. PRIVATE char apbuf[LAPBUF]; /* temporary storage for actual parameters */
  24. PRIVATE char *actparams[NPARAMS]; /* pointers to the text of the actuals */
  25. PRIVATE char *aptr; /* pointer to last inserted character in apbuf */
  26. #define copy(ch) ((aptr < &apbuf[LAPBUF]) ? (*aptr++ = ch) : overflow())
  27. PRIVATE int nr_of_params; /* number of actuals read until now */
  28. PRIVATE char **
  29. getactuals(idef)
  30. register struct idf *idef;
  31. {
  32. /* getactuals() collects the actual parameters and turns them
  33. into a list of strings, a pointer to which is returned.
  34. */
  35. register acnt = idef->id_macro->mc_nps;
  36. nr_of_params = 0;
  37. actparams[0] = aptr = &apbuf[0];
  38. copyact('(', ')', 0); /* read the actual parameters */
  39. copy(EOS); /* mark the end of it all */
  40. if (!nr_of_params++) { /* 0 or 1 parameter */
  41. /* there could be a ( <spaces, comment, ...> )
  42. */
  43. register char *p = actparams[0];
  44. while ((class(*p) == STSKIP) || (*p == '\n')) {
  45. ++p;
  46. }
  47. if (!*p) { /* the case () : 0 parameters */
  48. nr_of_params--;
  49. }
  50. }
  51. if (nr_of_params != acnt) {
  52. /* argument mismatch: too many or too few
  53. actual parameters.
  54. */
  55. lexwarning("argument mismatch, %s", idef->id_text);
  56. while (nr_of_params < acnt) {
  57. /* too few paraeters: remaining actuals are ""
  58. */
  59. actparams[nr_of_params] = "";
  60. nr_of_params++;
  61. }
  62. }
  63. return actparams;
  64. }
  65. PRIVATE
  66. copyact(ch1, ch2, lvl)
  67. char ch1, ch2;
  68. int lvl;
  69. {
  70. /* copyact() is taken from Ceriel Jacobs' LLgen, with
  71. permission. Its task is to build a list of actuals
  72. parameters, which list is surrounded by '(' and ')' and in
  73. which the parameters are separated by ',' if there are
  74. more than 1. The balancing of '(',')' and '[',']' and
  75. '{','}' is taken care of by calling this function
  76. recursively. At each level lvl, copyact() reads the input,
  77. upto the corresponding closing bracket.
  78. Opening bracket is ch1, closing bracket is ch2. If
  79. lvl != 0, copy opening and closing parameters too.
  80. */
  81. register int ch; /* Current char */
  82. register int match; /* used to read strings */
  83. if (lvl) {
  84. copy(ch1);
  85. }
  86. for (;;) {
  87. LoadChar(ch);
  88. if (ch == ch2) {
  89. if (lvl) {
  90. copy(ch);
  91. }
  92. return;
  93. }
  94. switch(ch) {
  95. #ifdef __MATCHING_PAR__
  96. case ')':
  97. case '}':
  98. case ']':
  99. lexerror("unbalanced parenthesis");
  100. break;
  101. #endif __MATCHING_PAR__
  102. case '(':
  103. copyact('(', ')', lvl+1);
  104. break;
  105. #ifdef __MATCHING_PAR__
  106. case '{':
  107. /* example:
  108. #define declare(v, t) t v
  109. declare(v, union{int i, j; float r;});
  110. */
  111. copyact('{', '}', lvl+1);
  112. break;
  113. case '[':
  114. copyact('[', ']', lvl+1);
  115. break;
  116. #endif __MATCHING_PAR__
  117. case '\n':
  118. LineNumber++;
  119. while (LoadChar(ch), ch == '#') {
  120. /* This piece of code needs some
  121. explanation: consider the call of
  122. the macro defined as:
  123. #define sum(b,c) (b + c)
  124. in the following form:
  125. sum(
  126. #include my_phone_number
  127. ,2)
  128. in which case the include must be
  129. interpreted as such.
  130. */
  131. domacro(); /* has read nl, vt or ff */
  132. /* Loop, for another control line */
  133. }
  134. PushBack();
  135. copy(' ');
  136. break;
  137. case '/':
  138. LoadChar(ch);
  139. if (ch == '*' && !InputLevel) { /* skip comment */
  140. skipcomment();
  141. continue;
  142. }
  143. PushBack();
  144. copy('/');
  145. break;
  146. case ',':
  147. if (!lvl) {
  148. /* next parameter encountered */
  149. copy(EOS);
  150. if (++nr_of_params >= NPARAMS) {
  151. fatal("too many actual parameters");
  152. }
  153. actparams[nr_of_params] = aptr;
  154. }
  155. else {
  156. copy(ch);
  157. }
  158. break;
  159. case '\'':
  160. case '"' :
  161. /* watch out for brackets in strings, they do
  162. not count !
  163. */
  164. match = ch;
  165. copy(ch);
  166. while (LoadChar(ch), ch != EOI) {
  167. if (ch == match) {
  168. break;
  169. }
  170. if (ch == '\\') {
  171. copy(ch);
  172. LoadChar(ch);
  173. }
  174. else
  175. if (ch == '\n') {
  176. lexerror("newline in string");
  177. LineNumber++;
  178. copy(match);
  179. break;
  180. }
  181. copy(ch);
  182. }
  183. if (ch == match) {
  184. copy(ch);
  185. break;
  186. }
  187. /* Fall through */
  188. case EOI :
  189. lexerror("unterminated macro call");
  190. return;
  191. default:
  192. copy(ch);
  193. break;
  194. }
  195. }
  196. }
  197. #endif NOPP