1 // Generalized simple lexical analizer.
2 // It can be used for simple syntax parsers, like lyxrc,
3 // texclass and others to come. [asierra30/03/96]
5 // Copyright 1996 Lyx Team.
10 #pragma implementation "lyxlex.h"
14 #include "lyxlex_pimpl.h"
15 #include "support/filetools.h"
22 LyXLex::LyXLex(keyword_item * tab, int num)
23 : pimpl_(new Pimpl(tab, num))
33 bool LyXLex::IsOK() const
35 return pimpl_->is.good();
39 void LyXLex::setLineNo(int l)
45 int LyXLex::GetLineNo() const
47 return pimpl_->lineno;
50 char const * LyXLex::text() const
52 return &pimpl_->buff[0];
56 std::istream & LyXLex::getStream()
62 void LyXLex::pushTable(keyword_item * tab, int num)
64 pimpl_->pushTable(tab, num);
68 void LyXLex::popTable()
74 void LyXLex::printTable(ostream & os)
76 pimpl_->printTable(os);
80 void LyXLex::printError(string const & message) const
82 pimpl_->printError(message);
86 bool LyXLex::setFile(string const & filename)
88 return pimpl_->setFile(filename);
92 void LyXLex::setStream(istream & i)
100 return pimpl_->lex();
104 int LyXLex::GetInteger() const
106 if (pimpl_->buff[0] > ' ')
107 return atoi(pimpl_->buff);
109 pimpl_->printError("Bad integer `$$Token'");
115 float LyXLex::GetFloat() const
117 if (pimpl_->buff[0] > ' ')
118 return atof(pimpl_->buff);
120 pimpl_->printError("Bad float `$$Token'");
126 string const LyXLex::GetString() const
128 return pimpl_->GetString();
132 // I would prefer to give a tag number instead of an explicit token
133 // here, but it is not possible because Buffer::readLyXformat2 uses
134 // explicit tokens (JMarc)
135 string LyXLex::getLongString(string const & endtoken)
138 bool firstline = true;
142 // blank line in the file being read
145 string const token = frontStrip(strip(GetString()), " \t");
147 lyxerr[Debug::PARSER] << "LongString: `"
148 << GetString() << '\'' << endl;
150 // We do a case independent comparison, like search_kw
152 if (compare_no_case(token, endtoken) != 0) {
153 string tmpstr = GetString();
156 while(i < tmpstr.length()
157 && tmpstr[i] == ' ') {
162 lyxerr[Debug::PARSER] << "Prefix = `" << prefix
167 && prefixIs(tmpstr, prefix.c_str())) {
168 tmpstr.erase(0, prefix.length() - 1);
170 str += tmpstr + '\n';
172 else // token == endtoken
176 printError("Long string not ended by `" + endtoken + '\'');
182 bool LyXLex::GetBool() const
184 if (compare(pimpl_->buff, "true") == 0)
186 else if (compare(pimpl_->buff, "false") != 0)
187 pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
192 bool LyXLex::EatLine()
194 return pimpl_->EatLine();
198 bool LyXLex::next(bool esc)
200 return pimpl_->next(esc);
204 bool LyXLex::nextToken()
206 return pimpl_->nextToken();
210 void LyXLex::pushToken(string const & pt)
212 pimpl_->pushToken(pt);
216 int LyXLex::FindToken(char const * str[])
221 if (compare(pimpl_->buff, "default")) {
222 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
224 pimpl_->printError("Unknown argument `$$Token'");
229 pimpl_->printError("file ended while scanning string token");
234 int LyXLex::CheckToken(char const * str[], int print_error)
238 if (compare(pimpl_->buff, "default")) {
239 for (i = 0; str[i][0] && compare(str[i], pimpl_->buff); ++i);
242 pimpl_->printError("Unknown argument `$$Token'");