Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quoted text grammar and parsing #1535

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/source-2.0/spec/idl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,20 @@ string support defined in :rfc:`7405`.
NodeKeywords :%s"true" / %s"false" / %s"null"
NodeStringValue :`ShapeId` / `TextBlock` / `QuotedText`
QuotedText :DQUOTE *`QuotedChar` DQUOTE
QuotedChar :%x20-21 ; space - "!"
QuotedChar :%x09 ; tab
:/ %x20-21 ; space - "!"
:/ %x23-5B ; "#" - "["
:/ %x5D-10FFFF ; "]"+
:/ `EscapedChar`
:/ `PreservedDouble`
:/ `NL`
EscapedChar :`Escape` (`Escape` / "'" / DQUOTE / %s"b"
: / %s"f" / %s"n" / %s"r" / %s"t"
: / "/" / `UnicodeEscape`)
EscapedChar :`Escape` (`Escape` / DQUOTE / %s"b" / %s"f"
: / %s"n" / %s"r" / %s"t" / "/"
: / `UnicodeEscape`)
UnicodeEscape :%s"u" `Hex` `Hex` `Hex` `Hex`
Hex :DIGIT / %x41-46 / %x61-66
PreservedDouble :`Escape` (%x20-21 / %x23-5B / %x5D-10FFFF)
Escape :%x5C ; backslash
TextBlock :`ThreeDquotes` *`SP` `NL` *`QuotedChar` `ThreeDquotes`
TextBlock :`ThreeDquotes` *`SP` `NL` *`TextBlockContent` `ThreeDquotes`
TextBlockContent :`QuotedChar` / (1*2DQUOTE 1*`QuotedChar`)
ThreeDquotes :DQUOTE DQUOTE DQUOTE

.. rubric:: Shapes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static String parseQuotedTextAndTextBlock(IdlModelParser parser, boolean triple)
while (!parser.eof()) {
char next = parser.peek();
if (next == '"' && (!triple || (parser.peek(1) == '"' && parser.peek(2) == '"'))) {
// Found closing quotes of quoted_text and/or text_block
break;
}
parser.skip();
Expand Down Expand Up @@ -82,8 +83,10 @@ private static String parseStringContents(IdlModelParser parser, String lexeme,
case NORMAL:
if (c == '\\') {
state = State.AFTER_ESCAPE;
} else {
} else if (isValidNormalCharacter(c, triple)) {
result.append(c);
} else {
throw parser.syntax("Invalid character: `" + c + "`");
}
break;
case AFTER_ESCAPE:
Expand Down Expand Up @@ -285,4 +288,17 @@ private static String createTextBlockLine(String line, int longestPadding) {

return endPosition >= startPosition ? line.substring(startPosition, endPosition + 1) : null;
}

private static boolean isValidNormalCharacter(char c, boolean isTextBlock) {
// Valid normal characters are the unescaped characters defined in the
// QuotedChar grammar:
// https://smithy.io/2.0/spec/idl.html#grammar-token-smithy-QuotedChar
return c == '\t'
|| c == '\n'
|| c == '\r'
|| (c >= 0x20 && c <= 0x21) // space - "!"
|| (isTextBlock && c == 0x22) // DQUOTE is allowed in text_block
|| (c >= 0x23 && c <= 0x5b) // "#" - "["
|| c >= 0x5d; // "]"+
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -18,15 +19,20 @@ public void parsesText(String input, String lexeme) {
assertThat(result.getValue(), equalTo(lexeme));
}

@ParameterizedTest
@MethodSource("invalidTextProvider")
public void throwsForInvalidText(String invalidInput) {
IdlModelParser parser = new IdlModelParser("/foo", invalidInput);
assertThrows(ModelSyntaxException.class, () -> IdlNodeParser.parseNode(parser).expectStringNode());
}

private static Stream<Arguments> validTextProvider() {
return Stream.of(
Arguments.of("\"foo\"", "foo"),
Arguments.of("\"foo\\\\bar\"", "foo\\bar"),
Arguments.of("\"\t\"", "\t"),
Arguments.of("\"\r\"", "\n"),
Arguments.of("\"\n\"", "\n"),
Arguments.of("\"\b\"", "\b"),
Arguments.of("\"\f\"", "\f"),
Arguments.of("\"\\t\"", "\t"),
Arguments.of("\"\\r\"", "\r"),
Arguments.of("\"\\n\"", "\n"),
Expand Down Expand Up @@ -59,8 +65,10 @@ private static Stream<Arguments> validTextProvider() {
Arguments.of("\"\"\"\n\n\n\"\"\"", "\n\n"),
Arguments.of("\"\"\"\n foo\n baz\n \"\"\"", "foo\nbaz\n"),
Arguments.of("\"\"\"\n foo\n baz\n \"\"\"", "foo\n baz\n"),
Arguments.of("\"\"\"\n\"foo\\\"\"\"\"", "\"foo\""),
Arguments.of("\"\"\"\n foo\\n bar\n baz\"\"\"", "foo\n bar\nbaz"),
Arguments.of("\"\"\"\n{ \"foo\": \"bar\" }\"\"\"", "{ \"foo\": \"bar\" }"),
Arguments.of("\"\"\"\n \"a\" \"\"\"", "\"a\""),
Arguments.of("\"\"\"\n \" \"\"\"", "\""),
// Empty lines and lines with only ws do not contribute to incidental ws.
Arguments.of("\"\"\"\n\n foo\n \n\n \n \"\"\"", "\nfoo\n\n\n\n"),
// If the last line is offset to the right, it's discarded since it's all whitespace.
Expand All @@ -69,4 +77,8 @@ private static Stream<Arguments> validTextProvider() {
Arguments.of("\"\"\"\r Foo\\\r Baz\"\"\"", "FooBaz"),
Arguments.of("\"\"\"\r\n Foo\\\r\n Baz\"\"\"", "FooBaz"));
}

private static Stream<String> invalidTextProvider() {
return Stream.of( "\"\b\"", "\"\"\"", "\"\\\"", "\"\\'\"");
}
}