scan.c 4.7 KB

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