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;
57 char const * LyXLex::text() const
59 return &pimpl_->buff[0];
63 std::istream & LyXLex::getStream()
69 void LyXLex::pushTable(keyword_item * tab, int num)
71 pimpl_->pushTable(tab, num);
75 void LyXLex::popTable()
81 void LyXLex::printTable(ostream & os)
83 pimpl_->printTable(os);
87 void LyXLex::printError(string const & message) const
89 pimpl_->printError(message);
93 bool LyXLex::setFile(string const & filename)
95 return pimpl_->setFile(filename);
99 void LyXLex::setStream(istream & i)
101 pimpl_->setStream(i);
107 return pimpl_->lex();
111 int LyXLex::GetInteger() const
113 if (pimpl_->buff[0] > ' ')
114 return atoi(pimpl_->buff);
116 pimpl_->printError("Bad integer `$$Token'");
122 float LyXLex::GetFloat() const
124 if (pimpl_->buff[0] > ' ')
125 return atof(pimpl_->buff);
127 pimpl_->printError("Bad float `$$Token'");
133 string LyXLex::GetString() const
135 return pimpl_->GetString();
139 // I would prefer to give a tag number instead of an explicit token
140 // here, but it is not possible because Buffer::readLyXformat2 uses
141 // explicit tokens (JMarc)
142 string LyXLex::getLongString(string const & endtoken)
145 bool firstline = true;
149 // blank line in the file being read
152 string const token = frontStrip(strip(GetString()), " \t");
154 lyxerr[Debug::PARSER] << "LongString: `"
155 << GetString() << '\'' << endl;
157 // We do a case independent comparison, like search_kw
159 if (compare_no_case(token, endtoken) != 0) {
160 string tmpstr = GetString();
163 while(i < tmpstr.length()
164 && tmpstr[i] == ' ') {
169 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
174 && prefixIs(tmpstr, prefix.c_str())) {
175 tmpstr.erase(0, prefix.length() - 1);
177 str += frontStrip(tmpstr, "\t") + '\n';
179 else // token == endtoken
183 printError("Long string not ended by `" + endtoken + '\'');
189 bool LyXLex::GetBool() const
191 if (compare(pimpl_->buff, "true") == 0)
193 else if (compare(pimpl_->buff, "false") != 0)
194 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
199 bool LyXLex::EatLine()
201 return pimpl_->EatLine();
205 bool LyXLex::next(bool esc)
207 return pimpl_->next(esc);
211 bool LyXLex::nextToken()
213 return pimpl_->nextToken();
217 void LyXLex::pushToken(string const & pt)
219 pimpl_->pushToken(pt);
223 int LyXLex::FindToken(char const * str[])
228 if (compare(pimpl_->buff, "default")) {
229 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
231 pimpl_->printError("Unknown argument `$$Token'");
236 pimpl_->printError("file ended while scanning string token");
241 int LyXLex::CheckToken(char const * str[], int print_error)
245 if (compare(pimpl_->buff, "default")) {
246 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
249 pimpl_->printError("Unknown argument `$$Token'");