2002-08-04 23:11:50 +00:00
|
|
|
/**
|
|
|
|
* \file lyxlex.C
|
|
|
|
* Copyright 1996-2002 the LyX Team
|
|
|
|
* Read the file COPYING
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2002-08-04 23:11:50 +00:00
|
|
|
* Generalized simple lexical analyzer.
|
|
|
|
* It can be used for simple syntax parsers, like lyxrc,
|
|
|
|
* texclass and others to come.
|
2000-07-24 13:53:19 +00:00
|
|
|
*
|
2002-08-04 23:11:50 +00:00
|
|
|
* \author Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "lyxlex.h"
|
2000-04-08 17:02:02 +00:00
|
|
|
#include "lyxlex_pimpl.h"
|
2001-07-29 15:34:18 +00:00
|
|
|
#include "debug.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/filetools.h"
|
2000-09-26 13:10:34 +00:00
|
|
|
#include "support/lstrings.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
using std::ostream;
|
|
|
|
using std::istream;
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
2000-03-09 03:36:48 +00:00
|
|
|
|
|
|
|
|
2000-04-08 17:02:02 +00:00
|
|
|
LyXLex::LyXLex(keyword_item * tab, int num)
|
|
|
|
: pimpl_(new Pimpl(tab, num))
|
|
|
|
{}
|
2000-03-09 03:36:48 +00:00
|
|
|
|
2000-01-06 11:35:29 +00:00
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
LyXLex::~LyXLex()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
delete pimpl_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
bool LyXLex::isOK() const
|
2000-04-08 17:02:02 +00:00
|
|
|
{
|
|
|
|
return pimpl_->is.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LyXLex::setLineNo(int l)
|
|
|
|
{
|
|
|
|
pimpl_->lineno = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
int LyXLex::getLineNo() const
|
2000-04-08 17:02:02 +00:00
|
|
|
{
|
|
|
|
return pimpl_->lineno;
|
|
|
|
}
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
istream & LyXLex::getStream()
|
2000-04-08 17:02:02 +00:00
|
|
|
{
|
|
|
|
return pimpl_->is;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
void LyXLex::pushTable(keyword_item * tab, int num)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
pimpl_->pushTable(tab, num);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LyXLex::popTable()
|
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
pimpl_->popTable();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-09 03:36:48 +00:00
|
|
|
void LyXLex::printTable(ostream & os)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
pimpl_->printTable(os);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-01 00:57:31 +00:00
|
|
|
void LyXLex::printError(string const & message) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
pimpl_->printError(message);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
bool LyXLex::setFile(string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
return pimpl_->setFile(filename);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-01-06 02:44:26 +00:00
|
|
|
void LyXLex::setStream(istream & i)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
pimpl_->setStream(i);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-08 15:19:55 +00:00
|
|
|
void LyXLex::setCommentChar(char c)
|
|
|
|
{
|
|
|
|
pimpl_->setCommentChar(c);
|
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
int LyXLex::lex()
|
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
return pimpl_->lex();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
int LyXLex::getInteger() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2003-02-26 18:03:48 +00:00
|
|
|
if (isStrInt(pimpl_->getString()))
|
2001-08-06 19:12:46 +00:00
|
|
|
return strToInt(pimpl_->getString());
|
2003-02-26 18:03:48 +00:00
|
|
|
pimpl_->printError("Bad integer `$$Token'");
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
float LyXLex::getFloat() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-09-26 13:10:34 +00:00
|
|
|
// replace comma with dot in case the file was written with
|
|
|
|
// the wrong locale (should be rare, but is easy enough to
|
2002-03-21 17:27:08 +00:00
|
|
|
// avoid).
|
2001-08-06 19:12:46 +00:00
|
|
|
string str = subst(pimpl_->getString(), ",", ".");
|
2000-09-26 13:10:34 +00:00
|
|
|
if (isStrDbl(str))
|
|
|
|
return strToDbl(str);
|
2003-02-26 18:03:48 +00:00
|
|
|
pimpl_->printError("Bad float `$$Token'");
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
string const LyXLex::getString() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-08-06 19:12:46 +00:00
|
|
|
return pimpl_->getString();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// I would prefer to give a tag number instead of an explicit token
|
2003-03-12 02:39:12 +00:00
|
|
|
// here, but it is not possible because Buffer::readBody uses
|
2002-03-21 17:27:08 +00:00
|
|
|
// explicit tokens (JMarc)
|
2000-08-05 05:17:18 +00:00
|
|
|
string const LyXLex::getLongString(string const & endtoken)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string str, prefix;
|
1999-09-27 18:44:28 +00:00
|
|
|
bool firstline = true;
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
while (isOK()) {
|
|
|
|
if (!eatLine())
|
1999-09-27 18:44:28 +00:00
|
|
|
// blank line in the file being read
|
|
|
|
continue;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
string const token = trim(getString(), " \t");
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::PARSER] << "LongString: `"
|
2001-08-06 19:12:46 +00:00
|
|
|
<< getString() << '\'' << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// We do a case independent comparison, like search_kw
|
|
|
|
// does.
|
2002-12-01 22:59:25 +00:00
|
|
|
if (compare_ascii_no_case(token, endtoken) == 0)
|
2002-08-02 16:18:15 +00:00
|
|
|
break;
|
2002-08-04 23:11:50 +00:00
|
|
|
|
|
|
|
string tmpstr = getString();
|
|
|
|
if (firstline) {
|
|
|
|
string::size_type i(tmpstr.find_first_not_of(' '));
|
|
|
|
if (i != string::npos)
|
|
|
|
prefix = tmpstr.substr(0, i);
|
|
|
|
firstline = false;
|
|
|
|
lyxerr[Debug::PARSER]
|
|
|
|
<< "Prefix = `" << prefix << "\'" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// further lines in long strings may have the same
|
|
|
|
// whitespace prefix as the first line. Remove it.
|
|
|
|
if (prefix.length() && prefixIs(tmpstr, prefix)) {
|
|
|
|
tmpstr.erase(0, prefix.length() - 1);
|
|
|
|
}
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
str += ltrim(tmpstr, "\t") + '\n';
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2001-12-05 23:04:07 +00:00
|
|
|
if (!isOK()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
printError("Long string not ended by `" + endtoken + '\'');
|
2001-12-05 23:04:07 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
bool LyXLex::getBool() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2003-04-14 12:44:36 +00:00
|
|
|
if (pimpl_->getString() == "true") {
|
2000-01-06 02:44:26 +00:00
|
|
|
return true;
|
2003-04-14 12:44:36 +00:00
|
|
|
} else if (pimpl_->getString() != "false") {
|
2000-04-08 17:02:02 +00:00
|
|
|
pimpl_->printError("Bad boolean `$$Token'. Use \"false\" or \"true\"");
|
2001-12-05 23:04:07 +00:00
|
|
|
}
|
2000-01-06 02:44:26 +00:00
|
|
|
return false;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
bool LyXLex::eatLine()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-08-06 19:12:46 +00:00
|
|
|
return pimpl_->eatLine();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LyXLex::next(bool esc)
|
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
return pimpl_->next(esc);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LyXLex::nextToken()
|
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
return pimpl_->nextToken();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-17 14:43:09 +00:00
|
|
|
void LyXLex::pushToken(string const & pt)
|
|
|
|
{
|
|
|
|
pimpl_->pushToken(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
int LyXLex::findToken(char const * str[])
|
2001-12-05 23:04:07 +00:00
|
|
|
{
|
2002-05-30 03:37:24 +00:00
|
|
|
if (!next()) {
|
|
|
|
pimpl_->printError("file ended while scanning string token");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2001-12-05 23:04:07 +00:00
|
|
|
int i = 0;
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2003-04-14 12:44:36 +00:00
|
|
|
string const search_token = pimpl_->getString();
|
|
|
|
|
|
|
|
if (search_token != "default") {
|
|
|
|
while (str[i][0] && str[i] != search_token) {
|
2002-05-30 03:37:24 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (!str[i][0]) {
|
|
|
|
pimpl_->printError("Unknown argument `$$Token'");
|
|
|
|
i = -1;
|
2001-12-05 23:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2001-12-05 23:04:07 +00:00
|
|
|
return i;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|