File: VariableInitializerParser.java

package info (click to toggle)
turbine-java 0.1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,556 kB
  • sloc: java: 37,940; xml: 354; makefile: 7
file content (350 lines) | stat: -rw-r--r-- 9,821 bytes parent folder | download
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.parse;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.turbine.diag.TurbineError;
import com.google.turbine.diag.TurbineError.ErrorKind;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

/**
 * Pre-process variable initializer expressions to handle multi-variable declarations.
 *
 * <p>Turbine needs to be able to parse compile-time constant expressions in constant variable
 * intializers and annotations. Parsing JLS 15.28 constant expressions is much easier than parsing
 * the full expression language, so we pre-process variable initializers to extract the expression
 * and then parse it with an simple constant expression parser that fails if it sees an expression
 * it doesn't understand.
 *
 * <p>To extract the (possibly constant) expression, we can usually just scan ahead to the
 * semi-colon at the end of the variable. To avoid matching on semi-colons inside lambdas or
 * anonymous class declarations, the preprocessor also matches braces.
 *
 * <p>That handles everything except multi-variable declarations (int x = 1, y = 2;), which in
 * hindsight were probably a mistake. Multi-variable declarations contain a list of name and
 * initializer pairs separated by commas. The initializer expressions may also contain commas, so
 * it's non-trivial to split on initializer boundaries. For example, consider {@code int x = a < b,
 * c = d;}. We can't tell looking at the prefix {@code a < b, c} whether that's a less-than
 * expression followed by another initializer, or the start of a generic type: {@code a<b, c>.foo(}.
 * Distinguishing between these cases requires arbitrary lookahead.
 *
 * <p>The preprocessor seems to be operationally correct. It's possible there are edge cases that it
 * doesn't handle, but it's extremely rare for compile-time constant multi-variable declarations to
 * contain complex generics. Multi-variable declarations are also disallowed by the Style guide.
 */
public class VariableInitializerParser {

  enum FieldInitState {
    /** The beginning of an initializer expression. */
    START,
    /** The state after `<identifier> <`. */
    TYPE,
  }

  /** Indices into {@code LT} tokens used for backtracking. */
  final ArrayDeque<Integer> ltIndices = new ArrayDeque<>();

  /** Indices into {@code commas} used for backtracking. */
  final ArrayDeque<Integer> commaIndices = new ArrayDeque<>();

  /** The saved tokens. */
  List<SavedToken> tokens = new ArrayList<>();

  /**
   * Indices of boundaries between variable initializers in {@code tokens} (which are indicated by
   * commas in the input).
   */
  List<Integer> commas = new ArrayList<>();

  public Token token;
  FieldInitState state = FieldInitState.START;
  int depth = 0;

  final Lexer lexer;

  public VariableInitializerParser(Token token, Lexer lexer) {
    this.token = token;
    this.lexer = lexer;
  }

  private void next() {
    token = lexer.next();
  }

  /** Returns lists of tokens for individual initializers in a (mutli-)variable initializer. */
  public List<List<SavedToken>> parseInitializers() {
    OUTER:
    while (true) {
      switch (token) {
        case IDENT:
          save();
          next();
          if (state == FieldInitState.START) {
            if (token == Token.LT) {
              state = FieldInitState.TYPE;
              depth = 1;
              ltIndices.clear();
              commaIndices.clear();
              ltIndices.addLast(tokens.size());
              commaIndices.addLast(commas.size());
              save();
              next();
              break;
            }
          }
          break;
        case LT:
          if (state == FieldInitState.TYPE) {
            depth++;
            ltIndices.addLast(tokens.size());
            commaIndices.addLast(commas.size());
          }
          save();
          next();
          break;
        case GTGTGT:
          save();
          next();
          dropBracks(3);
          break;
        case GTGT:
          save();
          next();
          dropBracks(2);
          break;
        case GT:
          save();
          next();
          dropBracks(1);
          break;
        case LPAREN:
          save();
          next();
          dropParens();
          break;
        case LBRACE:
          save();
          next();
          dropBraces();
          break;
        case SEMI:
          switch (state) {
            case START:
            case TYPE:
              break OUTER;
          }
          save();
          next();
          break;
        case COMMA:
          save();
          next();
          switch (state) {
            case START:
            case TYPE:
              commas.add(tokens.size());
              break;
          }
          break;
        case DOT:
          save();
          next();
          dropTypeArguments();
          break;
        case NEW:
          save();
          next();
          dropTypeArguments();
          while (token == Token.IDENT) {
            save();
            next();
            dropTypeArguments();
            if (token == Token.DOT) {
              next();
            } else {
              break;
            }
          }
          break;
        case COLONCOLON:
          save();
          next();
          dropTypeArguments();
          if (token == Token.NEW) {
            next();
          }
          break;
        case EOF:
          break OUTER;
        default:
          save();
          next();
          break;
      }
    }
    List<List<SavedToken>> result = new ArrayList<>();
    int start = 0;
    for (int idx : commas) {
      result.add(
          ImmutableList.<SavedToken>builder()
              .addAll(tokens.subList(start, idx - 1))
              .add(new SavedToken(Token.EOF, null, tokens.get(idx - 1).position))
              .build());
      start = idx;
    }
    result.add(
        ImmutableList.<SavedToken>builder()
            .addAll(tokens.subList(start, tokens.size()))
            .add(new SavedToken(Token.EOF, null, lexer.position()))
            .build());
    return result;
  }

  private void dropParens() {
    int depth = 1;
    while (depth > 0) {
      switch (token) {
        case LPAREN:
          save();
          next();
          depth++;
          break;
        case RPAREN:
          save();
          next();
          depth--;
          break;
        case EOF:
          throw error(ErrorKind.UNEXPECTED_EOF);
        default:
          save();
          next();
          break;
      }
    }
  }

  private void dropBraces() {
    int depth = 1;
    while (depth > 0) {
      switch (token) {
        case LBRACE:
          save();
          next();
          depth++;
          break;
        case RBRACE:
          save();
          next();
          depth--;
          break;
        case EOF:
          throw error(ErrorKind.UNEXPECTED_EOF);
        default:
          save();
          next();
          break;
      }
    }
  }

  private void save() {
    tokens.add(new SavedToken(token, lexer.stringValue(), lexer.position()));
  }

  private void dropBracks(int many) {
    if (state != FieldInitState.TYPE) {
      return;
    }
    if (depth <= many) {
      state = FieldInitState.START;
    }
    depth -= many;
    int lastType = -1;
    int lastComma = -1;
    for (int i = 0; i < many; i++) {
      if (ltIndices.isEmpty()) {
        throw error(ErrorKind.UNEXPECTED_TOKEN, ">");
      }
      lastType = ltIndices.removeLast();
      lastComma = commaIndices.removeLast();
    }
    // The only known type argument locations that require look-ahead to classify are method
    // references with parametric receivers, and qualified nested type names:
    switch (token) {
      case COLONCOLON:
      case DOT:
        this.tokens = tokens.subList(0, lastType);
        this.commas = commas.subList(0, lastComma);
        break;
      default:
        break;
    }
  }

  /**
   * Drops pairs of `<` `>` from the input. Should only be called in contexts where the braces are
   * unambiguously type argument lists, not less-than.
   *
   * <p>Since the lexer munches multiple close braces as a single token, there's handling of right
   * shifts for cases like the `>>` in `List<SavedToken<String, Integer>>`.
   */
  private void dropTypeArguments() {
    if (token != Token.LT) {
      return;
    }
    next();
    int depth = 1;
    while (depth > 0) {
      switch (token) {
        case LT:
          depth++;
          next();
          break;
        case GTGTGT:
          depth -= 3;
          next();
          break;
        case GTGT:
          depth -= 2;
          next();
          break;
        case GT:
          depth--;
          next();
          break;
        case EOF:
          throw error(ErrorKind.UNEXPECTED_EOF);
        default:
          next();
          break;
      }
    }
  }

  @CheckReturnValue
  private TurbineError error(ErrorKind kind, Object... args) {
    return TurbineError.format(
        lexer.source(),
        Math.min(lexer.position(), lexer.source().source().length() - 1),
        kind,
        args);
  }
}