diff --git a/grammar.js b/grammar.js index 1709b89..c763775 100644 --- a/grammar.js +++ b/grammar.js @@ -1,12 +1,12 @@ module.exports = grammar({ name: "d2", + // TODO: handle empty lines extras: ($) => [], word: ($) => $._word, rules: { - // TODO: add the actual grammar rules source_file: ($) => repeat($._definition), _definition: ($) => choice($._root_attribute, $.connection, $.shape), @@ -131,7 +131,8 @@ module.exports = grammar({ seq("`", repeat(token.immediate(/[^`\n]+/)), "`") ), - _end: ($) => choice(";", "\n", "\0"), + _eof: ($) => choice("\n", "\0"), + _end: ($) => choice(";", $._eof), dot: ($) => ".", }, diff --git a/src/grammar.json b/src/grammar.json index 59eec41..9ac1350 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -689,6 +689,19 @@ } ] }, + "_eof": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "\n" + }, + { + "type": "STRING", + "value": "\u0000" + } + ] + }, "_end": { "type": "CHOICE", "members": [ @@ -697,12 +710,8 @@ "value": ";" }, { - "type": "STRING", - "value": "\n" - }, - { - "type": "STRING", - "value": "\u0000" + "type": "SYMBOL", + "name": "_eof" } ] }, diff --git a/src/parser.c b/src/parser.c index 1ffc90f..6713083 100644 Binary files a/src/parser.c and b/src/parser.c differ diff --git a/test/corpus/attributes.txt b/test/corpus/attributes.txt index 4223905..0f6b8f2 100644 --- a/test/corpus/attributes.txt +++ b/test/corpus/attributes.txt @@ -1,33 +1,33 @@ -================== +================================================================================ Root attribute -================== +================================================================================ direction: value ---- +-------------------------------------------------------------------------------- (source_file (attr_key) (attr_value) ) -================== +================================================================================ Inline shape attribute -================== +================================================================================ foo.shape: oval foo.bar.baz.shape: oval ---- +-------------------------------------------------------------------------------- (source_file (shape (identifier) (dot) (attr_key) (attr_value)) (shape (identifier) (dot) (identifier) (dot) (identifier) (dot) (attr_key) (attr_value)) ) -================== +================================================================================ Inline style attribute -================== +================================================================================ foo.style.opacity: 5 ---- +-------------------------------------------------------------------------------- (source_file (shape (identifier) (dot) (attr_key) (dot) (attr_key) (attr_value)) diff --git a/test/corpus/connection.txt b/test/corpus/connection.txt index b351835..9368c63 100644 --- a/test/corpus/connection.txt +++ b/test/corpus/connection.txt @@ -1,12 +1,12 @@ -================== +================================================================================ Simple connection -================== +================================================================================ foo--bar biz->baz biz<->baz biz<-baz ---- +-------------------------------------------------------------------------------- (source_file (connection @@ -31,15 +31,15 @@ biz<-baz ) ) -================== +================================================================================ Formatted connection -================== +================================================================================ foo -- bar biz -> baz biz <-> baz biz <- baz ---- +-------------------------------------------------------------------------------- (source_file (connection @@ -65,13 +65,13 @@ biz <- baz ) -============================= +================================================================================ Complex identifier connection -============================= +================================================================================ Foo Bar -- Biz Baz -Bar-Foo- <- -Baz-Biz- ---- +-------------------------------------------------------------------------------- (source_file (connection @@ -86,12 +86,12 @@ Foo Bar -- Biz Baz ) ) -================== +================================================================================ Inline connection -================== +================================================================================ foo--bar->biz->baz ---- +-------------------------------------------------------------------------------- (source_file (connection @@ -105,12 +105,12 @@ foo--bar->biz->baz ) ) -================== +================================================================================ Many connections inline -================== +================================================================================ foo--bar;biz->baz ---- +-------------------------------------------------------------------------------- (source_file (connection @@ -125,13 +125,13 @@ foo--bar;biz->baz ) ) -================== -Descripted connection -================== +================================================================================ +Labeled connections +================================================================================ foo--bar: Question? -bar->baz: Yes +bar -> baz: Yes ---- +-------------------------------------------------------------------------------- (source_file (connection diff --git a/test/corpus/shape.txt b/test/corpus/shape.txt index 5809a81..71aeeae 100644 --- a/test/corpus/shape.txt +++ b/test/corpus/shape.txt @@ -1,35 +1,35 @@ -================== +================================================================================ Simple shape -================== +================================================================================ foo bar ---- +-------------------------------------------------------------------------------- (source_file (shape (identifier)) (shape (identifier)) ) -================== +================================================================================ Complex identifier -================== +================================================================================ Foo bar -Biz-baz- ---- +-------------------------------------------------------------------------------- (source_file (shape (identifier)) (shape (identifier)) ) -================== +================================================================================ Inline shapes -================== +================================================================================ foo.baz ---- +-------------------------------------------------------------------------------- (source_file (shape @@ -40,12 +40,12 @@ foo.baz ) -================== +================================================================================ Many shapes inline -================== +================================================================================ a;b;c ---- +-------------------------------------------------------------------------------- (source_file (shape (identifier)) @@ -53,13 +53,13 @@ a;b;c (shape (identifier)) ) -================== +================================================================================ Aliased shapes -================== +================================================================================ a: Foo Bar a: Foo Bar; b: Biz Baz ---- +-------------------------------------------------------------------------------- (source_file (shape (identifier) (label))