summaryrefslogtreecommitdiff
path: root/src/preproc/gettok.c
blob: 87fe5f0f418eccbd06d6050b69a393619440ca68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * This files contains routines for getting the "next" token.
 */
#include "../preproc/preproc.h"
#include "../preproc/ptoken.h"
#include "../preproc/pproto.h"

/*
 * next_tok - get the next raw token. No macros are expanded here (although
 *  the tokens themselves may be the result of a macro expansion initiated
 *  at a "higher" level). Only #line directives are processed here.
 */
struct token *next_tok()
   {
   struct token *t, *t1;
   struct tok_lst *tlst;
   struct char_src *cs;
   struct str_buf *sbuf;
   char *s;
   char *fname;
   int n;

   if (src_stack->flag == DummySrc)
      return NULL;    /* source stack is empty - end of input */

   /*
    * See if a directive pushed back any tokens.
    */
   if (src_stack->ntoks > 0)
      return src_stack->toks[--src_stack->ntoks];

   switch (src_stack->flag) {
      case CharSrc:
         /*
          * Tokens from a raw character "stream".
          */
         t = tokenize();
         if (t != NULL && src_stack->u.cs->f != NULL)
            t->flag |= LineChk;
         if (t != NULL && t->tok_id == PpLine) {
            /*
             * #line directives must be processed here so they are not
             *  put in macros.
             */
            cs = src_stack->u.cs;
            t1 = NULL;

            /*
             * Get the line number from the directive.
             */
            advance_tok(&t1);
            if (t1->tok_id != PpNumber)
               errt1(t1, "#line requires an integer argument");
            n = 0;
            for (s = t1->image; *s != '\0'; ++s) {
               if (*s >= '0' && *s <= '9')
                  n = 10 * n + (*s - '0');
               else
                  errt1(t1, "#line requires an integer argument");
               }

            /*
             * Get the file name, if there is one, from the directive.
             */
            advance_tok(&t1);
            fname = NULL;
            if (t1->tok_id == StrLit) {
               sbuf = get_sbuf();
               for (s = t1->image; *s != '\0'; ++s) {
                  if (s[0] == '\\' && (s[1] == '\\' || s[1] == '"'))
                     ++s;
                  AppChar(*sbuf, *s);
                  }
               fname = str_install(sbuf);
               rel_sbuf(sbuf);
               advance_tok(&t1);
               }
            if (t1->tok_id != PpDirEnd)
               errt1(t1, "syntax error in #line");

            /*
             * Note the effect of the line directive in the character
             *  source. Line number changes are handled as a relative
             *  adjustments to the line numbers of following lines.
             */
            if (fname != NULL)
               cs->fname = fname;
            cs->line_adj = n - cs->line_buf[next_char - first_char + 1];
            if (*next_char == '\n')
               ++cs->line_adj;  /* the next lines contains no characters */

            t = next_tok();     /* the caller does not see #line directives */
            }
         break;

      case MacExpand:
         /*
          * Tokens from macro expansion.
          */
         t = mac_tok();
         break;

      case TokLst:
         /*
          * Tokens from a macro argument.
          */
         tlst = src_stack->u.tlst;
         if (tlst == NULL)
            t = NULL;
         else {
            t = copy_t(tlst->t);
            src_stack->u.tlst = tlst->next;
            }
         break;

      case PasteLsts:
         /*
          * Tokens from token Pasting.
          */
         return paste();
      }

   if (t == NULL) {
      /*
       * We have exhausted this entry on the source stack without finding
       *  a token to return.
       */
      pop_src();
      return next_tok();
      }
   else
      return t;
   }

/*
 * Get the next raw non-white space token, freeing token that the argument
 *  used to point to.
 */
void nxt_non_wh(tp)
struct token **tp;
   {
   register struct token *t;

   t = next_tok();
   while (t != NULL && t->tok_id == WhiteSpace) {
      free_t(t);
      t = next_tok();
      }
   free_t(*tp);
   *tp = t;
   }

/*
 * advance_tok - skip past white space after expanding macros and
 *  executing preprocessor directives. This routine may only be
 *  called from within a preprocessor directive because it assumes
 *  it will not see EOF (the input routines insure that a terminating
 *  new-line, and thus, for a directive, the PpDirEnd token, will be
 *  seen immediately before EOF).
 */
void advance_tok(tp)
struct token **tp;
   {
   struct token *t;

   t = interp_dir();
   while (t->tok_id == WhiteSpace) {
      free_t(t);
      t = interp_dir();
      }
   free_t(*tp);
   *tp = t;
   }

/*
 * merge_whsp - merge a sequence of white space tokens into one token,
 *  returning it along with the next token. Whether these are raw or
 *  processed tokens depends on the token source function, t_src.
 */
void merge_whsp(whsp, next_t, t_src)
struct token **whsp;
struct token **next_t;
struct token *(*t_src)(void);
   {
   struct token *t1;
   struct str_buf *sbuf;
   int line = -1;
   char *fname = "";
   char *s;

   free_t(*whsp);
   t1 = (*t_src)();
   if (t1 == NULL || t1->tok_id != WhiteSpace)
      *whsp  = NULL;   /* no white space here */
   else {
      *whsp = t1;
      t1 = (*t_src)();
      if (t1 != NULL && t1->tok_id == WhiteSpace) {
         if (whsp_image == NoSpelling) {
            /*
             * We don't care what the white space looks like, so
             *  discard the rest of it.
             */
            while (t1 != NULL && t1->tok_id == WhiteSpace) {
               free_t(t1);
               t1 = (*t_src)();
               }
            }
         else {
            /*
             * Must actually merge white space. Put it all white space
             *  in a string buffer and use that as the image of the merged
             *  token. The line number and file name of the new token
             *  is that of the last token whose line number and file
             *  name is important for generating #line directives in
             *  the output.
             */
            sbuf = get_sbuf();
            if ((*whsp)->flag & LineChk) {
               line = (*whsp)->line;
               fname = (*whsp)->fname;
               }
            for (s = (*whsp)->image; *s != '\0'; ++s) {
               AppChar(*sbuf, *s);
               if (*s == '\n' && line != -1)
                  ++line;
               }
            while (t1 != NULL && t1->tok_id == WhiteSpace) {
               if (t1->flag & LineChk) {
                  line = t1->line;
                  fname = t1->fname;
                  }
               for (s = t1->image; *s != '\0'; ++s) {
                  AppChar(*sbuf, *s);
                  if (*s == '\n' && line != -1)
                     ++line;
                  }
               free_t(t1);
               t1 = (*t_src)();
               }
            (*whsp)->image = str_install(sbuf);
            rel_sbuf(sbuf);
            if (t1 != NULL && !(t1->flag & LineChk) && line != -1) {
               t1->flag |= LineChk;
               t1->line = line;
               t1->fname = fname;
               }
            }
         }
      }
   *next_t = t1;
   }