scan.c 4.4 KB

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