Skip to content
Closed
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
10 changes: 8 additions & 2 deletions src/main/java/graphql/parser/GraphqlAntlrToLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class GraphqlAntlrToLanguage extends GraphqlBaseVisitor<Void> {

Document result;
private Document result;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you revert it? This change is unrelated to the parsing.


private enum ContextProperty {
OperationDefinition,
Expand Down Expand Up @@ -109,6 +109,9 @@ private void newField(Field field) {
((SelectionSet) getFromContextStack(ContextProperty.SelectionSet)).getSelections().add(field);
}

Document getResult() {
return result;
}

@Override
public Void visitDocument(GraphqlParser.DocumentContext ctx) {
Expand Down Expand Up @@ -688,9 +691,12 @@ private Value getValue(GraphqlParser.ValueContext ctx) {
throw new ShouldNotHappenException();
}

private String parseString(String string) {
static String parseString(String string) {
assert string.length() >= 2;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use assert statements in prod code: they are turned off by default and don't have any effect.

Also it is not really needed to ensure the length or that the string starts wit ", this is already ensured by the parser/lexer.

StringWriter writer = new StringWriter(string.length() - 2);
int end = string.length() - 1;
assert string.charAt(0) == '"' : "string did not start with '\"'";
assert string.charAt(end) == '"' : "string did not end with '\"'";
for (int i = 1; i < end; i++) {
char c = string.charAt(i);
if (c != '\\') {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/graphql/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ public Document parseDocument(String input) {
parser.setErrorHandler(new BailErrorStrategy());
GraphqlParser.DocumentContext document = parser.document();


GraphqlAntlrToLanguage antlrToLanguage = new GraphqlAntlrToLanguage();
antlrToLanguage.visitDocument(document);
return antlrToLanguage.result;
return antlrToLanguage.getResult();
}
}
66 changes: 66 additions & 0 deletions src/test/groovy/graphql/parser/GraphqlAntlrToLanguageTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package graphql.parser

import spock.lang.Ignore
import spock.lang.Specification
/**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this comment? We don't maintain author comments.

* Created by jiayu on 9/3/16.
*/
class GraphqlAntlrToLanguageTest extends Specification {

def "parsing quoted string should work"() {
given:
def input = '''"simple quoted"'''

when:
String parsed = GraphqlAntlrToLanguage.parseString(input)

then:
parsed == "simple quoted"
}

def "parsing escaped json should work"() {
given:
def input = '''"{\"name\": \"graphql\", \"year\": 2015}"'''

when:
String parsed = GraphqlAntlrToLanguage.parseString(input)

then:
parsed == '''{\"name\": \"graphql\", \"year\": 2015}'''
}

def "parsing quoted quote should work"() {
given:
def input = '''"""'''

when:
String parsed = GraphqlAntlrToLanguage.parseString(input)

then:
parsed == '''"'''
}

@Ignore("sadly this does not work yet")
def "parsing emoji should work"() {
given:
def input = '''"\\u1f37a"'''

when:
String parsed = GraphqlAntlrToLanguage.parseString(input)

then:
parsed == '''🍺'''
}

def "parsing simple unicode should work"() {
given:
def input = '''"\\u56fe"'''

when:
String parsed = GraphqlAntlrToLanguage.parseString(input)

then:
parsed == '''图'''
}

}