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"
21 using namespace lyx::support;
28 LyXLex::LyXLex(keyword_item * tab, int num)
29 : pimpl_(new Pimpl(tab, num))
39 bool LyXLex::isOK() const
41 return pimpl_->is.good();
45 void LyXLex::setLineNo(int l)
51 int LyXLex::getLineNo() const
53 return pimpl_->lineno;
57 istream & LyXLex::getStream()
63 void LyXLex::pushTable(keyword_item * tab, int num)
65 pimpl_->pushTable(tab, num);
69 void LyXLex::popTable()
75 void LyXLex::printTable(ostream & os)
77 pimpl_->printTable(os);
81 void LyXLex::printError(string const & message) const
83 pimpl_->printError(message);
87 bool LyXLex::setFile(string const & filename)
89 return pimpl_->setFile(filename);
93 void LyXLex::setStream(istream & i)
99 void LyXLex::setCommentChar(char c)
101 pimpl_->setCommentChar(c);
106 return pimpl_->lex();
110 int LyXLex::getInteger() const
112 if (isStrInt(pimpl_->getString()))
113 return strToInt(pimpl_->getString());
114 pimpl_->printError("Bad integer `$$Token'");
119 float LyXLex::getFloat() const
121 // replace comma with dot in case the file was written with
122 // the wrong locale (should be rare, but is easy enough to
124 string str = subst(pimpl_->getString(), ",", ".");
126 return strToDbl(str);
127 pimpl_->printError("Bad float `$$Token'");
132 string const LyXLex::getString() const
134 return pimpl_->getString();
138 // I would prefer to give a tag number instead of an explicit token
139 // here, but it is not possible because Buffer::readBody uses
140 // explicit tokens (JMarc)
141 string const LyXLex::getLongString(string const & endtoken)
144 bool firstline = true;
148 // blank line in the file being read
151 string const token = trim(getString(), " \t");
153 lyxerr[Debug::PARSER] << "LongString: `"
154 << getString() << '\'' << endl;
156 // We do a case independent comparison, like search_kw
158 if (compare_ascii_no_case(token, endtoken) == 0)
161 string tmpstr = getString();
163 string::size_type i(tmpstr.find_first_not_of(' '));
164 if (i != string::npos)
165 prefix = tmpstr.substr(0, i);
167 lyxerr[Debug::PARSER]
168 << "Prefix = `" << prefix << "\'" << endl;
171 // further lines in long strings may have the same
172 // whitespace prefix as the first line. Remove it.
173 if (prefix.length() && prefixIs(tmpstr, prefix)) {
174 tmpstr.erase(0, prefix.length() - 1);
177 str += ltrim(tmpstr, "\t") + '\n';
181 printError("Long string not ended by `" + endtoken + '\'');
188 bool LyXLex::getBool() const
190 if (pimpl_->getString() == "true") {
192 } else if (pimpl_->getString() != "false") {
193 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
199 bool LyXLex::eatLine()
201 return pimpl_->eatLine();
205 bool LyXLex::next(bool esc)
207 return pimpl_->next(esc);
211 bool LyXLex::nextToken()
213 return pimpl_->nextToken();
217 void LyXLex::pushToken(string const & pt)
219 pimpl_->pushToken(pt);
223 int LyXLex::findToken(char const * str[])
226 pimpl_->printError("file ended while scanning string token");
232 string const search_token = pimpl_->getString();
234 if (search_token != "default") {
235 while (str[i][0] && str[i] != search_token) {
239 pimpl_->printError("Unknown argument `$$Token'");