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)) {
205 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
210 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
211 printError("Line too long");
212 c = '\n'; // Pretend we had an end of line
213 --lineno; // but don't increase line counter (netto effect)
214 ++i; // and preserve last character read.
218 buff[--i] = '\0'; // i can never be 0 here, so no danger
228 int LyXLex::search_kw(char const * const tag) const
230 int m, k = 0 , l = 0, r = no_items;
235 if (lyxerr.debugging(Debug::PARSER)) {
236 lyxerr << "LyXLex::search_kw: elem " << m
237 << " tag " << table[m].tag
238 << " search tag " << tag
243 k = compare_no_case(table[m].tag, tag);
245 return table[m].code;
247 if (k < 0) l = m + 1; else r = m;
253 bool LyXLex::next(bool esc)
256 unsigned char c = 0; // getc() returns an int
259 while (is && !status) {
263 // Read rest of line (fast :-)
264 is.getline(buff, sizeof(buff));
265 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
266 << buff << "'" << endl;
278 } while (c != '\"' && c != '\n' && is &&
279 i != (LEX_MAX_BUFF - 2));
281 if (i == (LEX_MAX_BUFF - 2)) {
282 printError("Line too long");
283 c = '\"'; // Pretend we got a "
288 printError("Missing quote");
299 continue; /* Skip ','s */
301 // using relational operators with chars other
302 // than == and != is not safe. And if it is done
303 // the type _have_ to be unsigned. It usually a
304 // lot better to use the functions from cctype
311 } while (c > ' ' && c != ',' && is
312 && (i != LEX_MAX_BUFF - 1) );
313 if (i == LEX_MAX_BUFF - 1) {
314 printError("Line too long");
320 if (c == '\r' && is) {
321 // The Windows support has lead to the
322 // possibility of "\r\n" at the end of
323 // a line. This will stop LyX choking
324 // when it expected to find a '\n'
333 if (status) return true;
335 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
339 unsigned char c = 0; // getc() returns an int
343 while (is && !status) {
348 if (c == ',') continue;
355 // escape the next char
362 } while (c > ' ' && c != ',' && is
363 && (i != LEX_MAX_BUFF - 1) );
364 if (i == LEX_MAX_BUFF - 1) {
365 printError("Line too long");
373 // Read rest of line (fast :-)
374 is.getline(buff, sizeof(buff));
375 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
376 << buff << "'" << endl;
384 bool escaped = false;
389 if (c == '\r') continue;
391 // escape the next char
398 if (!escaped && c == '\"') break;
399 } while (c != '\n' && is &&
400 i != (LEX_MAX_BUFF - 2));
402 if (i == (LEX_MAX_BUFF - 2)) {
403 printError("Line too long");
404 c = '\"'; // Pretend we got a "
409 printError("Missing quote");
423 // escape the next char
431 } while (c > ' ' && c != ',' && is
432 && (i != LEX_MAX_BUFF-1) );
433 if (i == LEX_MAX_BUFF-1) {
434 printError("Line too long");
444 if (status) return true;
446 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
453 bool LyXLex::nextToken()
456 while (is && !status) {
461 if (c >= ' ' && is) {
463 if (c == '\\') { // first char == '\\'
468 } while (c > ' ' && c != '\\' && is
469 && i != (LEX_MAX_BUFF-1));
475 } while (c >= ' ' && c != '\\' && is
476 && i != (LEX_MAX_BUFF-1));
479 if (i == (LEX_MAX_BUFF - 1)) {
480 printError("Line too long");
483 if (c == '\\') is.putback(c); // put it back
492 if (status) return true;
494 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
500 int LyXLex::FindToken(char const * str[])
505 if (compare(buff, "default")) {
506 for (i = 0; str[i][0] && compare(str[i], buff); ++i);
508 printError("Unknown argument `$$Token'");
513 printError("file ended while scanning string token");
518 int LyXLex::CheckToken(char const * str[], int print_error)
522 if (compare(buff, "default")) {
523 for (i = 0; str[i][0] && compare(str[i], buff); ++i);
526 printError("Unknown argument `$$Token'");