From 7ee50b2868d12df6fe36d3e4b42c40bd349a0a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=C3=A1=C5=A1=20Dujava?= <19737748+jdujava@users.noreply.github.com> Date: Wed, 24 Jul 2024 16:33:35 +0200 Subject: [PATCH] Reorder and refactor code (#152) Signed-off-by: Jonas Dujava --- grammar.js | 67 ++++----- src/grammar.json | 349 ++++++++++++++++++++++++----------------------- 2 files changed, 215 insertions(+), 201 deletions(-) diff --git a/grammar.js b/grammar.js index a63845406..9cce7884a 100644 --- a/grammar.js +++ b/grammar.js @@ -104,10 +104,7 @@ module.exports = grammar({ $.block_comment, $._command, $.text, - $.displayed_equation, - $.inline_formula, - $.math_delimiter, - $.text_mode, + $._math_content, '(', ')', ), @@ -433,6 +430,14 @@ module.exports = grammar({ //--- Math + _math_content: $ => + choice( + $.displayed_equation, + $.inline_formula, + $.math_delimiter, + $.text_mode, + ), + displayed_equation: $ => prec.left( seq(choice('$$', '\\['), repeat($._root_content), choice('$$', '\\]')), @@ -443,6 +448,32 @@ module.exports = grammar({ seq(choice('$', '\\('), repeat($._root_content), choice('$', '\\)')), ), + _math_delimiter_part: $ => + choice($.word, $.command_name, '[', ']', '(', ')', '|'), + + math_delimiter: $ => + prec.left( + seq( + field( + 'left_command', + choice('\\left', '\\bigl', '\\Bigl', '\\biggl', '\\Biggl'), + ), + field('left_delimiter', $._math_delimiter_part), + repeat($._root_content), + field( + 'right_command', + choice('\\right', '\\bigr', '\\Bigr', '\\biggr', '\\Biggr'), + ), + field('right_delimiter', $._math_delimiter_part), + ), + ), + + text_mode: $ => + seq( + field('command', choice('\\text', '\\intertext', '\\shortintertext')), + field('content', $.curly_group), + ), + //--- Environments begin: $ => @@ -942,26 +973,6 @@ module.exports = grammar({ field('implementation', $.command_name), ), - _math_delimiter_part: $ => - choice($.word, $.command_name, '[', ']', '(', ')', '|'), - - math_delimiter: $ => - prec.left( - seq( - field( - 'left_command', - choice('\\left', '\\bigl', '\\Bigl', '\\biggl', '\\Biggl'), - ), - field('left_delimiter', $._math_delimiter_part), - repeat($._root_content), - field( - 'right_command', - choice('\\right', '\\bigr', '\\Bigr', '\\biggr', '\\Biggr'), - ), - field('right_delimiter', $._math_delimiter_part), - ), - ), - paired_delimiter_definition: $ => prec.right( seq( @@ -1192,7 +1203,7 @@ module.exports = grammar({ seq( field( 'command', - choice('\\color', '\\pagecolor', '\\textcolor', '\\mathcolor','\\colorbox'), + choice('\\color', '\\pagecolor', '\\textcolor', '\\mathcolor', '\\colorbox'), ), choice( field('name', $.curly_group_text), @@ -1211,12 +1222,6 @@ module.exports = grammar({ field('paths', $.curly_group_path_list), ), - text_mode: $ => - seq( - field('command', choice('\\text', '\\intertext', '\\shortintertext')), - field('content', $.curly_group), - ), - hyperlink: $ => prec.right( seq( diff --git a/src/grammar.json b/src/grammar.json index dac43ab98..72f9a69eb 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -194,19 +194,7 @@ }, { "type": "SYMBOL", - "name": "displayed_equation" - }, - { - "type": "SYMBOL", - "name": "inline_formula" - }, - { - "type": "SYMBOL", - "name": "math_delimiter" - }, - { - "type": "SYMBOL", - "name": "text_mode" + "name": "_math_content" }, { "type": "STRING", @@ -1918,6 +1906,27 @@ ] } }, + "_math_content": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "displayed_equation" + }, + { + "type": "SYMBOL", + "name": "inline_formula" + }, + { + "type": "SYMBOL", + "name": "math_delimiter" + }, + { + "type": "SYMBOL", + "name": "text_mode" + } + ] + }, "displayed_equation": { "type": "PREC_LEFT", "value": 0, @@ -2002,6 +2011,163 @@ ] } }, + "_math_delimiter_part": { + "type": "CHOICE", + "members": [ + { + "type": "SYMBOL", + "name": "word" + }, + { + "type": "SYMBOL", + "name": "command_name" + }, + { + "type": "STRING", + "value": "[" + }, + { + "type": "STRING", + "value": "]" + }, + { + "type": "STRING", + "value": "(" + }, + { + "type": "STRING", + "value": ")" + }, + { + "type": "STRING", + "value": "|" + } + ] + }, + "math_delimiter": { + "type": "PREC_LEFT", + "value": 0, + "content": { + "type": "SEQ", + "members": [ + { + "type": "FIELD", + "name": "left_command", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "\\left" + }, + { + "type": "STRING", + "value": "\\bigl" + }, + { + "type": "STRING", + "value": "\\Bigl" + }, + { + "type": "STRING", + "value": "\\biggl" + }, + { + "type": "STRING", + "value": "\\Biggl" + } + ] + } + }, + { + "type": "FIELD", + "name": "left_delimiter", + "content": { + "type": "SYMBOL", + "name": "_math_delimiter_part" + } + }, + { + "type": "REPEAT", + "content": { + "type": "SYMBOL", + "name": "_root_content" + } + }, + { + "type": "FIELD", + "name": "right_command", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "\\right" + }, + { + "type": "STRING", + "value": "\\bigr" + }, + { + "type": "STRING", + "value": "\\Bigr" + }, + { + "type": "STRING", + "value": "\\biggr" + }, + { + "type": "STRING", + "value": "\\Biggr" + } + ] + } + }, + { + "type": "FIELD", + "name": "right_delimiter", + "content": { + "type": "SYMBOL", + "name": "_math_delimiter_part" + } + } + ] + } + }, + "text_mode": { + "type": "SEQ", + "members": [ + { + "type": "FIELD", + "name": "command", + "content": { + "type": "CHOICE", + "members": [ + { + "type": "STRING", + "value": "\\text" + }, + { + "type": "STRING", + "value": "\\intertext" + }, + { + "type": "STRING", + "value": "\\shortintertext" + } + ] + } + }, + { + "type": "FIELD", + "name": "content", + "content": { + "type": "SYMBOL", + "name": "curly_group" + } + } + ] + }, "begin": { "type": "PREC_RIGHT", "value": 0, @@ -5407,129 +5573,6 @@ } ] }, - "_math_delimiter_part": { - "type": "CHOICE", - "members": [ - { - "type": "SYMBOL", - "name": "word" - }, - { - "type": "SYMBOL", - "name": "command_name" - }, - { - "type": "STRING", - "value": "[" - }, - { - "type": "STRING", - "value": "]" - }, - { - "type": "STRING", - "value": "(" - }, - { - "type": "STRING", - "value": ")" - }, - { - "type": "STRING", - "value": "|" - } - ] - }, - "math_delimiter": { - "type": "PREC_LEFT", - "value": 0, - "content": { - "type": "SEQ", - "members": [ - { - "type": "FIELD", - "name": "left_command", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "\\left" - }, - { - "type": "STRING", - "value": "\\bigl" - }, - { - "type": "STRING", - "value": "\\Bigl" - }, - { - "type": "STRING", - "value": "\\biggl" - }, - { - "type": "STRING", - "value": "\\Biggl" - } - ] - } - }, - { - "type": "FIELD", - "name": "left_delimiter", - "content": { - "type": "SYMBOL", - "name": "_math_delimiter_part" - } - }, - { - "type": "REPEAT", - "content": { - "type": "SYMBOL", - "name": "_root_content" - } - }, - { - "type": "FIELD", - "name": "right_command", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "\\right" - }, - { - "type": "STRING", - "value": "\\bigr" - }, - { - "type": "STRING", - "value": "\\Bigr" - }, - { - "type": "STRING", - "value": "\\biggr" - }, - { - "type": "STRING", - "value": "\\Biggr" - } - ] - } - }, - { - "type": "FIELD", - "name": "right_delimiter", - "content": { - "type": "SYMBOL", - "name": "_math_delimiter_part" - } - } - ] - } - }, "paired_delimiter_definition": { "type": "PREC_RIGHT", "value": 0, @@ -6630,40 +6673,6 @@ } ] }, - "text_mode": { - "type": "SEQ", - "members": [ - { - "type": "FIELD", - "name": "command", - "content": { - "type": "CHOICE", - "members": [ - { - "type": "STRING", - "value": "\\text" - }, - { - "type": "STRING", - "value": "\\intertext" - }, - { - "type": "STRING", - "value": "\\shortintertext" - } - ] - } - }, - { - "type": "FIELD", - "name": "content", - "content": { - "type": "SYMBOL", - "name": "curly_group" - } - } - ] - }, "hyperlink": { "type": "PREC_RIGHT", "value": 0,