diff options
author | Colin Cross <ccross@android.com> | 2019-01-23 21:51:57 -0800 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2019-01-24 08:29:36 -0800 |
commit | 3fec25b90823d67df97620ac55b1c596bc8c993a (patch) | |
tree | fc48c84a601aa4170462d011afff6fb597364019 /java/com/google/turbine/parse/Parser.java | |
parent | f39733ccf6b98679aa70204682da7759e3644fac (diff) | |
parent | 5a26f8cf360ab8f450d6268574a8a8ab50588d0d (diff) | |
download | turbine-3fec25b90823d67df97620ac55b1c596bc8c993a.tar.gz |
Merge remote-tracking branch 'aosp/upstream-master' into master
Updates turbine to upstream 5a26f8cf360ab8f450d6268574a8a8ab50588d0d.
Test: m checkbuild
Change-Id: I611f1edd5c9a85cf06a23916410eb9b24cab811f
Diffstat (limited to 'java/com/google/turbine/parse/Parser.java')
-rw-r--r-- | java/com/google/turbine/parse/Parser.java | 150 |
1 files changed, 87 insertions, 63 deletions
diff --git a/java/com/google/turbine/parse/Parser.java b/java/com/google/turbine/parse/Parser.java index 3858619..ba76659 100644 --- a/java/com/google/turbine/parse/Parser.java +++ b/java/com/google/turbine/parse/Parser.java @@ -17,16 +17,12 @@ package com.google.turbine.parse; import static com.google.turbine.parse.Token.COMMA; -import static com.google.turbine.parse.Token.IDENT; import static com.google.turbine.parse.Token.INTERFACE; import static com.google.turbine.parse.Token.LPAREN; import static com.google.turbine.parse.Token.RPAREN; -import static com.google.turbine.parse.Token.STATIC; import static com.google.turbine.tree.TurbineModifier.PROTECTED; import static com.google.turbine.tree.TurbineModifier.PUBLIC; -import com.google.common.base.Joiner; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.CheckReturnValue; @@ -41,6 +37,7 @@ import com.google.turbine.tree.Tree.ArrTy; import com.google.turbine.tree.Tree.ClassTy; import com.google.turbine.tree.Tree.CompUnit; import com.google.turbine.tree.Tree.Expression; +import com.google.turbine.tree.Tree.Ident; import com.google.turbine.tree.Tree.ImportDecl; import com.google.turbine.tree.Tree.Kind; import com.google.turbine.tree.Tree.MethDecl; @@ -63,6 +60,7 @@ import java.util.ArrayDeque; import java.util.Deque; import java.util.EnumSet; import java.util.List; +import java.util.Optional; import javax.annotation.Nullable; /** @@ -95,8 +93,8 @@ public class Parser { // TODO(cushon): consider enforcing package, import, and declaration order // and make it bug-compatible with javac: // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-August/006968.html - Optional<PkgDecl> pkg = Optional.absent(); - Optional<ModDecl> mod = Optional.absent(); + Optional<PkgDecl> pkg = Optional.empty(); + Optional<ModDecl> mod = Optional.empty(); EnumSet<TurbineModifier> access = EnumSet.noneOf(TurbineModifier.class); ImmutableList.Builder<ImportDecl> imports = ImmutableList.builder(); ImmutableList.Builder<TyDecl> decls = ImmutableList.builder(); @@ -184,14 +182,15 @@ public class Parser { continue; case IDENT: { - String ident = lexer.stringValue(); - if (access.isEmpty() && (ident.equals("module") || ident.equals("open"))) { + Ident ident = ident(); + if (access.isEmpty() + && (ident.value().equals("module") || ident.value().equals("open"))) { boolean open = false; - if (ident.equals("open")) { + if (ident.value().equals("open")) { ident = eatIdent(); open = true; } - if (!ident.equals("module")) { + if (!ident.value().equals("module")) { throw error(token); } next(); @@ -215,7 +214,7 @@ public class Parser { private TyDecl interfaceDeclaration(EnumSet<TurbineModifier> access, ImmutableList<Anno> annos) { eat(Token.INTERFACE); int pos = position; - String name = eatIdent(); + Ident name = eatIdent(); ImmutableList<TyParam> typarams; if (token == Token.LT) { typarams = typarams(); @@ -238,7 +237,7 @@ public class Parser { annos, name, typarams, - Optional.<ClassTy>absent(), + Optional.<ClassTy>empty(), interfaces.build(), members, TurbineTyKind.INTERFACE); @@ -247,7 +246,7 @@ public class Parser { private TyDecl annotationDeclaration(EnumSet<TurbineModifier> access, ImmutableList<Anno> annos) { eat(Token.INTERFACE); int pos = position; - String name = eatIdent(); + Ident name = eatIdent(); eat(Token.LBRACE); ImmutableList<Tree> members = classMembers(); eat(Token.RBRACE); @@ -257,7 +256,7 @@ public class Parser { annos, name, ImmutableList.<TyParam>of(), - Optional.<ClassTy>absent(), + Optional.<ClassTy>empty(), ImmutableList.<ClassTy>of(), members, TurbineTyKind.ANNOTATION); @@ -266,7 +265,7 @@ public class Parser { private TyDecl enumDeclaration(EnumSet<TurbineModifier> access, ImmutableList<Anno> annos) { eat(Token.ENUM); int pos = position; - String name = eatIdent(); + Ident name = eatIdent(); ImmutableList.Builder<ClassTy> interfaces = ImmutableList.builder(); if (token == Token.IMPLEMENTS) { next(); @@ -284,18 +283,18 @@ public class Parser { annos, name, ImmutableList.<TyParam>of(), - Optional.<ClassTy>absent(), + Optional.<ClassTy>empty(), interfaces.build(), members, TurbineTyKind.ENUM); } private String moduleName() { - return Joiner.on('.').join(qualIdent()); + return flatname('.', qualIdent()); } private String packageName() { - return Joiner.on('/').join(qualIdent()); + return flatname('/', qualIdent()); } private ModDecl moduleDeclaration(boolean open, ImmutableList<Anno> annos) { @@ -340,6 +339,19 @@ public class Parser { return new ModDecl(pos, annos, open, moduleName, directives.build()); } + private String flatname(char join, ImmutableList<Ident> idents) { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (Ident ident : idents) { + if (!first) { + sb.append(join); + } + sb.append(ident.value()); + first = false; + } + return sb.toString(); + } + private ModRequires moduleRequires() { int pos = position; EnumSet<TurbineModifier> access = EnumSet.noneOf(TurbineModifier.class); @@ -356,6 +368,7 @@ public class Parser { } break; } + String moduleName = moduleName(); eat(Token.SEMI); return new ModRequires(pos, ImmutableSet.copyOf(access), moduleName); @@ -363,13 +376,14 @@ public class Parser { private ModExports moduleExports() { int pos = position; + String packageName = packageName(); ImmutableList.Builder<String> moduleNames = ImmutableList.builder(); if (lexer.stringValue().equals("to")) { next(); do { - String moduleName = moduleName(); - moduleNames.add(moduleName); + + moduleNames.add(moduleName()); } while (maybe(Token.COMMA)); } eat(Token.SEMI); @@ -378,11 +392,13 @@ public class Parser { private ModOpens moduleOpens() { int pos = position; + String packageName = packageName(); ImmutableList.Builder<String> moduleNames = ImmutableList.builder(); if (lexer.stringValue().equals("to")) { next(); do { + String moduleName = moduleName(); moduleNames.add(moduleName); } while (maybe(Token.COMMA)); @@ -393,20 +409,20 @@ public class Parser { private ModUses moduleUses() { int pos = position; - ImmutableList<String> uses = qualIdent(); + ImmutableList<Ident> uses = qualIdent(); eat(Token.SEMI); return new ModUses(pos, uses); } private ModProvides moduleProvides() { int pos = position; - ImmutableList<String> typeName = qualIdent(); - if (!eatIdent().equals("with")) { + ImmutableList<Ident> typeName = qualIdent(); + if (!eatIdent().value().equals("with")) { throw error(token); } - ImmutableList.Builder<ImmutableList<String>> implNames = ImmutableList.builder(); + ImmutableList.Builder<ImmutableList<Ident>> implNames = ImmutableList.builder(); do { - ImmutableList<String> implName = qualIdent(); + ImmutableList<Ident> implName = qualIdent(); implNames.add(implName); } while (maybe(Token.COMMA)); eat(Token.SEMI); @@ -420,7 +436,7 @@ public class Parser { TurbineModifier.ACC_ENUM, TurbineModifier.FINAL); - private ImmutableList<Tree> enumMembers(String enumName) { + private ImmutableList<Tree> enumMembers(Ident enumName) { ImmutableList.Builder<Tree> result = ImmutableList.builder(); ImmutableList.Builder<Anno> annos = ImmutableList.builder(); OUTER: @@ -428,7 +444,7 @@ public class Parser { switch (token) { case IDENT: { - String name = eatIdent(); + Ident name = eatIdent(); if (token == Token.LPAREN) { dropParens(); } @@ -444,12 +460,12 @@ public class Parser { annos.build(), new ClassTy( position, - Optional.<ClassTy>absent(), + Optional.<ClassTy>empty(), enumName, ImmutableList.<Type>of(), ImmutableList.of()), name, - Optional.<Expression>absent())); + Optional.<Expression>empty())); annos = ImmutableList.builder(); break; } @@ -474,7 +490,7 @@ public class Parser { private TyDecl classDeclaration(EnumSet<TurbineModifier> access, ImmutableList<Anno> annos) { eat(Token.CLASS); int pos = position; - String name = eatIdent(); + Ident name = eatIdent(); ImmutableList<TyParam> tyParams = ImmutableList.of(); if (token == Token.LT) { tyParams = typarams(); @@ -500,7 +516,7 @@ public class Parser { annos, name, tyParams, - Optional.fromNullable(xtnds), + Optional.ofNullable(xtnds), interfaces.build(), members, TurbineTyKind.CLASS); @@ -624,7 +640,7 @@ public class Parser { EnumSet<TurbineModifier> access, ImmutableList<Anno> annos) { ImmutableList<TyParam> typaram = ImmutableList.of(); Type result; - String name; + Ident name; if (token == Token.LT) { typaram = typarams(); @@ -660,7 +676,7 @@ public class Parser { case IDENT: { int pos = position; - String ident = eatIdent(); + Ident ident = eatIdent(); switch (token) { case LPAREN: { @@ -672,7 +688,7 @@ public class Parser { result = new ClassTy( position, - Optional.<ClassTy>absent(), + Optional.<ClassTy>empty(), ident, ImmutableList.<Type>of(), ImmutableList.of()); @@ -686,7 +702,7 @@ public class Parser { result = new ClassTy( position, - Optional.<ClassTy>absent(), + Optional.<ClassTy>empty(), ident, ImmutableList.<Type>of(), ImmutableList.of()); @@ -697,7 +713,7 @@ public class Parser { { result = new ClassTy( - position, Optional.<ClassTy>absent(), ident, tyargs(), ImmutableList.of()); + position, Optional.<ClassTy>empty(), ident, tyargs(), ImmutableList.of()); result = maybeDims(maybeAnnos(), result); break; } @@ -705,7 +721,7 @@ public class Parser { result = new ClassTy( position, - Optional.<ClassTy>absent(), + Optional.<ClassTy>empty(), ident, ImmutableList.<Type>of(), ImmutableList.of()); @@ -764,12 +780,13 @@ public class Parser { ImmutableList<Anno> annos, ImmutableList<TyParam> typaram, Type result, - String name) { + Ident name) { switch (token) { case ASSIGN: - case SEMI: - case LBRACK: + case AT: case COMMA: + case LBRACK: + case SEMI: { if (!typaram.isEmpty()) { throw error(ErrorKind.UNEXPECTED_TYPE_PARAMETER, typaram); @@ -788,7 +805,7 @@ public class Parser { EnumSet<TurbineModifier> access, ImmutableList<Anno> annos, Type baseTy, - String name) { + Ident name) { ImmutableList.Builder<Tree> result = ImmutableList.builder(); VariableInitializerParser initializerParser = new VariableInitializerParser(token, lexer); List<List<SavedToken>> bits = initializerParser.parseInitializers(); @@ -810,7 +827,7 @@ public class Parser { if (init != null && init.kind() == Tree.Kind.ARRAY_INIT) { init = null; } - result.add(new VarDecl(pos, access, annos, ty, name, Optional.fromNullable(init))); + result.add(new VarDecl(pos, access, annos, ty, name, Optional.ofNullable(init))); } eat(Token.SEMI); return result.build(); @@ -822,7 +839,7 @@ public class Parser { ImmutableList<Anno> annos, ImmutableList<TyParam> typaram, Type result, - String name) { + Ident name) { eat(Token.LPAREN); ImmutableList.Builder<VarDecl> formals = ImmutableList.builder(); formalParams(formals, access); @@ -863,18 +880,18 @@ public class Parser { throw error(token); } if (result == null) { - name = CTOR_NAME; + name = new Ident(position, CTOR_NAME); } return new MethDecl( pos, access, annos, typaram, - Optional.<Tree>fromNullable(result), + Optional.<Tree>ofNullable(result), name, formals.build(), exceptions.build(), - Optional.fromNullable(defaultValue)); + Optional.ofNullable(defaultValue)); } /** @@ -948,23 +965,24 @@ public class Parser { } // the parameter name is `this` for receiver parameters, and a qualified this expression // for inner classes - String name = identOrThis(); + Ident name = identOrThis(); while (token == Token.DOT) { eat(Token.DOT); // Overwrite everything up to the terminal 'this' for inner classes; we don't need it name = identOrThis(); } ty = extraDims(ty); - return new VarDecl(position, access, annos.build(), ty, name, Optional.<Expression>absent()); + return new VarDecl(position, access, annos.build(), ty, name, Optional.<Expression>empty()); } - private String identOrThis() { + private Ident identOrThis() { switch (token) { case IDENT: return eatIdent(); case THIS: + int position = lexer.position(); eat(Token.THIS); - return "this"; + return new Ident(position, "this"); default: throw error(token); } @@ -1016,7 +1034,7 @@ public class Parser { OUTER: while (true) { ImmutableList<Anno> annotations = maybeAnnos(); - String name = eatIdent(); + Ident name = eatIdent(); ImmutableList<Tree> bounds = ImmutableList.of(); if (token == Token.EXTENDS) { next(); @@ -1059,12 +1077,12 @@ public class Parser { if (typeAnnos == null) { typeAnnos = maybeAnnos(); } - String name = eatIdent(); + Ident name = eatIdent(); ImmutableList<Type> tyargs = ImmutableList.of(); if (token == Token.LT) { tyargs = tyargs(); } - ty = new ClassTy(pos, Optional.fromNullable(ty), name, tyargs, typeAnnos); + ty = new ClassTy(pos, Optional.ofNullable(ty), name, tyargs, typeAnnos); typeAnnos = null; } while (maybe(Token.DOT)); return ty; @@ -1085,25 +1103,25 @@ public class Parser { next(); Type upper = referenceType(maybeAnnos()); acc.add( - new WildTy(position, typeAnnos, Optional.of(upper), Optional.<Type>absent())); + new WildTy(position, typeAnnos, Optional.of(upper), Optional.<Type>empty())); break; case SUPER: next(); Type lower = referenceType(maybeAnnos()); acc.add( - new WildTy(position, typeAnnos, Optional.<Type>absent(), Optional.of(lower))); + new WildTy(position, typeAnnos, Optional.<Type>empty(), Optional.of(lower))); break; case COMMA: acc.add( new WildTy( - position, typeAnnos, Optional.<Type>absent(), Optional.<Type>absent())); + position, typeAnnos, Optional.<Type>empty(), Optional.<Type>empty())); continue OUTER; case GT: case GTGT: case GTGTGT: acc.add( new WildTy( - position, typeAnnos, Optional.<Type>absent(), Optional.<Type>absent())); + position, typeAnnos, Optional.<Type>empty(), Optional.<Type>empty())); break OUTER; default: throw error(token); @@ -1257,7 +1275,7 @@ public class Parser { boolean stat = maybe(Token.STATIC); int pos = position; - ImmutableList.Builder<String> type = ImmutableList.builder(); + ImmutableList.Builder<Ident> type = ImmutableList.builder(); type.add(eatIdent()); boolean wild = false; OUTER: @@ -1284,8 +1302,8 @@ public class Parser { return result; } - private ImmutableList<String> qualIdent() { - ImmutableList.Builder<String> name = ImmutableList.builder(); + private ImmutableList<Ident> qualIdent() { + ImmutableList.Builder<Ident> name = ImmutableList.builder(); name.add(eatIdent()); while (maybe(Token.DOT)) { name.add(eatIdent()); @@ -1295,7 +1313,7 @@ public class Parser { private Anno annotation() { int pos = position; - ImmutableList<String> name = qualIdent(); + ImmutableList<Ident> name = qualIdent(); ImmutableList.Builder<Expression> args = ImmutableList.builder(); if (token == Token.LPAREN) { @@ -1318,10 +1336,16 @@ public class Parser { return new Anno(pos, name, args.build()); } - private String eatIdent() { + private Ident ident() { + int position = lexer.position(); String value = lexer.stringValue(); + return new Ident(position, value); + } + + private Ident eatIdent() { + Ident ident = ident(); eat(Token.IDENT); - return value; + return ident; } private void eat(Token kind) { |