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.
17 #include "lyxlex_pimpl.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
22 using namespace lyx::support;
29 LyXLex::LyXLex(keyword_item * tab, int num)
30 : pimpl_(new Pimpl(tab, num))
40 bool LyXLex::isOK() const
42 return pimpl_->is.good();
46 void LyXLex::setLineNo(int l)
52 int LyXLex::getLineNo() const
54 return pimpl_->lineno;
58 istream & LyXLex::getStream()
64 void LyXLex::pushTable(keyword_item * tab, int num)
66 pimpl_->pushTable(tab, num);
70 void LyXLex::popTable()
76 void LyXLex::printTable(ostream & os)
78 pimpl_->printTable(os);
82 void LyXLex::printError(string const & message) const
84 pimpl_->printError(message);
88 bool LyXLex::setFile(string const & filename)
90 return pimpl_->setFile(filename);
94 void LyXLex::setStream(istream & i)
100 void LyXLex::setCommentChar(char c)
102 pimpl_->setCommentChar(c);
107 return pimpl_->lex();
111 int LyXLex::getInteger() const
113 if (isStrInt(pimpl_->getString()))
114 return strToInt(pimpl_->getString());
115 pimpl_->printError("Bad integer `$$Token'");
120 float LyXLex::getFloat() const
122 // replace comma with dot in case the file was written with
123 // the wrong locale (should be rare, but is easy enough to
125 string str = subst(pimpl_->getString(), ",", ".");
127 return strToDbl(str);
128 pimpl_->printError("Bad float `$$Token'");
133 string const LyXLex::getString() const
135 return pimpl_->getString();
139 // I would prefer to give a tag number instead of an explicit token
140 // here, but it is not possible because Buffer::readBody uses
141 // explicit tokens (JMarc)
142 string const LyXLex::getLongString(string const & endtoken)
145 bool firstline = true;
149 // blank line in the file being read
152 string const token = trim(getString(), " \t");
154 lyxerr[Debug::PARSER] << "LongString: `"
155 << getString() << '\'' << endl;
157 // We do a case independent comparison, like search_kw
159 if (compare_ascii_no_case(token, endtoken) == 0)
162 string tmpstr = getString();
164 string::size_type i(tmpstr.find_first_not_of(' '));
165 if (i != string::npos)
166 prefix = tmpstr.substr(0, i);
168 lyxerr[Debug::PARSER]
169 << "Prefix = `" << prefix << "\'" << endl;
172 // further lines in long strings may have the same
173 // whitespace prefix as the first line. Remove it.
174 if (prefix.length() && prefixIs(tmpstr, prefix)) {
175 tmpstr.erase(0, prefix.length() - 1);
178 str += ltrim(tmpstr, "\t") + '\n';
182 printError("Long string not ended by `" + endtoken + '\'');
189 bool LyXLex::getBool() const
191 if (pimpl_->getString() == "true") {
193 } else if (pimpl_->getString() != "false") {
194 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
200 bool LyXLex::eatLine()
202 return pimpl_->eatLine();
206 bool LyXLex::next(bool esc)
208 return pimpl_->next(esc);
212 bool LyXLex::nextToken()
214 return pimpl_->nextToken();
218 void LyXLex::pushToken(string const & pt)
220 pimpl_->pushToken(pt);
224 int LyXLex::findToken(char const * str[])
227 pimpl_->printError("file ended while scanning string token");
233 string const search_token = pimpl_->getString();
235 if (search_token != "default") {
236 while (str[i][0] && str[i] != search_token) {
240 pimpl_->printError("Unknown argument `$$Token'");