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 #include "lyxlex_pimpl.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
26 LyXLex::LyXLex(keyword_item * tab, int num)
27 : pimpl_(new Pimpl(tab, num))
37 bool LyXLex::isOK() const
39 return pimpl_->is.good();
43 void LyXLex::setLineNo(int l)
49 int LyXLex::getLineNo() const
51 return pimpl_->lineno;
55 string const LyXLex::text() const
57 return &pimpl_->buff[0];
61 istream & LyXLex::getStream()
67 void LyXLex::pushTable(keyword_item * tab, int num)
69 pimpl_->pushTable(tab, num);
73 void LyXLex::popTable()
79 void LyXLex::printTable(ostream & os)
81 pimpl_->printTable(os);
85 void LyXLex::printError(string const & message) const
87 pimpl_->printError(message);
91 bool LyXLex::setFile(string const & filename)
93 return pimpl_->setFile(filename);
97 void LyXLex::setStream(istream & i)
103 void LyXLex::setCommentChar(char c)
105 pimpl_->setCommentChar(c);
110 return pimpl_->lex();
114 int LyXLex::getInteger() const
116 if (isStrInt(pimpl_->getString())) {
117 return strToInt(pimpl_->getString());
119 pimpl_->printError("Bad integer `$$Token'");
125 float LyXLex::getFloat() const
127 // replace comma with dot in case the file was written with
128 // the wrong locale (should be rare, but is easy enough to
130 string str = subst(pimpl_->getString(), ",", ".");
132 return strToDbl(str);
134 pimpl_->printError("Bad float `$$Token'");
140 string const LyXLex::getString() const
142 return pimpl_->getString();
146 // I would prefer to give a tag number instead of an explicit token
147 // here, but it is not possible because Buffer::readLyXformat2 uses
148 // explicit tokens (JMarc)
149 string const LyXLex::getLongString(string const & endtoken)
152 bool firstline = true;
156 // blank line in the file being read
159 string const token = trim(getString(), " \t");
161 lyxerr[Debug::PARSER] << "LongString: `"
162 << getString() << '\'' << endl;
164 // We do a case independent comparison, like search_kw
166 if (compare_ascii_no_case(token, endtoken) == 0)
169 string tmpstr = getString();
171 string::size_type i(tmpstr.find_first_not_of(' '));
172 if (i != string::npos)
173 prefix = tmpstr.substr(0, i);
175 lyxerr[Debug::PARSER]
176 << "Prefix = `" << prefix << "\'" << endl;
179 // further lines in long strings may have the same
180 // whitespace prefix as the first line. Remove it.
181 if (prefix.length() && prefixIs(tmpstr, prefix)) {
182 tmpstr.erase(0, prefix.length() - 1);
185 str += ltrim(tmpstr, "\t") + '\n';
189 printError("Long string not ended by `" + endtoken + '\'');
196 bool LyXLex::getBool() const
198 if (compare(pimpl_->buff, "true") == 0) {
200 } else if (compare(pimpl_->buff, "false") != 0) {
201 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
207 bool LyXLex::eatLine()
209 return pimpl_->eatLine();
213 bool LyXLex::next(bool esc)
215 return pimpl_->next(esc);
219 bool LyXLex::nextToken()
221 return pimpl_->nextToken();
225 void LyXLex::pushToken(string const & pt)
227 pimpl_->pushToken(pt);
231 int LyXLex::findToken(char const * str[])
234 pimpl_->printError("file ended while scanning string token");
240 if (compare(pimpl_->buff, "default")) {
241 while (str[i][0] && compare(str[i], pimpl_->buff)) {
245 pimpl_->printError("Unknown argument `$$Token'");