Skip to content

Commit

Permalink
fix: explicitly parse EOL for better results
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Mar 4, 2024
1 parent 222397a commit d8452fd
Show file tree
Hide file tree
Showing 4 changed files with 874 additions and 663 deletions.
53 changes: 34 additions & 19 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,36 @@ module.exports = grammar({
$.__error_recovery,
],

extras: _ => [/\r?\n/],
extras: _ => [],

rules: {
file: $ => repeat($.test),
file: $ => repeat(
choice($.test, $._eol)
),

test: $ => seq(
test: $ => prec.right(seq(
$.header,
alias(repeat1(/./), $.input),
alias($._body, $.input),
alias($._dashes, $.separator),
alias(repeat(/./), $.output),
optional(alias($._body, $.output)),
)),

_line: _ => prec(-1, repeat1(/[^\r\n]/)),

_body: $ => repeat1(
choice($._line, $._eol)
),

header: $ => seq(
alias($._equals_begin, $.separator),
alias(repeat1(/[^\r\n]/), $.name),
alias($._line, $.name),
optional($.attributes),
alias($._equals_end, $.separator),
),

attributes: $ => repeat1($.attribute),
attributes: $ => repeat1(
choice($.attribute, $._eol)
),

attribute: $ => choice(
":skip",
Expand All @@ -44,21 +54,21 @@ module.exports = grammar({

_language: $ => seq(
":language",
token.immediate("("),
"(",
field("language", alias($._lang, $.parameter)),
token.immediate(")")
")"
),

_platform: $ => seq(
":platform",
token.immediate("("),
"(",
field("platform", alias($._os, $.parameter)),
token.immediate(")")
")"
),

_lang: _ => token.immediate(repeat1(/[\w.-]/)),
_lang: _ => token(repeat1(/[\w.-]/)),

_os: _ => token.immediate(choice(
_os: _ => token(choice(
"linux",
"macos",
"ios",
Expand All @@ -71,19 +81,24 @@ module.exports = grammar({
"windows",
)),

_equals_begin: $ => prec(1, seq(
_equals_begin: $ => prec(2, seq(
$._equals_begin_ext,
optional($._suffix_equals_begin)
optional($._suffix_equals_begin),
$._eol,
)),

_equals_end: $ => prec(1, seq(
_equals_end: $ => prec(2, seq(
$._equals_end_ext,
optional($._suffix_equals_end)
optional($._suffix_equals_end),
$._eol,
)),

_dashes: $ => prec(1, seq(
_dashes: $ => prec(2, seq(
token(seq("---", repeat("-"))),
optional($._suffix_dashes)
optional($._suffix_dashes),
$._eol,
)),

_eol: _ => token(prec(3, /\r?\n/))
}
});
Loading

0 comments on commit d8452fd

Please sign in to comment.