3 * Copyright 1996-2002 the LyX Team
4 * Read the file COPYING
6 * Generalized simple lexical analyzer.
7 * It can be used for simple syntax parsers, like lyxrc,
8 * texclass and others to come.
10 * \author Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
16 #pragma implementation "lyxlex.h"
20 #include "lyxlex_pimpl.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
30 LyXLex::LyXLex(keyword_item * tab, int num)
31 : pimpl_(new Pimpl(tab, num))
41 bool LyXLex::isOK() const
43 return pimpl_->is.good();
47 void LyXLex::setLineNo(int l)
53 int LyXLex::getLineNo() const
55 return pimpl_->lineno;
59 string const LyXLex::text() const
61 return &pimpl_->buff[0];
65 istream & LyXLex::getStream()
71 void LyXLex::pushTable(keyword_item * tab, int num)
73 pimpl_->pushTable(tab, num);
77 void LyXLex::popTable()
83 void LyXLex::printTable(ostream & os)
85 pimpl_->printTable(os);
89 void LyXLex::printError(string const & message) const
91 pimpl_->printError(message);
95 bool LyXLex::setFile(string const & filename)
97 return pimpl_->setFile(filename);
101 void LyXLex::setStream(istream & i)
103 pimpl_->setStream(i);
107 void LyXLex::setCommentChar(char c)
109 pimpl_->setCommentChar(c);
114 return pimpl_->lex();
118 int LyXLex::getInteger() const
120 if (isStrInt(pimpl_->getString())) {
121 return strToInt(pimpl_->getString());
123 pimpl_->printError("Bad integer `$$Token'");
129 float LyXLex::getFloat() const
131 // replace comma with dot in case the file was written with
132 // the wrong locale (should be rare, but is easy enough to
134 string str = subst(pimpl_->getString(), ",", ".");
136 return strToDbl(str);
138 pimpl_->printError("Bad float `$$Token'");
144 string const LyXLex::getString() const
146 return pimpl_->getString();
150 // I would prefer to give a tag number instead of an explicit token
151 // here, but it is not possible because Buffer::readLyXformat2 uses
152 // explicit tokens (JMarc)
153 string const LyXLex::getLongString(string const & endtoken)
156 bool firstline = true;
160 // blank line in the file being read
163 string const token = trim(getString(), " \t");
165 lyxerr[Debug::PARSER] << "LongString: `"
166 << getString() << '\'' << endl;
168 // We do a case independent comparison, like search_kw
170 if (compare_ascii_no_case(token, endtoken) == 0)
173 string tmpstr = getString();
175 string::size_type i(tmpstr.find_first_not_of(' '));
176 if (i != string::npos)
177 prefix = tmpstr.substr(0, i);
179 lyxerr[Debug::PARSER]
180 << "Prefix = `" << prefix << "\'" << endl;
183 // further lines in long strings may have the same
184 // whitespace prefix as the first line. Remove it.
185 if (prefix.length() && prefixIs(tmpstr, prefix)) {
186 tmpstr.erase(0, prefix.length() - 1);
189 str += ltrim(tmpstr, "\t") + '\n';
193 printError("Long string not ended by `" + endtoken + '\'');
200 bool LyXLex::getBool() const
202 if (compare(pimpl_->buff, "true") == 0) {
204 } else if (compare(pimpl_->buff, "false") != 0) {
205 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
211 bool LyXLex::eatLine()
213 return pimpl_->eatLine();
217 bool LyXLex::next(bool esc)
219 return pimpl_->next(esc);
223 bool LyXLex::nextToken()
225 return pimpl_->nextToken();
229 void LyXLex::pushToken(string const & pt)
231 pimpl_->pushToken(pt);
235 int LyXLex::findToken(char const * str[])
238 pimpl_->printError("file ended while scanning string token");
244 if (compare(pimpl_->buff, "default")) {
245 while (str[i][0] && compare(str[i], pimpl_->buff)) {
249 pimpl_->printError("Unknown argument `$$Token'");