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"
|
|
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
|
#include "support/convert.h"
|
2000-09-26 13:10:34 +00:00
|
|
|
|
#include "support/lstrings.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-08-12 10:39:12 +00:00
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using support::compare_ascii_no_case;
|
|
|
|
|
using support::isStrDbl;
|
|
|
|
|
using support::isStrInt;
|
|
|
|
|
using support::ltrim;
|
|
|
|
|
using support::prefixIs;
|
|
|
|
|
using support::subst;
|
|
|
|
|
using 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
|
|
|
|
{
|
2007-03-26 13:43:49 +00:00
|
|
|
|
return pimpl_->inputAvailable();
|
2000-04-08 17:02:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
bool LyXLex::setFile(support::FileName 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
|
|
|
|
{
|
2007-03-26 13:43:49 +00:00
|
|
|
|
lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
|
|
|
|
|
if (!lastReadOk_) {
|
|
|
|
|
pimpl_->printError("integer token missing");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-26 18:03:48 +00:00
|
|
|
|
if (isStrInt(pimpl_->getString()))
|
2005-01-27 21:05:44 +00:00
|
|
|
|
return convert<int>(pimpl_->getString());
|
2007-03-26 13:43:49 +00:00
|
|
|
|
|
|
|
|
|
lastReadOk_ = false;
|
2003-02-26 18:03:48 +00:00
|
|
|
|
pimpl_->printError("Bad integer `$$Token'");
|
|
|
|
|
return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
|
double 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).
|
2007-03-26 13:43:49 +00:00
|
|
|
|
lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
|
|
|
|
|
if (!lastReadOk_) {
|
|
|
|
|
pimpl_->printError("float token missing");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
|
string const str = subst(pimpl_->getString(), ",", ".");
|
2000-09-26 13:10:34 +00:00
|
|
|
|
if (isStrDbl(str))
|
2005-01-27 21:05:44 +00:00
|
|
|
|
return convert<double>(str);
|
2007-03-26 13:43:49 +00:00
|
|
|
|
|
|
|
|
|
lastReadOk_ = false;
|
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
|
|
|
|
{
|
2007-03-26 13:43:49 +00:00
|
|
|
|
lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
|
|
|
|
|
|
|
|
|
|
if (lastReadOk_)
|
2001-08-06 19:12:46 +00:00
|
|
|
|
return pimpl_->getString();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
|
|
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
docstring const LyXLex::getDocString() const
|
2006-08-13 22:54:59 +00:00
|
|
|
|
{
|
2007-03-26 13:43:49 +00:00
|
|
|
|
lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
|
|
|
|
|
|
|
|
|
|
if (lastReadOk_)
|
|
|
|
|
return pimpl_->getDocString();
|
|
|
|
|
|
|
|
|
|
return docstring();
|
2006-08-13 22:54:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// I would prefer to give a tag number instead of an explicit token
|
2004-08-15 21:52:13 +00:00
|
|
|
|
// here, but it is not possible because Buffer::readDocument 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;
|
|
|
|
|
|
2007-03-26 13:43:49 +00:00
|
|
|
|
while (pimpl_->is) { //< eatLine only reads from is, not from pushTok
|
2001-08-06 19:12:46 +00:00
|
|
|
|
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
|
|
|
|
|
2007-03-26 13:43:49 +00:00
|
|
|
|
if (!pimpl_->is) {
|
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") {
|
2007-03-26 13:43:49 +00:00
|
|
|
|
lastReadOk_ = 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\"");
|
2007-03-26 13:43:49 +00:00
|
|
|
|
lastReadOk_ = false;
|
2001-12-05 23:04:07 +00:00
|
|
|
|
}
|
2007-03-26 13:43:49 +00:00
|
|
|
|
lastReadOk_ = true;
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
2007-03-26 13:43:49 +00:00
|
|
|
|
return lastReadOk_? this: 0;
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool LyXLex::operator!() const
|
|
|
|
|
{
|
2007-03-26 13:43:49 +00:00
|
|
|
|
return !lastReadOk_;
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(std::string & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getString();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
lastReadOk_ = false;
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-03 15:16:45 +00:00
|
|
|
|
LyXLex & LyXLex::operator>>(docstring & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getDocString();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
lastReadOk_ = false;
|
2006-11-03 15:16:45 +00:00
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
|
LyXLex & LyXLex::operator>>(double & s)
|
2003-12-02 10:19:51 +00:00
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getFloat();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
lastReadOk_ = false;
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(int & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getInteger();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
lastReadOk_ = false;
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(unsigned int & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getInteger();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
lastReadOk_ = false;
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LyXLex & LyXLex::operator>>(bool & s)
|
|
|
|
|
{
|
|
|
|
|
if (isOK()) {
|
|
|
|
|
next();
|
|
|
|
|
s = getBool();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
lastReadOk_ = false;
|
2003-12-02 10:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2006-08-12 10:39:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// quotes a string, e.g. for use in preferences files or as an argument of the "log" dialog
|
|
|
|
|
string const LyXLex::quoteString(string const & arg)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
os << '"' << subst(subst(arg, "\\", "\\\\"), "\"", "\\\"") << '"';
|
|
|
|
|
return os.str();
|
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|