Class VariableInitializerParser
- java.lang.Object
-
- com.google.turbine.parse.VariableInitializerParser
-
public class VariableInitializerParser extends java.lang.ObjectPre-process variable initializer expressions to handle multi-variable declarations.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.
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.
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
int x = a < b, c = d;. We can't tell looking at the prefixa < b, cwhether that's a less-than expression followed by another initializer, or the start of a generic type:a<b, c>.foo(. Distinguishing between these cases requires arbitrary lookahead.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.
-
-
Constructor Summary
Constructors Constructor Description VariableInitializerParser(Token token, Lexer lexer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<java.util.List<com.google.turbine.parse.SavedToken>>parseInitializers()Returns lists of tokens for individual initializers in a (mutli-)variable initializer.
-
-
-
Field Detail
-
token
public Token token
-
-