Skip to content

Commit

Permalink
Tex: Allow omitting {} for some macros
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
techee committed Dec 29, 2021
1 parent af6af4d commit c6cbaf9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 41 deletions.
1 change: 1 addition & 0 deletions Units/parser-tex.r/newcommand.d/expected.tags
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
\\mysection0 input.tex /^\\newcommand{\\mysection0}{\\section{#1}}$/;" C
\\mysection1 input.tex /^\\newcommand{\\mysection1}[1]{\\section{#1}}$/;" C
\\mysection2 input.tex /^\\newcommand{\\mysection2}[1][1]{\\section{#1}}$/;" C
\\mysection3 input.tex /^\\newcommand\\mysection3[1][1]{\\section{#1}}$/;" C
\\op input.tex /^\\DeclareMathOperator{\\op}{foo}$/;" o
1 change: 1 addition & 0 deletions Units/parser-tex.r/newcommand.d/input.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
\newcommand{\mysection0}{\section{#1}}
\newcommand{\mysection1}[1]{\section{#1}}
\newcommand{\mysection2}[1][1]{\section{#1}}
\newcommand\mysection3[1][1]{\section{#1}}
\renewcommand{\foo}{\section{#1}}
\providecommand{\bar}{\section{#1}}
\def\baz{\section{#1}}
Expand Down
49 changes: 10 additions & 39 deletions parsers/tex.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,13 +622,15 @@ static bool parseWithStrategy (tokenInfo *token,
}
else if (s->type == '*' && isType (token, '*'))
next_token = true;
else if ((s->type == '{' && isType (token, '{')) || (s->type == '\\' && isType (token, TOKEN_IDENTIFIER)))
else if (((s->type == '{' || s->type == '\\') && isType (token, '{')) ||
(s->type == '\\' && isType (token, TOKEN_IDENTIFIER)))
{
int depth = 1;
bool missing_parens = isType (token, TOKEN_IDENTIFIER);

next_token = true;

if (s->type == '{' && !readToken (token))
if (!missing_parens && !readToken (token))
{
eof = true;
break;
Expand All @@ -638,7 +640,7 @@ static bool parseWithStrategy (tokenInfo *token,
copyToken (name, token);
vStringClear (name->string);
}
if (s->type == '\\')
if (missing_parens)
{
vStringCat (name->string, token->string);
depth = 0;
Expand Down Expand Up @@ -831,10 +833,12 @@ static bool parseNewcommandFull (tokenInfo *const token, bool *tokenUnprocessed,
{
bool eof = false;

/* \newcommand {cmd}[args][opt]{def} */
/* \newcommand{cmd}[args][opt]{def} */
/* \newcommand\cmd[args][opt]{def} */
/* \def\cmd{replacement} */
struct TexParseStrategy strategy [] = {
{
.type = '{',
.type = '\\',
.flags = 0,
.kindIndex = kind,
.roleIndex = ROLE_DEFINITION_INDEX,
Expand Down Expand Up @@ -875,37 +879,6 @@ static bool parseNewcommand (tokenInfo *const token, bool *tokenUnprocessed)
return parseNewcommandFull (token, tokenUnprocessed, TEXTAG_COMMAND);
}

static bool parseDef (tokenInfo *const token, bool *tokenUnprocessed)
{
bool eof = false;

/* \def\cmd{replacement} */
struct TexParseStrategy strategy [] = {
{
.type = '\\',
.flags = 0,
.kindIndex = TEXTAG_COMMAND,
.roleIndex = ROLE_DEFINITION_INDEX,
.name = NULL,
.unique = false,
},
{
.type = '{',
.flags = 0,
.kindIndex = KIND_GHOST_INDEX,
.name = NULL,
},
{
.type = 0
}
};

if (parseWithStrategy (token, strategy, tokenUnprocessed))
eof = true;

return eof;
}

static bool parseNewEnvironment (tokenInfo *const token, bool *tokenUnprocessed)
{
bool eof = false;
Expand Down Expand Up @@ -1088,10 +1061,8 @@ static void parseTexFile (tokenInfo *const token)
case KEYWORD_newcommand:
case KEYWORD_renewcommand:
case KEYWORD_providecommand:
eof = parseNewcommand (token, &tokenUnprocessed);
break;
case KEYWORD_def:
eof = parseDef (token, &tokenUnprocessed);
eof = parseNewcommand (token, &tokenUnprocessed);
break;
case KEYWORD_declaremathoperator:
eof = parseNewcommandFull (token, &tokenUnprocessed, TEXTAG_OPERATOR);
Expand Down
4 changes: 2 additions & 2 deletions parsers/tex.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ enum TexNameFlag {
};

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

0 comments on commit c6cbaf9

Please sign in to comment.