-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tex_grammar.g4
307 lines (244 loc) · 11.4 KB
/
Tex_grammar.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*----------------------------------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PARSER RULES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
------------------------------------------------------------------------------------------------------------------------
*/
grammar Tex_grammar;
root : docclass preamble* begindocument title* author* date* body_text enddocument EOF ;
docclass : DOCCLASS;
preamble : PKGAMSMATH
| PKGBALANCE
| PKGCAPTION
| PKGGRAPHICS
| PKGGRAPHICX
| PKGMATHTOOLS
| PKGMULTIROW
| PKGSUBCAPTION
| PKGTABULARX
| PKGURL
;
begindocument : BEGINDOC ;
enddocument : ENDDOC ;
body_text : body_text body_parts
| body_parts
;
body_parts : optionalAbstract|section | subsection | label|ref| ordered_list |unordered_list | maketitle | figure|fulltext ;
title : TITLE NEWLINE* ;
author : AUTHOR NEWLINE* ;
date : DATE NEWLINE* ;
maketitle : MAKE_TITLE;
optionalAbstract : BEGIN_ABSTRACT NEWLINE* fulltext NEWLINE* END_ABSTRACT ;
section : sectionName fulltext* text* fulltext*
;
sectionName : SECTION' '*'{' text'}'NEWLINE* ;
subsection : subsectionName fulltext text* fulltext*
;
subsectionName : SUBSECTION ' '*'{' text'}'NEWLINE* ;
par : PAR ;
center : BEGIN_CENTER fulltext* END_CENTER ;
graphicsHW : '[' TEXT ']';
graphicsName : '{' WORD '}' NEWLINE*;
graphics : INCLUDEGRAPHICS graphicsHW? graphicsName (' '|'\r\n')*
;
figure : BEGINFIGURE graphics label? caption? label? ENDFIGURE | BEGINFIGURE caption? graphics ENDFIGURE ;
ordered_list : (BEGIN_ENUMERATE item+ END_ENUMERATE)
;
unordered_list : BEGIN_ITEMIZE item+ END_ITEMIZE
;
item : (ITEM listtext)+
| ordered_list item
| unordered_list item
;
listtext : ' '* text* NEWLINE*;
stat : expr NEWLINE?
| ID '=' expr NEWLINE?
| NEWLINE
;
expr : expr ('*'|'/'|'+'|'-') expr
| INTEGER
| ID
| '(' expr ')'
;
operator : OPERATOR;
superscript : SUPEQN;
subscript : SUBEQN;
frac : FRAC fracdata NEWLINE*;
sqrt : SQRT mathdata NEWLINE*
| SQRT frac
| SQRT integral
;
sum : SUM SUBSCRIPT sumsubscript SUPERSCRIPT sumsuperscript NEWLINE*;
integralSign : INTEGRATION;
integral : integralSign SUBSCRIPT sumsubscript SUPERSCRIPT sumsuperscript NEWLINE*
| integralSign
;
integration : integral expr*;
sumsubscript : LEFTCURLYB (EQUALTO|ID|WORD|LETTERS|' '|SINGLECHAR) EQUALTO? (EQUALTO|ID|WORD|LETTERS|' '|SINGLECHAR)? RIGHTCURLYB;
sumsuperscript : LEFTCURLYB (EQUALTO|ID|WORD|LETTERS|' '|SINGLECHAR) EQUALTO? (EQUALTO|ID|WORD|LETTERS|' '|SINGLECHAR)? RIGHTCURLYB;
mathdata : LEFTCURLYB (EQUALTO|ID|WORD|LETTERS|' '|SINGLECHAR|sqrt|sum|integral) RIGHTCURLYB ;
fracdata : fracPart fracPart;
fracPart : LEFTCURLYB text RIGHTCURLYB
| LEFTCURLYB (sqrt|sum|integral) RIGHTCURLYB
;
inlinemath : SINGLEDOLLAR (text | superscript |integration| subscript |sum|frac|sqrt ' '|operator)+ SINGLEDOLLAR ;
displaymath : DOUBLEDOLLAR (superscript | subscript |sum|frac|sqrt|integration|text+|LEFTCURLYB|RIGHTCURLYB|LEFTPARENTHESES|RIGHTPARENTHESES | superscript | subscript |sum|frac|sqrt| ' '|operator)+ DOUBLEDOLLAR;
begin_table : BEGIN_TABLE;
end_table : END_TABLE;
begin_tabular : BEGIN_TABULAR;
end_tabular : END_TABULAR;
table_alignment : TABLE_ALIGNMENT;
table_row : ((' '*(table_column ' '+)+ ' '+)+ '\t'*' '* ) doubleslash?|'\\\\' ' '* ;
doubleslash : DOUBLESLASH;
table_column : (' ' | '\t')* text* (' ' | '\t')* AMPERSAND?(' ' | '\t')* ;
table : (begin_table? begin_tabular table_alignment?) (table_row ' '* doubleslash?)+ (end_tabular end_table?) ;
latex_newline : DOUBLESLASH | LATEX_NEWLINE | HFILL ' '* BREAK ;
boldtext : TEXTBF '{' text '}' ' '*;
italictext : TEXTIT '{' text '}';
underline : UNDERLINE '{' text '}' ;
caption : CAPTION '{' expr '}';
label : LABEL '{' text '}';
ref : REF '{' text '}';
text : NEWLINE* TEXT | WORD|INTEGER | LETTERS|'-'|' '|SINGLECHAR|EQUALTO|PLUS|MUL|'='|stat|'('|')';
fulltext : fulltext ' '* text
| fulltext boldtext text?
| fulltext italictext text?
| fulltext underline text?
| fulltext caption
| fulltext frac
| fulltext sqrt
| fulltext sum
| fulltext integration
| fulltext inlinemath
| fulltext displaymath
| fulltext table
| fulltext label
| fulltext ref
| fulltext center
| fulltext par
| text underline
| text boldtext
| text italictext
| inlinemath
| displaymath
| table
| boldtext
| italictext
| underline
| caption
| frac
| sqrt
| sum
| integration
| label
| ref
| par
| center
| latex_newline* text latex_newline* text*
;
word : WORD;
letter : LETTERS;
integer : INTEGER;
sentence : (word|letter|integer|' ')+;
line : (' '|'\t')* (WORD(' '|'\t')*)* ('\r'|'\n'|'\r\n');
/*----------------------------------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LEXER RULES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
------------------------------------------------------------------------------------------------------------------------
*/
ID : [a-zA-Z]+ ; // match lower-case identifiers
DOUBLESLASH : '\\\\' ;
LATEX_NEWLINE : '\\newline' ;
HFILL : '\\hfill' ;
BREAK : '\\break' ;
FRAC : '\\frac' ;
SQRT : '\\sqrt' ;
SUM : '\\sum' ;
INTEGRATION : '\\int' ;
LEFTCURLYB : '{' ;
RIGHTCURLYB : '}' ;
LEFTSQUAREB : '[' ;
RIGHTSQUAREB : ']' ;
LEFTPARENTHESES : '(' ;
RIGHTPARENTHESES : ')' ;
EQUALTO : '=';
PLUS : '+';
MUL : '*';
SUPERSCRIPT : '^';
SUBSCRIPT : '_';
AMPERSAND : '&';
DOUBLEDOLLAR : '$$';
SINGLEDOLLAR : '$';
SUPEQN : (ID|WORD|LETTERS|SINGLECHAR) SUPERSCRIPT (ID|WORD|LETTERS|SINGLECHAR);
SUBEQN : (ID|WORD|LETTERS|SINGLECHAR) SUBSCRIPT (ID|WORD|LETTERS|SINGLECHAR);
DOCCLASS : '\\documentclass[sigconf]{acmart}' NEWLINE*;
PKGBALANCE : '\\usepackage{balance}' NEWLINE*;
PKGGRAPHICX : '\\usepackage{graphicx}' NEWLINE*;
PKGURL : '\\usepackage{url}' NEWLINE*;
PKGAMSMATH : '\\usepackage{amsmath}' NEWLINE*;
PKGMATHTOOLS : '\\usepackage{mathtools}' NEWLINE*;
PKGTABULARX : '\\usepackage{tabularx}' NEWLINE*;
PKGCAPTION : '\\usepackage{caption}' NEWLINE*;
PKGSUBCAPTION : '\\usepackage{subcaption}' NEWLINE*;
PKGMULTIROW : '\\usepackage{multirow}' NEWLINE*;
PKGGRAPHICS : '\\usepackage{graphics}' NEWLINE*;
BEGINDOC : '\\begin{document}' NEWLINE*;
ENDDOC : '\\end{document}' NEWLINE*;
BEGINFIGURE : '\\begin{figure}' ;
ENDFIGURE : '\\end{figure}' ;
INCLUDEGRAPHICS : '\\includegraphics';
CAPTION : '\\caption';
BEGIN_CENTER : '\\begin{center}';
END_CENTER : '\\end{center}';
TITLE : '\\title''{' TEXTPART '}';
AUTHOR : '\\author''{'TEXTPART'}';
DATE : '\\date''{'TEXTPART'}';
MAKE_TITLE : '\\maketitle';
BEGIN_ABSTRACT : '\\begin{abstract}';
END_ABSTRACT : '\\end{abstract}';
SECTION : '\\section';
SUBSECTION : '\\subsection';
PARAGRAPH : '\\paragraph' ;
TEXTBF : '\\textbf';
TEXTIT : '\\textit';
UNDERLINE : '\\underline';
PAR : '\\par' ;
LABEL : '\\label';
REF : '\\ref';
BEGIN_ENUMERATE : '\\begin{enumerate}' NEWLINE*;
ITEM : '\\item';
END_ENUMERATE : '\\end{enumerate}'NEWLINE*;
BEGIN_ITEMIZE : '\\begin{itemize}'NEWLINE*;
END_ITEMIZE : '\\end{itemize}'NEWLINE*;
//table tokens
BEGIN_TABLE : '\\begin{table}';
END_TABLE : '\\end{table}';
BEGIN_TABULAR : '\\begin{tabular}';
END_TABULAR : '\\end{tabular}';
TABLE_ALIGNMENT : ('{'('|'? ('l'|'r'|'c')? '|'? ('l'|'r'|'c')? '|'? ('l'|'r'|'c')? '|'? )'}') ;
HLINE : '\\hline';
WORD : (((PUNCTUATION)|[a-zA-Z0-9]|(GREEK_LETTERS))+|'('|')');
GREEK_LETTERS : '\\alpha'
|'\\beta'|'\\gamma'|'\\delta'|'\\epsilon'
|'\\rho'|'\\sigma'|'\\tau'|'\\upsilon'|'\\phi'|'\\psi'|'\\omega'|'\\Alpha'
|'\\zeta'|'\\eta'|'\\theta'|'\\iota'|'\\kappa'
|'\\lambda'|'\\mu'|'\\nu'|'\\xi'|'\\omicron'|'\\pi'
|'\\Tau'|'\\Upsilon'|'\\Phi'|'\\Psi'|'\\Omega'|'\\int'|'\\inf'|'\\exp'
|'\\Beta'|'\\Gamma'|'\\Delta'|'\\Epsilon'|'\\Zeta'|'\\Eta'|'\\Theta'|'\\Iota'
|'\\Kappa'|'\\Lambda'|'\\Mu'|'\\Nu'|'\\Xi'|'\\Omicron'|'\\Pi'|'\\Rho'|'\\Sigma'
;
RESERVEWORD : ('\\' [a-zA-Z] | '\\'[a-zA-Z*]+);
SPECIALCHAR : ('_'|'&'|'$'|'#'|'@');
OPERATOR : '+'|'-'|'=';
SKIP2 : '\r\n' ->skip;
NEWLINE : ('\r'? '\n') ->skip ;
WS : (' '|'\t'|'\n'|'\r')+ ->skip ;
INTEGER : [0-9]+ ;
SINGLECHAR : '.';
LETTERS : [a-zA-Z];
TEXT : [a-zA-Z0-9_]TEXTPART*('\r'|'\n')*| ':' | '.' | '&' | '/' | ';'|' '|'//'|' '* ;
fragment HEXCHARS : '#' [0-9a-fA-F]+ ;
fragment HEXCHARS2 : '&#'[0-9a-fA-F]+ ;
fragment PUNCTUATION : ('.'|'\'|!|'?'|:|;|');
fragment TEXTPART : [a-zA-Z0-9][a-zA-Z0-9 '\t''.''?'',''!'':'';'' ''-''=']*;