Skip to content

Commit

Permalink
Tex: introduce command kind to capture \newcommand
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Feb 21, 2020
1 parent 512803a commit 3e2c7ce
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Units/parser-tex.r/newcommand.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\\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
9 changes: 9 additions & 0 deletions Units/parser-tex.r/newcommand.d/input.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\documentclass{article}
\newcommand{\mysection0}{\section{#1}}
\newcommand{\mysection1}[1]{\section{#1}}
\newcommand{\mysection2}[1][1]{\section{#1}}
\begin{document}
\mysection0{ABC}
\mysection1{EFG}
\mysection2{HIJ}
\end{document}
2 changes: 2 additions & 0 deletions Units/tex-review-needed.r/3526726.tex.t/expected.tags
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,7 @@ Why does snort report ``Packet loss statistics are unavailable under Linux?'' in
Why does the `error deleting alert' message occur when attempting to delete an alert with BASE? input.tex /^\\subsection{Why does the `error deleting alert' message occur when attempting to delete an aler/;" u section:Problems
Why does the portscan plugin log ``stealth'' packets even though the host is in the portscan-ignorehosts list? input.tex /^\\subsection{Why does the portscan plugin log ``stealth'' packets even though the host is in the/;" u section:Configuring Snort
Why does the program generate alerts on packets that have pass rules? input.tex /^\\subsection{Why does the program generate alerts on packets that have pass rules? }$/;" u section:Rules and Alerts
\\myquote input.tex /^\\newcommand{\\myquote}[1]{\\begin{quote}#1\\end{quote}}$/;" C
\\myref input.tex /^\\newcommand{\\myref}[1]{(see FAQ \\ref{#1})}$/;" C
stealth input.tex /^\\subsection{How do I setup snort on a `stealth' interface? }\\label{stealth}$/;" l
stream4 input.tex /^\\label{stream4}$/;" l
5 changes: 5 additions & 0 deletions Units/tex-review-needed.r/bug2886870.tex.t/expected.tags
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Tables input.tex /^\\section{Tables}$/;" s
Test for ctags input.tex /^\\subsubsection{Test for ctags}$/;" b subsection:Special Symbols""Common Greek letters
\\color{red}Use of Color input.tex /^\\section{\\color{red}Use of Color}$/;" s
\\label{morefig}Subfigures input.tex /^\\section{\\label{morefig}Subfigures}$/;" s
\\lb input.tex /^ \\newcommand{\\lb}{{\\langle}}$/;" C
\\lb input.tex /^\\newcommand{\\lb}{{\\langle}}$/;" C
\\rb input.tex /^ \\newcommand{\\rb}{{\\rangle}}$/;" C
\\rb input.tex /^\\newcommand{\\rb}{{\\rangle}}$/;" C
\\rv input.tex /^\\newcommand{\\rv}{\\textbf{r}}$/;" C
eq:fine input.tex /^I = \\! \\int_{-\\infty}^\\infty f(x)\\,dx \\label{eq:fine}.$/;" l
eq:ising input.tex /^\\label{eq:ising}$/;" l
eq:mdiv input.tex /^\\label{eq:mdiv}$/;" l
Expand Down
5 changes: 5 additions & 0 deletions Units/tex-review-needed.r/intro_orig.tex.t/expected.tags
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Special symbols input.tex /^\\subsection{Special symbols}$/;" u section:Special
Tables input.tex /^\\section{Tables}$/;" s
\\color{red}Use of Color input.tex /^\\section{\\color{red}Use of Color}$/;" s
\\label{morefig}Subfigures input.tex /^\\section{\\label{morefig}Subfigures}$/;" s
\\lb input.tex /^ \\newcommand{\\lb}{{\\langle}}$/;" C
\\lb input.tex /^\\newcommand{\\lb}{{\\langle}}$/;" C
\\rb input.tex /^ \\newcommand{\\rb}{{\\rangle}}$/;" C
\\rb input.tex /^\\newcommand{\\rb}{{\\rangle}}$/;" C
\\rv input.tex /^\\newcommand{\\rv}{\\textbf{r}}$/;" C
eq:fine input.tex /^I = \\! \\int_{-\\infty}^\\infty f(x)\\,dx \\label{eq:fine}.$/;" l
eq:ising input.tex /^\\label{eq:ising}$/;" l
eq:mdiv input.tex /^\\label{eq:mdiv}$/;" l
Expand Down
48 changes: 48 additions & 0 deletions parsers/tex.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ enum eKeywordId {
KEYWORD_end,
KEYWORD_bibitem,
KEYWORD_bibliography,
KEYWORD_newcommand,
};
typedef int keywordId; /* to allow KEYWORD_NONE */

Expand Down Expand Up @@ -114,6 +115,7 @@ typedef enum {
TEXTAG_LABEL,
TEXTAG_XINPUT,
TEXTAG_BIBITEM,
TEXTAG_COMMAND,
TEXTAG_COUNT
} texKind;

Expand Down Expand Up @@ -144,6 +146,7 @@ static kindDefinition TexKinds [] = {
{ true, 'i', "xinput", "external input files",
.referenceOnly = true, ATTACH_ROLES(TexInputRoles) },
{ true, 'B', "bibitem", "bibliography items" },
{ true, 'C', "command", "command created with \\newcommand" },
};

static const keywordTable TexKeywordTable [] = {
Expand All @@ -162,6 +165,7 @@ static const keywordTable TexKeywordTable [] = {
{ "end", KEYWORD_end },
{ "bibitem", KEYWORD_bibitem },
{ "bibliography", KEYWORD_bibliography },
{ "newcommand", KEYWORD_newcommand },
};

/*
Expand Down Expand Up @@ -735,6 +739,48 @@ static bool parseEnv (tokenInfo *const token, bool begin, bool *tokenUnprocessed

}

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

/* \newcommand {cmd}[args][opt]{def} */
struct TexParseStrategy strategy [] = {
{
.type = '{',
.flags = 0,
.kindIndex = TEXTAG_COMMAND,
.roleIndex = ROLE_DEFINITION_INDEX,
.name = NULL,
},
{
.type = '[',
.flags = TEX_NAME_FLAG_OPTIONAL,
.kindIndex = KIND_GHOST_INDEX,
.name = NULL,
},
{
.type = '[',
.flags = TEX_NAME_FLAG_OPTIONAL,
.kindIndex = KIND_GHOST_INDEX,
.name = NULL,
},
{
.type = '{',
.flags = 0,
.kindIndex = KIND_GHOST_INDEX,
.name = NULL,
},
{
.type = 0
}
};

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

return eof;
}

static void parseTexFile (tokenInfo *const token)
{
bool eof = false;
Expand Down Expand Up @@ -797,6 +843,8 @@ static void parseTexFile (tokenInfo *const token)
case KEYWORD_bibliography:
eof = parseTagFull (token, TEXTAG_XINPUT, TEX_XINPUT_BIBLIOGRAPHY,
false, &tokenUnprocessed);
case KEYWORD_newcommand:
eof = parseNewcommand (token, &tokenUnprocessed);
break;
default:
break;
Expand Down

0 comments on commit 3e2c7ce

Please sign in to comment.