]> git.lyx.org Git - lyx.git/blob - src/lyxlex.C
in addition to the changes mentioned in ChangeLog, there is the usual batch of whites...
[lyx.git] / src / lyxlex.C
1 //  Generalized simple lexical analizer.
2 //  It can be used for simple syntax parsers, like lyxrc,
3 //  texclass and others to come.   [asierra30/03/96]
4 //
5 //   (C) 1996 Lyx Team.
6
7 #include <config.h>
8
9 #include <cstdlib>
10
11 #ifdef __GNUG__
12 #pragma implementation "lyxlex.h"
13 #endif
14
15 #include "lyxlex.h"
16 #include "debug.h"
17 #include "support/filetools.h"
18
19 LyXLex::LyXLex(keyword_item * tab, int num)
20         : table(tab), no_items(num)
21 {
22         file = 0;
23         owns_file = false;
24         status = 0;
25         pushed = 0;
26 }
27
28
29 void LyXLex::pushTable(keyword_item * tab, int num)
30 {
31         pushed_table * tmppu = new pushed_table;
32         tmppu->next = pushed;
33         tmppu->table_elem = table;
34         tmppu->table_siz = no_items;
35         pushed = tmppu;
36         table = tab;
37         no_items = num;
38 }
39
40
41 void LyXLex::popTable()
42 {
43         if (pushed == 0)
44                 lyxerr << "LyXLex error: nothing to pop!" << endl;
45
46         pushed_table * tmp;
47         tmp = pushed;
48         table = tmp->table_elem;
49         no_items = tmp->table_siz;
50         tmp->table_elem = 0;
51         pushed = tmp->next;
52         delete tmp;
53 }
54
55
56 void LyXLex::printTable()
57 {
58         lyxerr << "\nNumber of tags: " << no_items << endl;
59         for(int i= 0; i < no_items; ++i)
60                 lyxerr << "table[" << i
61                        << "]:  tag: `" << table[i].tag
62                        << "'  code:" << table[i].code << endl;
63         lyxerr << endl;
64 }
65
66
67 void LyXLex::printError(string const & message) const
68 {
69         string tmpmsg = subst(message, "$$Token", GetString());
70         lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
71                << " of file " << MakeDisplayPath(name) << ']' << endl;
72 }
73
74
75 bool LyXLex::setFile(string const & filename)
76 {
77         if (file)
78                 lyxerr << "Error in LyXLex::setFile: file already set." <<endl;
79         file = fopen(filename.c_str(), "r");
80         name = filename;
81         owns_file = true;
82         lineno = 0;
83         return (file ? true : false);
84 }
85
86
87 void LyXLex::setFile(FILE * f)
88 {
89         if (file) 
90                 lyxerr << "Error in LyXLex::setFile: file already set."
91                        << endl;
92         file = f;
93         owns_file = false;
94         lineno = 0; // this is bogus if the file already has been read from
95 }
96
97
98 int LyXLex::lex()
99 {
100         //NOTE: possible bug.
101    if (next() && status == LEX_TOKEN)
102        return search_kw(buff);
103    else
104        return status;
105 }
106
107
108 int LyXLex::GetInteger() const
109 {
110    if (buff[0] > ' ')   
111        return atoi(buff);
112    else {
113         printError("Bad integer `$$Token'");
114         return -1;
115    }
116 }
117
118
119 float LyXLex::GetFloat() const
120 {
121    if (buff[0] > ' ')   
122        return atof(buff);
123    else {
124         printError("Bad float `$$Token'");
125         return -1;
126    }
127 }
128
129
130 string LyXLex::GetString() const
131 {
132         return string(buff);
133 }
134
135
136 // I would prefer to give a tag number instead of an explicit token
137 // here, but it is not possible because Buffer::readLyXformat2 uses
138 // explicit tokens (JMarc) 
139 string LyXLex::getLongString(string const & endtoken)
140 {
141         string str, prefix;
142         bool firstline = true;
143
144         while (IsOK()) {
145                 if (!EatLine())
146                         // blank line in the file being read
147                         continue;
148                 
149                 string const token = frontStrip(strip(GetString()), " \t");
150                 
151                 lyxerr[Debug::PARSER] << "LongString: `"
152                                       << GetString() << '\'' << endl;
153
154                 // We do a case independent comparison, like search_kw
155                 // does.
156                 if (compare_no_case(token, endtoken) != 0) {
157                         string tmpstr = GetString();
158                         if (firstline) {
159                                 unsigned int i = 0;
160                                 while(i < tmpstr.length()
161                                       && tmpstr[i] == ' ') {
162                                         ++i;
163                                         prefix += ' ';
164                                 }
165                                 firstline = false;
166                                 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
167                                                       << '\'' << endl;
168                         } 
169
170                         if (!prefix.empty() 
171                             && prefixIs(tmpstr, prefix.c_str())) {
172                                 tmpstr.erase(0, prefix.length() - 1);
173                         }
174                         str += tmpstr + '\n';
175                 }
176                 else // token == endtoken
177                         break;
178         }
179         if (!IsOK())
180                 printError("Long string not ended by `" + endtoken + '\'');
181
182         return str;
183 }
184
185
186 bool LyXLex::GetBool() const
187 {
188    if (compare(buff, "true") == 0)
189         return true;
190    else if (compare(buff, "false") != 0)
191         printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
192    return false;
193 }
194
195
196 bool LyXLex::EatLine()
197 {
198         int i = 0;
199         int c = '\0'; // getc() returns an int
200
201         while (!feof(file) && c!= '\n' && i!= (LEX_MAX_BUFF-1)) {
202                 c = getc(file);
203                 if (c != '\r')
204                         buff[i++] = c;
205         }
206         if (i == (LEX_MAX_BUFF-1) && c != '\n') {
207                 printError("Line too long");
208                 c = '\n'; // Pretend we had an end of line
209                 --lineno; // but don't increase line counter (netto effect)
210                 ++i; // and preserve last character read.
211         }
212         if (c == '\n') {
213                 ++lineno;
214                 buff[--i] = '\0'; // i can never be 0 here, so no danger
215                 status = LEX_DATA;
216                 return true;
217         } else {
218                 buff[i] = '\0';
219                 return false;
220         }
221 }
222
223
224 int LyXLex::search_kw(char const * const tag) const
225 {
226         int m, k = 0 , l = 0, r = no_items;
227
228         while (l < r) {
229                 m = (l + r) / 2;
230
231                 if (lyxerr.debugging(Debug::PARSER)) {
232                         lyxerr << "LyXLex::search_kw: elem " << m
233                                << " tag " << table[m].tag
234                                << " search tag " << tag
235                                << endl;
236                 }
237
238                 if (table[m].tag)
239                         k = compare_no_case(table[m].tag, tag);
240                 if (k == 0)
241                         return table[m].code;
242                 else
243                         if (k < 0) l = m + 1; else r = m;
244         }
245         return LEX_UNDEF;
246 }
247
248
249 bool LyXLex::next(bool esc)
250 {
251         if (!esc) {
252                 int c; // getc() returns an int
253                 
254                 status = 0;
255                 while (!feof(file) && !status) { 
256                         c = getc(file);
257                         if (c == '#') {
258                                 // Read rest of line (fast :-)
259                                 fgets(buff, sizeof(buff), file);
260                                 ++lineno;
261                                 continue;
262                         }
263                         
264                         if (c == '\"') {
265                                 int i = -1;
266                                 do {
267                                         c = getc(file);
268                                         if (c != '\r')
269                                                 buff[++i] = c;
270                                 } while (c!= '\"' && c!= '\n' && !feof(file) &&
271                                          i!= (LEX_MAX_BUFF-2));
272                                 
273                                 if (i == (LEX_MAX_BUFF-2)) {
274                                         printError("Line too long");
275                                         c = '\"'; // Pretend we got a "
276                                         ++i;
277                                 }
278                                 
279                                 if (c!= '\"') {
280                                         printError("Missing quote");
281                                         if (c == '\n')
282                                                 ++lineno;
283                                 }
284                                 
285                                 buff[i] = '\0';
286                                 status = LEX_DATA;
287                                 break; 
288                         }
289                         
290                         if (c == ',')
291                                 continue;              /* Skip ','s */
292                         
293                         if (c > ' ' && !feof(file))  {
294                                 int i = 0;
295                                 do {
296                                         buff[i++] = c;
297                                         c = getc(file);
298                                 } while (c > ' ' && c != ',' && !feof(file) &&
299                                          (i != LEX_MAX_BUFF-1) );
300                                 if (i == LEX_MAX_BUFF-1) {
301                                         printError("Line too long");
302                                 }
303                                 buff[i] = '\0';
304                                 status = LEX_TOKEN;
305                         }
306                         
307                         if (c == '\r' && !feof(file)) {
308                                 // The Windows support has lead to the
309                                 // possibility of "\r\n" at the end of
310                                 // a line.  This will stop LyX choking
311                                 // when it expected to find a '\n'
312                                 c = getc(file);
313                         }
314
315                         if (c == '\n')
316                                 ++lineno;
317                         
318                 }
319                 if (status) return true;
320                 
321                 status = (feof(file)) ? LEX_FEOF: LEX_UNDEF;
322                 buff[0] = '\0';
323                 return false;
324         } else {
325                 int c; // getc() returns an int
326                 
327                 status = 0;
328                 while (!feof(file) && !status) { 
329                         c = getc(file);
330
331                         // skip ','s
332                         if (c == ',') continue;
333                         
334                         if (c == '\\') {
335                                 // escape
336                                 int i = 0;
337                                 do {
338                                         if (c == '\\') {
339                                                 // escape the next char
340                                                 c = getc(file);
341                                         }
342                                         buff[i++] = c;
343                                         c = getc(file);
344                                 } while (c > ' ' && c != ',' && !feof(file) &&
345                                          (i != LEX_MAX_BUFF-1) );
346                                 if (i == LEX_MAX_BUFF-1) {
347                                         printError("Line too long");
348                                 }
349                                 buff[i] = '\0';
350                                 status = LEX_TOKEN;
351                                 continue;
352                         }
353                         
354                         if (c == '#') {
355                                 // Read rest of line (fast :-)
356                                 fgets(buff, sizeof(buff), file);
357                                 ++lineno;
358                                 continue;
359                         }
360
361                         // string
362                         if (c == '\"') {
363                                 int i = -1;
364                                 bool escaped = false;
365                                 do {
366                                         escaped = false;
367                                         c = getc(file);
368                                         if (c == '\r') continue;
369                                         if (c == '\\') {
370                                                 // escape the next char
371                                                 c = getc(file);
372                                                 escaped = true;
373                                         }
374                                         buff[++i] = c;
375                                 
376                                         if (!escaped && c == '\"') break;
377                                 } while (c!= '\n' && !feof(file) &&
378                                          i!= (LEX_MAX_BUFF-2));
379                                 
380                                 if (i == (LEX_MAX_BUFF-2)) {
381                                         printError("Line too long");
382                                         c = '\"'; // Pretend we got a "
383                                         ++i;
384                                 }
385                                 
386                                 if (c!= '\"') {
387                                         printError("Missing quote");
388                                         if (c == '\n')
389                                                 ++lineno;
390                                 }
391                                 
392                                 buff[i] = '\0';
393                                 status = LEX_DATA;
394                                 break; 
395                         }
396                         
397                         if (c > ' ' && !feof(file))  {
398                                 int i = 0;
399                                 do {
400                                         if (c == '\\') {
401                                                 // escape the next char
402                                                 c = getc(file);
403                                                 //escaped = true;
404                                         }
405                                         buff[i++] = c;
406                                         c = getc(file);
407                                 } while (c > ' ' && c != ',' && !feof(file) &&
408                                          (i != LEX_MAX_BUFF-1) );
409                                 if (i == LEX_MAX_BUFF-1) {
410                                         printError("Line too long");
411                                 }
412                                 buff[i] = '\0';
413                                 status = LEX_TOKEN;
414                         }
415
416                         // new line
417                         if (c == '\n')
418                                 ++lineno;
419                 }
420                 
421                 if (status) return true;
422                 
423                 status = (feof(file)) ? LEX_FEOF: LEX_UNDEF;
424                 buff[0] = '\0';
425                 return false;   
426         }
427 }
428
429
430 bool LyXLex::nextToken()
431 {
432         status = 0;
433         while (!feof(file) && !status) { 
434                 int c = getc(file); // getc() returns an int
435            
436                 if (c >= ' ' && !feof(file))  {
437                         int i = 0;
438                         if (c == '\\') { // first char == '\\'
439                                 do {
440                                         buff[i++] = c;
441                                         c = getc(file);
442                                 } while (c > ' ' && c != '\\' && !feof(file) &&
443                                          i != (LEX_MAX_BUFF-1));
444                         } else {
445                                 do {
446                                         buff[i++] = c;
447                                         c = getc(file);
448                                 } while (c >= ' ' && c != '\\' && !feof(file)
449                                          && i != (LEX_MAX_BUFF-1));
450                         }
451
452                         if (i == (LEX_MAX_BUFF-1)) {
453                                 printError("Line too long");
454                         }
455
456                         if (c == '\\') ungetc(c, file); // put it back
457                         buff[i] = '\0';
458                         status = LEX_TOKEN;
459                 }
460                   
461                 if (c == '\n')
462                         ++lineno;
463         
464         }
465         if (status)  return true;
466         
467         status = (feof(file)) ? LEX_FEOF: LEX_UNDEF;
468         buff[0] = '\0';
469         return false;
470 }
471
472
473 int LyXLex::FindToken(char const * str[])
474 {  
475    int i = -1;
476    
477    if (next()) {
478       if (compare(buff, "default")) {
479          for (i = 0; str[i][0] && compare(str[i], buff); ++i);
480          if (!str[i][0]) {
481             printError("Unknown argument `$$Token'");
482             i = -1;
483          }
484       }  
485    } else
486      printError("file ended while scanning string token");
487    return i;
488 }
489
490
491 int LyXLex::CheckToken(char const * str[], int print_error)
492 {  
493    int i = -1;
494    
495    if (compare(buff, "default")) {
496        for (i = 0; str[i][0] && compare(str[i], buff); ++i);
497        if (!str[i][0]) {
498            if (print_error)
499                printError("Unknown argument `$$Token'");
500            i = -1;
501        }
502    }
503    return i;
504 }