Skip to content

Commit c6cbaf9

Browse files
committed
Tex: Allow omitting {} for some macros
Macros like \newcommand can be written both as \newcommand{\foo}{bar} and \newcommand\foo{bar} Unify \def parsing and \newcommand parsing so that when strategy type is defined as '\\', the {} pair may or may not be present.
1 parent af6af4d commit c6cbaf9

File tree

4 files changed

+14
-41
lines changed

4 files changed

+14
-41
lines changed

Units/parser-tex.r/newcommand.d/expected.tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
\\mysection0 input.tex /^\\newcommand{\\mysection0}{\\section{#1}}$/;" C
55
\\mysection1 input.tex /^\\newcommand{\\mysection1}[1]{\\section{#1}}$/;" C
66
\\mysection2 input.tex /^\\newcommand{\\mysection2}[1][1]{\\section{#1}}$/;" C
7+
\\mysection3 input.tex /^\\newcommand\\mysection3[1][1]{\\section{#1}}$/;" C
78
\\op input.tex /^\\DeclareMathOperator{\\op}{foo}$/;" o

Units/parser-tex.r/newcommand.d/input.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
\newcommand{\mysection0}{\section{#1}}
33
\newcommand{\mysection1}[1]{\section{#1}}
44
\newcommand{\mysection2}[1][1]{\section{#1}}
5+
\newcommand\mysection3[1][1]{\section{#1}}
56
\renewcommand{\foo}{\section{#1}}
67
\providecommand{\bar}{\section{#1}}
78
\def\baz{\section{#1}}

parsers/tex.c

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,15 @@ static bool parseWithStrategy (tokenInfo *token,
622622
}
623623
else if (s->type == '*' && isType (token, '*'))
624624
next_token = true;
625-
else if ((s->type == '{' && isType (token, '{')) || (s->type == '\\' && isType (token, TOKEN_IDENTIFIER)))
625+
else if (((s->type == '{' || s->type == '\\') && isType (token, '{')) ||
626+
(s->type == '\\' && isType (token, TOKEN_IDENTIFIER)))
626627
{
627628
int depth = 1;
629+
bool missing_parens = isType (token, TOKEN_IDENTIFIER);
628630

629631
next_token = true;
630632

631-
if (s->type == '{' && !readToken (token))
633+
if (!missing_parens && !readToken (token))
632634
{
633635
eof = true;
634636
break;
@@ -638,7 +640,7 @@ static bool parseWithStrategy (tokenInfo *token,
638640
copyToken (name, token);
639641
vStringClear (name->string);
640642
}
641-
if (s->type == '\\')
643+
if (missing_parens)
642644
{
643645
vStringCat (name->string, token->string);
644646
depth = 0;
@@ -831,10 +833,12 @@ static bool parseNewcommandFull (tokenInfo *const token, bool *tokenUnprocessed,
831833
{
832834
bool eof = false;
833835

834-
/* \newcommand {cmd}[args][opt]{def} */
836+
/* \newcommand{cmd}[args][opt]{def} */
837+
/* \newcommand\cmd[args][opt]{def} */
838+
/* \def\cmd{replacement} */
835839
struct TexParseStrategy strategy [] = {
836840
{
837-
.type = '{',
841+
.type = '\\',
838842
.flags = 0,
839843
.kindIndex = kind,
840844
.roleIndex = ROLE_DEFINITION_INDEX,
@@ -875,37 +879,6 @@ static bool parseNewcommand (tokenInfo *const token, bool *tokenUnprocessed)
875879
return parseNewcommandFull (token, tokenUnprocessed, TEXTAG_COMMAND);
876880
}
877881

878-
static bool parseDef (tokenInfo *const token, bool *tokenUnprocessed)
879-
{
880-
bool eof = false;
881-
882-
/* \def\cmd{replacement} */
883-
struct TexParseStrategy strategy [] = {
884-
{
885-
.type = '\\',
886-
.flags = 0,
887-
.kindIndex = TEXTAG_COMMAND,
888-
.roleIndex = ROLE_DEFINITION_INDEX,
889-
.name = NULL,
890-
.unique = false,
891-
},
892-
{
893-
.type = '{',
894-
.flags = 0,
895-
.kindIndex = KIND_GHOST_INDEX,
896-
.name = NULL,
897-
},
898-
{
899-
.type = 0
900-
}
901-
};
902-
903-
if (parseWithStrategy (token, strategy, tokenUnprocessed))
904-
eof = true;
905-
906-
return eof;
907-
}
908-
909882
static bool parseNewEnvironment (tokenInfo *const token, bool *tokenUnprocessed)
910883
{
911884
bool eof = false;
@@ -1088,10 +1061,8 @@ static void parseTexFile (tokenInfo *const token)
10881061
case KEYWORD_newcommand:
10891062
case KEYWORD_renewcommand:
10901063
case KEYWORD_providecommand:
1091-
eof = parseNewcommand (token, &tokenUnprocessed);
1092-
break;
10931064
case KEYWORD_def:
1094-
eof = parseDef (token, &tokenUnprocessed);
1065+
eof = parseNewcommand (token, &tokenUnprocessed);
10951066
break;
10961067
case KEYWORD_declaremathoperator:
10971068
eof = parseNewcommandFull (token, &tokenUnprocessed, TEXTAG_OPERATOR);

parsers/tex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ enum TexNameFlag {
4242
};
4343

4444
struct TexParseStrategy {
45-
/* Expected token type '<', '[', '*', and '{' are supported.
46-
* 0 means the end of strategies.
45+
/* Expected token type '<', '[', '*', '{', and '\\' are supported.
46+
* 0 means the end of strategies. '\\' means {} pair may be omitted.
4747
*
4848
* A string between <>, [], or {} (pairs) can be tagged or store to
4949
* a vString. See kindIndex and name field of this structure.

0 commit comments

Comments
 (0)