6 #include "../hunspell/csutil.hxx"
7 #include "latexparser.hxx"
17 { { "\\(", "\\)" } , 0 },
18 { { "$$", "$$" } , 0 },
20 { { "\\begin{math}", "\\end{math}" } , 0 },
21 { { "\\[", "\\]" } , 0 },
22 { { "\\begin{displaymath}", "\\end{displaymath}" } , 0 },
23 { { "\\begin{equation}", "\\end{equation}" } , 0 },
24 { { "\\begin{equation*}", "\\end{equation*}" } , 0 },
25 { { "\\cite", NULL } , 1 },
26 { { "\\nocite", NULL } , 1 },
27 { { "\\index", NULL } , 1 },
28 { { "\\label", NULL } , 1 },
29 { { "\\ref", NULL } , 1 },
30 { { "\\pageref", NULL } , 1 },
31 { { "\\parbox", NULL } , 1 },
32 { { "\\begin{verbatim}", "\\end{verbatim}" } , 0 },
33 { { "\\verb+", "+" } , 0 },
34 { { "\\verb|", "|" } , 0 },
35 { { "\\verb#", "#" } , 0 },
36 { { "\\verb*", "*" } , 0 },
37 { { "\\documentstyle", "\\begin{document}" } , 0 },
38 { { "\\documentclass", "\\begin{document}" } , 0 },
39 // { { "\\documentclass", NULL } , 1 },
40 { { "\\usepackage", NULL } , 1 },
41 { { "\\includeonly", NULL } , 1 },
42 { { "\\include", NULL } , 1 },
43 { { "\\input", NULL } , 1 },
44 { { "\\vspace", NULL } , 1 },
45 { { "\\setlength", NULL } , 2 },
46 { { "\\addtolength", NULL } , 2 },
47 { { "\\settowidth", NULL } , 2 },
48 { { "\\rule", NULL } , 2 },
49 { { "\\hspace", NULL } , 1 } ,
50 { { "\\vspace", NULL } , 1 } ,
51 { { "\\\\[", "]" } , 0 },
52 { { "\\pagebreak[", "]" } , 0 } ,
53 { { "\\nopagebreak[", "]" } , 0 } ,
54 { { "\\enlargethispage", NULL } , 1 } ,
55 { { "\\begin{tabular}", NULL } , 1 } ,
56 { { "\\addcontentsline", NULL } , 2 } ,
57 { { "\\begin{thebibliography}", NULL } , 1 } ,
58 { { "\\bibliography", NULL } , 1 } ,
59 { { "\\bibliographystyle", NULL } , 1 } ,
60 { { "\\bibitem", NULL } , 1 } ,
61 { { "\\begin", NULL } , 1 } ,
62 { { "\\end", NULL } , 1 } ,
63 { { "\\pagestyle", NULL } , 1 } ,
64 { { "\\pagenumbering", NULL } , 1 } ,
65 { { "\\thispagestyle", NULL } , 1 } ,
66 { { "\\newtheorem", NULL } , 2 },
67 { { "\\newcommand", NULL } , 2 },
68 { { "\\renewcommand", NULL } , 2 },
69 { { "\\setcounter", NULL } , 2 },
70 { { "\\addtocounter", NULL } , 1 },
71 { { "\\stepcounter", NULL } , 1 },
72 { { "\\selectlanguage", NULL } , 1 },
73 { { "\\inputencoding", NULL } , 1 },
74 { { "\\hyphenation", NULL } , 1 },
75 { { "\\definecolor", NULL } , 3 },
76 { { "\\color", NULL } , 1 },
77 { { "\\textcolor", NULL } , 1 },
78 { { "\\pagecolor", NULL } , 1 },
79 { { "\\colorbox", NULL } , 2 },
80 { { "\\fcolorbox", NULL } , 2 },
81 { { "\\declaregraphicsextensions", NULL } , 1 },
82 { { "\\psfig", NULL } , 1 },
83 { { "\\url", NULL } , 1 },
84 { { "\\eqref", NULL } , 1 },
85 { { "\\vskip", NULL } , 1 },
86 { { "\\vglue", NULL } , 1 },
87 { { "\'\'", NULL } , 1 }
90 #define PATTERN_LEN (sizeof(PATTERN) / sizeof(PATTERN[0]))
92 LaTeXParser::LaTeXParser(const char * wordchars)
97 LaTeXParser::LaTeXParser(unsigned short * wordchars, int len)
102 LaTeXParser::~LaTeXParser()
106 int LaTeXParser::look_pattern(int col)
108 for (unsigned int i = 0; i < PATTERN_LEN; i++) {
109 char * j = line[actual] + head;
110 const char * k = PATTERN[i].pat[col];
112 while ((*k != '\0') && (tolower(*j) == *k)) {
116 if (*k == '\0') return i;
124 * state 0: not wordchar
128 * state 4: commands with arguments
134 char * LaTeXParser::next_token()
140 // fprintf(stderr,"depth: %d, state: %d, , arg: %d, token: %s\n",depth,state,arg,line[actual]+head);
144 case 0: // non word chars
145 if ((pattern_num = look_pattern(0)) != -1) {
146 if (PATTERN[pattern_num].pat[1]) {
154 head += strlen(PATTERN[pattern_num].pat[0]) - 1;
155 } else if ((line[actual][head] == '%')) {
157 } else if (is_wordchar(line[actual] + head)) {
160 } else if (line[actual][head] == '\\') {
161 if (line[actual][head + 1] == '\\' || // \\ (linebreak)
162 (line[actual][head + 1] == '$') || // \$ (dollar sign)
163 (line[actual][head + 1] == '%')) { // \% (percent)
168 } else if (line[actual][head] == '%') {
169 if ((head==0) || (line[actual][head - 1] != '\\')) state = 5;
174 if (! is_wordchar(line[actual] + head) ||
175 (line[actual][head] == '\'' && line[actual][head+1] == '\'' && ++apostrophe)) {
177 char * t = alloc_token(token, &head);
178 if (apostrophe) head += 2;
182 case 2: // comment, labels, etc
183 if (((i = look_pattern(1)) != -1) &&
184 (strcmp(PATTERN[i].pat[1],PATTERN[pattern_num].pat[1]) == 0)) {
186 head += strlen(PATTERN[pattern_num].pat[1]) - 1;
190 if ((tolower(line[actual][head]) < 'a') || (tolower(line[actual][head]) > 'z')) {
195 case 4: // command with arguments
196 if (slash && (line[actual][head] != '\0')) {
200 } else if (line[actual][head]=='\\') {
202 } else if ((line[actual][head] == '{') ||
203 ((opt) && (line[actual][head] == '['))) {
206 } else if (line[actual][head] == '}') {
212 if (((depth == 0) && (arg == PATTERN[pattern_num].arg)) ||
214 state = 0; // XXX not handles the last optional arg.
216 } else if (line[actual][head] == ']') depth--;
218 if (next_char(line[actual], &head)) {
219 if (state == 5) state = 0;