2002-08-04 23:11:50 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file lyxlex.C
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Alejandro Aguilar Sierra
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
|
* \author John Levon
|
2000-07-24 13:53:19 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-08-04 23:11:50 +00:00
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "lyxlex.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
|
2001-07-29 15:34:18 +00:00
|
|
|
|
#include "debug.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
#include "lyxlex_pimpl.h"
|
|
|
|
|
|
2000-09-26 13:10:34 +00:00
|
|
|
|
#include "support/lstrings.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::compare_ascii_no_case;
|
|
|
|
|
using lyx::support::isStrDbl;
|
|
|
|
|
using lyx::support::isStrInt;
|
|
|
|
|
using lyx::support::ltrim;
|
|
|
|
|
using lyx::support::prefixIs;
|
|
|
|
|
using lyx::support::strToDbl;
|
|
|
|
|
using lyx::support::strToInt;
|
|
|
|
|
using lyx::support::subst;
|
|
|
|
|
using lyx::support::trim;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::endl;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using std::istream;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
|
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
|
|
|
|
|
2003-09-05 16:31:30 +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") {
|
2003-09-05 16:31:30 +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
|
|
|
|
}
|
2003-12-02 10:19:51 +00:00
|
|
|
|
|
|
|
|
|
|
2003-12-02 12:27:07 +00:00
|
|
|
|
LyXLex::operator void const *() const
|
2003-12-02 10:19:51 +00:00
|
|
|
|
{
|
2003-12-02 12:27:07 +00:00
|
|
|
|
// This behaviour is NOT the same as the std::streams which would
|
|
|
|
|
// use fail() here. However, our implementation of getString() et al.
|
|
|
|
|
// can cause the eof() and fail() bits to be set, even though we
|
|
|
|
|
// haven't tried to read 'em.
|
|
|
|
|
return pimpl_->is.bad() ? 0 : this;
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool LyXLex::operator!() const
|
|
|
|
|
{
|
2003-12-02 12:27:07 +00:00
|
|
|
|
return pimpl_->is.bad();
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(std::string & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getString();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(float & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getFloat();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(int & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getInteger();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(unsigned int & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getInteger();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(bool & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getBool();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|