Skip to content

Commit

Permalink
parser: Extract 'optionalMany' utility function (#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Jul 31, 2019
1 parent a5bbe71 commit b13f283
Showing 1 changed file with 49 additions and 38 deletions.
87 changes: 49 additions & 38 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,11 @@ class Parser {
* VariableDefinitions : ( VariableDefinition+ )
*/
parseVariableDefinitions(): Array<VariableDefinitionNode> {
return this.peek(TokenKind.PAREN_L)
? this.many(
TokenKind.PAREN_L,
this.parseVariableDefinition,
TokenKind.PAREN_R,
)
: [];
return this.optionalMany(
TokenKind.PAREN_L,
this.parseVariableDefinition,
TokenKind.PAREN_R,
);
}

/**
Expand Down Expand Up @@ -430,9 +428,7 @@ class Parser {
*/
parseArguments(isConst: boolean): Array<ArgumentNode> {
const item = isConst ? this.parseConstArgument : this.parseArgument;
return this.peek(TokenKind.PAREN_L)
? this.many(TokenKind.PAREN_L, item, TokenKind.PAREN_R)
: [];
return this.optionalMany(TokenKind.PAREN_L, item, TokenKind.PAREN_R);
}

/**
Expand Down Expand Up @@ -919,13 +915,11 @@ class Parser {
this._lexer.advance();
return [];
}
return this.peek(TokenKind.BRACE_L)
? this.many(
TokenKind.BRACE_L,
this.parseFieldDefinition,
TokenKind.BRACE_R,
)
: [];
return this.optionalMany(
TokenKind.BRACE_L,
this.parseFieldDefinition,
TokenKind.BRACE_R,
);
}

/**
Expand Down Expand Up @@ -955,10 +949,7 @@ class Parser {
* ArgumentsDefinition : ( InputValueDefinition+ )
*/
parseArgumentDefs(): Array<InputValueDefinitionNode> {
if (!this.peek(TokenKind.PAREN_L)) {
return [];
}
return this.many(
return this.optionalMany(
TokenKind.PAREN_L,
this.parseInputValueDef,
TokenKind.PAREN_R,
Expand Down Expand Up @@ -1075,13 +1066,11 @@ class Parser {
* EnumValuesDefinition : { EnumValueDefinition+ }
*/
parseEnumValuesDefinition(): Array<EnumValueDefinitionNode> {
return this.peek(TokenKind.BRACE_L)
? this.many(
TokenKind.BRACE_L,
this.parseEnumValueDefinition,
TokenKind.BRACE_R,
)
: [];
return this.optionalMany(
TokenKind.BRACE_L,
this.parseEnumValueDefinition,
TokenKind.BRACE_R,
);
}

/**
Expand Down Expand Up @@ -1128,9 +1117,11 @@ class Parser {
* InputFieldsDefinition : { InputValueDefinition+ }
*/
parseInputFieldsDefinition(): Array<InputValueDefinitionNode> {
return this.peek(TokenKind.BRACE_L)
? this.many(TokenKind.BRACE_L, this.parseInputValueDef, TokenKind.BRACE_R)
: [];
return this.optionalMany(
TokenKind.BRACE_L,
this.parseInputValueDef,
TokenKind.BRACE_R,
);
}

/**
Expand Down Expand Up @@ -1181,13 +1172,11 @@ class Parser {
this.expectKeyword('extend');
this.expectKeyword('schema');
const directives = this.parseDirectives(true);
const operationTypes = this.peek(TokenKind.BRACE_L)
? this.many(
TokenKind.BRACE_L,
this.parseOperationTypeDefinition,
TokenKind.BRACE_R,
)
: [];
const operationTypes = this.optionalMany(
TokenKind.BRACE_L,
this.parseOperationTypeDefinition,
TokenKind.BRACE_R,
);
if (directives.length === 0 && operationTypes.length === 0) {
throw this.unexpected();
}
Expand Down Expand Up @@ -1535,6 +1524,28 @@ class Parser {
return nodes;
}

/**
* Returns a list of parse nodes, determined by the parseFn.
* It can be empty only if open token is missing otherwise it will always
* return non-empty list that begins with a lex token of openKind and ends
* with a lex token of closeKind. Advances the parser to the next lex token
* after the closing token.
*/
optionalMany<T>(
openKind: TokenKindEnum,
parseFn: () => T,
closeKind: TokenKindEnum,
): Array<T> {
if (this.expectOptionalToken(openKind)) {
const nodes = [];
do {
nodes.push(parseFn.call(this));
} while (!this.expectOptionalToken(closeKind));
return nodes;
}
return [];
}

/**
* Returns a non-empty list of parse nodes, determined by
* the parseFn. This list begins with a lex token of openKind
Expand Down

0 comments on commit b13f283

Please sign in to comment.