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]
12 #pragma implementation "lyxlex.h"
17 #include "support/filetools.h"
19 LyXLex::LyXLex(keyword_item * tab, int num)
20 : is(&fb__), table(tab), no_items(num)
28 void LyXLex::pushTable(keyword_item * tab, int num)
30 pushed_table * tmppu = new pushed_table;
32 tmppu->table_elem = table;
33 tmppu->table_siz = no_items;
40 void LyXLex::popTable()
43 lyxerr << "LyXLex error: nothing to pop!" << endl;
47 table = tmp->table_elem;
48 no_items = tmp->table_siz;
55 void LyXLex::printTable()
57 lyxerr << "\nNumber of tags: " << no_items << endl;
58 for(int i= 0; i < no_items; ++i)
59 lyxerr << "table[" << i
60 << "]: tag: `" << table[i].tag
61 << "' code:" << table[i].code << endl;
66 void LyXLex::printError(string const & message) const
68 string tmpmsg = subst(message, "$$Token", GetString());
69 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
70 << " of file " << MakeDisplayPath(name) << ']' << endl;
74 bool LyXLex::setFile(string const & filename)
77 lyxerr << "Error in LyXLex::setFile: "
78 "file or stream already set." << endl;
79 fb__.open(filename.c_str(), ios::in);
83 return fb__.is_open() && is.good();
87 void LyXLex::setStream(istream & i)
89 if (fb__.is_open() || is.rdbuf()->in_avail())
90 lyxerr << "Error in LyXLex::setStream: "
91 "file or stream already set." << endl;
100 if (next() && status == LEX_TOKEN)
101 return search_kw(buff);
107 int LyXLex::GetInteger() const
112 printError("Bad integer `$$Token'");
118 float LyXLex::GetFloat() const
123 printError("Bad float `$$Token'");
129 string LyXLex::GetString() const
135 // I would prefer to give a tag number instead of an explicit token
136 // here, but it is not possible because Buffer::readLyXformat2 uses
137 // explicit tokens (JMarc)
138 string LyXLex::getLongString(string const & endtoken)
141 bool firstline = true;
145 // blank line in the file being read
148 string const token = frontStrip(strip(GetString()), " \t");
150 lyxerr[Debug::PARSER] << "LongString: `"
151 << GetString() << '\'' << endl;
153 // We do a case independent comparison, like search_kw
155 if (compare_no_case(token, endtoken) != 0) {
156 string tmpstr = GetString();
159 while(i < tmpstr.length()
160 && tmpstr[i] == ' ') {
165 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
170 && prefixIs(tmpstr, prefix.c_str())) {
171 tmpstr.erase(0, prefix.length() - 1);
173 str += tmpstr + '\n';
175 else // token == endtoken
179 printError("Long string not ended by `" + endtoken + '\'');
185 bool LyXLex::GetBool() const
187 if (compare(buff, "true") == 0)
189 else if (compare(buff, "false") != 0)
190 printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
195 bool LyXLex::EatLine()
198 unsigned char c = '\0';
200 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
205 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
206 printError("Line too long");
207 c = '\n'; // Pretend we had an end of line
208 --lineno; // but don't increase line counter (netto effect)
209 ++i; // and preserve last character read.
213 buff[--i] = '\0'; // i can never be 0 here, so no danger
223 int LyXLex::search_kw(char const * const tag) const
225 int m, k = 0 , l = 0, r = no_items;
230 if (lyxerr.debugging(Debug::PARSER)) {
231 lyxerr << "LyXLex::search_kw: elem " << m
232 << " tag " << table[m].tag
233 << " search tag " << tag
238 k = compare_no_case(table[m].tag, tag);
240 return table[m].code;
242 if (k < 0) l = m + 1; else r = m;
248 bool LyXLex::next(bool esc)
251 unsigned char c; // getc() returns an int
254 while (is && !status) {
257 // Read rest of line (fast :-)
258 is.get(buff, sizeof(buff));
259 lyxerr[Debug::LYXLEX] << "Comment read: " << c << buff << endl;
270 } while (c != '\"' && c != '\n' && is &&
271 i != (LEX_MAX_BUFF - 2));
273 if (i == (LEX_MAX_BUFF - 2)) {
274 printError("Line too long");
275 c = '\"'; // Pretend we got a "
280 printError("Missing quote");
291 continue; /* Skip ','s */
293 // using relational operators with chars other
294 // than == and != is not safe. And if it is done
295 // the type _have_ to be unsigned. It usually a
296 // lot better to use the functions from cctype
298 #warning Verify this! (Lgb)
299 //if (isalnum(static_cast<unsigned char>(c)) && is) {
304 } while (c > ' ' && c != ',' && is
305 //} while (isalnum(static_cast<unsigned char>(c))
307 && (i != LEX_MAX_BUFF - 1) );
308 if (i == LEX_MAX_BUFF - 1) {
309 printError("Line too long");
315 if (c == '\r' && is) {
316 // The Windows support has lead to the
317 // possibility of "\r\n" at the end of
318 // a line. This will stop LyX choking
319 // when it expected to find a '\n'
327 if (status) return true;
329 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
333 unsigned char c; // getc() returns an int
336 while (is && !status) {
340 if (c == ',') continue;
347 // escape the next char
352 } while (c > ' ' && c != ',' && is
353 //} while (isalnum(static_cast<unsigned char>(c))
355 && (i != LEX_MAX_BUFF - 1) );
356 if (i == LEX_MAX_BUFF - 1) {
357 printError("Line too long");
365 // Read rest of line (fast :-)
366 is.get(buff, sizeof(buff));
367 lyxerr[Debug::LYXLEX] << "Comment read: " << c << buff << endl;
375 bool escaped = false;
379 if (c == '\r') continue;
381 // escape the next char
387 if (!escaped && c == '\"') break;
388 } while (c != '\n' && is &&
389 i != (LEX_MAX_BUFF - 2));
391 if (i == (LEX_MAX_BUFF - 2)) {
392 printError("Line too long");
393 c = '\"'; // Pretend we got a "
398 printError("Missing quote");
409 //if (isalnum(static_cast<unsigned char>(c)) && is) {
413 // escape the next char
419 } while (c > ' ' && c != ',' && is
420 //} while (isalnum(static_cast<unsigned char>(c))
422 && (i != LEX_MAX_BUFF-1) );
423 if (i == LEX_MAX_BUFF-1) {
424 printError("Line too long");
434 if (status) return true;
436 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
443 bool LyXLex::nextToken()
446 while (is && !status) {
450 if (c >= ' ' && is) {
451 //if (isprint(static_cast<unsigned char>(c)) && is) {
453 if (c == '\\') { // first char == '\\'
457 } while (c > ' ' && c != '\\' && is
458 //} while (isalnum(static_cast<unsigned char>(c))
459 // && c != '\\' && is
460 && i != (LEX_MAX_BUFF-1));
465 } while (c >= ' ' && c != '\\' && is
466 //} while (isprint(static_cast<unsigned char>(c))
467 // && c != '\\' && is
468 && i != (LEX_MAX_BUFF-1));
471 if (i == (LEX_MAX_BUFF - 1)) {
472 printError("Line too long");
475 if (c == '\\') is.putback(c); // put it back
484 if (status) return true;
486 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
492 int LyXLex::FindToken(char const * str[])
497 if (compare(buff, "default")) {
498 for (i = 0; str[i][0] && compare(str[i], buff); ++i);
500 printError("Unknown argument `$$Token'");
505 printError("file ended while scanning string token");
510 int LyXLex::CheckToken(char const * str[], int print_error)
514 if (compare(buff, "default")) {
515 for (i = 0; str[i][0] && compare(str[i], buff); ++i);
518 printError("Unknown argument `$$Token'");