Skip to content

Commit 04d5c98

Browse files
committed
Tex: parse \def\cmd{replacement}
1 parent c359e13 commit 04d5c98

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
\\bar input.tex /^\\providecommand{\\bar}{\\section{#1}}$/;" C
2+
\\baz input.tex /^\\def\\baz{\\section{#1}}$/;" C
23
\\foo input.tex /^\\renewcommand{\\foo}{\\section{#1}}$/;" C
34
\\mysection0 input.tex /^\\newcommand{\\mysection0}{\\section{#1}}$/;" C
45
\\mysection1 input.tex /^\\newcommand{\\mysection1}[1]{\\section{#1}}$/;" C

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
\newcommand{\mysection2}[1][1]{\section{#1}}
55
\renewcommand{\foo}{\section{#1}}
66
\providecommand{\bar}{\section{#1}}
7+
\def\baz{\section{#1}}
78
\begin{document}
89
\mysection0{ABC}
910
\mysection1{EFG}

parsers/tex.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ enum eKeywordId {
6565
KEYWORD_newcommand,
6666
KEYWORD_renewcommand,
6767
KEYWORD_providecommand,
68+
KEYWORD_def,
6869
KEYWORD_newcounter,
6970
};
7071
typedef int keywordId; /* to allow KEYWORD_NONE */
@@ -173,6 +174,7 @@ static const keywordTable TexKeywordTable [] = {
173174
{ "newcommand", KEYWORD_newcommand },
174175
{ "renewcommand", KEYWORD_renewcommand },
175176
{ "providecommand", KEYWORD_providecommand },
177+
{ "def", KEYWORD_def },
176178
{ "newcounter", KEYWORD_newcounter },
177179
};
178180

@@ -606,13 +608,13 @@ static bool parseWithStrategy (tokenInfo *token,
606608
}
607609
else if (s->type == '*' && isType (token, '*'))
608610
next_token = true;
609-
else if (s->type == '{' && isType (token, '{'))
611+
else if ((s->type == '{' && isType (token, '{')) || (s->type == '\\' && isType (token, TOKEN_IDENTIFIER)))
610612
{
611613
int depth = 1;
612614

613615
next_token = true;
614616

615-
if (!readToken (token))
617+
if (s->type == '{' && !readToken (token))
616618
{
617619
eof = true;
618620
break;
@@ -622,6 +624,11 @@ static bool parseWithStrategy (tokenInfo *token,
622624
copyToken (name, token);
623625
vStringClear (name->string);
624626
}
627+
if (s->type == '\\')
628+
{
629+
vStringCat (name->string, token->string);
630+
depth = 0;
631+
}
625632

626633
/* Handle the case the code like \section{} */
627634
if (isType (token, '}'))
@@ -845,6 +852,37 @@ static bool parseNewcommand (tokenInfo *const token, bool *tokenUnprocessed)
845852
return eof;
846853
}
847854

855+
static bool parseDef (tokenInfo *const token, bool *tokenUnprocessed)
856+
{
857+
bool eof = false;
858+
859+
/* \def\cmd{replacement} */
860+
struct TexParseStrategy strategy [] = {
861+
{
862+
.type = '\\',
863+
.flags = 0,
864+
.kindIndex = TEXTAG_COMMAND,
865+
.roleIndex = ROLE_DEFINITION_INDEX,
866+
.name = NULL,
867+
.unique = false,
868+
},
869+
{
870+
.type = '{',
871+
.flags = 0,
872+
.kindIndex = KIND_GHOST_INDEX,
873+
.name = NULL,
874+
},
875+
{
876+
.type = 0
877+
}
878+
};
879+
880+
if (parseWithStrategy (token, strategy, tokenUnprocessed))
881+
eof = true;
882+
883+
return eof;
884+
}
885+
848886
static bool parseNewcounter (tokenInfo *const token, bool *tokenUnprocessed)
849887
{
850888
bool eof = false;
@@ -942,6 +980,9 @@ static void parseTexFile (tokenInfo *const token)
942980
case KEYWORD_providecommand:
943981
eof = parseNewcommand (token, &tokenUnprocessed);
944982
break;
983+
case KEYWORD_def:
984+
eof = parseDef (token, &tokenUnprocessed);
985+
break;
945986
case KEYWORD_newcounter:
946987
eof = parseNewcounter (token, &tokenUnprocessed);
947988
break;

0 commit comments

Comments
 (0)