From 915ac255aff5e4c11ea75b5fb4b4939359cdff8b Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 31 Mar 2019 16:07:25 -0400 Subject: [PATCH] Add nextName instead of using reserved names --- graphql_parser/CHANGELOG.md | 4 +++ graphql_parser/lib/src/language/lexer.dart | 16 +++++------ graphql_parser/lib/src/language/parser.dart | 27 +++++++++++++------ .../lib/src/language/token_type.dart | 15 ++++++----- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/graphql_parser/CHANGELOG.md b/graphql_parser/CHANGELOG.md index 69d7a621..1d4243fa 100644 --- a/graphql_parser/CHANGELOG.md +++ b/graphql_parser/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.1.3 +* Add `Parser.nextName`, and remove all formerly-reserved words from the lexer. +Resolves [#19](https://github.com/angel-dart/graphql/issues/19). + # 1.1.2 * Parse the `subscription` keyword. diff --git a/graphql_parser/lib/src/language/lexer.dart b/graphql_parser/lib/src/language/lexer.dart index 1859fb8d..85ff83c0 100644 --- a/graphql_parser/lib/src/language/lexer.dart +++ b/graphql_parser/lib/src/language/lexer.dart @@ -6,7 +6,7 @@ import 'token_type.dart'; final RegExp _comment = new RegExp(r'#[^\n]*'); final RegExp _whitespace = new RegExp('[ \t\n\r]+'); -final RegExp _boolean = new RegExp(r'true|false'); +// final RegExp _boolean = new RegExp(r'true|false'); final RegExp _number = new RegExp(r'-?[0-9]+(\.[0-9]+)?(E|e(\+|-)?[0-9]+)?'); final RegExp _string = new RegExp( r'"((\\(["\\/bfnrt]|(u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])))|([^"\\]))*"'); @@ -27,13 +27,13 @@ final Map _patterns = { ']': TokenType.RBRACKET, '(': TokenType.LPAREN, ')': TokenType.RPAREN, - 'fragment': TokenType.FRAGMENT, - 'mutation': TokenType.MUTATION, - 'subscription': TokenType.SUBSCRIPTION, - 'on': TokenType.ON, - 'query': TokenType.QUERY, - 'null': TokenType.NULL, - _boolean: TokenType.BOOLEAN, + // 'fragment': TokenType.FRAGMENT, + // 'mutation': TokenType.MUTATION, + // 'subscription': TokenType.SUBSCRIPTION, + // 'on': TokenType.ON, + // 'query': TokenType.QUERY, + // 'null': TokenType.NULL, + // _boolean: TokenType.BOOLEAN, _number: TokenType.NUMBER, _string: TokenType.STRING, _blockString: TokenType.BLOCK_STRING, diff --git a/graphql_parser/lib/src/language/parser.dart b/graphql_parser/lib/src/language/parser.dart index db1ef323..517904af 100644 --- a/graphql_parser/lib/src/language/parser.dart +++ b/graphql_parser/lib/src/language/parser.dart @@ -25,6 +25,15 @@ class Parser { return false; } + bool nextName(String name) { + var tok = peek(); + if (tok?.type == TokenType.NAME && tok.span.text == name) { + return next(TokenType.NAME); + } + + return false; + } + Token peek() { if (_index < tokens.length - 1) { return tokens[_index + 1]; @@ -61,9 +70,9 @@ class Parser { if (selectionSet != null) return new OperationDefinitionContext(null, null, null, selectionSet); else { - if (next(TokenType.MUTATION) || - next(TokenType.QUERY) || - next(TokenType.SUBSCRIPTION)) { + if (nextName('mutation') || + nextName('query') || + nextName('subscription')) { var TYPE = current; Token NAME = next(TokenType.NAME) ? current : null; var variables = parseVariableDefinitions(); @@ -85,11 +94,11 @@ class Parser { } FragmentDefinitionContext parseFragmentDefinition() { - if (next(TokenType.FRAGMENT)) { + if (nextName('fragment')) { var FRAGMENT = current; if (next(TokenType.NAME)) { var NAME = current; - if (next(TokenType.ON)) { + if (nextName('on')) { var ON = current; var typeCondition = parseTypeCondition(); if (typeCondition != null) { @@ -145,7 +154,7 @@ class Parser { InlineFragmentContext parseInlineFragment() { if (next(TokenType.ELLIPSIS)) { var ELLIPSIS = current; - if (next(TokenType.ON)) { + if (nextName('on')) { var ON = current; var typeCondition = parseTypeCondition(); if (typeCondition != null) { @@ -508,13 +517,15 @@ class Parser { next(TokenType.NUMBER) ? new NumberValueContext(current) : null; BooleanValueContext parseBooleanValue() => - next(TokenType.BOOLEAN) ? new BooleanValueContext(current) : null; + (nextName('true') || nextName('false')) + ? new BooleanValueContext(current) + : null; EnumValueContext parseEnumValue() => next(TokenType.NAME) ? new EnumValueContext(current) : null; NullValueContext parseNullValue() => - next(TokenType.NULL) ? new NullValueContext(current) : null; + nextName('null') ? new NullValueContext(current) : null; ListValueContext parseListValue() { if (next(TokenType.LBRACKET)) { diff --git a/graphql_parser/lib/src/language/token_type.dart b/graphql_parser/lib/src/language/token_type.dart index 4f42d33a..8eb50aa2 100644 --- a/graphql_parser/lib/src/language/token_type.dart +++ b/graphql_parser/lib/src/language/token_type.dart @@ -13,17 +13,18 @@ enum TokenType { LPAREN, RPAREN, - FRAGMENT, - MUTATION, - SUBSCRIPTION, - ON, - QUERY, + // Note: these are *not* reserved names. + // FRAGMENT, + // MUTATION, + // SUBSCRIPTION, + // ON, + // QUERY, + // NULL + // BOOLEAN, - BOOLEAN, NUMBER, STRING, BLOCK_STRING, NAME, - NULL }