From f23cf647fb715cf30fd2e4ed0c6574b13d0c14ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=C3=A1=C5=A1=20Dujava?= <19737748+jdujava@users.noreply.github.com> Date: Sat, 27 Jul 2024 22:14:14 +0200 Subject: [PATCH] Implement *Copy macros for commands/environments (#154) This completes the implementation of all command/environment definition macros which are provided by LaTeX kernel and described in https://ctan.org/pkg/usrguide. Signed-off-by: Jonas Dujava --- grammar.js | 64 +++++++--- src/grammar.json | 257 +++++++++++++++++++++++++++------------ src/node-types.json | 58 ++++++++- test/corpus/commands.txt | 15 +++ 4 files changed, 292 insertions(+), 102 deletions(-) diff --git a/grammar.js b/grammar.js index 9cce7884a..ac43d814b 100644 --- a/grammar.js +++ b/grammar.js @@ -911,7 +911,7 @@ module.exports = grammar({ ), new_command_definition: $ => - choice($._new_command_definition, $._newer_command_definition), + choice($._new_command_definition, $._newer_command_definition, $._new_command_copy), _new_command_definition: $ => seq( @@ -941,25 +941,37 @@ module.exports = grammar({ ), _newer_command_definition: $ => - prec.right( - seq( - field( - 'command', - choice( - '\\NewDocumentCommand', - '\\RenewDocumentCommand', - '\\ProvideDocumentCommand', - '\\DeclareDocumentCommand', - '\\NewExpandableDocumentCommand', - '\\RenewExpandableDocumentCommand', - '\\ProvideExpandableDocumentCommand', - '\\DeclareExpandableDocumentCommand', - ), + seq( + field( + 'command', + choice( + '\\NewDocumentCommand', + '\\RenewDocumentCommand', + '\\ProvideDocumentCommand', + '\\DeclareDocumentCommand', + '\\NewExpandableDocumentCommand', + '\\RenewExpandableDocumentCommand', + '\\ProvideExpandableDocumentCommand', + '\\DeclareExpandableDocumentCommand', + ), + ), + field('declaration', choice($.curly_group_command_name, $.command_name)), + field('spec', $.curly_group_spec), + field('implementation', $.curly_group), + ), + + _new_command_copy: $ => + seq( + field( + 'command', + choice( + '\\NewCommandCopy', + '\\RenewCommandCopy', + '\\DeclareCommandCopy', ), - field('declaration', choice($.curly_group_command_name, $.command_name)), - field('spec', $.curly_group_spec), - field('implementation', $.curly_group), ), + field('declaration', choice($.curly_group_command_name, $.command_name)), + field('implementation', $.curly_group_command_name), ), old_command_definition: $ => @@ -989,7 +1001,7 @@ module.exports = grammar({ ), environment_definition: $ => - choice($._environment_definition, $._newer_environment_definition), + choice($._environment_definition, $._newer_environment_definition, $._new_environment_copy), _environment_definition: $ => seq( @@ -1023,6 +1035,20 @@ module.exports = grammar({ field('end', $.curly_group_impl), ), + _new_environment_copy: $ => + seq( + field( + 'command', + choice( + '\\NewEnvironmentCopy', + '\\RenewEnvironmentCopy', + '\\DeclareEnvironmentCopy', + ), + ), + field('name', $.curly_group_text), + field('name', $.curly_group_text), + ), + glossary_entry_definition: $ => seq( field('command', '\\newglossaryentry'), diff --git a/src/grammar.json b/src/grammar.json index 72f9a69eb..093c2441d 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -5309,6 +5309,10 @@ { "type": "SYMBOL", "name": "_newer_command_definition" + }, + { + "type": "SYMBOL", + "name": "_new_command_copy" } ] }, @@ -5429,87 +5433,134 @@ ] }, "_newer_command_definition": { - "type": "PREC_RIGHT", - "value": 0, - "content": { - "type": "SEQ", - "members": [ - { - "type": "FIELD", - "name": "command", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "\\NewDocumentCommand" - }, - { - "type": "STRING", - "value": "\\RenewDocumentCommand" - }, - { - "type": "STRING", - "value": "\\ProvideDocumentCommand" - }, - { - "type": "STRING", - "value": "\\DeclareDocumentCommand" - }, - { - "type": "STRING", - "value": "\\NewExpandableDocumentCommand" - }, - { - "type": "STRING", - "value": "\\RenewExpandableDocumentCommand" - }, - { - "type": "STRING", - "value": "\\ProvideExpandableDocumentCommand" - }, - { - "type": "STRING", - "value": "\\DeclareExpandableDocumentCommand" - } - ] - } - }, - { - "type": "FIELD", - "name": "declaration", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "curly_group_command_name" - }, - { - "type": "SYMBOL", - "name": "command_name" - } - ] - } - }, - { - "type": "FIELD", - "name": "spec", - "content": { - "type": "SYMBOL", - "name": "curly_group_spec" - } - }, - { - "type": "FIELD", - "name": "implementation", - "content": { - "type": "SYMBOL", - "name": "curly_group" - } + "type": "SEQ", + "members": [ + { + "type": "FIELD", + "name": "command", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "\\NewDocumentCommand" + }, + { + "type": "STRING", + "value": "\\RenewDocumentCommand" + }, + { + "type": "STRING", + "value": "\\ProvideDocumentCommand" + }, + { + "type": "STRING", + "value": "\\DeclareDocumentCommand" + }, + { + "type": "STRING", + "value": "\\NewExpandableDocumentCommand" + }, + { + "type": "STRING", + "value": "\\RenewExpandableDocumentCommand" + }, + { + "type": "STRING", + "value": "\\ProvideExpandableDocumentCommand" + }, + { + "type": "STRING", + "value": "\\DeclareExpandableDocumentCommand" + } + ] } - ] - } + }, + { + "type": "FIELD", + "name": "declaration", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "curly_group_command_name" + }, + { + "type": "SYMBOL", + "name": "command_name" + } + ] + } + }, + { + "type": "FIELD", + "name": "spec", + "content": { + "type": "SYMBOL", + "name": "curly_group_spec" + } + }, + { + "type": "FIELD", + "name": "implementation", + "content": { + "type": "SYMBOL", + "name": "curly_group" + } + } + ] + }, + "_new_command_copy": { + "type": "SEQ", + "members": [ + { + "type": "FIELD", + "name": "command", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "\\NewCommandCopy" + }, + { + "type": "STRING", + "value": "\\RenewCommandCopy" + }, + { + "type": "STRING", + "value": "\\DeclareCommandCopy" + } + ] + } + }, + { + "type": "FIELD", + "name": "declaration", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "curly_group_command_name" + }, + { + "type": "SYMBOL", + "name": "command_name" + } + ] + } + }, + { + "type": "FIELD", + "name": "implementation", + "content": { + "type": "SYMBOL", + "name": "curly_group_command_name" + } + } + ] }, "old_command_definition": { "type": "SEQ", @@ -5683,6 +5734,10 @@ { "type": "SYMBOL", "name": "_newer_environment_definition" + }, + { + "type": "SYMBOL", + "name": "_new_environment_copy" } ] }, @@ -5810,6 +5865,48 @@ } ] }, + "_new_environment_copy": { + "type": "SEQ", + "members": [ + { + "type": "FIELD", + "name": "command", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "\\NewEnvironmentCopy" + }, + { + "type": "STRING", + "value": "\\RenewEnvironmentCopy" + }, + { + "type": "STRING", + "value": "\\DeclareEnvironmentCopy" + } + ] + } + }, + { + "type": "FIELD", + "name": "name", + "content": { + "type": "SYMBOL", + "name": "curly_group_text" + } + }, + { + "type": "FIELD", + "name": "name", + "content": { + "type": "SYMBOL", + "name": "curly_group_text" + } + } + ] + }, "glossary_entry_definition": { "type": "SEQ", "members": [ diff --git a/src/node-types.json b/src/node-types.json index 8323475a4..44200e369 100644 --- a/src/node-types.json +++ b/src/node-types.json @@ -3184,7 +3184,7 @@ }, "begin": { "multiple": false, - "required": true, + "required": false, "types": [ { "type": "curly_group_impl", @@ -3200,10 +3200,18 @@ "type": "\\DeclareDocumentEnvironment", "named": false }, + { + "type": "\\DeclareEnvironmentCopy", + "named": false + }, { "type": "\\NewDocumentEnvironment", "named": false }, + { + "type": "\\NewEnvironmentCopy", + "named": false + }, { "type": "\\ProvideDocumentEnvironment", "named": false @@ -3212,6 +3220,10 @@ "type": "\\RenewDocumentEnvironment", "named": false }, + { + "type": "\\RenewEnvironmentCopy", + "named": false + }, { "type": "\\newenvironment", "named": false @@ -3224,7 +3236,7 @@ }, "end": { "multiple": false, - "required": true, + "required": false, "types": [ { "type": "curly_group_impl", @@ -3233,7 +3245,7 @@ ] }, "name": { - "multiple": false, + "multiple": true, "required": true, "types": [ { @@ -5302,6 +5314,10 @@ "multiple": false, "required": true, "types": [ + { + "type": "\\DeclareCommandCopy", + "named": false + }, { "type": "\\DeclareDocumentCommand", "named": false @@ -5326,6 +5342,10 @@ "type": "\\DeclareRobustCommand*", "named": false }, + { + "type": "\\NewCommandCopy", + "named": false + }, { "type": "\\NewDocumentCommand", "named": false @@ -5342,6 +5362,10 @@ "type": "\\ProvideExpandableDocumentCommand", "named": false }, + { + "type": "\\RenewCommandCopy", + "named": false + }, { "type": "\\RenewDocumentCommand", "named": false @@ -5407,6 +5431,10 @@ { "type": "curly_group", "named": true + }, + { + "type": "curly_group_command_name", + "named": true } ] }, @@ -8499,6 +8527,10 @@ "type": "\\Crefrange*", "named": false }, + { + "type": "\\DeclareCommandCopy", + "named": false + }, { "type": "\\DeclareDocumentCommand", "named": false @@ -8507,6 +8539,10 @@ "type": "\\DeclareDocumentEnvironment", "named": false }, + { + "type": "\\DeclareEnvironmentCopy", + "named": false + }, { "type": "\\DeclareExpandableDocumentCommand", "named": false @@ -8671,6 +8707,10 @@ "type": "\\Glsuservi", "named": false }, + { + "type": "\\NewCommandCopy", + "named": false + }, { "type": "\\NewDocumentCommand", "named": false @@ -8679,6 +8719,10 @@ "type": "\\NewDocumentEnvironment", "named": false }, + { + "type": "\\NewEnvironmentCopy", + "named": false + }, { "type": "\\NewExpandableDocumentCommand", "named": false @@ -8711,6 +8755,10 @@ "type": "\\Pvolcite", "named": false }, + { + "type": "\\RenewCommandCopy", + "named": false + }, { "type": "\\RenewDocumentCommand", "named": false @@ -8719,6 +8767,10 @@ "type": "\\RenewDocumentEnvironment", "named": false }, + { + "type": "\\RenewEnvironmentCopy", + "named": false + }, { "type": "\\RenewExpandableDocumentCommand", "named": false diff --git a/test/corpus/commands.txt b/test/corpus/commands.txt index b6d2b2cb4..3e82b8f77 100644 --- a/test/corpus/commands.txt +++ b/test/corpus/commands.txt @@ -195,6 +195,21 @@ Command definition with optional argument (xparse) (placeholder) (placeholder))))) +================================================================================ +Command copy (of command defined in grammar which requires a following node) +================================================================================ + +\NewCommandCopy{\foo}{\ref} + +-------------------------------------------------------------------------------- + +(source_file + (new_command_definition + (curly_group_command_name + (command_name)) + (curly_group_command_name + (command_name)))) + ================================================================================ Author command ================================================================================