Variable also work

This commit is contained in:
thosakwe 2017-07-03 11:53:19 -04:00
parent 0f68a1c500
commit 5c2649d2d9
17 changed files with 130 additions and 31 deletions

18
.idea/graphql_parser.iml Normal file
View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/example/packages" />
<excludeFolder url="file://$MODULE_DIR$/packages" />
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Dart Packages" level="project" />
</component>
</module>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/graphql_parser.iml" filepath="$PROJECT_DIR$/.idea/graphql_parser.iml" />
</modules>
</component>
</project>

View file

@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All Tests" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true">
<option name="filePath" value="$PROJECT_DIR$/test/all.dart" />
<method />
</configuration>
</component>

View file

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="tests in graphql_parser" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true" nameIsGenerated="true">
<option name="filePath" value="$PROJECT_DIR$" />
<option name="scope" value="FOLDER" />
<option name="testRunnerOptions" value="-j 4" />
<method />
</configuration>
</component>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -7,7 +7,8 @@ final String INPUT = '''
tagline tagline
} }
} }
'''.trim(); '''
.trim();
main() { main() {
var tokens = scan(INPUT); var tokens = scan(INPUT);

View file

@ -1,2 +1,2 @@
export 'src/language/ast/ast.dart'; export 'src/language/ast/ast.dart';
export 'src/language/language.dart'; export 'src/language/language.dart';

View file

@ -1,5 +1,3 @@
import 'node.dart'; import 'node.dart';
abstract class DefinitionContext extends Node { abstract class DefinitionContext extends Node {}
}

View file

@ -6,10 +6,10 @@ class TypeConditionContext extends Node {
final TypeNameContext typeName; final TypeNameContext typeName;
TypeConditionContext(this.typeName); TypeConditionContext(this.typeName);
@override @override
SourceSpan get span => typeName.span; SourceSpan get span => typeName.span;
@override @override
String toSource() => typeName.toSource(); String toSource() => typeName.toSource();
} }

View file

@ -7,6 +7,8 @@ class VariableContext extends Node {
VariableContext(this.DOLLAR, this.NAME); VariableContext(this.DOLLAR, this.NAME);
String get name => NAME.text;
@override @override
SourceSpan get span => SourceSpan get span =>
new SourceSpan(DOLLAR?.span?.start, NAME?.span?.end, toSource()); new SourceSpan(DOLLAR?.span?.start, NAME?.span?.end, toSource());

View file

@ -4,4 +4,4 @@ export 'lexer.dart';
export 'parser.dart'; export 'parser.dart';
export 'syntax_error.dart'; export 'syntax_error.dart';
export 'token.dart'; export 'token.dart';
export 'token_type.dart'; export 'token_type.dart';

View file

@ -45,7 +45,8 @@ List<Token> scan(String text) {
for (var pattern in _patterns.keys) { for (var pattern in _patterns.keys) {
if (scanner.matches(pattern)) { if (scanner.matches(pattern)) {
potential.add(new Token(_patterns[pattern], scanner.lastMatch[0], scanner.lastSpan)); potential.add(new Token(
_patterns[pattern], scanner.lastMatch[0], scanner.lastSpan));
} }
} }
@ -57,7 +58,6 @@ List<Token> scan(String text) {
// Choose longest token // Choose longest token
potential.sort((a, b) => b.text.length.compareTo(a.text.length)); potential.sort((a, b) => b.text.length.compareTo(a.text.length));
var chosen = potential.first; var chosen = potential.first;
var start = scanner.state;
out.add(chosen); out.add(chosen);
scanner.scan(chosen.text); scanner.scan(chosen.text);
} }

View file

@ -23,4 +23,4 @@ enum TokenType {
STRING, STRING,
NAME NAME
} }

6
test/all.dart Normal file
View file

@ -0,0 +1,6 @@
import 'package:test/test.dart';
import 'value_test.dart' as value;
main() {
group('value', value.main);
}

View file

@ -1,23 +1,7 @@
import 'package:graphql_parser/graphql_parser.dart'; import 'package:graphql_parser/graphql_parser.dart';
import 'package:matcher/matcher.dart'; import 'package:matcher/matcher.dart';
import 'package:test/test.dart';
Parser parse(String text) => new Parser(scan(text)); Parser parse(String text) => new Parser(scan(text));
Matcher equalsParsed(value) => new _EqualsParsed(value); final Matcher throwsSyntaxError = throwsA(const isInstanceOf<SyntaxError>());
class _EqualsParsed extends Matcher {
final value;
_EqualsParsed(this.value);
@override
Description describe(Description description)
=> description.add('equals $value when parsed as a GraphQL value');
@override
bool matches(String item, Map matchState) {
var p = parse(item);
var v = p.parseValue();
return equals(value).matches(v.value, matchState);
}
}

View file

@ -1,4 +1,5 @@
import 'dart:math' as math; import 'dart:math' as math;
import 'package:graphql_parser/graphql_parser.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'common.dart'; import 'common.dart';
@ -20,8 +21,8 @@ main() {
test('array', () { test('array', () {
expect('[]', equalsParsed([])); expect('[]', equalsParsed([]));
expect('[1,2]', equalsParsed([1,2])); expect('[1,2]', equalsParsed([1, 2]));
expect('[1,2, 3]', equalsParsed([1,2,3])); expect('[1,2, 3]', equalsParsed([1, 2, 3]));
expect('["a"]', equalsParsed(['a'])); expect('["a"]', equalsParsed(['a']));
}); });
@ -38,4 +39,28 @@ main() {
expect('"\\u0123"', equalsParsed('\u0123')); expect('"\\u0123"', equalsParsed('\u0123'));
expect('"\\u0123\\u4567"', equalsParsed('\u0123\u4567')); expect('"\\u0123\\u4567"', equalsParsed('\u0123\u4567'));
}); });
test('exceptions', () {
expect(() => parseValue('[1'), throwsSyntaxError);
});
}
ValueContext parseValue(String text) => parse(text).parseValue();
Matcher equalsParsed(value) => new _EqualsParsed(value);
class _EqualsParsed extends Matcher {
final value;
_EqualsParsed(this.value);
@override
Description describe(Description description) =>
description.add('equals $value when parsed as a GraphQL value');
@override
bool matches(String item, Map matchState) {
var p = parse(item);
var v = p.parseValue();
return equals(value).matches(v.value, matchState);
}
} }

37
test/variable_test.dart Normal file
View file

@ -0,0 +1,37 @@
import 'package:test/test.dart';
import 'common.dart';
main() {
test('variables', () {
expect(r'$a', isVariable('a'));
expect(r'$abc', isVariable('abc'));
expect(r'$abc123', isVariable('abc123'));
expect(r'$_', isVariable('_'));
expect(r'$___', isVariable('___'));
expect(r'$_123', isVariable('_123'));
});
test('exceptions', () {
expect(() => parse(r'$').parseVariable(), throwsSyntaxError);
});
}
Matcher isVariable(String name) => new _IsVariable(name);
class _IsVariable extends Matcher {
final String name;
_IsVariable(this.name);
@override
Description describe(Description description) {
return description.add('parses as a variable named "$name"');
}
@override
bool matches(String item, Map matchState) {
var p = parse(item);
var v = p.parseVariable();
return equals(name).matches(v?.name, matchState);
}
}