Skip to content

Commit

Permalink
Fixed the compilation unit directives, many real Dart sources now parse.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Nov 1, 2015
1 parent 00c65fa commit 1dfcc9f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
65 changes: 22 additions & 43 deletions lib/src/dart/grammar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,26 @@ class DartGrammarDefinition extends GrammarDefinition {

// Pseudo-keywords that should also be valid identifiers.
ABSTRACT() => ref(token, 'abstract');
AS() => ref(token, 'as');
ASSERT() => ref(token, 'assert');
CLASS() => ref(token, 'class');
EXPORT() => ref(token, 'export');
EXTENDS() => ref(token, 'extends');
FACTORY() => ref(token, 'factory');
GET() => ref(token, 'get');
HIDE() => ref(token, 'hide');
IMPLEMENTS() => ref(token, 'implements');
IMPORT() => ref(token, 'import');
INTERFACE() => ref(token, 'interface');
IS() => ref(token, 'is');
LIBRARY() => ref(token, 'library');
NATIVE() => ref(token, 'native');
NEGATE() => ref(token, 'negate');
OF() => ref(token, 'of');
OPERATOR() => ref(token, 'operator');
PART() => ref(token, 'part');
SET() => ref(token, 'set');
SHOW() => ref(token, 'show');
SOURCE() => ref(token, 'source');
STATIC() => ref(token, 'static');
TYPEDEF() => ref(token, 'typedef');
Expand All @@ -82,14 +88,23 @@ class DartGrammarDefinition extends GrammarDefinition {

compilationUnit() =>
ref(HASHBANG).optional()
& ref(directive).star()
& ref(libraryDirective).optional()
& ref(importDirective).star()
& ref(topLevelDefinition).star();

directive() =>
ref(token, '#')
& ref(identifier)
& ref(arguments)
& ref(token, ';');
libraryDirective() =>
ref(LIBRARY) & ref(qualified) & ref(token, ';')
| ref(PART) & ref(OF) & ref(qualified) & ref(token, ';');

importDirective() =>
ref(IMPORT) & ref(SINGLE_LINE_STRING)
& (ref(AS) & ref(identifier)).optional()
& ((ref(SHOW) | ref(HIDE)) & ref(identifier).separatedBy(ref(token, ','))).optional()
& ref(token, ';')
| ref(EXPORT) & ref(SINGLE_LINE_STRING)
& ((ref(SHOW) | ref(HIDE)) & ref(identifier).separatedBy(ref(token, ','))).optional()
& ref(token, ';')
| ref(PART) & ref(SINGLE_LINE_STRING) & ref(token, ';');

topLevelDefinition() =>
ref(classDefinition)
Expand Down Expand Up @@ -324,7 +339,7 @@ class DartGrammarDefinition extends GrammarDefinition {
identifier() => ref(token, ref(IDENTIFIER));

qualified() =>
ref(identifier) & (ref(token, '.') & ref(identifier)).optional()
ref(identifier) & (ref(token, '.') & ref(identifier)).star()
;

type() =>
Expand Down Expand Up @@ -625,42 +640,6 @@ class DartGrammarDefinition extends GrammarDefinition {
| ref(block)
;

// -----------------------------------------------------------------
// Library files.
// -----------------------------------------------------------------
libraryUnit() =>
ref(libraryDefinition).end()
;

libraryDefinition() =>
ref(LIBRARY) & ref(token, '{') & ref(libraryBody) & ref(token, '}')
;

libraryBody() =>
ref(libraryImport).optional() & ref(librarySource).optional()
;

libraryImport() =>
ref(IMPORT) & ref(token, '=') & ref(token, '[') & ref(importReferences).optional() & ref(token, ']')
;

importReferences() =>
ref(importReference) & (ref(token, ',') & ref(importReference)).star() & ref(token, ',').optional()
;

importReference() =>
(ref(token, IDENTIFIER) & ref(token, ':')).optional() & ref(token, STRING)
;

librarySource() =>
ref(SOURCE) & ref(token, '=') & ref(token, '[') & ref(sourceUrls).optional() & ref(token, ']')
;

sourceUrls() =>
ref(token, STRING) & (ref(token, ',') & ref(token, STRING)).star() & ref(token, ',').optional()
;


// -----------------------------------------------------------------
// Lexical tokens.
// -----------------------------------------------------------------
Expand Down
36 changes: 34 additions & 2 deletions test/dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,40 @@ import 'package:petitparser/dart.dart';
void main() {
var definition = new DartGrammarDefinition();
var dart = new DartGrammar();
group('basic', () {
test('structure', () {
group('directives', () {
test('hashbang', () {
expect('#!/bin/dart\n', accept(dart));
});
test('library', () {
expect('library a;', accept(dart));
expect('library a.b;', accept(dart));
expect('library a.b.c_d;', accept(dart));
});
test('part of', () {
expect('part of a;', accept(dart));
expect('part of a.b;', accept(dart));
expect('part of a.b.c_d;', accept(dart));
});
test('part', () {
expect('part "abc";', accept(dart));
});
test('import', () {
expect('import "abc";', accept(dart));
expect('import "abc" as a;', accept(dart));
expect('import "abc" show a;', accept(dart));
expect('import "abc" show a, b;', accept(dart));
expect('import "abc" hide a;', accept(dart));
expect('import "abc" hide a, b;', accept(dart));
});
test('export', () {
expect('export "abc";', accept(dart));
expect('export "abc" as a;', accept(dart));
expect('export "abc" show a;', accept(dart));
expect('export "abc" show a, b;', accept(dart));
expect('export "abc" hide a;', accept(dart));
expect('export "abc" hide a, b;', accept(dart));
});
test('full', () {
expect('library test;', accept(dart));
expect('library test; void main() { }', accept(dart));
expect('library test; void main() { print(2 + 3); }', accept(dart));
Expand Down

0 comments on commit 1dfcc9f

Please sign in to comment.