Package graphql.parser
Class SafeTokenSource
- java.lang.Object
-
- graphql.parser.SafeTokenSource
-
- All Implemented Interfaces:
org.antlr.v4.runtime.TokenSource
public class SafeTokenSource extends java.lang.Object implements org.antlr.v4.runtime.TokenSource
This token source can wrap a lexer and if it asks for more than a maximum number of tokens the user can take some action, typically throw an exception to stop lexing. It tracks the maximum number per token channel, so we have 3 at the moment, and they will all be tracked. This is used to protect us from evil input. The lexer will eagerly try to find all tokens at times and certain inputs (directives butted together for example) will cause the lexer to keep doing work even though before the tokens are presented back to the parser and hence before it has a chance to stop work once too much as been done.
-
-
Field Summary
Fields Modifier and Type Field Description private int[]
channelCounts
private org.antlr.v4.runtime.TokenSource
lexer
private int
maxTokens
private int
maxWhitespaceTokens
private java.util.function.BiConsumer<java.lang.Integer,org.antlr.v4.runtime.Token>
whenMaxTokensExceeded
-
Constructor Summary
Constructors Constructor Description SafeTokenSource(org.antlr.v4.runtime.TokenSource lexer, int maxTokens, int maxWhitespaceTokens, java.util.function.BiConsumer<java.lang.Integer,org.antlr.v4.runtime.Token> whenMaxTokensExceeded)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
callbackIfMaxExceeded(int maxCount, int currentCount, org.antlr.v4.runtime.Token token)
int
getCharPositionInLine()
org.antlr.v4.runtime.CharStream
getInputStream()
int
getLine()
java.lang.String
getSourceName()
org.antlr.v4.runtime.TokenFactory<?>
getTokenFactory()
org.antlr.v4.runtime.Token
nextToken()
void
setTokenFactory(org.antlr.v4.runtime.TokenFactory<?> factory)
-
-
-
Field Detail
-
lexer
private final org.antlr.v4.runtime.TokenSource lexer
-
maxTokens
private final int maxTokens
-
maxWhitespaceTokens
private final int maxWhitespaceTokens
-
whenMaxTokensExceeded
private final java.util.function.BiConsumer<java.lang.Integer,org.antlr.v4.runtime.Token> whenMaxTokensExceeded
-
channelCounts
private final int[] channelCounts
-
-
Method Detail
-
nextToken
public org.antlr.v4.runtime.Token nextToken()
- Specified by:
nextToken
in interfaceorg.antlr.v4.runtime.TokenSource
-
callbackIfMaxExceeded
private void callbackIfMaxExceeded(int maxCount, int currentCount, org.antlr.v4.runtime.Token token)
-
getLine
public int getLine()
- Specified by:
getLine
in interfaceorg.antlr.v4.runtime.TokenSource
-
getCharPositionInLine
public int getCharPositionInLine()
- Specified by:
getCharPositionInLine
in interfaceorg.antlr.v4.runtime.TokenSource
-
getInputStream
public org.antlr.v4.runtime.CharStream getInputStream()
- Specified by:
getInputStream
in interfaceorg.antlr.v4.runtime.TokenSource
-
getSourceName
public java.lang.String getSourceName()
- Specified by:
getSourceName
in interfaceorg.antlr.v4.runtime.TokenSource
-
setTokenFactory
public void setTokenFactory(org.antlr.v4.runtime.TokenFactory<?> factory)
- Specified by:
setTokenFactory
in interfaceorg.antlr.v4.runtime.TokenSource
-
getTokenFactory
public org.antlr.v4.runtime.TokenFactory<?> getTokenFactory()
- Specified by:
getTokenFactory
in interfaceorg.antlr.v4.runtime.TokenSource
-
-