1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1996-2000 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"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
30 LyXLex::LyXLex(keyword_item * tab, int num)
31 : pimpl_(new Pimpl(tab, num))
41 bool LyXLex::IsOK() const
43 return pimpl_->is.good();
47 void LyXLex::setLineNo(int l)
53 int LyXLex::GetLineNo() const
55 return pimpl_->lineno;
59 string const LyXLex::text() const
61 return &pimpl_->buff[0];
65 std::istream & LyXLex::getStream()
71 void LyXLex::pushTable(keyword_item * tab, int num)
73 pimpl_->pushTable(tab, num);
77 void LyXLex::popTable()
83 void LyXLex::printTable(ostream & os)
85 pimpl_->printTable(os);
89 void LyXLex::printError(string const & message) const
91 pimpl_->printError(message);
95 bool LyXLex::setFile(string const & filename)
97 return pimpl_->setFile(filename);
101 void LyXLex::setStream(istream & i)
103 pimpl_->setStream(i);
107 void LyXLex::setCommentChar(char c)
109 pimpl_->setCommentChar(c);
114 return pimpl_->lex();
118 int LyXLex::GetInteger() const
120 if (isStrInt(pimpl_->GetString()))
121 return strToInt(pimpl_->GetString());
123 pimpl_->printError("Bad integer `$$Token'");
129 float LyXLex::GetFloat() const
131 // replace comma with dot in case the file was written with
132 // the wrong locale (should be rare, but is easy enough to
134 string str = subst(pimpl_->GetString(), ",", ".");
136 return strToDbl(str);
138 pimpl_->printError("Bad float `$$Token'");
144 string const LyXLex::GetString() const
146 return pimpl_->GetString();
150 // I would prefer to give a tag number instead of an explicit token
151 // here, but it is not possible because Buffer::readLyXformat2 uses
152 // explicit tokens (JMarc)
153 string const LyXLex::getLongString(string const & endtoken)
156 bool firstline = true;
160 // blank line in the file being read
163 string const token = frontStrip(strip(GetString()), " \t");
165 lyxerr[Debug::PARSER] << "LongString: `"
166 << GetString() << '\'' << endl;
168 // We do a case independent comparison, like search_kw
170 if (compare_no_case(token, endtoken) != 0) {
171 string tmpstr = GetString();
174 while(i < tmpstr.length()
175 && tmpstr[i] == ' ') {
180 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
185 && prefixIs(tmpstr, prefix)) {
186 tmpstr.erase(0, prefix.length() - 1);
188 str += frontStrip(tmpstr, "\t") + '\n';
190 else // token == endtoken
194 printError("Long string not ended by `" + endtoken + '\'');
200 bool LyXLex::GetBool() const
202 if (compare(pimpl_->buff, "true") == 0)
204 else if (compare(pimpl_->buff, "false") != 0)
205 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
210 bool LyXLex::EatLine()
212 return pimpl_->EatLine();
216 bool LyXLex::next(bool esc)
218 return pimpl_->next(esc);
222 bool LyXLex::nextToken()
224 return pimpl_->nextToken();
228 void LyXLex::pushToken(string const & pt)
230 pimpl_->pushToken(pt);
234 int LyXLex::FindToken(char const * str[])
239 if (compare(pimpl_->buff, "default")) {
240 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
242 pimpl_->printError("Unknown argument `$$Token'");
247 pimpl_->printError("file ended while scanning string token");
252 int LyXLex::CheckToken(char const * str[], int print_error)
256 if (compare(pimpl_->buff, "default")) {
257 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
260 pimpl_->printError("Unknown argument `$$Token'");