ack/lang/cem/cemcom/scan.c

238 lines
4.7 KiB
C
Raw Normal View History

/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1986-03-10 13:07:55 +00:00
/* $Header$ */
/* PREPROCESSOR: SCANNER FOR THE ACTUAL PARAMETERS OF MACROS */
#include "nopp.h"
#ifndef NOPP
/* This file contains the function getactuals() which scans an actual
parameter list and splits it up into a list of strings, each one
representing an actual parameter.
*/
#include "lapbuf.h" /* UF */
#include "nparams.h" /* UF */
#include "input.h"
#include "class.h"
#include "idf.h"
#include "macro.h"
#include "interface.h"
#include "file_info.h"
1986-03-10 13:07:55 +00:00
#define EOS '\0'
#define overflow() (fatal("actual parameter buffer overflow"))
PRIVATE char apbuf[LAPBUF]; /* temporary storage for actual parameters */
PRIVATE char *actparams[NPARAMS]; /* pointers to the text of the actuals */
PRIVATE char *aptr; /* pointer to last inserted character in apbuf */
#define copy(ch) ((aptr < &apbuf[LAPBUF]) ? (*aptr++ = ch) : overflow())
PRIVATE int nr_of_params; /* number of actuals read until now */
PRIVATE char **
getactuals(idef)
register struct idf *idef;
1986-03-10 13:07:55 +00:00
{
/* getactuals() collects the actual parameters and turns them
into a list of strings, a pointer to which is returned.
*/
register acnt = idef->id_macro->mc_nps;
nr_of_params = 0;
actparams[0] = aptr = &apbuf[0];
copyact('(', ')', 0); /* read the actual parameters */
copy(EOS); /* mark the end of it all */
if (!nr_of_params++) { /* 0 or 1 parameter */
/* there could be a ( <spaces, comment, ...> )
*/
register char *p = actparams[0];
while ((class(*p) == STSKIP) || (*p == '\n')) {
++p;
}
if (!*p) { /* the case () : 0 parameters */
nr_of_params--;
}
}
if (nr_of_params != acnt) {
/* argument mismatch: too many or too few
actual parameters.
*/
lexwarning("argument mismatch, %s", idef->id_text);
1986-03-10 13:07:55 +00:00
1987-08-07 19:53:54 +00:00
while (nr_of_params < acnt) {
1986-03-10 13:07:55 +00:00
/* too few paraeters: remaining actuals are ""
*/
actparams[nr_of_params] = "";
1987-08-07 19:53:54 +00:00
nr_of_params++;
1986-03-10 13:07:55 +00:00
}
}
return actparams;
}
PRIVATE
1988-08-23 12:45:11 +00:00
copyact(ch1, ch2, lvl)
1986-03-10 13:07:55 +00:00
char ch1, ch2;
1988-08-23 12:45:11 +00:00
int lvl;
1986-03-10 13:07:55 +00:00
{
/* copyact() is taken from Ceriel Jacobs' LLgen, with
permission. Its task is to build a list of actuals
parameters, which list is surrounded by '(' and ')' and in
which the parameters are separated by ',' if there are
more than 1. The balancing of '(',')' and '[',']' and
'{','}' is taken care of by calling this function
1988-08-23 12:45:11 +00:00
recursively. At each level lvl, copyact() reads the input,
1986-03-10 13:07:55 +00:00
upto the corresponding closing bracket.
Opening bracket is ch1, closing bracket is ch2. If
1988-08-23 12:45:11 +00:00
lvl != 0, copy opening and closing parameters too.
1986-03-10 13:07:55 +00:00
*/
register int ch; /* Current char */
register int match; /* used to read strings */
1988-08-23 12:45:11 +00:00
if (lvl) {
1986-03-10 13:07:55 +00:00
copy(ch1);
}
for (;;) {
LoadChar(ch);
if (ch == ch2) {
1988-08-23 12:45:11 +00:00
if (lvl) {
1986-03-10 13:07:55 +00:00
copy(ch);
}
return;
}
switch(ch) {
1987-08-07 19:53:54 +00:00
#ifdef __MATCHING_PAR__
1986-03-10 13:07:55 +00:00
case ')':
case '}':
case ']':
lexerror("unbalanced parenthesis");
break;
1987-08-07 19:53:54 +00:00
#endif __MATCHING_PAR__
1986-03-10 13:07:55 +00:00
case '(':
1988-08-23 12:45:11 +00:00
copyact('(', ')', lvl+1);
1986-03-10 13:07:55 +00:00
break;
1987-08-07 19:53:54 +00:00
#ifdef __MATCHING_PAR__
1986-03-10 13:07:55 +00:00
case '{':
/* example:
#define declare(v, t) t v
declare(v, union{int i, j; float r;});
*/
1988-08-23 12:45:11 +00:00
copyact('{', '}', lvl+1);
1986-03-10 13:07:55 +00:00
break;
case '[':
1988-08-23 12:45:11 +00:00
copyact('[', ']', lvl+1);
1986-03-10 13:07:55 +00:00
break;
1987-08-07 19:53:54 +00:00
#endif __MATCHING_PAR__
1986-03-10 13:07:55 +00:00
case '\n':
LineNumber++;
1986-03-10 13:07:55 +00:00
while (LoadChar(ch), ch == '#') {
/* This piece of code needs some
explanation: consider the call of
the macro defined as:
#define sum(b,c) (b + c)
in the following form:
sum(
#include my_phone_number
,2)
in which case the include must be
interpreted as such.
*/
domacro(); /* has read nl, vt or ff */
/* Loop, for another control line */
}
PushBack();
copy('\n');
break;
case '/':
LoadChar(ch);
if (ch == '*') { /* skip comment */
skipcomment();
continue;
}
PushBack();
copy('/');
break;
case ',':
1988-08-23 12:45:11 +00:00
if (!lvl) {
1986-03-17 17:47:04 +00:00
/* next parameter encountered */
1986-03-10 13:07:55 +00:00
copy(EOS);
if (++nr_of_params >= NPARAMS) {
fatal("(getact) too many actuals");
}
actparams[nr_of_params] = aptr;
}
else {
copy(ch);
}
break;
case '\'':
case '"' :
/* watch out for brackets in strings, they do
not count !
*/
match = ch;
copy(ch);
while (LoadChar(ch), ch != EOI) {
if (ch == match) {
break;
}
if (ch == '\\') {
copy(ch);
LoadChar(ch);
}
else
if (ch == '\n') {
lexerror("newline in string");
LineNumber++;
1986-03-10 13:07:55 +00:00
copy(match);
break;
}
copy(ch);
}
if (ch == match) {
copy(ch);
break;
}
/* Fall through */
case EOI :
lexerror("unterminated macro call");
return;
default:
copy(ch);
break;
}
}
}
#endif NOPP