Skip to content

Parser: Add 'skipKeyword' function #1545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,22 +488,19 @@ function parseFragment(
): FragmentSpreadNode | InlineFragmentNode {
const start = lexer.token;
expect(lexer, TokenKind.SPREAD);
if (peek(lexer, TokenKind.NAME) && lexer.token.value !== 'on') {

const hasTypeCondition = skipKeyword(lexer, 'on');
if (!hasTypeCondition && peek(lexer, TokenKind.NAME)) {
return {
kind: Kind.FRAGMENT_SPREAD,
name: parseFragmentName(lexer),
directives: parseDirectives(lexer, false),
loc: loc(lexer, start),
};
}
let typeCondition;
if (lexer.token.value === 'on') {
lexer.advance();
typeCondition = parseNamedType(lexer);
}
return {
kind: Kind.INLINE_FRAGMENT,
typeCondition,
typeCondition: hasTypeCondition ? parseNamedType(lexer) : undefined,
directives: parseDirectives(lexer, false),
selectionSet: parseSelectionSet(lexer),
loc: loc(lexer, start),
Expand Down Expand Up @@ -912,8 +909,7 @@ function parseObjectTypeDefinition(lexer: Lexer<*>): ObjectTypeDefinitionNode {
*/
function parseImplementsInterfaces(lexer: Lexer<*>): Array<NamedTypeNode> {
const types = [];
if (lexer.token.value === 'implements') {
lexer.advance();
if (skipKeyword(lexer, 'implements')) {
// Optional leading ampersand
skip(lexer, TokenKind.AMP);
do {
Expand Down Expand Up @@ -1487,11 +1483,11 @@ function peek(lexer: Lexer<*>, kind: TokenKindEnum): boolean {
* the lexer. Otherwise, do not change the parser state and return false.
*/
function skip(lexer: Lexer<*>, kind: TokenKindEnum): boolean {
const match = lexer.token.kind === kind;
if (match) {
if (lexer.token.kind === kind) {
lexer.advance();
return true;
}
return match;
return false;
}

/**
Expand All @@ -1512,21 +1508,31 @@ function expect(lexer: Lexer<*>, kind: TokenKindEnum): Token {
}

/**
* If the next token is a keyword with the given value, return that token after
* advancing the lexer. Otherwise, do not change the parser state and return
* false.
* If the next token is a keyword with the given value, return true after advancing
* the lexer. Otherwise, do not change the parser state and return false.
*/
function expectKeyword(lexer: Lexer<*>, value: string): Token {
function skipKeyword(lexer: Lexer<*>, value: string): boolean {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we already have skip/expect pair with matching functionality I think we need to have skipKeyword/expectKeyword.

const token = lexer.token;
if (token.kind === TokenKind.NAME && token.value === value) {
lexer.advance();
return token;
return true;
}
return false;
}

/**
* If the next token is a keyword with the given value, return that token after
* advancing the lexer. Otherwise, do not change the parser state and throw
* an error.
*/
function expectKeyword(lexer: Lexer<*>, value: string): void {
if (!skipKeyword(lexer, value)) {
throw syntaxError(
lexer.source,
lexer.token.start,
`Expected "${value}", found ${getTokenDesc(lexer.token)}`,
);
}
throw syntaxError(
lexer.source,
token.start,
`Expected "${value}", found ${getTokenDesc(token)}`,
);
}

/**
Expand Down