Skip to content

Commit

Permalink
Fix whitespace handling in mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
JordonPhillips committed Jan 10, 2022
1 parent 16a1e02 commit fe23e07
Show file tree
Hide file tree
Showing 23 changed files with 219 additions and 19 deletions.
16 changes: 8 additions & 8 deletions designs/mixins.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,14 +606,14 @@ shape being defined and MUST be marked with the `@mixin` trait.

```
simple_shape_statement = simple_type_name ws identifier [mixins]
list_statement = "list" ws identifier [mixins ws] shape_members
set_statement = "set" ws identifier [mixins ws] shape_members
map_statement = "map" ws identifier [mixins ws] shape_members
structure_statement = "structure" ws identifier [mixins ws] structure_members
union_statement = "union" ws identifier [mixins ws] union_members
service_statement = "service" ws identifier [mixins ws] node_object
operation_statement = "operation" ws identifier [mixins ws] node_object
resource_statement = "resource" ws identifier [mixins ws] node_object
list_statement = "list" ws identifier [mixins] ws shape_members
set_statement = "set" ws identifier [mixins] ws shape_members
map_statement = "map" ws identifier [mixins] ws shape_members
structure_statement = "structure" ws identifier [mixins] ws structure_members
union_statement = "union" ws identifier [mixins] ws union_members
service_statement = "service" ws identifier [mixins] ws node_object
operation_statement = "operation" ws identifier [mixins] ws node_object
resource_statement = "resource" ws identifier [mixins] ws node_object
mixins = sp "with" ws "[" 1*(ws shape_id) ws "]"
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,11 @@ private ShapeId parseShapeName() {
private void parseSimpleShape(ShapeId id, SourceLocation location, AbstractShapeBuilder builder) {
modelFile.onShape(builder.source(location).id(id));
parseMixins(id);
// Pending docs get cleared by parseMixins. It isn't done here because the
// mixin parser needs to consume whitespace that may include doc comments for
// the next shape in the model.
}

// See parseMap for information on why members are parsed before the
// list/set is registered with the ModelFile.
private void parseCollection(ShapeId id, SourceLocation location, CollectionShape.Builder builder) {
ws();
builder.id(id).source(location);
parseMixins(id);
parseMembers(id, SetUtils.of("member"));
Expand Down Expand Up @@ -558,17 +554,13 @@ private void parseStructuredShape(
// "Member `foo.baz#Foo$Baz` cannot be added to software.amazon.smithy.model.shapes.OperationShape$Builder"
modelFile.onShape(builder.id(id).source(location));

ws();

// Parse optional "with" statements to add mixins, but only if it's supported by the version.
parseMixins(id);
parseMembers(id, Collections.emptySet());
clearPendingDocs();
}

private void parseMixins(ShapeId id) {
// This is fine for now, but if we ever add any other keywords that start with
// 'w' then we'll need to peek farther.
sp();
if (peek() != 'w') {
return;
Expand Down Expand Up @@ -698,7 +690,6 @@ private void parseIdList(Consumer<ShapeId> consumer) {
}

private void parseServiceStatement(ShapeId id, SourceLocation location) {
ws();
parseMixins(id);
ws();
ServiceShape.Builder builder = new ServiceShape.Builder().id(id).source(location);
Expand Down Expand Up @@ -729,7 +720,6 @@ private void optionalIdList(ObjectNode node, String name, Consumer<ShapeId> cons
}

private void parseResourceStatement(ShapeId id, SourceLocation location) {
ws();
parseMixins(id);
ws();
ResourceShape.Builder builder = ResourceShape.builder().id(id).source(location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void mustNotContainMembersInShapeId() {

@Test
public void requiresMember() {
Assertions.assertThrows(IllegalStateException.class, () -> {
Assertions.assertThrows(SourceException.class, () -> {
ListShape.builder().id("ns.foo#bar").build();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
bigDecimal MixinBigdecimal

bigDecimal MixedBigdecimal
with [MixinBigdecimal]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
bigInteger MixinBiginteger

bigInteger MixedBiginteger
with [MixinBiginteger]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
blob MixinBlob

blob MixedBlob
with [MixinBlob]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
boolean MixinBoolean

boolean MixedBoolean
with [MixinBoolean]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
byte MixinByte

byte MixedByte
with [MixinByte]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
document MixinDocument

document MixedDocument
with [MixinDocument]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
double MixinDouble

double MixedDouble
with [MixinDouble]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
float MixinFloat

float MixedFloat
with [MixinFloat]

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
integer MixinInteger

integer MixedInteger
with [MixinInteger]

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Parse error at line 7, column 5 near `with`: Expected: '{', but found 'w'
$version: "2.0"

namespace smithy.example

list MixedList
with [MixinList] {}

@mixin
list MixinList {
member: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
long MixinLong

long MixedLong
with [MixinLong]

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Parse error at line 7, column 5 near `with`: Expected: '{', but found 'w'
$version: "2.0"

namespace smithy.example

map MixedMap
with [MixinMap] {}

@mixin
map MixinMap {
key: String
value: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Parse error at line 7, column 5 near `with`: Expected: '{', but found 'w'
$version: "2.0"

namespace smithy.example

resource MixedResource
with [MixinResource] {}

@mixin
resource MixinResource {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Parse error at line 7, column 5 near `with`: Expected: '{', but found 'w'
$version: "2.0"

namespace smithy.example

service MixedService
with [MixinService] {}

@mixin
service MixinService {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Parse error at line 7, column 5 near `with`: Expected: '{', but found 'w'
$version: "2.0"

namespace smithy.example

map MixedMap
with [MixinMap] {}

@mixin
map MixinMap {
key: String
value: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
short MixinShort

short MixedShort
with [MixinShort]

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Parse error at line 7, column 5 near `with`: Expected: '{', but found 'w'
$version: "2.0"

namespace smithy.example

structure MixedStructure
with [MixinStructure] {}

@mixin
structure MixinStructure {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Parse error at line 10, column 9 near ` `: Unexpected shape type: with
$version: "2.0"

namespace smithy.example

@mixin
timestamp MixinTimestamp

timestamp MixedTimestamp
with [MixinTimestamp]

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Parse error at line 7, column 5 near `with`: Expected: '{', but found 'w'
$version: "2.0"

namespace smithy.example

union MixedUnion
with [MixinUnion] {}

@mixin
union MixinUnion {}

0 comments on commit fe23e07

Please sign in to comment.