Properly support definition commands from xparse (#148)

Additionally, handle command definition without curly braces
around the defined command name.

Signed-off-by: Jonas Dujava <jonas.dujava@gmail.com>
pull/813/head
Jonáš Dujava 2024-07-21 08:52:18 +07:00 committed by GitHub
parent d018f2e662
commit 55ef65a416
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 540 additions and 53 deletions

@ -322,6 +322,9 @@ module.exports = grammar({
curly_group_text: $ => seq('{', field('text', $.text), '}'),
curly_group_spec: $ =>
seq('{', repeat(choice($._text_content, '=')), '}'),
curly_group_text_list: $ =>
seq('{', sepBy(field('text', $.text), ','), '}'),
@ -871,6 +874,9 @@ module.exports = grammar({
),
new_command_definition: $ =>
choice($._new_command_definition, $._newer_command_definition),
_new_command_definition: $ =>
seq(
field(
'command',
@ -879,10 +885,30 @@ module.exports = grammar({
'\\newcommand*',
'\\renewcommand',
'\\renewcommand*',
'\\providecommand',
'\\providecommand*',
'\\DeclareRobustCommand',
'\\DeclareRobustCommand*',
'\\DeclareMathOperator',
'\\DeclareMathOperator*',
),
),
field('declaration', choice($.curly_group_command_name, $.command_name)),
optional(
seq(
field('argc', $.brack_group_argc),
field('default', optional($.brack_group)),
),
),
field('implementation', $.curly_group),
),
_newer_command_definition: $ =>
prec.right(
seq(
field(
'command',
choice(
'\\NewDocumentCommand',
'\\RenewDocumentCommand',
'\\ProvideDocumentCommand',
@ -893,15 +919,11 @@ module.exports = grammar({
'\\DeclareExpandableDocumentCommand',
),
),
field('declaration', $.curly_group_command_name),
optional(
seq(
field('argc', $.brack_group_argc),
field('default', optional($.brack_group)),
),
),
field('declaration', choice($.curly_group_command_name, $.command_name)),
field('spec', $.curly_group_spec),
field('implementation', $.curly_group),
),
),
old_command_definition: $ =>
seq(field('command', '\\def'), field('declaration', $.command_name)),
@ -950,12 +972,28 @@ module.exports = grammar({
),
environment_definition: $ =>
choice($._environment_definition, $._newer_environment_definition),
_environment_definition: $ =>
seq(
field(
'command',
choice(
'\\newenvironment',
'\\renewenvironment',
),
),
field('name', $.curly_group_text),
field('argc', optional($.brack_group_argc)),
field('begin', $.curly_group_impl),
field('end', $.curly_group_impl),
),
_newer_environment_definition: $ =>
seq(
field(
'command',
choice(
'\\NewDocumentEnvironment',
'\\RenewDocumentEnvironment',
'\\ProvideDocumentEnvironment',
@ -963,7 +1001,7 @@ module.exports = grammar({
),
),
field('name', $.curly_group_text),
field('argc', optional($.brack_group_argc)),
field('spec', $.curly_group_spec),
field('begin', $.curly_group_impl),
field('end', $.curly_group_impl),
),

@ -1139,6 +1139,35 @@
}
]
},
"curly_group_spec": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_text_content"
},
{
"type": "STRING",
"value": "="
}
]
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"curly_group_text_list": {
"type": "SEQ",
"members": [
@ -5081,6 +5110,19 @@
]
},
"new_command_definition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_new_command_definition"
},
{
"type": "SYMBOL",
"name": "_newer_command_definition"
}
]
},
"_new_command_definition": {
"type": "SEQ",
"members": [
{
@ -5105,6 +5147,14 @@
"type": "STRING",
"value": "\\renewcommand*"
},
{
"type": "STRING",
"value": "\\providecommand"
},
{
"type": "STRING",
"value": "\\providecommand*"
},
{
"type": "STRING",
"value": "\\DeclareRobustCommand"
@ -5120,7 +5170,86 @@
{
"type": "STRING",
"value": "\\DeclareMathOperator*"
}
]
}
},
{
"type": "FIELD",
"name": "declaration",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "curly_group_command_name"
},
{
"type": "SYMBOL",
"name": "command_name"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "argc",
"content": {
"type": "SYMBOL",
"name": "brack_group_argc"
}
},
{
"type": "FIELD",
"name": "default",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "brack_group"
},
{
"type": "BLANK"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "FIELD",
"name": "implementation",
"content": {
"type": "SYMBOL",
"name": "curly_group"
}
}
]
},
"_newer_command_definition": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "command",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\\NewDocumentCommand"
@ -5160,46 +5289,26 @@
"type": "FIELD",
"name": "declaration",
"content": {
"type": "SYMBOL",
"name": "curly_group_command_name"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "argc",
"content": {
"type": "SYMBOL",
"name": "brack_group_argc"
}
"name": "curly_group_command_name"
},
{
"type": "FIELD",
"name": "default",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "brack_group"
},
{
"type": "BLANK"
"name": "command_name"
}
]
}
}
]
},
{
"type": "BLANK"
"type": "FIELD",
"name": "spec",
"content": {
"type": "SYMBOL",
"name": "curly_group_spec"
}
]
},
{
"type": "FIELD",
@ -5210,6 +5319,7 @@
}
}
]
}
},
"old_command_definition": {
"type": "SEQ",
@ -5497,6 +5607,19 @@
}
},
"environment_definition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_environment_definition"
},
{
"type": "SYMBOL",
"name": "_newer_environment_definition"
}
]
},
"_environment_definition": {
"type": "SEQ",
"members": [
{
@ -5512,7 +5635,61 @@
{
"type": "STRING",
"value": "\\renewenvironment"
}
]
}
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "curly_group_text"
}
},
{
"type": "FIELD",
"name": "argc",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "brack_group_argc"
},
{
"type": "BLANK"
}
]
}
},
{
"type": "FIELD",
"name": "begin",
"content": {
"type": "SYMBOL",
"name": "curly_group_impl"
}
},
{
"type": "FIELD",
"name": "end",
"content": {
"type": "SYMBOL",
"name": "curly_group_impl"
}
}
]
},
"_newer_environment_definition": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "command",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\\NewDocumentEnvironment"
@ -5542,18 +5719,10 @@
},
{
"type": "FIELD",
"name": "argc",
"name": "spec",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "brack_group_argc"
},
{
"type": "BLANK"
}
]
"name": "curly_group_spec"
}
},
{

@ -2370,6 +2370,185 @@
}
}
},
{
"type": "curly_group_spec",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "acronym_definition",
"named": true
},
{
"type": "acronym_reference",
"named": true
},
{
"type": "author_declaration",
"named": true
},
{
"type": "biblatex_include",
"named": true
},
{
"type": "bibstyle_include",
"named": true
},
{
"type": "bibtex_include",
"named": true
},
{
"type": "block_comment",
"named": true
},
{
"type": "caption",
"named": true
},
{
"type": "citation",
"named": true
},
{
"type": "class_include",
"named": true
},
{
"type": "color_definition",
"named": true
},
{
"type": "color_reference",
"named": true
},
{
"type": "color_set_definition",
"named": true
},
{
"type": "curly_group",
"named": true
},
{
"type": "displayed_equation",
"named": true
},
{
"type": "environment_definition",
"named": true
},
{
"type": "generic_command",
"named": true
},
{
"type": "glossary_entry_definition",
"named": true
},
{
"type": "glossary_entry_reference",
"named": true
},
{
"type": "graphics_include",
"named": true
},
{
"type": "hyperlink",
"named": true
},
{
"type": "import_include",
"named": true
},
{
"type": "inkscape_include",
"named": true
},
{
"type": "inline_formula",
"named": true
},
{
"type": "label_definition",
"named": true
},
{
"type": "label_number",
"named": true
},
{
"type": "label_reference",
"named": true
},
{
"type": "label_reference_range",
"named": true
},
{
"type": "latex_include",
"named": true
},
{
"type": "let_command_definition",
"named": true
},
{
"type": "math_delimiter",
"named": true
},
{
"type": "new_command_definition",
"named": true
},
{
"type": "old_command_definition",
"named": true
},
{
"type": "package_include",
"named": true
},
{
"type": "paired_delimiter_definition",
"named": true
},
{
"type": "svg_include",
"named": true
},
{
"type": "text",
"named": true
},
{
"type": "text_mode",
"named": true
},
{
"type": "theorem_definition",
"named": true
},
{
"type": "tikz_library_import",
"named": true
},
{
"type": "title_declaration",
"named": true
},
{
"type": "verbatim_include",
"named": true
}
]
}
},
{
"type": "curly_group_text",
"named": true,
@ -3028,6 +3207,16 @@
"named": true
}
]
},
"spec": {
"multiple": false,
"required": false,
"types": [
{
"type": "curly_group_spec",
"named": true
}
]
}
}
},
@ -5135,6 +5324,14 @@
"type": "\\newcommand*",
"named": false
},
{
"type": "\\providecommand",
"named": false
},
{
"type": "\\providecommand*",
"named": false
},
{
"type": "\\renewcommand",
"named": false
@ -5149,6 +5346,10 @@
"multiple": false,
"required": true,
"types": [
{
"type": "command_name",
"named": true
},
{
"type": "curly_group_command_name",
"named": true
@ -5174,6 +5375,16 @@
"named": true
}
]
},
"spec": {
"multiple": false,
"required": false,
"types": [
{
"type": "curly_group_spec",
"named": true
}
]
}
}
},
@ -9102,6 +9313,14 @@
"type": "\\pnotecite",
"named": false
},
{
"type": "\\providecommand",
"named": false
},
{
"type": "\\providecommand*",
"named": false
},
{
"type": "\\pvolcite",
"named": false

@ -134,6 +134,67 @@ Command definition with default arg
(text
(placeholder)))))
================================================================================
Command definition without curly braces
================================================================================
\newcommand\foo[1]{#1}
--------------------------------------------------------------------------------
(source_file
(new_command_definition
(command_name)
(brack_group_argc
(argc))
(curly_group
(text
(placeholder)))))
================================================================================
Command definition (xparse)
================================================================================
\NewDocumentCommand{\foo}{m}{#1}
--------------------------------------------------------------------------------
(source_file
(new_command_definition
(curly_group_command_name
(command_name))
(curly_group_spec
(text
(word)))
(curly_group
(text
(placeholder)))))
================================================================================
Command definition with optional argument (xparse)
================================================================================
\NewDocumentCommand{\foo}{o{default} m}{#1 #2}
--------------------------------------------------------------------------------
(source_file
(new_command_definition
(curly_group_command_name
(command_name))
(curly_group_spec
(text
(word))
(curly_group
(text
(word)))
(text
(word)))
(curly_group
(text
(placeholder)
(placeholder)))))
================================================================================
Author command
================================================================================