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"
21 LyXLex::LyXLex(keyword_item * tab, int num)
22 : is(&fb__), table(tab), no_items(num)
30 void LyXLex::pushTable(keyword_item * tab, int num)
32 pushed_table * tmppu = new pushed_table;
34 tmppu->table_elem = table;
35 tmppu->table_siz = no_items;
42 void LyXLex::popTable()
45 lyxerr << "LyXLex error: nothing to pop!" << endl;
49 table = tmp->table_elem;
50 no_items = tmp->table_siz;
57 void LyXLex::printTable()
59 lyxerr << "\nNumber of tags: " << no_items << endl;
60 for(int i= 0; i < no_items; ++i)
61 lyxerr << "table[" << i
62 << "]: tag: `" << table[i].tag
63 << "' code:" << table[i].code << endl;
68 void LyXLex::printError(string const & message) const
70 string tmpmsg = subst(message, "$$Token", GetString());
71 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
72 << " of file " << MakeDisplayPath(name) << ']' << endl;
76 bool LyXLex::setFile(string const & filename)
79 lyxerr << "Error in LyXLex::setFile: "
80 "file or stream already set." << endl;
81 fb__.open(filename.c_str(), ios::in);
85 return fb__.is_open() && is.good();
89 void LyXLex::setStream(istream & i)
91 if (fb__.is_open() || is.rdbuf()->in_avail())
92 lyxerr << "Error in LyXLex::setStream: "
93 "file or stream already set." << endl;
101 //NOTE: possible bug.
102 if (next() && status == LEX_TOKEN)
103 return search_kw(buff);
109 int LyXLex::GetInteger() const
114 printError("Bad integer `$$Token'");
120 float LyXLex::GetFloat() const
125 printError("Bad float `$$Token'");
131 string LyXLex::GetString() const
137 // I would prefer to give a tag number instead of an explicit token
138 // here, but it is not possible because Buffer::readLyXformat2 uses
139 // explicit tokens (JMarc)
140 string LyXLex::getLongString(string const & endtoken)
143 bool firstline = true;
147 // blank line in the file being read
150 string const token = frontStrip(strip(GetString()), " \t");
152 lyxerr[Debug::PARSER] << "LongString: `"
153 << GetString() << '\'' << endl;
155 // We do a case independent comparison, like search_kw
157 if (compare_no_case(token, endtoken) != 0) {
158 string tmpstr = GetString();
161 while(i < tmpstr.length()
162 && tmpstr[i] == ' ') {
167 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
172 && prefixIs(tmpstr, prefix.c_str())) {
173 tmpstr.erase(0, prefix.length() - 1);
175 str += tmpstr + '\n';
177 else // token == endtoken
181 printError("Long string not ended by `" + endtoken + '\'');
187 bool LyXLex::GetBool() const
189 if (compare(buff, "true") == 0)
191 else if (compare(buff, "false") != 0)
192 printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
197 bool LyXLex::EatLine()
200 unsigned char c = '\0';
202 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
207 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
208 printError("Line too long");
209 c = '\n'; // Pretend we had an end of line
210 --lineno; // but don't increase line counter (netto effect)
211 ++i; // and preserve last character read.
215 buff[--i] = '\0'; // i can never be 0 here, so no danger
225 int LyXLex::search_kw(char const * const tag) const
227 int m, k = 0 , l = 0, r = no_items;
232 if (lyxerr.debugging(Debug::PARSER)) {
233 lyxerr << "LyXLex::search_kw: elem " << m
234 << " tag " << table[m].tag
235 << " search tag " << tag
240 k = compare_no_case(table[m].tag, tag);
242 return table[m].code;
244 if (k < 0) l = m + 1; else r = m;
250 bool LyXLex::next(bool esc)
253 unsigned char c; // getc() returns an int
256 while (is && !status) {
259 // Read rest of line (fast :-)
260 is.get(buff, sizeof(buff));
261 lyxerr[Debug::LYXLEX] << "Comment read: " << c << buff << endl;
272 } while (c != '\"' && c != '\n' && is &&
273 i != (LEX_MAX_BUFF - 2));
275 if (i == (LEX_MAX_BUFF - 2)) {
276 printError("Line too long");
277 c = '\"'; // Pretend we got a "
282 printError("Missing quote");
293 continue; /* Skip ','s */
295 // using relational operators with chars other
296 // than == and != is not safe. And if it is done
297 // the type _have_ to be unsigned. It usually a
298 // lot better to use the functions from cctype
300 #warning Verify this! (Lgb)
301 //if (isalnum(static_cast<unsigned char>(c)) && is) {
306 } while (c > ' ' && c != ',' && is
307 //} while (isalnum(static_cast<unsigned char>(c))
309 && (i != LEX_MAX_BUFF - 1) );
310 if (i == LEX_MAX_BUFF - 1) {
311 printError("Line too long");
317 if (c == '\r' && is) {
318 // The Windows support has lead to the
319 // possibility of "\r\n" at the end of
320 // a line. This will stop LyX choking
321 // when it expected to find a '\n'
329 if (status) return true;
331 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
335 unsigned char c; // getc() returns an int
338 while (is && !status) {
342 if (c == ',') continue;
349 // escape the next char
354 } while (c > ' ' && c != ',' && is
355 //} while (isalnum(static_cast<unsigned char>(c))
357 && (i != LEX_MAX_BUFF - 1) );
358 if (i == LEX_MAX_BUFF - 1) {
359 printError("Line too long");
367 // Read rest of line (fast :-)
368 is.get(buff, sizeof(buff));
369 lyxerr[Debug::LYXLEX] << "Comment read: " << c << buff << endl;
377 bool escaped = false;
381 if (c == '\r') continue;
383 // escape the next char
389 if (!escaped && c == '\"') break;
390 } while (c != '\n' && is &&
391 i != (LEX_MAX_BUFF - 2));
393 if (i == (LEX_MAX_BUFF - 2)) {
394 printError("Line too long");
395 c = '\"'; // Pretend we got a "
400 printError("Missing quote");
411 //if (isalnum(static_cast<unsigned char>(c)) && is) {
415 // escape the next char
421 } while (c > ' ' && c != ',' && is
422 //} while (isalnum(static_cast<unsigned char>(c))
424 && (i != LEX_MAX_BUFF-1) );
425 if (i == LEX_MAX_BUFF-1) {
426 printError("Line too long");
436 if (status) return true;
438 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
445 bool LyXLex::nextToken()
448 while (is && !status) {
452 if (c >= ' ' && is) {
453 //if (isprint(static_cast<unsigned char>(c)) && is) {
455 if (c == '\\') { // first char == '\\'
459 } while (c > ' ' && c != '\\' && is
460 //} while (isalnum(static_cast<unsigned char>(c))
461 // && c != '\\' && is
462 && i != (LEX_MAX_BUFF-1));
467 } while (c >= ' ' && c != '\\' && is
468 //} while (isprint(static_cast<unsigned char>(c))
469 // && c != '\\' && is
470 && i != (LEX_MAX_BUFF-1));
473 if (i == (LEX_MAX_BUFF - 1)) {
474 printError("Line too long");
477 if (c == '\\') is.putback(c); // put it back
486 if (status) return true;
488 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
494 int LyXLex::FindToken(char const * str[])
499 if (compare(buff, "default")) {
500 for (i = 0; str[i][0] && compare(str[i], buff); ++i);
502 printError("Unknown argument `$$Token'");
507 printError("file ended while scanning string token");
512 int LyXLex::CheckToken(char const * str[], int print_error)
516 if (compare(buff, "default")) {
517 for (i = 0; str[i][0] && compare(str[i], buff); ++i);
520 printError("Unknown argument `$$Token'");