Add nextName instead of using reserved names

This commit is contained in:
Tobe O 2019-03-31 16:07:25 -04:00
parent 2bedc2bb00
commit 915ac255af
4 changed files with 39 additions and 23 deletions

View file

@ -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.

View file

@ -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<Pattern, TokenType> _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,

View file

@ -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)) {

View file

@ -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
}