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/lstrings.h"
23 using lyx::support::compare_ascii_no_case;
24 using lyx::support::isStrDbl;
25 using lyx::support::isStrInt;
26 using lyx::support::ltrim;
27 using lyx::support::prefixIs;
28 using lyx::support::strToDbl;
29 using lyx::support::strToInt;
30 using lyx::support::subst;
31 using lyx::support::trim;
39 LyXLex::LyXLex(keyword_item * tab, int num)
40 : pimpl_(new Pimpl(tab, num))
50 bool LyXLex::isOK() const
52 return pimpl_->is.good();
56 void LyXLex::setLineNo(int l)
62 int LyXLex::getLineNo() const
64 return pimpl_->lineno;
68 istream & LyXLex::getStream()
74 void LyXLex::pushTable(keyword_item * tab, int num)
76 pimpl_->pushTable(tab, num);
80 void LyXLex::popTable()
86 void LyXLex::printTable(ostream & os)
88 pimpl_->printTable(os);
92 void LyXLex::printError(string const & message) const
94 pimpl_->printError(message);
98 bool LyXLex::setFile(string const & filename)
100 return pimpl_->setFile(filename);
104 void LyXLex::setStream(istream & i)
106 pimpl_->setStream(i);
110 void LyXLex::setCommentChar(char c)
112 pimpl_->setCommentChar(c);
117 return pimpl_->lex();
121 int LyXLex::getInteger() const
123 if (isStrInt(pimpl_->getString()))
124 return strToInt(pimpl_->getString());
125 pimpl_->printError("Bad integer `$$Token'");
130 float LyXLex::getFloat() const
132 // replace comma with dot in case the file was written with
133 // the wrong locale (should be rare, but is easy enough to
135 string str = subst(pimpl_->getString(), ",", ".");
137 return strToDbl(str);
138 pimpl_->printError("Bad float `$$Token'");
143 string const LyXLex::getString() const
145 return pimpl_->getString();
149 // I would prefer to give a tag number instead of an explicit token
150 // here, but it is not possible because Buffer::readBody uses
151 // explicit tokens (JMarc)
152 string const LyXLex::getLongString(string const & endtoken)
155 bool firstline = true;
159 // blank line in the file being read
162 string const token = trim(getString(), " \t");
164 lyxerr[Debug::PARSER] << "LongString: `"
165 << getString() << '\'' << endl;
167 // We do a case independent comparison, like search_kw does.
168 if (compare_ascii_no_case(token, endtoken) == 0)
171 string tmpstr = getString();
173 string::size_type i(tmpstr.find_first_not_of(' '));
174 if (i != string::npos)
175 prefix = tmpstr.substr(0, i);
177 lyxerr[Debug::PARSER]
178 << "Prefix = `" << prefix << "\'" << endl;
181 // further lines in long strings may have the same
182 // whitespace prefix as the first line. Remove it.
183 if (prefix.length() && prefixIs(tmpstr, prefix)) {
184 tmpstr.erase(0, prefix.length() - 1);
187 str += ltrim(tmpstr, "\t") + '\n';
191 printError("Long string not ended by `" + endtoken + '\'');
198 bool LyXLex::getBool() const
200 if (pimpl_->getString() == "true") {
202 } else if (pimpl_->getString() != "false") {
203 pimpl_->printError("Bad boolean `$$Token'. "
204 "Use \"false\" or \"true\"");
210 bool LyXLex::eatLine()
212 return pimpl_->eatLine();
216 bool LyXLex::next(bool esc)
218 return pimpl_->next(esc);
222 bool LyXLex::nextToken()
224 return pimpl_->nextToken();
228 void LyXLex::pushToken(string const & pt)
230 pimpl_->pushToken(pt);
234 int LyXLex::findToken(char const * str[])
237 pimpl_->printError("file ended while scanning string token");
243 string const search_token = pimpl_->getString();
245 if (search_token != "default") {
246 while (str[i][0] && str[i] != search_token) {
250 pimpl_->printError("Unknown argument `$$Token'");