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 istream & LyXLex::getStream()
61 void LyXLex::pushTable(keyword_item * tab, int num)
63 pimpl_->pushTable(tab, num);
67 void LyXLex::popTable()
73 void LyXLex::printTable(ostream & os)
75 pimpl_->printTable(os);
79 void LyXLex::printError(string const & message) const
81 pimpl_->printError(message);
85 bool LyXLex::setFile(string const & filename)
87 return pimpl_->setFile(filename);
91 void LyXLex::setStream(istream & i)
97 void LyXLex::setCommentChar(char c)
99 pimpl_->setCommentChar(c);
104 return pimpl_->lex();
108 int LyXLex::getInteger() const
110 if (isStrInt(pimpl_->getString()))
111 return strToInt(pimpl_->getString());
112 pimpl_->printError("Bad integer `$$Token'");
117 float LyXLex::getFloat() const
119 // replace comma with dot in case the file was written with
120 // the wrong locale (should be rare, but is easy enough to
122 string str = subst(pimpl_->getString(), ",", ".");
124 return strToDbl(str);
125 pimpl_->printError("Bad float `$$Token'");
130 string const LyXLex::getString() const
132 return pimpl_->getString();
136 // I would prefer to give a tag number instead of an explicit token
137 // here, but it is not possible because Buffer::readBody uses
138 // explicit tokens (JMarc)
139 string const LyXLex::getLongString(string const & endtoken)
142 bool firstline = true;
146 // blank line in the file being read
149 string const token = trim(getString(), " \t");
151 lyxerr[Debug::PARSER] << "LongString: `"
152 << getString() << '\'' << endl;
154 // We do a case independent comparison, like search_kw
156 if (compare_ascii_no_case(token, endtoken) == 0)
159 string tmpstr = getString();
161 string::size_type i(tmpstr.find_first_not_of(' '));
162 if (i != string::npos)
163 prefix = tmpstr.substr(0, i);
165 lyxerr[Debug::PARSER]
166 << "Prefix = `" << prefix << "\'" << endl;
169 // further lines in long strings may have the same
170 // whitespace prefix as the first line. Remove it.
171 if (prefix.length() && prefixIs(tmpstr, prefix)) {
172 tmpstr.erase(0, prefix.length() - 1);
175 str += ltrim(tmpstr, "\t") + '\n';
179 printError("Long string not ended by `" + endtoken + '\'');
186 bool LyXLex::getBool() const
188 if (pimpl_->getString() == "true") {
190 } else if (pimpl_->getString() != "false") {
191 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
197 bool LyXLex::eatLine()
199 return pimpl_->eatLine();
203 bool LyXLex::next(bool esc)
205 return pimpl_->next(esc);
209 bool LyXLex::nextToken()
211 return pimpl_->nextToken();
215 void LyXLex::pushToken(string const & pt)
217 pimpl_->pushToken(pt);
221 int LyXLex::findToken(char const * str[])
224 pimpl_->printError("file ended while scanning string token");
230 string const search_token = pimpl_->getString();
232 if (search_token != "default") {
233 while (str[i][0] && str[i] != search_token) {
237 pimpl_->printError("Unknown argument `$$Token'");