summaryrefslogtreecommitdiffstats
path: root/dimension/tokenize.c
blob: 4a9e9c65e8af7d46147b44d80ff1e7e5c199ed5c (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
/*************************************************************************
 * Copyright (C) 2010 Tavian Barnes <tavianator@gmail.com>               *
 *                                                                       *
 * This file is part of Dimension.                                       *
 *                                                                       *
 * Dimension is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the *
 * Free Software Foundation; either version 3 of the License, or (at     *
 * your option) any later version.                                       *
 *                                                                       *
 * Dimension is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 * General Public License for more details.                              *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *************************************************************************/

#include "tokenize.h"
#include "directives.h"
#include "utility.h"

typedef struct dmnsn_buffered_token {
  int type;
  dmnsn_parse_item lval;
  dmnsn_parse_location lloc;
} dmnsn_buffered_token;

typedef struct dmnsn_yylex_extra {
  int type;
  /* Indicate that the first token should be returned as-is */
  #define DMNSN_T_LEX_VERBATIM DMNSN_T_EOF

  dmnsn_array *buffered;
  unsigned int i;

  struct dmnsn_yylex_extra *prev;
} dmnsn_yylex_extra;

dmnsn_yylex_extra *
dmnsn_new_yylex_extra(int type)
{
  dmnsn_yylex_extra *extra = malloc(sizeof(dmnsn_yylex_extra));
  if (!extra) {
    dmnsn_error(DMNSN_SEVERITY_HIGH, "Failed to allocate token buffer.");
  }

  extra->type = type;
  extra->buffered = dmnsn_new_array(sizeof(dmnsn_buffered_token));
  extra->i = 0;
  extra->prev = NULL;
  return extra;
}

void
dmnsn_delete_yylex_extra(dmnsn_yylex_extra *extra)
{
  if (extra) {
    dmnsn_delete_array(extra->buffered);
    free(extra);
  }
}

int dmnsn_yylex_impl(dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
                     const char *filename, void *yyscanner);
int dmnsn_ld_yyparse(const char *filename, void *yyscanner,
                     dmnsn_symbol_table *symtable);

int
dmnsn_yylex(dmnsn_parse_item *lvalp, dmnsn_parse_location *llocp,
            const char *filename, dmnsn_symbol_table *symtable, void *yyscanner)
{
  /*
   * So... this is kind of ugly.  POV-Ray's language directives are not parsable
   * by reasonable bison grammar, since some require skipping arbitrary amounts
   * of tokens, or repeatedly parsing tokens.  Instead, they are implemented
   * transparently by the lexer.  The lexing function calls a separate parser
   * to handle language directives as they arrise, and returns only non-
   * directive tokens.
   *
   * Ideally we'd use a push parser for the language directives, but bison
   * doesn't support GLR push parsers.
   */

  dmnsn_yylex_extra *extra = dmnsn_yyget_extra(yyscanner);

  int token;
  while (1) {
    if (extra) {
      /* We are returning buffered tokens */
      dmnsn_buffered_token buffered;

      if (extra->i < dmnsn_array_size(extra->buffered)) {
        dmnsn_array_get(extra->buffered, extra->i, &buffered);
        token = buffered.type;
        *lvalp = buffered.lval;
        *llocp = buffered.lloc;
        ++extra->i;

        if (extra->type == DMNSN_T_LEX_VERBATIM && extra->i == 1) {
          /* Don't reprocess the first token in some situations */
          return token;
        }
      } else {
        dmnsn_yyset_extra(extra->prev, yyscanner);
        dmnsn_delete_yylex_extra(extra);
        extra = dmnsn_yyget_extra(yyscanner);
        continue;
      }
    } else {
      token = dmnsn_yylex_impl(lvalp, llocp, filename, yyscanner);
    }

    switch (token) {
    case DMNSN_T_DECLARE:
    case DMNSN_T_LOCAL:
      {
        dmnsn_yylex_extra *ex = dmnsn_new_yylex_extra(DMNSN_T_LEX_VERBATIM);
        ex->prev = extra;

        /* Buffer the current token */
        dmnsn_buffered_token buffered = {
          .type = token,
          .lval = *lvalp,
          .lloc = *llocp
        };
        dmnsn_array_push(ex->buffered, &buffered);

        /* Grab all the tokens belonging to the #declare/#local */
        int bracelevel = -1;
        while (1) {
          /* Recursive call */
          buffered.type = dmnsn_yylex(&buffered.lval, &buffered.lloc,
                                      filename, symtable, yyscanner);

          if (buffered.type == DMNSN_T_EOF) {
            dmnsn_diagnostic(filename, buffered.lloc.first_line,
                             buffered.lloc.first_column,
                             "unexpected end-of-file");
            dmnsn_delete_yylex_extra(ex);
            return DMNSN_T_LEX_ERROR;
          } else if (buffered.type == DMNSN_T_LEX_ERROR) {
            dmnsn_delete_yylex_extra(ex);
            return DMNSN_T_LEX_ERROR;
          }

          dmnsn_array_push(ex->buffered, &buffered);

          if (buffered.type == DMNSN_T_LBRACE) {
            if (bracelevel < 0)
              bracelevel = 1;
            else
              ++bracelevel;
          } else if (buffered.type == DMNSN_T_RBRACE) {
            --bracelevel;
            if (bracelevel == 0) {
              break;
            }
          } else if (buffered.type == DMNSN_T_SEMICOLON) {
            break;
          }
        }

        /* Fake EOF */
        buffered.type = DMNSN_T_EOF;
        dmnsn_array_push(ex->buffered, &buffered);

        dmnsn_yyset_extra(ex, yyscanner);
        if (dmnsn_ld_yyparse(filename, yyscanner, symtable) != 0) {
          dmnsn_yyset_extra(ex->prev, yyscanner);
          dmnsn_delete_yylex_extra(ex);
          return DMNSN_T_LEX_ERROR;
        }

        /* Restore the previous extra pointer */
        dmnsn_yyset_extra(ex->prev, yyscanner);
        dmnsn_delete_yylex_extra(ex);
        break;
      }

    case DMNSN_T_UNDEF:
      {
        dmnsn_yylex_extra *ex = dmnsn_new_yylex_extra(DMNSN_T_LEX_VERBATIM);
        ex->prev = extra;

        /* Buffer the current token */
        dmnsn_buffered_token buffered = {
          .type = token,
          .lval = *lvalp,
          .lloc = *llocp
        };
        dmnsn_array_push(ex->buffered, &buffered);

        /* Recursive call */
        buffered.type = dmnsn_yylex(&buffered.lval, &buffered.lloc,
                                    filename, symtable, yyscanner);

        if (buffered.type == DMNSN_T_EOF) {
          dmnsn_diagnostic(filename, buffered.lloc.first_line,
                           buffered.lloc.first_column,
                           "unexpected end-of-file");
          dmnsn_delete_yylex_extra(ex);
          return DMNSN_T_LEX_ERROR;
        } else if (buffered.type == DMNSN_T_LEX_ERROR) {
          dmnsn_delete_yylex_extra(ex);
          return DMNSN_T_LEX_ERROR;
        }
        /* Buffer the next token */
        dmnsn_array_push(ex->buffered, &buffered);

        /* Fake EOF */
        buffered.type = DMNSN_T_EOF;
        dmnsn_array_push(ex->buffered, &buffered);

        dmnsn_yyset_extra(ex, yyscanner);
        if (dmnsn_ld_yyparse(filename, yyscanner, symtable) != 0) {
          dmnsn_yyset_extra(ex->prev, yyscanner);
          dmnsn_delete_yylex_extra(ex);
          return DMNSN_T_LEX_ERROR;
        }

        /* Restore the previous extra pointer */
        dmnsn_yyset_extra(ex->prev, yyscanner);
        dmnsn_delete_yylex_extra(ex);
        break;
      }

    default:
      return token;
    }
  }
}