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"
29 LyXLex::LyXLex(keyword_item * tab, int num)
30 : pimpl_(new Pimpl(tab, num))
40 bool LyXLex::IsOK() const
42 return pimpl_->is.good();
46 void LyXLex::setLineNo(int l)
52 int LyXLex::GetLineNo() const
54 return pimpl_->lineno;
58 string const LyXLex::text() const
60 return &pimpl_->buff[0];
64 std::istream & LyXLex::getStream()
70 void LyXLex::pushTable(keyword_item * tab, int num)
72 pimpl_->pushTable(tab, num);
76 void LyXLex::popTable()
82 void LyXLex::printTable(ostream & os)
84 pimpl_->printTable(os);
88 void LyXLex::printError(string const & message) const
90 pimpl_->printError(message);
94 bool LyXLex::setFile(string const & filename)
96 return pimpl_->setFile(filename);
100 void LyXLex::setStream(istream & i)
102 pimpl_->setStream(i);
108 return pimpl_->lex();
112 int LyXLex::GetInteger() const
114 if (pimpl_->buff[0] > ' ')
115 return atoi(pimpl_->buff);
117 pimpl_->printError("Bad integer `$$Token'");
123 float LyXLex::GetFloat() const
125 if (pimpl_->buff[0] > ' ')
126 return atof(pimpl_->buff);
128 pimpl_->printError("Bad float `$$Token'");
134 string const LyXLex::GetString() const
136 return pimpl_->GetString();
140 // I would prefer to give a tag number instead of an explicit token
141 // here, but it is not possible because Buffer::readLyXformat2 uses
142 // explicit tokens (JMarc)
143 string const LyXLex::getLongString(string const & endtoken)
146 bool firstline = true;
150 // blank line in the file being read
153 string const token = frontStrip(strip(GetString()), " \t");
155 lyxerr[Debug::PARSER] << "LongString: `"
156 << GetString() << '\'' << endl;
158 // We do a case independent comparison, like search_kw
160 if (compare_no_case(token, endtoken) != 0) {
161 string tmpstr = GetString();
164 while(i < tmpstr.length()
165 && tmpstr[i] == ' ') {
170 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
175 && prefixIs(tmpstr, prefix.c_str())) {
176 tmpstr.erase(0, prefix.length() - 1);
178 str += frontStrip(tmpstr, "\t") + '\n';
180 else // token == endtoken
184 printError("Long string not ended by `" + endtoken + '\'');
190 bool LyXLex::GetBool() const
192 if (compare(pimpl_->buff, "true") == 0)
194 else if (compare(pimpl_->buff, "false") != 0)
195 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
200 bool LyXLex::EatLine()
202 return pimpl_->EatLine();
206 bool LyXLex::next(bool esc)
208 return pimpl_->next(esc);
212 bool LyXLex::nextToken()
214 return pimpl_->nextToken();
218 void LyXLex::pushToken(string const & pt)
220 pimpl_->pushToken(pt);
224 int LyXLex::FindToken(char const * str[])
229 if (compare(pimpl_->buff, "default")) {
230 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
232 pimpl_->printError("Unknown argument `$$Token'");
237 pimpl_->printError("file ended while scanning string token");
242 int LyXLex::CheckToken(char const * str[], int print_error)
246 if (compare(pimpl_->buff, "default")) {
247 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
250 pimpl_->printError("Unknown argument `$$Token'");