tree-sitter-rdoc/grammar.js

97 lines
2.3 KiB
JavaScript

module.exports = grammar({
name: 'rdoc',
rules: {
document: $ => seq(
optional($._newlines),
optional($._paragraphs),
repeat(alias(
choice(
$._section1,
$._section2,
$._section3,
$._section4,
),
$.section,
)),
),
_section1: $ => prec.right(seq(
$.heading1,
optional($._paragraphs),
repeat(alias($._section2, $.section)),
)),
heading1: $ => seq(
alias("=", $.marker),
$._heading_after_marker,
),
_section2: $ => prec.right(seq(
$.heading2,
optional($._paragraphs),
repeat(alias($._section3, $.section)),
)),
heading2: $ => seq(
alias("==", $.marker),
$._heading_after_marker,
),
_section3: $ => prec.right(seq(
$.heading3,
optional($._paragraphs),
repeat(alias($._section4, $.section)),
)),
heading3: $ => seq(
alias("===", $.marker),
$._heading_after_marker,
),
_section4: $ => prec.right(seq(
$.heading4,
optional($._paragraphs),
)),
heading4: $ => seq(
alias("====", $.marker),
$._heading_after_marker,
),
_heading_after_marker: $ => seq(
/ +/,
alias(/[^\n]+/, $.content),
$._newlines,
),
_paragraphs: $ => seq(repeat1(choice(
$.paragraph,
$.codeblock,
$.bullet_list,
)), optional($._newlines)),
paragraph: $ => prec.right(1, seq(
$._paragraph_line,
repeat(seq("\n", $._paragraph_line))
)),
_paragraph_line: $ => /[^\n= *][^\n]*/,
codeblock: $ => prec.right(repeat1(/ +[^\n]+\n/)),
bullet_list: $ => alias($._bullet_list_item, $.item),
_bullet_list_item: $ => seq(
alias("*", $.marker),
/ +/,
alias(/[^\n]+/, $.content),
),
_newlines: $ => repeat1($._newline),
_newline: $ => "\n",
},
extras: $ => [],
});