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 # 1.1.2
* Parse the `subscription` keyword. * Parse the `subscription` keyword.

View file

@ -6,7 +6,7 @@ import 'token_type.dart';
final RegExp _comment = new RegExp(r'#[^\n]*'); final RegExp _comment = new RegExp(r'#[^\n]*');
final RegExp _whitespace = new RegExp('[ \t\n\r]+'); 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 _number = new RegExp(r'-?[0-9]+(\.[0-9]+)?(E|e(\+|-)?[0-9]+)?');
final RegExp _string = new RegExp( final RegExp _string = new RegExp(
r'"((\\(["\\/bfnrt]|(u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])))|([^"\\]))*"'); 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.RBRACKET,
'(': TokenType.LPAREN, '(': TokenType.LPAREN,
')': TokenType.RPAREN, ')': TokenType.RPAREN,
'fragment': TokenType.FRAGMENT, // 'fragment': TokenType.FRAGMENT,
'mutation': TokenType.MUTATION, // 'mutation': TokenType.MUTATION,
'subscription': TokenType.SUBSCRIPTION, // 'subscription': TokenType.SUBSCRIPTION,
'on': TokenType.ON, // 'on': TokenType.ON,
'query': TokenType.QUERY, // 'query': TokenType.QUERY,
'null': TokenType.NULL, // 'null': TokenType.NULL,
_boolean: TokenType.BOOLEAN, // _boolean: TokenType.BOOLEAN,
_number: TokenType.NUMBER, _number: TokenType.NUMBER,
_string: TokenType.STRING, _string: TokenType.STRING,
_blockString: TokenType.BLOCK_STRING, _blockString: TokenType.BLOCK_STRING,

View file

@ -25,6 +25,15 @@ class Parser {
return false; 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() { Token peek() {
if (_index < tokens.length - 1) { if (_index < tokens.length - 1) {
return tokens[_index + 1]; return tokens[_index + 1];
@ -61,9 +70,9 @@ class Parser {
if (selectionSet != null) if (selectionSet != null)
return new OperationDefinitionContext(null, null, null, selectionSet); return new OperationDefinitionContext(null, null, null, selectionSet);
else { else {
if (next(TokenType.MUTATION) || if (nextName('mutation') ||
next(TokenType.QUERY) || nextName('query') ||
next(TokenType.SUBSCRIPTION)) { nextName('subscription')) {
var TYPE = current; var TYPE = current;
Token NAME = next(TokenType.NAME) ? current : null; Token NAME = next(TokenType.NAME) ? current : null;
var variables = parseVariableDefinitions(); var variables = parseVariableDefinitions();
@ -85,11 +94,11 @@ class Parser {
} }
FragmentDefinitionContext parseFragmentDefinition() { FragmentDefinitionContext parseFragmentDefinition() {
if (next(TokenType.FRAGMENT)) { if (nextName('fragment')) {
var FRAGMENT = current; var FRAGMENT = current;
if (next(TokenType.NAME)) { if (next(TokenType.NAME)) {
var NAME = current; var NAME = current;
if (next(TokenType.ON)) { if (nextName('on')) {
var ON = current; var ON = current;
var typeCondition = parseTypeCondition(); var typeCondition = parseTypeCondition();
if (typeCondition != null) { if (typeCondition != null) {
@ -145,7 +154,7 @@ class Parser {
InlineFragmentContext parseInlineFragment() { InlineFragmentContext parseInlineFragment() {
if (next(TokenType.ELLIPSIS)) { if (next(TokenType.ELLIPSIS)) {
var ELLIPSIS = current; var ELLIPSIS = current;
if (next(TokenType.ON)) { if (nextName('on')) {
var ON = current; var ON = current;
var typeCondition = parseTypeCondition(); var typeCondition = parseTypeCondition();
if (typeCondition != null) { if (typeCondition != null) {
@ -508,13 +517,15 @@ class Parser {
next(TokenType.NUMBER) ? new NumberValueContext(current) : null; next(TokenType.NUMBER) ? new NumberValueContext(current) : null;
BooleanValueContext parseBooleanValue() => BooleanValueContext parseBooleanValue() =>
next(TokenType.BOOLEAN) ? new BooleanValueContext(current) : null; (nextName('true') || nextName('false'))
? new BooleanValueContext(current)
: null;
EnumValueContext parseEnumValue() => EnumValueContext parseEnumValue() =>
next(TokenType.NAME) ? new EnumValueContext(current) : null; next(TokenType.NAME) ? new EnumValueContext(current) : null;
NullValueContext parseNullValue() => NullValueContext parseNullValue() =>
next(TokenType.NULL) ? new NullValueContext(current) : null; nextName('null') ? new NullValueContext(current) : null;
ListValueContext parseListValue() { ListValueContext parseListValue() {
if (next(TokenType.LBRACKET)) { if (next(TokenType.LBRACKET)) {

View file

@ -13,17 +13,18 @@ enum TokenType {
LPAREN, LPAREN,
RPAREN, RPAREN,
FRAGMENT, // Note: these are *not* reserved names.
MUTATION, // FRAGMENT,
SUBSCRIPTION, // MUTATION,
ON, // SUBSCRIPTION,
QUERY, // ON,
// QUERY,
// NULL
// BOOLEAN,
BOOLEAN,
NUMBER, NUMBER,
STRING, STRING,
BLOCK_STRING, BLOCK_STRING,
NAME, NAME,
NULL
} }