Skip to content
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
1 change: 0 additions & 1 deletion src/main/antlr/GraphqlCommon.g4
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ Comment: '#' SourceCharacterWithoutLineFeed* -> channel(2);

LF: [\n] -> channel(3);
CR: [\r] -> channel(3);
LineTerminator: [\u2028\u2029] -> channel(3);

Space : [\u0020] -> channel(3);
Tab : [\u0009] -> channel(3);
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/graphql/parser/GraphqlLexerATNSimulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package graphql.parser;

import graphql.Internal;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.atn.LexerATNSimulator;
import org.jspecify.annotations.NullMarked;

@Internal
@NullMarked
public class GraphqlLexerATNSimulator extends LexerATNSimulator {

public GraphqlLexerATNSimulator(Lexer lexer) {
super(lexer, lexer.getATN(), lexer.getInterpreter().decisionToDFA, lexer.getInterpreter().getSharedContextCache());
}

@Override
public void consume(CharStream input) {
int codePoint = input.LA(1);
// ANTLR only counts LF. GraphQL also counts a standalone CR, while CRLF counts once.
if (codePoint == '\n' || (codePoint == '\r' && input.LA(2) != '\n')) {
setLine(getLine() + 1);
setCharPositionInLine(0);
} else {
setCharPositionInLine(getCharPositionInLine() + 1);
}
input.consume();
}
}
1 change: 1 addition & 0 deletions src/main/java/graphql/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ private static CodePointCharStream readCodePointCharStream(Reader reader, int bu
@NonNull
private static GraphqlLexer setupGraphqlLexer(ParserEnvironment environment, MultiSourceReader multiSourceReader, CodePointCharStream charStream) {
GraphqlLexer lexer = new GraphqlLexer(charStream);
lexer.setInterpreter(new GraphqlLexerATNSimulator(lexer));
lexer.removeErrorListeners();
lexer.addErrorListener(new BaseErrorListener() {
@Override
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/graphql/parser/StringValueParsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import graphql.i18n.I18n;
import graphql.language.SourceLocation;

import java.util.regex.Pattern;

/**
* Contains parsing code for the StringValue types in the grammar
*/
@Internal
public class StringValueParsing {
private final static String ESCAPED_TRIPLE_QUOTES = "\\\"\"\"";
private final static String THREE_QUOTES = "\"\"\"";
private static final Pattern LINE_TERMINATORS = Pattern.compile("\\r\\n|[\\r\\n]");

public static String parseTripleQuotedString(String strText) {
int end = strText.length() - 3;
Expand All @@ -24,7 +27,7 @@ public static String parseTripleQuotedString(String strText) {
See https://github.com/facebook/graphql/pull/327/files#diff-fe406b08746616e2f5f00909488cce66R758
*/
public static String removeIndentation(String rawValue) {
String[] lines = rawValue.split("\n");
String[] lines = LINE_TERMINATORS.split(rawValue);
Integer commonIndent = null;
for (int i = 0; i < lines.length; i++) {
if (i == 0) {
Expand Down
89 changes: 89 additions & 0 deletions src/test/groovy/graphql/parser/IgnoredCharactersTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package graphql.parser

import graphql.language.IgnoredChars
import graphql.language.Node
import spock.lang.Specification

class IgnoredCharactersTest extends Specification {

def "#nodeType captures surrounding ignored characters only when enabled"() {
given:
def left = ' \t\r\n,'
def right = ',\r\t\n '
def source = template.replace('<', left).replace('>', right)

when:
def captured = parseNode(source, nodeType, true)

then:
captured != null
captured.ignoredChars.left*.value.join() == left
captured.ignoredChars.right*.value.join() == right

when:
def uncaptured = parseNode(source, nodeType, false)

then:
uncaptured != null
uncaptured.ignoredChars.is(IgnoredChars.EMPTY)

where:
nodeType | template
'Document' | '<{ f }>'
'OperationDefinition' | '<query Q { f }>'
'SelectionSet' | 'query Q<{ f }>'
'Field' | '{<f>}'
'Argument' | '{ f(<arg: 1>) }'
'VariableDefinition' | 'query Q(<$v: Int>) { f }'
'VariableReference' | '{ f(arg:<$v>) }'
'FragmentDefinition' | '<fragment F on Query { f }>'
'FragmentSpread' | '{<...F>}'
'InlineFragment' | '{<... on Query { f }>}'
'Directive' | '{ f<@d>}'
'IntValue' | '{ f(arg:<1>) }'
'FloatValue' | '{ f(arg:<1.5>) }'
'BooleanValue' | '{ f(arg:<true>) }'
'NullValue' | '{ f(arg:<null>) }'
'StringValue' | '{ f(arg:<"value">) }'
'EnumValue' | '{ f(arg:<VALUE>) }'
'ArrayValue' | '{ f(arg:<[1]>) }'
'ObjectValue' | '{ f(arg:<{key: 1}>) }'
'TypeName' | 'query Q($v:<Int>) { f }'
'ListType' | 'query Q($v:<[Int]>) { f }'
'NonNullType' | 'query Q($v:<Int!>) { f }'
'SchemaDefinition' | '<schema { query: Query }>'
'SchemaExtensionDefinition' | '<extend schema @d>'
'OperationTypeDefinition' | 'schema {<query: Query>}'
'ScalarTypeDefinition' | '<scalar S>'
'ScalarTypeExtensionDefinition' | '<extend scalar S @d>'
'ObjectTypeDefinition' | '<type Query { f: Int }>'
'ObjectTypeExtensionDefinition' | '<extend type Query { f: Int }>'
'FieldDefinition' | 'type Query {<f: Int>}'
'InterfaceTypeDefinition' | '<interface I { f: Int }>'
'InterfaceTypeExtensionDefinition' | '<extend interface I { f: Int }>'
'UnionTypeDefinition' | '<union U = Query>'
'UnionTypeExtensionDefinition' | '<extend union U = Query>'
'EnumTypeDefinition' | '<enum E { VALUE }>'
'EnumTypeExtensionDefinition' | '<extend enum E { VALUE }>'
'EnumValueDefinition' | 'enum E {<VALUE>}'
'InputObjectTypeDefinition' | '<input I { f: Int }>'
'InputObjectTypeExtensionDefinition' | '<extend input I { f: Int }>'
'InputValueDefinition' | 'input I {<f: Int>}'
'DirectiveDefinition' | '<directive @d on FIELD>'
'DirectiveExtensionDefinition' | '<extend directive @d @other>'
'DirectiveLocation' | 'directive @d on<FIELD>'
}

private static Node parseNode(String source, String nodeType, boolean captureIgnoredChars) {
def options = ParserOptions.newParserOptions().captureIgnoredChars(captureIgnoredChars).build()
def environment = ParserEnvironment.newParserEnvironment().document(source).parserOptions(options).build()
return findNode(Parser.parse(environment), nodeType)
}

private static Node findNode(Node node, String nodeType) {
if (node.class.simpleName == nodeType) {
return node
}
return node.children.findResult { findNode(it, nodeType) }
}
}
224 changes: 224 additions & 0 deletions src/test/groovy/graphql/parser/LineTerminatorTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package graphql.parser

import graphql.TestUtil
import graphql.language.Field
import graphql.language.OperationDefinition
import graphql.language.SourceLocation
import graphql.language.StringValue
import spock.lang.Specification

class LineTerminatorTest extends Specification {

def "AST locations and comments support #label"() {
given:
def query = ['{', ' first # comment 😀', ' second', '}'].join(lineTerminator)

when:
def document = new Parser().parseDocument(new StringReader(query))
def operation = document.getDefinitionsOfType(OperationDefinition)[0]
def fields = operation.selectionSet.getSelectionsOfType(Field)

then:
fields[0].sourceLocation == new SourceLocation(2, 3)
fields[1].sourceLocation == new SourceLocation(3, 3)
fields[1].comments*.content == [' comment 😀']

where:
label | lineTerminator
'LF' | '\n'
'CR' | '\r'
'CRLF' | '\r\n'
}

def "syntax errors after #label retain code point columns"() {
when:
Parser.parse('{ f(arg: "😀")' + lineTerminator + ' f(arg: "😀") ) }')

then:
def error = thrown(InvalidSyntaxException)
error.location == new SourceLocation(2, 15)

where:
label | lineTerminator
'LF' | '\n'
'CR' | '\r'
'CRLF' | '\r\n'
}

def "lexer errors after #label have the right location"() {
when:
Parser.parse('{ first' + lineTerminator + ' ? }')

then:
def error = thrown(InvalidSyntaxException)
error.location == new SourceLocation(2, 3)

where:
label | lineTerminator
'LF' | '\n'
'CR' | '\r'
'CRLF' | '\r\n'
}

def "EOF after a comment and #label starts a new line"() {
when:
Parser.parse('{ first # comment' + lineTerminator)

then:
def error = thrown(InvalidSyntaxException)
error.location == new SourceLocation(2, 1)

where:
label | lineTerminator
'LF' | '\n'
'CR' | '\r'
'CRLF' | '\r\n'
}

def "block string locations support #label"() {
when:
Parser.parse('{ f(arg: """first' + lineTerminator + 'second""")' + lineTerminator + ' ) }')

then:
def error = thrown(InvalidSyntaxException)
error.location == new SourceLocation(3, 3)

where:
label | lineTerminator
'LF' | '\n'
'CR' | '\r'
'CRLF' | '\r\n'
}

def "block string values normalize #label before removing indentation"() {
given:
def input = ['"""', ' first', ' second', '', ' third', '"""'].join(lineTerminator)

when:
def value = Parser.parseValue(input) as StringValue

then:
value.value == 'first\n second\n\nthird'

where:
label | lineTerminator
'LF' | '\n'
'CR' | '\r'
'CRLF' | '\r\n'
}

def "captured ignored characters preserve #label"() {
given:
def options = ParserOptions.newParserOptions().captureIgnoredChars(true).build()
def environment = ParserEnvironment.newParserEnvironment()
.document('{' + lineTerminator + ' first }')
.parserOptions(options)
.build()

when:
def document = Parser.parse(environment)
def field = document.getDefinitionsOfType(OperationDefinition)[0].selectionSet.getSelectionsOfType(Field)[0]

then:
field.sourceLocation == new SourceLocation(2, 3)
field.ignoredChars.left*.value.join() == lineTerminator + ' '

where:
label | lineTerminator
'LF' | '\n'
'CR' | '\r'
'CRLF' | '\r\n'
}

def "multi source error locations support #label"() {
given:
def reader = MultiSourceReader.newMultiSourceReader()
.string('{ first }' + lineTerminator, 'first')
.string('{' + lineTerminator + ' )' + lineTerminator + '}', 'second')
.build()

when:
new Parser().parseDocument(reader)

then:
def error = thrown(InvalidSyntaxException)
error.location == new SourceLocation(2, 3, 'second')

where:
label | lineTerminator
'LF' | '\n'
'CR' | '\r'
'CRLF' | '\r\n'
}

def "mixed and consecutive line terminators count CRLF once"() {
when:
Parser.parse('# comment\r\r\n\n\r{ ) }')

then:
def error = thrown(InvalidSyntaxException)
error.location == new SourceLocation(5, 3)
}

def "CRLF split across reader buffers counts once"() {
when:
Parser.parse('#' + ('a' * 4094) + '\r\n{ ) }')

then:
def error = thrown(InvalidSyntaxException)
error.location == new SourceLocation(2, 3)
}

def "validation errors use CR line boundaries"() {
given:
def graphQL = TestUtil.graphQL('type Query { first: String }').build()

when:
def result = graphQL.execute('{ first\r unknown }')

then:
result.errors.size() == 1
result.errors[0].locations == [new SourceLocation(2, 3)]
}

def "Unicode separator #codePoint is not ignored between tokens"() {
when:
Parser.parse('{ first' + separator + ' second }')

then:
def error = thrown(InvalidSyntaxException)
error.location == new SourceLocation(1, 8)

where:
codePoint | separator
'U+2028' | '\u2028'
'U+2029' | '\u2029'
}

def "Unicode separator #codePoint remains valid in strings and comments"() {
given:
def text = 'before' + separator + 'after'

expect:
(Parser.parseValue('"' + text + '"') as StringValue).value == text
(Parser.parseValue('"""' + text + '"""') as StringValue).value == text

when:
def document = Parser.parse('#' + text + '\n{ first }')
def operation = document.getDefinitionsOfType(OperationDefinition)[0]

then:
operation.sourceLocation == new SourceLocation(2, 1)
operation.comments*.content == [text]

where:
codePoint | separator
'U+2028' | '\u2028'
'U+2029' | '\u2029'
}

def "escaped line terminators in ordinary strings retain their value"() {
expect:
(Parser.parseValue('"\\r\\n"') as StringValue).value == '\r\n'
}
}
Loading
Loading