1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1996-2001 The LyX Team.
8 * Generalized simple lexical analizer.
9 * It can be used for simple syntax parsers, like lyxrc,
10 * texclass and others to come. [asierra30/03/96]
12 * ====================================================== */
17 #pragma implementation "lyxlex.h"
21 #include "lyxlex_pimpl.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
31 LyXLex::LyXLex(keyword_item * tab, int num)
32 : pimpl_(new Pimpl(tab, num))
42 bool LyXLex::isOK() const
44 return pimpl_->is.good();
48 void LyXLex::setLineNo(int l)
54 int LyXLex::getLineNo() const
56 return pimpl_->lineno;
60 string const LyXLex::text() const
62 return &pimpl_->buff[0];
66 std::istream & LyXLex::getStream()
72 void LyXLex::pushTable(keyword_item * tab, int num)
74 pimpl_->pushTable(tab, num);
78 void LyXLex::popTable()
84 void LyXLex::printTable(ostream & os)
86 pimpl_->printTable(os);
90 void LyXLex::printError(string const & message) const
92 pimpl_->printError(message);
96 bool LyXLex::setFile(string const & filename)
98 return pimpl_->setFile(filename);
102 void LyXLex::setStream(istream & i)
104 pimpl_->setStream(i);
108 void LyXLex::setCommentChar(char c)
110 pimpl_->setCommentChar(c);
115 return pimpl_->lex();
119 int LyXLex::getInteger() const
121 if (isStrInt(pimpl_->getString()))
122 return strToInt(pimpl_->getString());
124 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);
139 pimpl_->printError("Bad float `$$Token'");
145 string const LyXLex::getString() const
147 return pimpl_->getString();
151 // I would prefer to give a tag number instead of an explicit token
152 // here, but it is not possible because Buffer::readLyXformat2 uses
153 // explicit tokens (JMarc)
154 string const LyXLex::getLongString(string const & endtoken)
157 bool firstline = true;
161 // blank line in the file being read
164 string const token = frontStrip(strip(getString()), " \t");
166 lyxerr[Debug::PARSER] << "LongString: `"
167 << getString() << '\'' << endl;
169 // We do a case independent comparison, like search_kw
171 if (compare_no_case(token, endtoken) != 0) {
172 string tmpstr = getString();
175 while(i < tmpstr.length()
176 && tmpstr[i] == ' ') {
181 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
186 && prefixIs(tmpstr, prefix)) {
187 tmpstr.erase(0, prefix.length() - 1);
189 str += frontStrip(tmpstr, "\t") + '\n';
191 else // token == endtoken
195 printError("Long string not ended by `" + endtoken + '\'');
201 bool LyXLex::getBool() const
203 if (compare(pimpl_->buff, "true") == 0)
205 else if (compare(pimpl_->buff, "false") != 0)
206 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
211 bool LyXLex::eatLine()
213 return pimpl_->eatLine();
217 bool LyXLex::next(bool esc)
219 return pimpl_->next(esc);
223 bool LyXLex::nextToken()
225 return pimpl_->nextToken();
229 void LyXLex::pushToken(string const & pt)
231 pimpl_->pushToken(pt);
235 int LyXLex::findToken(char const * str[])
240 if (compare(pimpl_->buff, "default")) {
241 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
243 pimpl_->printError("Unknown argument `$$Token'");
248 pimpl_->printError("file ended while scanning string token");
253 int LyXLex::checkToken(char const * str[], int print_error)
257 if (compare(pimpl_->buff, "default")) {
258 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
261 pimpl_->printError("Unknown argument `$$Token'");