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 string const LyXLex::text() const
57 return &pimpl_->buff[0];
61 istream & LyXLex::getStream()
67 void LyXLex::pushTable(keyword_item * tab, int num)
69 pimpl_->pushTable(tab, num);
73 void LyXLex::popTable()
79 void LyXLex::printTable(ostream & os)
81 pimpl_->printTable(os);
85 void LyXLex::printError(string const & message) const
87 pimpl_->printError(message);
91 bool LyXLex::setFile(string const & filename)
93 return pimpl_->setFile(filename);
97 void LyXLex::setStream(istream & i)
103 void LyXLex::setCommentChar(char c)
105 pimpl_->setCommentChar(c);
110 return pimpl_->lex();
114 int LyXLex::getInteger() const
116 if (isStrInt(pimpl_->getString()))
117 return strToInt(pimpl_->getString());
118 pimpl_->printError("Bad integer `$$Token'");
123 float LyXLex::getFloat() const
125 // replace comma with dot in case the file was written with
126 // the wrong locale (should be rare, but is easy enough to
128 string str = subst(pimpl_->getString(), ",", ".");
130 return strToDbl(str);
131 pimpl_->printError("Bad float `$$Token'");
136 string const LyXLex::getString() const
138 return pimpl_->getString();
142 // I would prefer to give a tag number instead of an explicit token
143 // here, but it is not possible because Buffer::readBody uses
144 // explicit tokens (JMarc)
145 string const LyXLex::getLongString(string const & endtoken)
148 bool firstline = true;
152 // blank line in the file being read
155 string const token = trim(getString(), " \t");
157 lyxerr[Debug::PARSER] << "LongString: `"
158 << getString() << '\'' << endl;
160 // We do a case independent comparison, like search_kw
162 if (compare_ascii_no_case(token, endtoken) == 0)
165 string tmpstr = getString();
167 string::size_type i(tmpstr.find_first_not_of(' '));
168 if (i != string::npos)
169 prefix = tmpstr.substr(0, i);
171 lyxerr[Debug::PARSER]
172 << "Prefix = `" << prefix << "\'" << endl;
175 // further lines in long strings may have the same
176 // whitespace prefix as the first line. Remove it.
177 if (prefix.length() && prefixIs(tmpstr, prefix)) {
178 tmpstr.erase(0, prefix.length() - 1);
181 str += ltrim(tmpstr, "\t") + '\n';
185 printError("Long string not ended by `" + endtoken + '\'');
192 bool LyXLex::getBool() const
194 if (compare(pimpl_->buff, "true") == 0) {
196 } else if (compare(pimpl_->buff, "false") != 0) {
197 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
203 bool LyXLex::eatLine()
205 return pimpl_->eatLine();
209 bool LyXLex::next(bool esc)
211 return pimpl_->next(esc);
215 bool LyXLex::nextToken()
217 return pimpl_->nextToken();
221 void LyXLex::pushToken(string const & pt)
223 pimpl_->pushToken(pt);
227 int LyXLex::findToken(char const * str[])
230 pimpl_->printError("file ended while scanning string token");
236 if (compare(pimpl_->buff, "default")) {
237 while (str[i][0] && compare(str[i], pimpl_->buff)) {
241 pimpl_->printError("Unknown argument `$$Token'");