3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
11 * Full author contact details are available in file CREDITS.
19 #include "lyxlex_pimpl.h"
21 #include "support/convert.h"
22 #include "support/lstrings.h"
24 using lyx::support::compare_ascii_no_case;
25 using lyx::support::isStrDbl;
26 using lyx::support::isStrInt;
27 using lyx::support::ltrim;
28 using lyx::support::prefixIs;
29 using lyx::support::subst;
30 using lyx::support::trim;
38 LyXLex::LyXLex(keyword_item * tab, int num)
39 : pimpl_(new Pimpl(tab, num))
49 bool LyXLex::isOK() const
51 return pimpl_->is.good();
55 void LyXLex::setLineNo(int l)
61 int LyXLex::getLineNo() const
63 return pimpl_->lineno;
67 istream & LyXLex::getStream()
73 void LyXLex::pushTable(keyword_item * tab, int num)
75 pimpl_->pushTable(tab, num);
79 void LyXLex::popTable()
85 void LyXLex::printTable(ostream & os)
87 pimpl_->printTable(os);
91 void LyXLex::printError(string const & message) const
93 pimpl_->printError(message);
97 bool LyXLex::setFile(string const & filename)
99 return pimpl_->setFile(filename);
103 void LyXLex::setStream(istream & i)
105 pimpl_->setStream(i);
109 void LyXLex::setCommentChar(char c)
111 pimpl_->setCommentChar(c);
116 return pimpl_->lex();
120 int LyXLex::getInteger() const
122 if (isStrInt(pimpl_->getString()))
123 return convert<int>(pimpl_->getString());
124 pimpl_->printError("Bad integer `$$Token'");
129 double 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 const str = subst(pimpl_->getString(), ",", ".");
136 return convert<double>(str);
137 pimpl_->printError("Bad float `$$Token'");
142 string const LyXLex::getString() const
144 return pimpl_->getString();
148 // I would prefer to give a tag number instead of an explicit token
149 // here, but it is not possible because Buffer::readDocument uses
150 // explicit tokens (JMarc)
151 string const LyXLex::getLongString(string const & endtoken)
154 bool firstline = true;
158 // blank line in the file being read
161 string const token = trim(getString(), " \t");
163 lyxerr[Debug::PARSER] << "LongString: `"
164 << getString() << '\'' << endl;
166 // We do a case independent comparison, like search_kw does.
167 if (compare_ascii_no_case(token, endtoken) == 0)
170 string tmpstr = getString();
172 string::size_type i(tmpstr.find_first_not_of(' '));
173 if (i != string::npos)
174 prefix = tmpstr.substr(0, i);
176 lyxerr[Debug::PARSER]
177 << "Prefix = `" << prefix << "\'" << endl;
180 // further lines in long strings may have the same
181 // whitespace prefix as the first line. Remove it.
182 if (prefix.length() && prefixIs(tmpstr, prefix)) {
183 tmpstr.erase(0, prefix.length() - 1);
186 str += ltrim(tmpstr, "\t") + '\n';
190 printError("Long string not ended by `" + endtoken + '\'');
197 bool LyXLex::getBool() const
199 if (pimpl_->getString() == "true") {
201 } else if (pimpl_->getString() != "false") {
202 pimpl_->printError("Bad boolean `$$Token'. "
203 "Use \"false\" or \"true\"");
209 bool LyXLex::eatLine()
211 return pimpl_->eatLine();
215 bool LyXLex::next(bool esc)
217 return pimpl_->next(esc);
221 bool LyXLex::nextToken()
223 return pimpl_->nextToken();
227 void LyXLex::pushToken(string const & pt)
229 pimpl_->pushToken(pt);
232 LyXLex::operator void const *() const
234 // This behaviour is NOT the same as the std::streams which would
235 // use fail() here. However, our implementation of getString() et al.
236 // can cause the eof() and fail() bits to be set, even though we
237 // haven't tried to read 'em.
238 return pimpl_->is.bad() ? 0 : this;
242 bool LyXLex::operator!() const
244 return pimpl_->is.bad();
248 LyXLex & LyXLex::operator>>(std::string & s)
258 LyXLex & LyXLex::operator>>(double & s)
268 LyXLex & LyXLex::operator>>(int & s)
278 LyXLex & LyXLex::operator>>(unsigned int & s)
288 LyXLex & LyXLex::operator>>(bool & s)