From 9c5fbc77c20c78acd86220d3428f08078d0745ce Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Thu, 4 Aug 2022 16:26:57 -0700 Subject: [PATCH] Relax apply statement grammar to allow ws before trait --- docs/source-2.0/spec/idl.rst | 4 ++-- .../smithy/model/loader/IdlModelParser.java | 16 +++++++++++++--- .../validation/testrunner/SmithyTestCase.java | 2 +- .../invalid/apply/apply-missing-space.smithy | 2 +- .../apply/apply-missing-trait-value.smithy | 2 +- .../invalid/apply/apply-multiple-lines.smithy | 7 ------- .../elision/list-member-elision-in-v1.smithy | 2 +- .../structure-member-elision-in-v1.smithy | 2 +- .../metadata/not-newline-after-metadata.smithy | 2 +- .../invalid-newline-after-shape-type.smithy | 2 +- 10 files changed, 22 insertions(+), 19 deletions(-) delete mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-multiple-lines.smithy diff --git a/docs/source-2.0/spec/idl.rst b/docs/source-2.0/spec/idl.rst index d285fb3de0a..f46a0cf50f2 100644 --- a/docs/source-2.0/spec/idl.rst +++ b/docs/source-2.0/spec/idl.rst @@ -237,8 +237,8 @@ string support defined in `RFC 5234 `_. TraitStructure :`TraitStructureKvp` *(*`WS` `TraitStructureKvp`) TraitStructureKvp :`NodeObjectKey` *`WS` ":" *`WS` `NodeValue` ApplyStatement :(`ApplyStatementSingular` / `ApplyStatementBlock`) - ApplyStatementSingular :%s"apply" `SP` `ShapeId` `SP` `Trait` `BR` - ApplyStatementBlock :%s"apply" `SP` `ShapeId` `SP` "{" `TraitStatements` "}" `BR` + ApplyStatementSingular :%s"apply" `WS` `ShapeId` `WS` `Trait` `BR` + ApplyStatementBlock :%s"apply" `SP` `ShapeId` `WS` "{" `TraitStatements` "}" `BR` .. rubric:: Shape ID diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlModelParser.java b/smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlModelParser.java index 4d3d39f914c..a81b2a348c0 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlModelParser.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlModelParser.java @@ -222,7 +222,7 @@ public void ws() { } // required space - public void rsp() { + private void rsp() { int cc = column(); sp(); if (column() == cc) { @@ -230,6 +230,16 @@ public void rsp() { } } + // Required whitespace. + private void rws() { + int line = line(); + int column = column(); + ws(); + if (line() == line && column == column()) { + throw syntax("Expected one or more whitespace characters"); + } + } + @Override public void sp() { while (isSpaceOrComma(peek())) { @@ -1122,10 +1132,10 @@ private void parseApplyStatement() { expect('p'); expect('l'); expect('y'); - rsp(); + sp(); String name = ParserUtils.parseShapeId(this); - rsp(); + rws(); // Account for singular or block apply statements. List traitsToApply; diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java index 9b85457da1a..82196018cc0 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java @@ -133,7 +133,7 @@ private static boolean compareEvents(ValidationEvent expected, ValidationEvent a normalizedActualMessage += " (" + actual.getSuppressionReason().get() + ")"; } - String comparedMessage = expected.getMessage().replace("\n", "\\n"); + String comparedMessage = expected.getMessage().replace("\n", "\\n").replace("\r", "\\n"); return expected.getSeverity() == actual.getSeverity() && expected.getId().equals(actual.getId()) && expected.getShapeId().equals(actual.getShapeId()) diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-missing-space.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-missing-space.smithy index e08fe049a04..832b2746ea5 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-missing-space.smithy +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-missing-space.smithy @@ -1,4 +1,4 @@ -// Parse error at line 5, column 10 near `{ `: Expected one or more spaces | Model +// Parse error at line 5, column 10 near `{ `: Expected one or more whitespace characters | Model $version: "2.0" namespace smithy.example diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-missing-trait-value.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-missing-trait-value.smithy index 02270631439..38e1baa5fea 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-missing-trait-value.smithy +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-missing-trait-value.smithy @@ -1,4 +1,4 @@ -// Parse error at line 4, column 17 near `//`: Expected: '@', but found '/' | Model +// Parse error at line 5, column 1 near `string`: Expected: '@', but found 's' namespace com.foo apply SomeShape // comment so spaces aren't eaten diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-multiple-lines.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-multiple-lines.smithy deleted file mode 100644 index 8209b34adfb..00000000000 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/apply/apply-multiple-lines.smithy +++ /dev/null @@ -1,7 +0,0 @@ -// Parse error at line 6, column 10 near `\n@`: Expected one or more spaces | Model -$version: "2.0" - -namespace smithy.example - -apply Foo -@sensitive diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/elision/list-member-elision-in-v1.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/elision/list-member-elision-in-v1.smithy index 5a653fe30b6..e71c9339941 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/elision/list-member-elision-in-v1.smithy +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/elision/list-member-elision-in-v1.smithy @@ -1,4 +1,4 @@ -// Parse error at line 6, column 5 near `$m`: Members can only elide targets in IDL version 2 or later +// Members can only elide targets in IDL version 2 or later $version: "1.0" namespace smithy.example diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/elision/structure-member-elision-in-v1.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/elision/structure-member-elision-in-v1.smithy index 2e995c6fda1..d1f29cf2a52 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/elision/structure-member-elision-in-v1.smithy +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/elision/structure-member-elision-in-v1.smithy @@ -1,4 +1,4 @@ -// Parse error at line 6, column 9 near `\n}`: Members can only elide targets in IDL version 2 or later +// Members can only elide targets in IDL version 2 or later $version: "1.0" namespace smithy.example diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/metadata/not-newline-after-metadata.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/metadata/not-newline-after-metadata.smithy index 7b2125e08d3..6501ecb95b4 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/metadata/not-newline-after-metadata.smithy +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/metadata/not-newline-after-metadata.smithy @@ -1,4 +1,4 @@ -// Parse error at line 3, column 9 near `\nf`: Expected one or more spaces | Model +// Parse error at line 3, column 9 $version: "2.0" metadata foo = "bar" diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/parse-errors/invalid-newline-after-shape-type.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/parse-errors/invalid-newline-after-shape-type.smithy index a6b9d04a8b3..90ea9d09bff 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/parse-errors/invalid-newline-after-shape-type.smithy +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/loader/invalid/parse-errors/invalid-newline-after-shape-type.smithy @@ -1,4 +1,4 @@ -// Parse error at line 5, column 10 near `\nF`: Expected one or more spaces | Model +// Parse error at line 5, column 10 $version: "2.0" namespace smithy.example