Skip to content

Commit bcf90ed

Browse files
committed
Tex: parse \newenvironment and \renewenvironment
1 parent 952983d commit bcf90ed

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boxed input.tex /^\\newenvironment{boxed}$/;" e
2+
boxedA input.tex /^\\renewenvironment{boxedA}$/;" e
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
\documentclass{article}
2+
\newenvironment{boxed}
3+
{\begin{center}
4+
\begin{tabular}{|p{0.9\textwidth}|}
5+
\hline\\
6+
}
7+
{
8+
\\\\\hline
9+
\end{tabular}
10+
\end{center}
11+
}
12+
\renewenvironment{boxedA}
13+
{\begin{center}
14+
\begin{tabular}{|p{0.9\textwidth}|}
15+
\hline\\
16+
}
17+
{
18+
\\\\\hline
19+
\end{tabular}
20+
\end{center}
21+
}
22+
\begin{document}
23+
\begin{boxed}
24+
foo
25+
\end{boxed}
26+
\begin{boxedA}
27+
foo
28+
\end{boxedA}
29+
\end{document}

parsers/tex.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ enum eKeywordId {
6767
KEYWORD_providecommand,
6868
KEYWORD_def,
6969
KEYWORD_declaremathoperator,
70+
KEYWORD_newenvironment,
71+
KEYWORD_renewenvironment,
7072
KEYWORD_newcounter,
7173
};
7274
typedef int keywordId; /* to allow KEYWORD_NONE */
@@ -122,6 +124,7 @@ typedef enum {
122124
TEXTAG_BIBITEM,
123125
TEXTAG_COMMAND,
124126
TEXTAG_OPERATOR,
127+
TEXTAG_ENVIRONMENT,
125128
TEXTAG_COUNTER,
126129
TEXTAG_COUNT
127130
} texKind;
@@ -155,6 +158,7 @@ static kindDefinition TexKinds [] = {
155158
{ true, 'B', "bibitem", "bibliography items" },
156159
{ true, 'C', "command", "command created with \\newcommand" },
157160
{ true, 'o', "operator", "math operator created with \\DeclareMathOperator" },
161+
{ true, 'e', "environment", "environment created with \\newenvironment" },
158162
{ true, 'N', "counter", "counter created with \\newcounter" },
159163
};
160164

@@ -179,6 +183,8 @@ static const keywordTable TexKeywordTable [] = {
179183
{ "providecommand", KEYWORD_providecommand },
180184
{ "def", KEYWORD_def },
181185
{ "DeclareMathOperator", KEYWORD_declaremathoperator },
186+
{ "newenvironment", KEYWORD_newenvironment },
187+
{ "renewenvironment", KEYWORD_renewenvironment},
182188
{ "newcounter", KEYWORD_newcounter },
183189
};
184190

@@ -892,6 +898,48 @@ static bool parseDef (tokenInfo *const token, bool *tokenUnprocessed)
892898
return eof;
893899
}
894900

901+
static bool parseNewEnvironment (tokenInfo *const token, bool *tokenUnprocessed)
902+
{
903+
bool eof = false;
904+
/* \newenvironment{nam}[args]{begdef}{enddef} */
905+
struct TexParseStrategy strategy [] = {
906+
{
907+
.type = '{',
908+
.flags = 0,
909+
.kindIndex = TEXTAG_ENVIRONMENT,
910+
.roleIndex = ROLE_DEFINITION_INDEX,
911+
.name = NULL,
912+
.unique = false,
913+
},
914+
{
915+
.type = '[',
916+
.flags = TEX_NAME_FLAG_OPTIONAL,
917+
.kindIndex = KIND_GHOST_INDEX,
918+
.name = NULL,
919+
},
920+
{
921+
.type = '{',
922+
.flags = 0,
923+
.kindIndex = KIND_GHOST_INDEX,
924+
.name = NULL,
925+
},
926+
{
927+
.type = '{',
928+
.flags = 0,
929+
.kindIndex = KIND_GHOST_INDEX,
930+
.name = NULL,
931+
},
932+
{
933+
.type = 0
934+
}
935+
};
936+
937+
if (parseWithStrategy (token, strategy, tokenUnprocessed))
938+
eof = true;
939+
940+
return eof;
941+
}
942+
895943
static bool parseNewcounter (tokenInfo *const token, bool *tokenUnprocessed)
896944
{
897945
bool eof = false;
@@ -921,6 +969,7 @@ static bool parseNewcounter (tokenInfo *const token, bool *tokenUnprocessed)
921969

922970
return eof;
923971
}
972+
924973
static void parseTexFile (tokenInfo *const token)
925974
{
926975
bool eof = false;
@@ -995,6 +1044,10 @@ static void parseTexFile (tokenInfo *const token)
9951044
case KEYWORD_declaremathoperator:
9961045
eof = parseNewcommandFull (token, &tokenUnprocessed, TEXTAG_OPERATOR);
9971046
break;
1047+
case KEYWORD_newenvironment:
1048+
case KEYWORD_renewenvironment:
1049+
eof = parseNewEnvironment (token, &tokenUnprocessed);
1050+
break;
9981051
case KEYWORD_newcounter:
9991052
eof = parseNewcounter (token, &tokenUnprocessed);
10001053
break;

0 commit comments

Comments
 (0)