diff options
author | Liam Miller-Cushon <cushon@google.com> | 2021-09-17 16:01:01 -0700 |
---|---|---|
committer | Javac Team <javac-team+copybara@google.com> | 2021-09-17 16:01:40 -0700 |
commit | 798df575015b50761a174e90f3bd1127b8fbcd95 (patch) | |
tree | 182e255f0be796a1a5937920520092a567475e52 /java | |
parent | c0e81c3b7359e3339ed538df21f48d45255e90ab (diff) | |
download | turbine-798df575015b50761a174e90f3bd1127b8fbcd95.tar.gz |
Add parser support for records
PiperOrigin-RevId: 397417470
Diffstat (limited to 'java')
-rw-r--r-- | java/com/google/turbine/binder/CompUnitPreprocessor.java | 8 | ||||
-rw-r--r-- | java/com/google/turbine/model/TurbineTyKind.java | 3 | ||||
-rw-r--r-- | java/com/google/turbine/parse/Parser.java | 54 | ||||
-rw-r--r-- | java/com/google/turbine/processing/TurbineElement.java | 4 | ||||
-rw-r--r-- | java/com/google/turbine/tree/Pretty.java | 16 | ||||
-rw-r--r-- | java/com/google/turbine/tree/Tree.java | 7 |
6 files changed, 91 insertions, 1 deletions
diff --git a/java/com/google/turbine/binder/CompUnitPreprocessor.java b/java/com/google/turbine/binder/CompUnitPreprocessor.java index 9e9a0bb..079cd4c 100644 --- a/java/com/google/turbine/binder/CompUnitPreprocessor.java +++ b/java/com/google/turbine/binder/CompUnitPreprocessor.java @@ -175,6 +175,9 @@ public final class CompUnitPreprocessor { case ANNOTATION: access |= TurbineFlag.ACC_ABSTRACT | TurbineFlag.ACC_INTERFACE | TurbineFlag.ACC_ANNOTATION; break; + case RECORD: + // TODO(b/200222393): add support for records + throw new AssertionError(tykind); } return access; } @@ -201,6 +204,10 @@ public final class CompUnitPreprocessor { if ((enclosing & (TurbineFlag.ACC_INTERFACE | TurbineFlag.ACC_ANNOTATION)) != 0) { access |= TurbineFlag.ACC_STATIC; } + break; + case RECORD: + // TODO(b/200222393): add support for records + throw new AssertionError(decl.tykind()); } // propagate strictfp to nested types @@ -219,6 +226,7 @@ public final class CompUnitPreprocessor { Optional.empty(), ImmutableList.of(), ImmutableList.of(), + ImmutableList.of(), TurbineTyKind.INTERFACE, /* javadoc= */ null); } diff --git a/java/com/google/turbine/model/TurbineTyKind.java b/java/com/google/turbine/model/TurbineTyKind.java index 6b49f50..b61d6c9 100644 --- a/java/com/google/turbine/model/TurbineTyKind.java +++ b/java/com/google/turbine/model/TurbineTyKind.java @@ -21,5 +21,6 @@ public enum TurbineTyKind { CLASS, INTERFACE, ENUM, - ANNOTATION + ANNOTATION, + RECORD } diff --git a/java/com/google/turbine/parse/Parser.java b/java/com/google/turbine/parse/Parser.java index c26900f..2a60cc3 100644 --- a/java/com/google/turbine/parse/Parser.java +++ b/java/com/google/turbine/parse/Parser.java @@ -186,6 +186,13 @@ public class Parser { case IDENT: { Ident ident = ident(); + if (ident.value().equals("record")) { + ident = eatIdent(); + decls.add(recordDeclaration(access, annos.build())); + access = EnumSet.noneOf(TurbineModifier.class); + annos = ImmutableList.builder(); + break; + } if (access.isEmpty() && (ident.value().equals("module") || ident.value().equals("open"))) { boolean open = false; @@ -214,6 +221,46 @@ public class Parser { position = lexer.position(); } + private TyDecl recordDeclaration(EnumSet<TurbineModifier> access, ImmutableList<Anno> annos) { + String javadoc = lexer.javadoc(); + int pos = position; + Ident name = eatIdent(); + ImmutableList<TyParam> typarams; + if (token == Token.LT) { + typarams = typarams(); + } else { + typarams = ImmutableList.of(); + } + ImmutableList.Builder<VarDecl> formals = ImmutableList.builder(); + if (token == Token.LPAREN) { + next(); + formalParams(formals, EnumSet.noneOf(TurbineModifier.class)); + eat(Token.RPAREN); + } + ImmutableList.Builder<ClassTy> interfaces = ImmutableList.builder(); + if (token == Token.IMPLEMENTS) { + next(); + do { + interfaces.add(classty()); + } while (maybe(Token.COMMA)); + } + eat(Token.LBRACE); + ImmutableList<Tree> members = classMembers(); + eat(Token.RBRACE); + return new TyDecl( + pos, + access, + annos, + name, + typarams, + Optional.<ClassTy>empty(), + interfaces.build(), + members, + formals.build(), + TurbineTyKind.RECORD, + javadoc); + } + private TyDecl interfaceDeclaration(EnumSet<TurbineModifier> access, ImmutableList<Anno> annos) { String javadoc = lexer.javadoc(); eat(Token.INTERFACE); @@ -244,6 +291,7 @@ public class Parser { Optional.<ClassTy>empty(), interfaces.build(), members, + ImmutableList.of(), TurbineTyKind.INTERFACE, javadoc); } @@ -265,6 +313,7 @@ public class Parser { Optional.<ClassTy>empty(), ImmutableList.<ClassTy>of(), members, + ImmutableList.of(), TurbineTyKind.ANNOTATION, javadoc); } @@ -294,6 +343,7 @@ public class Parser { Optional.<ClassTy>empty(), interfaces.build(), members, + ImmutableList.of(), TurbineTyKind.ENUM, javadoc); } @@ -539,6 +589,7 @@ public class Parser { Optional.ofNullable(xtnds), interfaces.build(), members, + ImmutableList.of(), TurbineTyKind.CLASS, javadoc); } @@ -699,6 +750,9 @@ public class Parser { { int pos = position; Ident ident = eatIdent(); + if (ident.value().equals("record")) { + return ImmutableList.of(recordDeclaration(access, annos)); + } switch (token) { case LPAREN: { diff --git a/java/com/google/turbine/processing/TurbineElement.java b/java/com/google/turbine/processing/TurbineElement.java index 71c03d0..69f6ec0 100644 --- a/java/com/google/turbine/processing/TurbineElement.java +++ b/java/com/google/turbine/processing/TurbineElement.java @@ -280,6 +280,8 @@ public abstract class TurbineElement implements Element { case INTERFACE: case ANNOTATION: return factory.noType(); + case RECORD: + // TODO(b/200222393): add support for records } throw new AssertionError(info.kind()); } @@ -376,6 +378,8 @@ public abstract class TurbineElement implements Element { return ElementKind.ENUM; case ANNOTATION: return ElementKind.ANNOTATION_TYPE; + case RECORD: + // TODO(b/200222393): add support for records } throw new AssertionError(info.kind()); } diff --git a/java/com/google/turbine/tree/Pretty.java b/java/com/google/turbine/tree/Pretty.java index 1c758cb..84b4ab5 100644 --- a/java/com/google/turbine/tree/Pretty.java +++ b/java/com/google/turbine/tree/Pretty.java @@ -21,6 +21,7 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.CanIgnoreReturnValue; +import com.google.turbine.model.TurbineTyKind; import com.google.turbine.tree.Tree.Anno; import com.google.turbine.tree.Tree.ClassLiteral; import com.google.turbine.tree.Tree.Ident; @@ -414,6 +415,9 @@ public class Pretty implements Tree.Visitor<@Nullable Void, @Nullable Void> { case ANNOTATION: append("@interface"); break; + case RECORD: + append("record"); + break; } append(' ').append(tyDecl.name().value()); if (!tyDecl.typarams().isEmpty()) { @@ -428,6 +432,18 @@ public class Pretty implements Tree.Visitor<@Nullable Void, @Nullable Void> { } append('>'); } + if (tyDecl.tykind().equals(TurbineTyKind.RECORD)) { + append("("); + boolean first = true; + for (Tree.VarDecl c : tyDecl.components()) { + if (!first) { + append(", "); + } + printVarDecl(c); + first = false; + } + append(")"); + } if (tyDecl.xtnds().isPresent()) { append(" extends "); tyDecl.xtnds().get().accept(this, null); diff --git a/java/com/google/turbine/tree/Tree.java b/java/com/google/turbine/tree/Tree.java index 7d04312..ce32628 100644 --- a/java/com/google/turbine/tree/Tree.java +++ b/java/com/google/turbine/tree/Tree.java @@ -935,6 +935,7 @@ public abstract class Tree { private final Optional<ClassTy> xtnds; private final ImmutableList<ClassTy> impls; private final ImmutableList<Tree> members; + private final ImmutableList<VarDecl> components; private final TurbineTyKind tykind; private final @Nullable String javadoc; @@ -947,6 +948,7 @@ public abstract class Tree { Optional<ClassTy> xtnds, ImmutableList<ClassTy> impls, ImmutableList<Tree> members, + ImmutableList<VarDecl> components, TurbineTyKind tykind, @Nullable String javadoc) { super(position); @@ -957,6 +959,7 @@ public abstract class Tree { this.xtnds = xtnds; this.impls = impls; this.members = members; + this.components = components; this.tykind = tykind; this.javadoc = javadoc; } @@ -1000,6 +1003,10 @@ public abstract class Tree { return members; } + public ImmutableList<VarDecl> components() { + return components; + } + public TurbineTyKind tykind() { return tykind; } |