Reformat grammar.js

pull/813/head
Patrick Förster 2024-04-28 09:20:40 +07:00
parent ae2134af67
commit 01973c115c
2 changed files with 139 additions and 174 deletions

@ -1,4 +1,5 @@
{
"singleQuote": true,
"arrowParens": "avoid"
"arrowParens": "avoid",
"trailingComma": "all"
}

@ -15,20 +15,20 @@ const specialEnvironment = ({ rule, name, content, options }) => {
seq(
field('begin', alias($[beginRule], $.begin)),
content($),
field('end', alias($[endRule], $.end))
field('end', alias($[endRule], $.end)),
),
[beginRule]: $ =>
seq(
field('command', '\\begin'),
field('name', alias($[groupRule], $.curly_group_text)),
options ? options($) : seq()
options ? options($) : seq(),
),
[endRule]: $ =>
seq(
field('command', '\\end'),
field('name', alias($[groupRule], $.curly_group_text))
field('name', alias($[groupRule], $.curly_group_text)),
),
[groupRule]: $ => seq('{', field('text', alias($[nameRule], $.text)), '}'),
@ -64,7 +64,7 @@ module.exports = grammar({
seq(
field('begin', '\\iffalse'),
field('comment', optional(alias($._trivia_raw_fi, $.comment))),
field('end', optional('\\fi'))
field('end', optional('\\fi')),
),
//--- Content
@ -86,7 +86,7 @@ module.exports = grammar({
$.sageblock_environment,
$.generic_environment,
$.math_environment,
$._text_content
$._text_content,
),
_text_content: $ =>
@ -102,8 +102,8 @@ module.exports = grammar({
$.math_delimiter,
$.text_mode,
'(',
')'
)
')',
),
),
//--- Sections
@ -115,8 +115,8 @@ module.exports = grammar({
repeat1($.chapter),
repeat1($.section),
repeat1($.subsection),
repeat1($.subsubsection)
)
repeat1($.subsubsection),
),
),
_paragraph: $ =>
@ -124,8 +124,8 @@ module.exports = grammar({
choice(
repeat1($.paragraph),
repeat1($.subparagraph),
repeat1($.enum_item)
)
repeat1($.enum_item),
),
),
_section_part: $ =>
@ -136,10 +136,10 @@ module.exports = grammar({
seq(
field(
'command',
choice('\\part', '\\part*', '\\addpart', '\\addpart*')
choice('\\part', '\\part*', '\\addpart', '\\addpart*'),
),
optional($._section_part)
)
optional($._section_part),
),
),
part: $ =>
@ -156,10 +156,10 @@ module.exports = grammar({
repeat1($.section),
repeat1($.subsection),
repeat1($.subsubsection),
)
)
)
)
),
),
),
),
),
_chapter_declaration: $ =>
@ -167,10 +167,10 @@ module.exports = grammar({
seq(
field(
'command',
choice('\\chapter', '\\chapter*', '\\addchap', '\\addchap*')
choice('\\chapter', '\\chapter*', '\\addchap', '\\addchap*'),
),
optional($._section_part)
)
optional($._section_part),
),
),
chapter: $ =>
@ -186,10 +186,10 @@ module.exports = grammar({
repeat1($.section),
repeat1($.subsection),
repeat1($.subsubsection),
)
)
)
)
),
),
),
),
),
_section_declaration: $ =>
@ -197,10 +197,10 @@ module.exports = grammar({
seq(
field(
'command',
choice('\\section', '\\section*', '\\addsec', '\\addsec*')
choice('\\section', '\\section*', '\\addsec', '\\addsec*'),
),
optional($._section_part)
)
optional($._section_part),
),
),
section: $ =>
@ -211,22 +211,17 @@ module.exports = grammar({
repeat($._flat_content),
optional(prec.right(-1, $._paragraph)),
optional(
prec.right(
choice(
repeat1($.subsection),
repeat1($.subsubsection)
)
)
)
)
prec.right(choice(repeat1($.subsection), repeat1($.subsubsection))),
),
),
),
_subsection_declaration: $ =>
prec.right(
seq(
field('command', choice('\\subsection', '\\subsection*')),
optional($._section_part)
)
optional($._section_part),
),
),
subsection: $ =>
@ -236,20 +231,16 @@ module.exports = grammar({
$._subsection_declaration,
repeat($._flat_content),
optional(prec.right(-1, $._paragraph)),
optional(
prec.right(
repeat1($.subsubsection)
)
)
)
optional(prec.right(repeat1($.subsubsection))),
),
),
_subsubsection_declaration: $ =>
prec.right(
seq(
field('command', choice('\\subsubsection', '\\subsubsection*')),
optional($._section_part)
)
optional($._section_part),
),
),
subsubsection: $ =>
@ -259,15 +250,15 @@ module.exports = grammar({
$._subsubsection_declaration,
repeat($._flat_content),
optional(prec.right(-1, $._paragraph)),
)
),
),
_paragraph_declaration: $ =>
prec.right(
seq(
field('command', choice('\\paragraph', '\\paragraph*')),
optional($._section_part)
)
optional($._section_part),
),
),
paragraph: $ =>
@ -277,22 +268,17 @@ module.exports = grammar({
$._paragraph_declaration,
repeat($._flat_content),
optional(
prec.right(
choice(
repeat1($.subparagraph),
repeat1($.enum_item)
)
)
)
)
prec.right(choice(repeat1($.subparagraph), repeat1($.enum_item))),
),
),
),
_subparagraph_declaration: $ =>
prec.right(
seq(
field('command', choice('\\subparagraph', '\\subparagraph*')),
optional($._section_part)
)
optional($._section_part),
),
),
subparagraph: $ =>
@ -301,22 +287,16 @@ module.exports = grammar({
seq(
$._subparagraph_declaration,
repeat($._flat_content),
optional(
prec.right(
choice(
repeat1($.enum_item)
)
)
)
)
optional(prec.right(choice(repeat1($.enum_item)))),
),
),
_enum_itemdeclaration: $ =>
prec.right(
seq(
field('command', choice('\\item', '\\item*')),
field('label', optional($.brack_group_text))
)
field('label', optional($.brack_group_text)),
),
),
enum_item: $ =>
@ -325,12 +305,8 @@ module.exports = grammar({
seq(
$._enum_itemdeclaration,
repeat($._flat_content),
optional(
prec.right(
choice()
)
)
)
optional(prec.right(choice())),
),
),
//--- Group
@ -364,9 +340,9 @@ module.exports = grammar({
'{',
sepBy(
alias(repeat1($._text_content), $.author),
alias('\\and', $.command_name)
alias('\\and', $.command_name),
),
'}'
'}',
),
brack_group: $ =>
@ -393,10 +369,10 @@ module.exports = grammar({
$.block_comment,
$._command,
$.superscript,
$.subscript
)
)
)
$.subscript,
),
),
),
),
word: $ => /[^\s\\%\{\},\$\[\]\(\)=\#_\^\-\+\/\*]+/,
@ -412,7 +388,7 @@ module.exports = grammar({
_glob_pattern_fragment: $ =>
choice(
seq('{', repeat($._glob_pattern_fragment), '}'),
/[^\"\[\]:;\|\{\}<>]+/
/[^\"\[\]:;\|\{\}<>]+/,
),
operator: $ => choice('+', '-', '*', '/', '<', '>', '!', '|', ':', "'"),
@ -420,13 +396,13 @@ module.exports = grammar({
subscript: $ =>
seq(
'_',
field('subscript', choice($.curly_group, $.word, $.generic_command))
field('subscript', choice($.curly_group, $.word, $.generic_command)),
),
superscript: $ =>
seq(
'^',
field('superscript', choice($.curly_group, $.word, $.generic_command))
field('superscript', choice($.curly_group, $.word, $.generic_command)),
),
//--- Key / Value
@ -440,12 +416,12 @@ module.exports = grammar({
displayed_equation: $ =>
prec.left(
seq(choice('$$', '\\['), repeat($._root_content), choice('$$', '\\]'))
seq(choice('$$', '\\['), repeat($._root_content), choice('$$', '\\]')),
),
inline_formula: $ =>
prec.left(
seq(choice('$', '\\('), repeat($._root_content), choice('$', '\\)'))
seq(choice('$', '\\('), repeat($._root_content), choice('$', '\\)')),
),
//--- Environments
@ -455,20 +431,20 @@ module.exports = grammar({
seq(
field('command', '\\begin'),
field('name', $.curly_group_text),
field('options', optional($.brack_group))
)
field('options', optional($.brack_group)),
),
),
end: $ =>
prec.right(
seq(field('command', '\\end'), field('name', $.curly_group_text))
seq(field('command', '\\end'), field('name', $.curly_group_text)),
),
generic_environment: $ =>
seq(
field('begin', $.begin),
repeat($._root_content),
field('end', $.end)
field('end', $.end),
),
//--- Trivia environments
@ -505,7 +481,7 @@ module.exports = grammar({
options: $ =>
seq(
field('options', optional($.brack_group_key_value)),
field('language', $.curly_group_text)
field('language', $.curly_group_text),
),
}),
@ -556,7 +532,7 @@ module.exports = grammar({
'gather',
'gather*',
'flalign',
'flalign*'
'flalign*',
),
content: $ => repeat($._flat_content),
options: undefined,
@ -599,15 +575,15 @@ module.exports = grammar({
$.color_set_definition,
$.color_reference,
$.tikz_library_import,
$.generic_command
$.generic_command,
),
generic_command: $ =>
prec.right(
seq(
field('command', $.command_name),
repeat(field('arg', $.curly_group))
)
repeat(field('arg', $.curly_group)),
),
),
command_name: $ => /\\([^\r\n]|[@a-zA-Z]+\*?)?/,
@ -616,83 +592,83 @@ module.exports = grammar({
seq(
field('command', '\\title'),
field('options', optional($.brack_group)),
field('text', $.curly_group)
field('text', $.curly_group),
),
author_declaration: $ =>
seq(
field('command', '\\author'),
field('options', optional($.brack_group)),
field('authors', $.curly_group_author_list)
field('authors', $.curly_group_author_list),
),
package_include: $ =>
seq(
field('command', choice('\\usepackage', '\\RequirePackage')),
field('options', optional($.brack_group_key_value)),
field('paths', $.curly_group_path_list)
field('paths', $.curly_group_path_list),
),
class_include: $ =>
seq(
field('command', '\\documentclass'),
field('options', optional($.brack_group_key_value)),
field('path', $.curly_group_path)
field('path', $.curly_group_path),
),
latex_include: $ =>
seq(
field(
'command',
choice('\\include', '\\subfileinclude', '\\input', '\\subfile')
choice('\\include', '\\subfileinclude', '\\input', '\\subfile'),
),
field('path', $.curly_group_path)
field('path', $.curly_group_path),
),
biblatex_include: $ =>
seq(
'\\addbibresource',
field('options', optional($.brack_group_key_value)),
field('glob', $.curly_group_glob_pattern)
field('glob', $.curly_group_glob_pattern),
),
bibstyle_include: $ =>
seq(
field('command', '\\bibliographystyle'),
field('path', $.curly_group_path)
field('path', $.curly_group_path),
),
bibtex_include: $ =>
seq(
field('command', '\\bibliography'),
field('paths', $.curly_group_path_list)
field('paths', $.curly_group_path_list),
),
graphics_include: $ =>
seq(
field('command', '\\includegraphics'),
field('options', optional($.brack_group_key_value)),
field('path', $.curly_group_path)
field('path', $.curly_group_path),
),
svg_include: $ =>
seq(
field('command', '\\includesvg'),
field('options', optional($.brack_group_key_value)),
field('path', $.curly_group_path)
field('path', $.curly_group_path),
),
inkscape_include: $ =>
seq(
field('command', '\\includeinkscape'),
field('options', optional($.brack_group_key_value)),
field('path', $.curly_group_path)
field('path', $.curly_group_path),
),
verbatim_include: $ =>
seq(
field('command', choice('\\verbatiminput', '\\VerbatimInput')),
field('path', $.curly_group_path)
field('path', $.curly_group_path),
),
import_include: $ =>
@ -705,18 +681,18 @@ module.exports = grammar({
'\\inputfrom',
'\\subimportfrom',
'\\includefrom',
'\\subincludefrom'
)
'\\subincludefrom',
),
),
field('directory', $.curly_group_path),
field('file', $.curly_group_path)
field('file', $.curly_group_path),
),
caption: $ =>
seq(
field('command', '\\caption'),
field('short', optional($.brack_group)),
field('long', $.curly_group)
field('long', $.curly_group),
),
citation: $ =>
@ -783,16 +759,16 @@ module.exports = grammar({
'\\Notecite',
'\\pnotecite',
'\\Pnotecite',
'\\fnotecite'
)
'\\fnotecite',
),
),
optional(
seq(
field('prenote', $.brack_group),
field('postnote', optional($.brack_group))
)
field('postnote', optional($.brack_group)),
),
),
field('keys', $.curly_group_text_list)
field('keys', $.curly_group_text_list),
),
label_definition: $ =>
@ -820,27 +796,27 @@ module.exports = grammar({
'\\nameCrefs',
'\\lcnamecrefs',
'\\labelcref',
'\\labelcpageref'
)
'\\labelcpageref',
),
),
field('names', $.curly_group_text_list)
field('names', $.curly_group_text_list),
),
label_reference_range: $ =>
seq(
field(
'command',
choice('\\crefrange', '\\crefrange*', '\\Crefrange', '\\Crefrange*')
choice('\\crefrange', '\\crefrange*', '\\Crefrange', '\\Crefrange*'),
),
field('from', $.curly_group_text),
field('to', $.curly_group_text)
field('to', $.curly_group_text),
),
label_number: $ =>
seq(
field('command', '\\newlabel'),
field('name', $.curly_group_text),
field('number', $.curly_group)
field('number', $.curly_group),
),
new_command_definition: $ =>
@ -863,17 +839,17 @@ module.exports = grammar({
'\\NewExpandableDocumentCommand',
'\\RenewExpandableDocumentCommand',
'\\ProvideExpandableDocumentCommand',
'\\DeclareExpandableDocumentCommand'
)
'\\DeclareExpandableDocumentCommand',
),
),
field('declaration', $.curly_group_command_name),
optional(
seq(
field('argc', $.brack_group_argc),
field('default', optional($.brack_group))
)
field('default', optional($.brack_group)),
),
),
field('implementation', $.curly_group)
field('implementation', $.curly_group),
),
old_command_definition: $ =>
@ -884,7 +860,7 @@ module.exports = grammar({
field('command', '\\let'),
field('declaration', $.command_name),
optional('='),
field('implementation', $.command_name)
field('implementation', $.command_name),
),
_math_delimiter_part: $ =>
@ -895,28 +871,16 @@ module.exports = grammar({
seq(
field(
'left_command',
choice(
'\\left',
'\\bigl',
'\\Bigl',
'\\biggl',
'\\Biggl'
)
choice('\\left', '\\bigl', '\\Bigl', '\\biggl', '\\Biggl'),
),
field('left_delimiter', $._math_delimiter_part),
repeat($._root_content),
field(
'right_command',
choice(
'\\right',
'\\bigr',
'\\Bigr',
'\\biggr',
'\\Biggr'
)
choice('\\right', '\\bigr', '\\Bigr', '\\biggr', '\\Biggr'),
),
field('right_delimiter', $._math_delimiter_part)
)
field('right_delimiter', $._math_delimiter_part),
),
),
paired_delimiter_definition: $ =>
@ -924,14 +888,14 @@ module.exports = grammar({
seq(
field(
'command',
choice('\\DeclarePairedDelimiter', '\\DeclarePairedDelimiterX')
choice('\\DeclarePairedDelimiter', '\\DeclarePairedDelimiterX'),
),
field('declaration', $.curly_group_command_name),
field('argc', optional($.brack_group_argc)),
field('left', choice($.curly_group_impl, $.command_name)),
field('right', choice($.curly_group_impl, $.command_name)),
field('body', optional($.curly_group))
)
field('body', optional($.curly_group)),
),
),
environment_definition: $ =>
@ -944,20 +908,20 @@ module.exports = grammar({
'\\NewDocumentEnvironment',
'\\RenewDocumentEnvironment',
'\\ProvideDocumentEnvironment',
'\\DeclareDocumentEnvironment'
)
'\\DeclareDocumentEnvironment',
),
),
field('name', $.curly_group_text),
field('argc', optional($.brack_group_argc)),
field('begin', $.curly_group_impl),
field('end', $.curly_group_impl)
field('end', $.curly_group_impl),
),
glossary_entry_definition: $ =>
seq(
field('command', '\\newglossaryentry'),
field('name', $.curly_group_text),
field('options', $.curly_group_key_value)
field('options', $.curly_group_key_value),
),
glossary_entry_reference: $ =>
@ -1010,11 +974,11 @@ module.exports = grammar({
'\\GLSuserv',
'\\glsuservi',
'\\Glsuservi',
'\\GLSuservi'
)
'\\GLSuservi',
),
),
field('options', optional($.brack_group_key_value)),
field('name', $.curly_group_text)
field('name', $.curly_group_text),
),
acronym_definition: $ =>
@ -1023,7 +987,7 @@ module.exports = grammar({
field('options', optional($.brack_group_key_value)),
field('name', $.curly_group_text),
field('short', $.curly_group),
field('long', $.curly_group)
field('long', $.curly_group),
),
acronym_reference: $ =>
@ -1073,11 +1037,11 @@ module.exports = grammar({
'\\glsentryshortpl',
'\\Glsentryshortpl',
'\\glsentryfullpl',
'\\Glsentryfullpl'
)
'\\Glsentryfullpl',
),
),
field('options', optional($.brack_group_key_value)),
field('name', $.curly_group_text)
field('name', $.curly_group_text),
),
theorem_definition: $ =>
@ -1089,8 +1053,8 @@ module.exports = grammar({
'\\newtheorem',
'\\newtheorem*',
'\\declaretheorem',
'\\declaretheorem*'
)
'\\declaretheorem*',
),
),
optional(field('options', $.brack_group_key_value)),
field('name', $.curly_group_text),
@ -1098,15 +1062,15 @@ module.exports = grammar({
choice(
seq(
field('title', $.curly_group),
field('counter', optional($.brack_group_text))
field('counter', optional($.brack_group_text)),
),
seq(
field('counter', $.brack_group_text),
field('title', $.curly_group)
)
)
)
)
field('title', $.curly_group),
),
),
),
),
),
color_definition: $ =>
@ -1115,7 +1079,7 @@ module.exports = grammar({
optional($.brack_group_text),
field('name', $.curly_group_text),
field('model', $.curly_group_text),
field('spec', $.curly_group)
field('spec', $.curly_group),
),
color_set_definition: $ =>
@ -1125,28 +1089,28 @@ module.exports = grammar({
field('model', $.curly_group_text_list),
field('head', $.curly_group),
field('tail', $.curly_group),
field('spec', $.curly_group)
field('spec', $.curly_group),
),
color_reference: $ =>
seq(
field(
'command',
choice('\\color', '\\colorbox', '\\textcolor', '\\pagecolor')
choice('\\color', '\\colorbox', '\\textcolor', '\\pagecolor'),
),
field('name', $.curly_group_text)
field('name', $.curly_group_text),
),
tikz_library_import: $ =>
seq(
field('command', choice('\\usepgflibrary', '\\usetikzlibrary')),
field('paths', $.curly_group_path_list)
field('paths', $.curly_group_path_list),
),
text_mode: $ =>
seq(
field('command', choice('\\text', '\\intertext', '\\shortintertext')),
field('content', $.curly_group)
field('content', $.curly_group),
),
},
});