Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #2

Merged
merged 11 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
test.d2
.vscode
92 changes: 47 additions & 45 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = grammar({
source_file: $ => repeat(choice(
// $.direction,
$.expression,
$._comment,
)),

extras: $ => choice(
Expand All @@ -19,65 +20,66 @@ module.exports = grammar({
"down"
)),

expression: $ => seq(
$.shape_title,
optional($.shape_label),
choice(";", /\n+/),
),
expression: $ => prec.right(seq(
optional("("),
repeat1(choice(
$.identifier,
$.connection,
)),
optional(")"),
optional(seq(
$.connection_identifier,
$.sub_identifier
)),
optional(seq(":", optional($.label), optional($.container))),
optional(choice(/\n+\s*/, ";")),
)),

shape_title: $ => prec.right(repeat1(choice(
$.connection,
$.identifier,
))),
label_block: $ => choice(
seq(":", optional($.label), optional($.container)),
// seq(":", $.container),
// seq(":", $.label),
),

shape_label: $ => seq(":", choice(
seq("|", /.+/, "|"),
/.+/,
)),
container: $ => seq("{", repeat($.expression), "}"),

// shape_body: $ => prec.right(seq(
// "{",
// /\s*/,
// repeat(seq(/\s*/, $.shape_param)),
// /\s*/,
// "}"
// )),

// shape_param: $ => seq(
// $.identifier,
// /\s*/,
// ":",
// optional(/\s+/),
// $.param_value,
// optional($.param_body),
// ),

// param_body: $ => seq("{", /.+/, "}"),
label: _ => /.+/,

// building blocks

identifier: $ => seq(
$._ident_regex,
// repeat(seq(/\s+/, $._ident_regex)),
// optional($.sub_identifier),
),
identifier: $ => prec.right(repeat1(
choice(
$._ident_regex,
$.sub_identifier,
"-",
)
)),

sub_identifier: $ => seq(".", $.identifier),

connection: _ => token(choice(
seq("-", repeat1("-")),
seq("<", repeat1("-")),
seq(repeat1("-"), ">"),
seq("<", repeat1("-"), ">"),
)),
// Trust me it's better that way than doing it via seq's/repeat's
connection: _ => choice(
/--+/,
/<-+/,
/-+>/,
/<-+>/,

// Multiline arrows
seq(/--+/, /\\\n+\s+/, /--+/),
seq(/<-+/, /\\\n+\s+/, /--+/),
seq(/--+/, /\\\n+\s+/, /-+>/),
seq(/<-+/, /\\\n+\s+/, /-+>/),
),

connection_identifier: _ => /\[\d+\]/,

param_value: _ => /[\w\-_]+/i,
// param_value: _ => /[\w\-_]+/i,

// const-like rules

_ident_regex: _ => /[\p{L}0-9\-_"' ]+/,
_ident_regex: _ => /[\p{L}0-9_"' ]+/,

_comment: _ => token(seq('#', /.*/)),
_comment: _ => token(seq('#', /.*\n/)),
}
});

Loading