2000-07-24 13:53:19 +00:00
|
|
|
/* This file is part of
|
|
|
|
* ======================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
*
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1996-2001 The LyX Team.
|
2000-07-24 13:53:19 +00:00
|
|
|
*
|
|
|
|
* Generalized simple lexical analizer.
|
|
|
|
* It can be used for simple syntax parsers, like lyxrc,
|
|
|
|
* texclass and others to come. [asierra30/03/96]
|
|
|
|
*
|
|
|
|
* ====================================================== */
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "lyxlex.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
|
2000-04-08 17:02:02 +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
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const LyXLex::text() const
|
2000-04-08 17:02:02 +00:00
|
|
|
{
|
|
|
|
return &pimpl_->buff[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::istream & LyXLex::getStream()
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2001-12-05 23:04:07 +00:00
|
|
|
if (isStrInt(pimpl_->getString())) {
|
2001-08-06 19:12:46 +00:00
|
|
|
return strToInt(pimpl_->getString());
|
2001-12-05 23:04:07 +00:00
|
|
|
} else {
|
2000-04-08 17:02:02 +00:00
|
|
|
pimpl_->printError("Bad integer `$$Token'");
|
2000-01-06 02:44:26 +00:00
|
|
|
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
|
|
|
|
// 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);
|
|
|
|
else {
|
|
|
|
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
|
|
|
|
// here, but it is not possible because Buffer::readLyXformat2 uses
|
|
|
|
// 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;
|
|
|
|
|
2001-08-06 19:12:46 +00:00
|
|
|
string const token = frontStrip(strip(getString()), " \t");
|
1999-09-27 18:44:28 +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.
|
1999-10-02 16:21:10 +00:00
|
|
|
if (compare_no_case(token, endtoken) != 0) {
|
2001-08-06 19:12:46 +00:00
|
|
|
string tmpstr = getString();
|
1999-09-27 18:44:28 +00:00
|
|
|
if (firstline) {
|
1999-10-02 16:21:10 +00:00
|
|
|
unsigned int i = 0;
|
2001-12-05 08:04:20 +00:00
|
|
|
while (i < tmpstr.length()
|
1999-09-27 18:44:28 +00:00
|
|
|
&& tmpstr[i] == ' ') {
|
1999-10-02 16:21:10 +00:00
|
|
|
++i;
|
1999-09-27 18:44:28 +00:00
|
|
|
prefix += ' ';
|
|
|
|
}
|
|
|
|
firstline = false;
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::PARSER] << "Prefix = `" << prefix
|
|
|
|
<< '\'' << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!prefix.empty()
|
2000-09-26 13:54:57 +00:00
|
|
|
&& prefixIs(tmpstr, prefix)) {
|
1999-10-02 16:21:10 +00:00
|
|
|
tmpstr.erase(0, prefix.length() - 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2000-06-12 11:27:15 +00:00
|
|
|
str += frontStrip(tmpstr, "\t") + '\n';
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
else // token == endtoken
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
{
|
2001-12-05 23:04:07 +00:00
|
|
|
if (compare(pimpl_->buff, "true") == 0) {
|
2000-01-06 02:44:26 +00:00
|
|
|
return true;
|
2001-12-05 23:04:07 +00:00
|
|
|
} else if (compare(pimpl_->buff, "false") != 0) {
|
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
|
|
|
{
|
|
|
|
int i = 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-12-05 23:04:07 +00:00
|
|
|
if (next()) {
|
|
|
|
if (compare(pimpl_->buff, "default")) {
|
|
|
|
while (str[i][0] && compare(str[i], pimpl_->buff)) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (!str[i][0]) {
|
|
|
|
pimpl_->printError("Unknown argument `$$Token'");
|
|
|
|
i = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pimpl_->printError("file ended while scanning string token");
|
|
|
|
i = -1;
|
|
|
|
}
|
|
|
|
return i;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|