Skip to content

Commit b13f283

Browse files
parser: Extract 'optionalMany' utility function (#2068)
1 parent a5bbe71 commit b13f283

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

src/language/parser.js

+49-38
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,11 @@ class Parser {
327327
* VariableDefinitions : ( VariableDefinition+ )
328328
*/
329329
parseVariableDefinitions(): Array<VariableDefinitionNode> {
330-
return this.peek(TokenKind.PAREN_L)
331-
? this.many(
332-
TokenKind.PAREN_L,
333-
this.parseVariableDefinition,
334-
TokenKind.PAREN_R,
335-
)
336-
: [];
330+
return this.optionalMany(
331+
TokenKind.PAREN_L,
332+
this.parseVariableDefinition,
333+
TokenKind.PAREN_R,
334+
);
337335
}
338336

339337
/**
@@ -430,9 +428,7 @@ class Parser {
430428
*/
431429
parseArguments(isConst: boolean): Array<ArgumentNode> {
432430
const item = isConst ? this.parseConstArgument : this.parseArgument;
433-
return this.peek(TokenKind.PAREN_L)
434-
? this.many(TokenKind.PAREN_L, item, TokenKind.PAREN_R)
435-
: [];
431+
return this.optionalMany(TokenKind.PAREN_L, item, TokenKind.PAREN_R);
436432
}
437433

438434
/**
@@ -919,13 +915,11 @@ class Parser {
919915
this._lexer.advance();
920916
return [];
921917
}
922-
return this.peek(TokenKind.BRACE_L)
923-
? this.many(
924-
TokenKind.BRACE_L,
925-
this.parseFieldDefinition,
926-
TokenKind.BRACE_R,
927-
)
928-
: [];
918+
return this.optionalMany(
919+
TokenKind.BRACE_L,
920+
this.parseFieldDefinition,
921+
TokenKind.BRACE_R,
922+
);
929923
}
930924

931925
/**
@@ -955,10 +949,7 @@ class Parser {
955949
* ArgumentsDefinition : ( InputValueDefinition+ )
956950
*/
957951
parseArgumentDefs(): Array<InputValueDefinitionNode> {
958-
if (!this.peek(TokenKind.PAREN_L)) {
959-
return [];
960-
}
961-
return this.many(
952+
return this.optionalMany(
962953
TokenKind.PAREN_L,
963954
this.parseInputValueDef,
964955
TokenKind.PAREN_R,
@@ -1075,13 +1066,11 @@ class Parser {
10751066
* EnumValuesDefinition : { EnumValueDefinition+ }
10761067
*/
10771068
parseEnumValuesDefinition(): Array<EnumValueDefinitionNode> {
1078-
return this.peek(TokenKind.BRACE_L)
1079-
? this.many(
1080-
TokenKind.BRACE_L,
1081-
this.parseEnumValueDefinition,
1082-
TokenKind.BRACE_R,
1083-
)
1084-
: [];
1069+
return this.optionalMany(
1070+
TokenKind.BRACE_L,
1071+
this.parseEnumValueDefinition,
1072+
TokenKind.BRACE_R,
1073+
);
10851074
}
10861075

10871076
/**
@@ -1128,9 +1117,11 @@ class Parser {
11281117
* InputFieldsDefinition : { InputValueDefinition+ }
11291118
*/
11301119
parseInputFieldsDefinition(): Array<InputValueDefinitionNode> {
1131-
return this.peek(TokenKind.BRACE_L)
1132-
? this.many(TokenKind.BRACE_L, this.parseInputValueDef, TokenKind.BRACE_R)
1133-
: [];
1120+
return this.optionalMany(
1121+
TokenKind.BRACE_L,
1122+
this.parseInputValueDef,
1123+
TokenKind.BRACE_R,
1124+
);
11341125
}
11351126

11361127
/**
@@ -1181,13 +1172,11 @@ class Parser {
11811172
this.expectKeyword('extend');
11821173
this.expectKeyword('schema');
11831174
const directives = this.parseDirectives(true);
1184-
const operationTypes = this.peek(TokenKind.BRACE_L)
1185-
? this.many(
1186-
TokenKind.BRACE_L,
1187-
this.parseOperationTypeDefinition,
1188-
TokenKind.BRACE_R,
1189-
)
1190-
: [];
1175+
const operationTypes = this.optionalMany(
1176+
TokenKind.BRACE_L,
1177+
this.parseOperationTypeDefinition,
1178+
TokenKind.BRACE_R,
1179+
);
11911180
if (directives.length === 0 && operationTypes.length === 0) {
11921181
throw this.unexpected();
11931182
}
@@ -1535,6 +1524,28 @@ class Parser {
15351524
return nodes;
15361525
}
15371526

1527+
/**
1528+
* Returns a list of parse nodes, determined by the parseFn.
1529+
* It can be empty only if open token is missing otherwise it will always
1530+
* return non-empty list that begins with a lex token of openKind and ends
1531+
* with a lex token of closeKind. Advances the parser to the next lex token
1532+
* after the closing token.
1533+
*/
1534+
optionalMany<T>(
1535+
openKind: TokenKindEnum,
1536+
parseFn: () => T,
1537+
closeKind: TokenKindEnum,
1538+
): Array<T> {
1539+
if (this.expectOptionalToken(openKind)) {
1540+
const nodes = [];
1541+
do {
1542+
nodes.push(parseFn.call(this));
1543+
} while (!this.expectOptionalToken(closeKind));
1544+
return nodes;
1545+
}
1546+
return [];
1547+
}
1548+
15381549
/**
15391550
* Returns a non-empty list of parse nodes, determined by
15401551
* the parseFn. This list begins with a lex token of openKind

0 commit comments

Comments
 (0)