11package graphql .parser ;
22
3+ import graphql .Internal ;
34import graphql .language .SourceLocation ;
45import org .antlr .v4 .runtime .BailErrorStrategy ;
56import org .antlr .v4 .runtime .Parser ;
67import org .antlr .v4 .runtime .RecognitionException ;
78import org .antlr .v4 .runtime .Token ;
89import org .antlr .v4 .runtime .misc .ParseCancellationException ;
910
10- import java .util .List ;
11-
12- import static graphql .parser .SourceLocationHelper .mkSourceLocation ;
13-
11+ @ Internal
1412public class ExtendedBailStrategy extends BailErrorStrategy {
1513 private final MultiSourceReader multiSourceReader ;
1614
@@ -37,8 +35,8 @@ public Token recoverInline(Parser recognizer) throws RecognitionException {
3735 }
3836
3937 InvalidSyntaxException mkMoreTokensException (Token token ) {
40- SourceLocation sourceLocation = mkSourceLocation (multiSourceReader , token );
41- String sourcePreview = mkPreview ( token );
38+ SourceLocation sourceLocation = AntlrHelper . createSourceLocation (multiSourceReader , token );
39+ String sourcePreview = AntlrHelper . createPreview ( multiSourceReader , token . getLine () );
4240 return new InvalidSyntaxException (sourceLocation ,
4341 "There are more tokens in the query that have not been consumed" ,
4442 sourcePreview , token .getText (), null );
@@ -51,26 +49,11 @@ private InvalidSyntaxException mkException(Parser recognizer, RecognitionExcepti
5149 SourceLocation sourceLocation = null ;
5250 Token currentToken = recognizer .getCurrentToken ();
5351 if (currentToken != null ) {
54- sourceLocation = mkSourceLocation (multiSourceReader , currentToken );
52+ sourceLocation = AntlrHelper . createSourceLocation (multiSourceReader , currentToken );
5553 offendingToken = currentToken .getText ();
56- sourcePreview = mkPreview ( currentToken );
54+ sourcePreview = AntlrHelper . createPreview ( multiSourceReader , currentToken . getLine () );
5755 }
5856 return new InvalidSyntaxException (sourceLocation , null , sourcePreview , offendingToken , cause );
5957 }
6058
61- /* grabs 3 lines before and after the syntax error */
62- private String mkPreview (Token token ) {
63- int line = token .getLine () - 1 ;
64- StringBuilder sb = new StringBuilder ();
65- int startLine = line - 3 ;
66- int endLine = line + 3 ;
67- List <String > lines = multiSourceReader .getData ();
68- for (int i = 0 ; i < lines .size (); i ++) {
69- if (i >= startLine && i <= endLine ) {
70- sb .append (lines .get (i )).append ('\n' );
71- }
72- }
73- return sb .toString ();
74- }
75-
7659}
0 commit comments