@@ -111,10 +111,17 @@ export interface ParseOptions {
111111 * ```
112112 */
113113 allowLegacyFragmentVariables ?: boolean ;
114+ }
114115
116+ /**
117+ * @internal
118+ */
119+ export interface ParseOptionsInternal extends ParseOptions {
115120 /**
116121 * You may override the Lexer class used to lex the source; this is used by
117122 * schema coordinates to introduce a lexer with a restricted syntax.
123+ *
124+ * Cannot be set if `maxTokens` is set.
118125 */
119126 lexer ?: LexerInterface | undefined ;
120127}
@@ -204,10 +211,15 @@ export function parseType(
204211 */
205212export function parseSchemaCoordinate (
206213 source : string | Source ,
214+ options ?: ParseOptions | undefined ,
207215) : SchemaCoordinateNode {
208216 const sourceObj = isSource ( source ) ? source : new Source ( source ) ;
209- const lexer = new SchemaCoordinateLexer ( sourceObj ) ;
210- const parser = new Parser ( source , { lexer } ) ;
217+ const lexer = new SchemaCoordinateLexer ( sourceObj , options ) ;
218+ const parser = new Parser ( source , {
219+ ...options ,
220+ maxTokens : undefined , // Handled by SchemaCoordinateLexer
221+ lexer,
222+ } ) ;
211223 parser . expectToken ( TokenKind . SOF ) ;
212224 const coordinate = parser . parseSchemaCoordinate ( ) ;
213225 parser . expectToken ( TokenKind . EOF ) ;
@@ -226,26 +238,30 @@ export function parseSchemaCoordinate(
226238 * @internal
227239 */
228240export class Parser {
229- protected _options : Omit < ParseOptions , 'lexer' > ;
241+ protected _options : ParseOptions ;
230242 protected _lexer : LexerInterface ;
231- protected _tokenCounter : number ;
232243
233- constructor ( source : string | Source , options : ParseOptions = { } ) {
244+ constructor ( source : string | Source , options : ParseOptionsInternal = { } ) {
234245 const { lexer, ..._options } = options ;
235246
236247 if ( lexer ) {
248+ if ( options . maxTokens != null ) {
249+ throw new Error (
250+ 'Setting maxTokens has no effect when a custom lexer is passed' ,
251+ ) ;
252+ }
237253 this . _lexer = lexer ;
238254 } else {
239255 const sourceObj = isSource ( source ) ? source : new Source ( source ) ;
240- this . _lexer = new Lexer ( sourceObj ) ;
256+ const { maxTokens } = options ;
257+ this . _lexer = new Lexer ( sourceObj , { maxTokens } ) ;
241258 }
242259
243260 this . _options = _options ;
244- this . _tokenCounter = 0 ;
245261 }
246262
247263 get tokenCount ( ) : number {
248- return this . _tokenCounter ;
264+ return this . _lexer . tokenCount ;
249265 }
250266
251267 /**
@@ -1690,19 +1706,7 @@ export class Parser {
16901706 }
16911707
16921708 advanceLexer ( ) : void {
1693- const { maxTokens } = this . _options ;
1694- const token = this . _lexer . advance ( ) ;
1695-
1696- if ( token . kind !== TokenKind . EOF ) {
1697- ++ this . _tokenCounter ;
1698- if ( maxTokens !== undefined && this . _tokenCounter > maxTokens ) {
1699- throw syntaxError (
1700- this . _lexer . source ,
1701- token . start ,
1702- `Document contains more that ${ maxTokens } tokens. Parsing aborted.` ,
1703- ) ;
1704- }
1705- }
1709+ this . _lexer . advance ( ) ;
17061710 }
17071711}
17081712
0 commit comments