Skip to content

Commit 44567cf

Browse files
authored
[LaTeX] Fix macro definition termination (#4402)
* [LaTeX] Fix macro definition termination This commit... 1. fixes incomplete macro definitions, missing macro names. 2. fixes macro definition termination. Parameter-text may contain any text until `{` indicating start of replacement text. They are used as delimiter to specify how macro invocation must look like. * [LaTeX] Add bailout from incomplete brackets * [LaTeX] Fix optional macro arguments This commit removes tex commands from brackets in macro parameter text, as no commands are supported (evaluated) within. * [LaTeX] Fix nested groups in macro parameter text
1 parent 6cdd97a commit 44567cf

File tree

3 files changed

+129
-26
lines changed

3 files changed

+129
-26
lines changed

LaTeX/LaTeX.sublime-syntax

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ contexts:
5454
- include: braces
5555
- match: '[A-Za-z[:digit:]-]*(?=\s*\=)'
5656
scope: variable.parameter.bracket.latex
57+
- include: stray-brace-pop
5758

5859
argument-brace:
5960
- meta_scope: meta.group.brace.tex
@@ -109,9 +110,7 @@ contexts:
109110
latex-newcommand-argspec-body:
110111
- clear_scopes: 1
111112
- meta_scope: meta.function.parameters.latex
112-
- match: \]
113-
scope: punctuation.definition.group.bracket.end.latex
114-
pop: 2
113+
- include: latex-newcommand-optarg-end
115114
- match: \d+\b
116115
scope: meta.number.integer.decimal.latex constant.numeric.value.latex
117116
- include: general-constants
@@ -128,13 +127,18 @@ contexts:
128127
latex-newcommand-optarg-body:
129128
- clear_scopes: 1
130129
- meta_scope: meta.function.parameters.default-value.latex
131-
- match: \]
132-
scope: punctuation.definition.group.bracket.end.latex
133-
pop: 2
130+
- include: latex-newcommand-optarg-end
134131
- include: general-constants
135132
- include: general-commands
136133
- include: macro-braces
137134

135+
latex-newcommand-optarg-end:
136+
- match: \]
137+
scope: punctuation.definition.group.bracket.end.latex
138+
pop: 2
139+
- match: (?=})
140+
pop: 2
141+
138142
latex-newcommand-commandname:
139143
# version one: brace-wrapped command name
140144
- match: (?=\{)

LaTeX/TeX.sublime-syntax

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,18 @@ contexts:
485485
captures:
486486
1: punctuation.definition.backslash.tex
487487
set: def-function-parameters
488-
- include: paragraph-pop
488+
- match: (?=[#\[\{])
489+
set: def-function-parameters
489490
- include: else-pop
490491

491492
def-function-parameters:
492493
- meta_content_scope: meta.function.parameters.tex
493494
- include: def-function-block
494495
- include: def-function-parameter-brackets
495496
- include: macro-parameters
496-
- include: escaped-character
497-
- include: paragraph-pop
497+
- include: general-constants
498+
- include: stray-brace-pop
499+
- include: stray-bracket-pop
498500

499501
def-function-parameter-brackets:
500502
- match: \[
@@ -506,9 +508,25 @@ contexts:
506508
- match: \]
507509
scope: punctuation.definition.group.bracket.end.tex
508510
pop: 1
511+
- include: def-function-parameter-brackets
512+
- include: def-function-parameter-braces
509513
- include: macro-parameters
510514
- include: general-constants
511-
- include: general-commands
515+
- include: stray-brace-pop
516+
517+
def-function-parameter-braces:
518+
- match: \{
519+
scope: punctuation.definition.group.brace.begin.tex
520+
push: def-function-parameter-braces-body
521+
522+
def-function-parameter-braces-body:
523+
- meta_scope: meta.group.brace.tex
524+
- include: brace-group-end
525+
- include: def-function-parameter-brackets
526+
- include: def-function-parameter-braces
527+
- include: macro-parameters
528+
- include: general-constants
529+
- include: stray-bracket-pop
512530

513531
def-function-block:
514532
- match: \{
@@ -723,3 +741,7 @@ contexts:
723741
stray-brace-pop:
724742
- match: (?=})
725743
pop: 1
744+
745+
stray-bracket-pop:
746+
- match: (?=])
747+
pop: 1

LaTeX/tests/syntax_test_tex.tex

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@
3838
% Check the main scopes
3939
\def\macro{replacement}
4040
% ^^^^ meta.function.tex keyword.declaration.function.tex
41-
% ^^^^^^ meta.function.identifier.tex entity.name.function.tex
42-
% ^^^^^^^^^^^^ meta.function.body.tex
43-
44-
% Check punctuation scopes
45-
\def\macro{replacement}
4641
% ^ punctuation.definition.backslash.tex
42+
% ^^^^^^ meta.function.identifier.tex entity.name.function.tex
4743
% ^ punctuation.definition.backslash.tex
44+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
4845
% ^ punctuation.definition.group.brace.begin.tex
4946
% ^ punctuation.definition.group.brace.end.tex
5047

51-
52-
% examples taken from https://en.wikibooks.org/wiki/TeX/def
48+
% Check missing macro name
49+
\def{replacement}
50+
% ^^^^ meta.function.tex keyword.declaration.function.tex
51+
% ^ punctuation.definition.backslash.tex
52+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
53+
% ^ punctuation.definition.group.brace.begin.tex
54+
% ^ punctuation.definition.group.brace.end.tex
5355

5456
% With spaces and parameter specification
5557
\def \foo [#1]#2{The first argument is ``#1'', the second one is ``#2''}
@@ -116,24 +118,77 @@
116118
% hash
117119
\def\macro\#1#1{replacement}
118120
% ^^^^^ meta.function.parameters.tex
121+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
119122
% ^^ constant.character.escape.tex
120-
% ^ - variable.parameter.tex
121123
% ^^ variable.parameter.tex
122124

123125
% open-brace
124126
\def\macro\{#1{replacement}
125127
% ^^^^ meta.function.parameters.tex
128+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
126129
% ^^ constant.character.escape.tex
127130
% ^^ variable.parameter.tex
128-
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
129-
130131

131132
% double escape
132133
\def\macro\\#1{replacement}
133134
% ^^^^ meta.function.parameters.tex
134-
% ^^constant.character.escape.tex
135+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
136+
% ^^ constant.character.newline.tex
135137
% ^^ variable.parameter.tex
136138

139+
% hash in brackets
140+
\def \macro[\#1A]{replacement}
141+
% ^^^^^^ meta.function.parameters.tex meta.group.bracket.tex
142+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
143+
% ^ punctuation.definition.group.bracket.begin.tex
144+
% ^^ constant.character.escape.tex
145+
% ^ punctuation.definition.backslash.tex
146+
% ^^ - variable
147+
% ^ punctuation.definition.group.bracket.end.tex
148+
149+
% open-brace in brackets
150+
\def \macro[\{#1A]{replacement}
151+
% ^^^^^^^ meta.function.parameters.tex meta.group.bracket.tex
152+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
153+
% ^ punctuation.definition.group.bracket.begin.tex
154+
% ^^ constant.character.escape.tex
155+
% ^ punctuation.definition.backslash.tex
156+
% ^^ variable.parameter.tex
157+
% ^ punctuation.definition.group.bracket.end.tex
158+
159+
% double escape in brackets
160+
\def \macro[\\#1A]{replacement}
161+
% ^^^^^^^ meta.function.parameters.tex meta.group.bracket.tex
162+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
163+
% ^ punctuation.definition.group.bracket.begin.tex
164+
% ^^ constant.character.newline.tex
165+
% ^^ variable.parameter.tex
166+
% ^ punctuation.definition.group.bracket.end.tex
167+
168+
% no commands in parameter text
169+
\def \macro\A[\A#1A]{replacement}
170+
% ^^ meta.function.parameters.tex - meta.group
171+
% ^^^^^^^ meta.function.parameters.tex meta.group.bracket.tex
172+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
173+
% ^^ - support - punctuation
174+
% ^ punctuation.definition.group.bracket.begin.tex
175+
% ^^ - support - punctuation
176+
% ^^ variable.parameter.tex
177+
% ^ punctuation.definition.group.bracket.end.tex
178+
179+
% nested brackets in parameter text
180+
\def\macro[#1={#2,{key=[#4,#5]}}] {replacement}
181+
%^^^ meta.function.tex keyword.declaration.function.tex
182+
% ^^^^^^ meta.function.identifier.tex entity.name.function.tex
183+
% ^^^^ meta.function.parameters.tex meta.group.bracket.tex - meta.group meta.group
184+
% ^^^^ meta.function.parameters.tex meta.group.bracket.tex meta.group.brace.tex - meta.group meta.group meta.group
185+
% ^^^^^ meta.function.parameters.tex meta.group.bracket.tex meta.group.brace.tex meta.group.brace.tex - meta.group meta.group meta.group meta.group
186+
% ^^^^^^^ meta.function.parameters.tex meta.group.bracket.tex meta.group.brace.tex meta.group.brace.tex meta.group.bracket.tex
187+
% ^ meta.function.parameters.tex meta.group.bracket.tex meta.group.brace.tex meta.group.brace.tex - meta.group meta.group meta.group meta.group
188+
% ^ meta.function.parameters.tex meta.group.bracket.tex meta.group.brace.tex - meta.group meta.group meta.group
189+
% ^ meta.function.parameters.tex meta.group.bracket.tex - meta.group meta.group
190+
% ^ meta.function.parameters.tex - meta.group
191+
% ^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
137192

138193
% defining special character macros
139194
\def\_{underscore}
@@ -144,14 +199,26 @@
144199
% stop scope for incomplete defs
145200
\def\text
146201

147-
some other text
148-
% ^^^^^^^^^^^^^^ - meta.function%
202+
any delimiter token
203+
%^^^^^^^^^^^^^^^^^^ meta.function.parameters.tex
204+
205+
{replacement}
206+
% <- meta.function.body.tex meta.group.brace.tex punctuation.definition.group.brace.begin.tex
207+
%^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
208+
% ^ punctuation.definition.group.brace.end.tex
149209

150210
% incomplete but with parameters; the { comes too late to open the function definition
151-
\def\text A#1A
211+
\def\text \A#1A
212+
% ^^^^^^ meta.function.parameters.tex
213+
% ^^ - support
214+
% ^^ variable.parameter.tex
152215

153-
{ some other text}
154-
% ^^^^^^^^^^^^^^ - meta.function
216+
{ some other text }
217+
% <- meta.function.parameters.tex
218+
%^ meta.function.parameters.tex
219+
% ^^^^^^^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
220+
% ^ punctuation.definition.group.brace.begin.tex
221+
% ^ punctuation.definition.group.brace.end.tex
155222

156223
% command just prefixed with def. Needs to be picked up as a custom command
157224
\deftext
@@ -247,6 +314,16 @@
247314
% ^^ variable.other.readwrite.tex
248315
% ^^^^^^ keyword.other.math.greek.tex
249316

317+
% incomplete nested macro definitions
318+
\def \macro #1 { \def \incomplete #1 }
319+
% ^^^^^^^^^^^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
320+
% ^ punctuation.definition.group.brace.begin.tex
321+
% ^ punctuation.definition.group.brace.end.tex
322+
323+
\def \macro #1 { \def \incomplete [#1 }
324+
% ^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.body.tex meta.group.brace.tex
325+
% ^ punctuation.definition.group.brace.begin.tex
326+
% ^ punctuation.definition.group.brace.end.tex
250327

251328
% testing incomplete register specification
252329
\def\test{\dimen} other text

0 commit comments

Comments
 (0)