2002-08-04 23:11:50 +00:00
|
|
|
/**
|
2007-04-26 11:30:54 +00:00
|
|
|
* \file Lexer.cpp
|
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
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \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>
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
#include "Lexer.h"
|
2012-02-21 20:29:44 +00:00
|
|
|
#include "Format.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
#include "support/convert.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-12-17 16:04:46 +00:00
|
|
|
#include "support/FileName.h"
|
2007-04-24 10:52:22 +00:00
|
|
|
#include "support/filetools.h"
|
2007-08-01 15:16:44 +00:00
|
|
|
#include "support/gzstream.h"
|
2009-06-21 18:23:10 +00:00
|
|
|
#include "support/lassert.h"
|
2000-09-26 13:10:34 +00:00
|
|
|
#include "support/lstrings.h"
|
2007-04-24 10:52:22 +00:00
|
|
|
#include "support/lyxalgo.h"
|
|
|
|
#include "support/types.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-04-24 10:52:22 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <istream>
|
|
|
|
#include <stack>
|
|
|
|
#include <vector>
|
2006-08-12 10:39:12 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2007-04-24 10:52:22 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2007-04-26 11:30:54 +00:00
|
|
|
// Lexer::Pimpl
|
2007-04-24 10:52:22 +00:00
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
///
|
2008-03-21 21:08:57 +00:00
|
|
|
class Lexer::Pimpl {
|
2007-04-24 10:52:22 +00:00
|
|
|
public:
|
|
|
|
///
|
2008-04-02 23:06:22 +00:00
|
|
|
Pimpl(LexerKeyword * tab, int num);
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
2007-12-12 19:28:07 +00:00
|
|
|
string const getString() const;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
docstring const getDocString() const;
|
|
|
|
///
|
2007-12-12 19:28:07 +00:00
|
|
|
void printError(string const & message) const;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
2007-12-12 19:28:07 +00:00
|
|
|
void printTable(ostream & os);
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
2008-04-02 23:06:22 +00:00
|
|
|
void pushTable(LexerKeyword * tab, int num);
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
void popTable();
|
|
|
|
///
|
2007-12-12 19:57:42 +00:00
|
|
|
bool setFile(FileName const & filename);
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
2007-12-12 19:28:07 +00:00
|
|
|
void setStream(istream & i);
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
void setCommentChar(char c);
|
|
|
|
///
|
|
|
|
bool next(bool esc = false);
|
|
|
|
///
|
2008-04-03 20:55:09 +00:00
|
|
|
int searchKeyword(char const * const tag) const;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
int lex();
|
|
|
|
///
|
|
|
|
bool eatLine();
|
|
|
|
///
|
|
|
|
bool nextToken();
|
|
|
|
/// test if there is a pushed token or the stream is ok
|
|
|
|
bool inputAvailable();
|
|
|
|
///
|
2007-12-12 19:28:07 +00:00
|
|
|
void pushToken(string const &);
|
2007-04-24 10:52:22 +00:00
|
|
|
/// fb_ is only used to open files, the stream is accessed through is.
|
2007-12-12 19:28:07 +00:00
|
|
|
filebuf fb_;
|
2007-04-24 10:52:22 +00:00
|
|
|
|
|
|
|
/// gz_ is only used to open files, the stream is accessed through is.
|
2007-08-01 15:16:44 +00:00
|
|
|
gz::gzstreambuf gz_;
|
2007-04-24 10:52:22 +00:00
|
|
|
|
|
|
|
/// the stream that we use.
|
2007-12-12 19:28:07 +00:00
|
|
|
istream is;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
2007-12-12 19:28:07 +00:00
|
|
|
string name;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
2008-04-02 23:06:22 +00:00
|
|
|
LexerKeyword * table;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
int no_items;
|
|
|
|
///
|
2007-12-12 19:28:07 +00:00
|
|
|
string buff;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
int status;
|
|
|
|
///
|
|
|
|
int lineno;
|
|
|
|
///
|
2007-12-12 19:28:07 +00:00
|
|
|
string pushTok;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
char commentChar;
|
2008-04-03 22:38:14 +00:00
|
|
|
/// used for error messages
|
|
|
|
string context;
|
2007-04-24 10:52:22 +00:00
|
|
|
private:
|
2008-03-21 21:08:57 +00:00
|
|
|
/// non-copyable
|
|
|
|
Pimpl(Pimpl const &);
|
|
|
|
void operator=(Pimpl const &);
|
|
|
|
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
void verifyTable();
|
|
|
|
///
|
2008-01-12 21:49:48 +00:00
|
|
|
class PushedTable {
|
2007-04-24 10:52:22 +00:00
|
|
|
public:
|
|
|
|
///
|
2008-01-12 21:49:48 +00:00
|
|
|
PushedTable()
|
2007-04-24 10:52:22 +00:00
|
|
|
: table_elem(0), table_siz(0) {}
|
|
|
|
///
|
2008-04-02 23:06:22 +00:00
|
|
|
PushedTable(LexerKeyword * ki, int siz)
|
2007-04-24 10:52:22 +00:00
|
|
|
: table_elem(ki), table_siz(siz) {}
|
|
|
|
///
|
2008-04-02 23:06:22 +00:00
|
|
|
LexerKeyword * table_elem;
|
2007-04-24 10:52:22 +00:00
|
|
|
///
|
|
|
|
int table_siz;
|
|
|
|
};
|
|
|
|
///
|
2008-01-12 21:49:48 +00:00
|
|
|
stack<PushedTable> pushed;
|
2007-04-24 10:52:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-01-12 21:49:48 +00:00
|
|
|
class CompareTags
|
2008-04-02 23:06:22 +00:00
|
|
|
: public binary_function<LexerKeyword, LexerKeyword, bool> {
|
2007-04-24 10:52:22 +00:00
|
|
|
public:
|
|
|
|
// used by lower_bound, sort and sorted
|
2008-04-02 23:06:22 +00:00
|
|
|
bool operator()(LexerKeyword const & a, LexerKeyword const & b) const
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
// we use the ascii version, because in turkish, 'i'
|
|
|
|
// is not the lowercase version of 'I', and thus
|
|
|
|
// turkish locale breaks parsing of tags.
|
|
|
|
return compare_ascii_no_case(a.tag, b.tag) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end of anon namespace
|
|
|
|
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
Lexer::Pimpl::Pimpl(LexerKeyword * tab, int num)
|
2007-04-24 10:52:22 +00:00
|
|
|
: is(&fb_), table(tab), no_items(num),
|
|
|
|
status(0), lineno(0), commentChar('#')
|
|
|
|
{
|
|
|
|
verifyTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
string const Lexer::Pimpl::getString() const
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
docstring const Lexer::Pimpl::getDocString() const
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
return from_utf8(buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::Pimpl::printError(string const & message) const
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
string const tmpmsg = subst(message, "$$Token", getString());
|
|
|
|
lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
|
2008-04-05 21:24:57 +00:00
|
|
|
<< " of file " << to_utf8(makeDisplayPath(name))
|
|
|
|
<< " current token: '" << getString() << "'"
|
|
|
|
<< " context: '" << context << "']" << endl;
|
2007-04-24 10:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::Pimpl::printTable(ostream & os)
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
os << "\nNumber of tags: " << no_items << endl;
|
|
|
|
for (int i= 0; i < no_items; ++i)
|
|
|
|
os << "table[" << i
|
|
|
|
<< "]: tag: `" << table[i].tag
|
|
|
|
<< "' code:" << table[i].code << '\n';
|
|
|
|
os.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::Pimpl::verifyTable()
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
// Check if the table is sorted and if not, sort it.
|
|
|
|
if (table
|
2008-01-12 21:49:48 +00:00
|
|
|
&& !lyx::sorted(table, table + no_items, CompareTags())) {
|
2007-04-26 11:30:54 +00:00
|
|
|
lyxerr << "The table passed to Lexer is not sorted!\n"
|
2007-04-24 10:52:22 +00:00
|
|
|
<< "Tell the developers to fix it!" << endl;
|
|
|
|
// We sort it anyway to avoid problems.
|
|
|
|
lyxerr << "\nUnsorted:" << endl;
|
|
|
|
printTable(lyxerr);
|
|
|
|
|
2008-01-12 21:49:48 +00:00
|
|
|
sort(table, table + no_items, CompareTags());
|
2007-04-24 10:52:22 +00:00
|
|
|
lyxerr << "\nSorted:" << endl;
|
|
|
|
printTable(lyxerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
void Lexer::Pimpl::pushTable(LexerKeyword * tab, int num)
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
2008-01-12 21:49:48 +00:00
|
|
|
PushedTable tmppu(table, no_items);
|
2007-04-24 10:52:22 +00:00
|
|
|
pushed.push(tmppu);
|
|
|
|
|
|
|
|
table = tab;
|
|
|
|
no_items = num;
|
|
|
|
|
|
|
|
verifyTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::Pimpl::popTable()
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
if (pushed.empty()) {
|
2007-04-26 11:30:54 +00:00
|
|
|
lyxerr << "Lexer error: nothing to pop!" << endl;
|
2007-04-24 10:52:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-12 21:49:48 +00:00
|
|
|
PushedTable tmp = pushed.top();
|
2007-04-24 10:52:22 +00:00
|
|
|
pushed.pop();
|
|
|
|
table = tmp.table_elem;
|
|
|
|
no_items = tmp.table_siz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::Pimpl::setFile(FileName const & filename)
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
// Check the format of the file.
|
2017-03-14 03:33:40 +00:00
|
|
|
if (theFormats().isZippedFile(filename)) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXLEX, "lyxlex: compressed");
|
2007-04-24 10:52:22 +00:00
|
|
|
// The check only outputs a debug message, because it triggers
|
|
|
|
// a bug in compaq cxx 6.2, where is_open() returns 'true' for
|
|
|
|
// a fresh new filebuf. (JMarc)
|
2007-08-01 15:16:44 +00:00
|
|
|
if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
|
2007-11-28 22:12:03 +00:00
|
|
|
LYXERR(Debug::LYXLEX, "Error in LyXLex::setFile: "
|
|
|
|
"file or stream already set.");
|
2007-08-01 15:16:44 +00:00
|
|
|
gz_.open(filename.toFilesystemEncoding().c_str(), ios::in);
|
2007-04-24 10:52:22 +00:00
|
|
|
is.rdbuf(&gz_);
|
2010-04-21 01:19:09 +00:00
|
|
|
name = filename.absFileName();
|
2007-04-24 10:52:22 +00:00
|
|
|
lineno = 0;
|
2009-06-21 18:23:10 +00:00
|
|
|
if (!gz_.is_open() || !is.good())
|
|
|
|
return false;
|
2007-04-24 10:52:22 +00:00
|
|
|
} else {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXLEX, "lyxlex: UNcompressed");
|
2007-04-24 10:52:22 +00:00
|
|
|
|
|
|
|
// The check only outputs a debug message, because it triggers
|
|
|
|
// a bug in compaq cxx 6.2, where is_open() returns 'true' for
|
|
|
|
// a fresh new filebuf. (JMarc)
|
2007-09-16 10:36:57 +00:00
|
|
|
if (fb_.is_open() || istream::off_type(is.tellg()) > 0) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXLEX, "Error in Lexer::setFile: "
|
|
|
|
"file or stream already set.");
|
2007-09-16 10:36:57 +00:00
|
|
|
}
|
2010-02-21 04:51:27 +00:00
|
|
|
fb_.open(filename.toSafeFilesystemEncoding().c_str(), ios::in);
|
2007-04-24 10:52:22 +00:00
|
|
|
is.rdbuf(&fb_);
|
2010-04-21 01:19:09 +00:00
|
|
|
name = filename.absFileName();
|
2007-04-24 10:52:22 +00:00
|
|
|
lineno = 0;
|
2009-06-21 18:23:10 +00:00
|
|
|
if (!fb_.is_open() || !is.good())
|
|
|
|
return false;
|
2007-04-24 10:52:22 +00:00
|
|
|
}
|
2009-06-21 18:23:10 +00:00
|
|
|
|
|
|
|
// Skip byte order mark.
|
|
|
|
if (is.peek() == 0xef) {
|
2011-09-23 17:51:39 +00:00
|
|
|
is.get();
|
2009-06-21 18:23:10 +00:00
|
|
|
if (is.peek() == 0xbb) {
|
2011-09-23 17:51:39 +00:00
|
|
|
is.get();
|
2009-06-21 18:23:10 +00:00
|
|
|
LASSERT(is.get() == 0xbf, /**/);
|
|
|
|
} else
|
|
|
|
is.unget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2007-04-24 10:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::Pimpl::setStream(istream & i)
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
2007-09-16 10:36:57 +00:00
|
|
|
if (fb_.is_open() || istream::off_type(is.tellg()) > 0) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LYXLEX, "Error in Lexer::setStream: "
|
|
|
|
"file or stream already set.");
|
2007-09-16 10:36:57 +00:00
|
|
|
}
|
2007-04-24 10:52:22 +00:00
|
|
|
is.rdbuf(i.rdbuf());
|
|
|
|
lineno = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::Pimpl::setCommentChar(char c)
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
commentChar = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::Pimpl::next(bool esc /* = false */)
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
if (!pushTok.empty()) {
|
|
|
|
// There can have been a whole line pushed so
|
|
|
|
// we extract the first word and leaves the rest
|
|
|
|
// in pushTok. (Lgb)
|
|
|
|
if (pushTok[0] == '\\' && pushTok.find(' ') != string::npos) {
|
|
|
|
buff.clear();
|
|
|
|
pushTok = split(pushTok, buff, ' ');
|
|
|
|
} else {
|
|
|
|
buff = pushTok;
|
|
|
|
pushTok.clear();
|
|
|
|
}
|
|
|
|
status = LEX_TOKEN;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-03 20:00:04 +00:00
|
|
|
char cc = 0;
|
|
|
|
status = 0;
|
|
|
|
while (is && !status) {
|
|
|
|
is.get(cc);
|
2015-09-20 08:42:35 +00:00
|
|
|
unsigned char c = cc;
|
2008-04-03 20:00:04 +00:00
|
|
|
|
|
|
|
if (c == commentChar) {
|
|
|
|
// Read rest of line (fast :-)
|
2007-04-24 10:52:22 +00:00
|
|
|
#if 1
|
2008-04-03 20:00:04 +00:00
|
|
|
// That is not fast... (Lgb)
|
|
|
|
string dummy;
|
|
|
|
getline(is, dummy);
|
2007-04-24 10:52:22 +00:00
|
|
|
|
2008-04-03 20:00:04 +00:00
|
|
|
LYXERR(Debug::LYXLEX, "Comment read: `" << c << dummy << '\'');
|
2007-04-24 10:52:22 +00:00
|
|
|
#else
|
2008-04-03 20:00:04 +00:00
|
|
|
// unfortunately ignore is buggy (Lgb)
|
|
|
|
is.ignore(100, '\n');
|
2007-04-24 10:52:22 +00:00
|
|
|
#endif
|
2008-04-03 20:00:04 +00:00
|
|
|
++lineno;
|
|
|
|
continue;
|
|
|
|
}
|
2007-04-24 10:52:22 +00:00
|
|
|
|
2008-04-03 20:00:04 +00:00
|
|
|
if (c == '\"') {
|
|
|
|
buff.clear();
|
|
|
|
|
|
|
|
if (esc) {
|
2007-04-24 10:52:22 +00:00
|
|
|
|
|
|
|
do {
|
2015-09-20 08:42:35 +00:00
|
|
|
bool escaped = false;
|
2007-04-24 10:52:22 +00:00
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
|
|
|
if (c == '\r') continue;
|
|
|
|
if (c == '\\') {
|
|
|
|
// escape the next char
|
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
|
|
|
if (c == '\"' || c == '\\')
|
|
|
|
escaped = true;
|
|
|
|
else
|
|
|
|
buff.push_back('\\');
|
|
|
|
}
|
|
|
|
buff.push_back(c);
|
|
|
|
|
|
|
|
if (!escaped && c == '\"')
|
|
|
|
break;
|
|
|
|
} while (c != '\n' && is);
|
|
|
|
|
2008-04-03 20:00:04 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
do {
|
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
|
|
|
if (c != '\r')
|
|
|
|
buff.push_back(c);
|
|
|
|
} while (c != '\"' && c != '\n' && is);
|
2007-04-24 10:52:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-03 20:00:04 +00:00
|
|
|
if (c != '\"') {
|
|
|
|
printError("Missing quote");
|
|
|
|
if (c == '\n')
|
|
|
|
++lineno;
|
|
|
|
}
|
2007-04-24 10:52:22 +00:00
|
|
|
|
2008-04-03 20:00:04 +00:00
|
|
|
buff.resize(buff.size() - 1);
|
|
|
|
status = LEX_DATA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-05 19:27:02 +00:00
|
|
|
if (c == ',')
|
2008-04-03 20:00:04 +00:00
|
|
|
continue; /* Skip ','s */
|
|
|
|
|
2008-04-05 19:27:02 +00:00
|
|
|
// using relational operators with chars other
|
|
|
|
// than == and != is not safe. And if it is done
|
|
|
|
// the type _have_ to be unsigned. It usually a
|
|
|
|
// lot better to use the functions from cctype
|
2008-04-03 20:00:04 +00:00
|
|
|
if (c > ' ' && is) {
|
|
|
|
buff.clear();
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (esc && c == '\\') {
|
|
|
|
// escape the next char
|
2007-04-24 10:52:22 +00:00
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
2008-04-03 20:00:04 +00:00
|
|
|
//escaped = true;
|
|
|
|
}
|
|
|
|
buff.push_back(c);
|
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
|
|
|
} while (c > ' ' && c != ',' && is);
|
|
|
|
status = LEX_TOKEN;
|
|
|
|
}
|
2007-04-24 10:52:22 +00:00
|
|
|
|
2008-04-05 19:27:02 +00:00
|
|
|
if (c == '\r' && is) {
|
2008-04-03 20:00:04 +00:00
|
|
|
// The Windows support has lead to the
|
|
|
|
// possibility of "\r\n" at the end of
|
|
|
|
// a line. This will stop LyX choking
|
|
|
|
// when it expected to find a '\n'
|
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
2007-04-24 10:52:22 +00:00
|
|
|
}
|
|
|
|
|
2008-04-03 20:00:04 +00:00
|
|
|
if (c == '\n')
|
|
|
|
++lineno;
|
2007-04-24 10:52:22 +00:00
|
|
|
|
|
|
|
}
|
2008-04-03 20:00:04 +00:00
|
|
|
if (status)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
status = is.eof() ? LEX_FEOF: LEX_UNDEF;
|
|
|
|
buff.clear();
|
|
|
|
return false;
|
2007-04-24 10:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-03 20:55:09 +00:00
|
|
|
int Lexer::Pimpl::searchKeyword(char const * const tag) const
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
2008-04-02 23:06:22 +00:00
|
|
|
LexerKeyword search_tag = { tag, 0 };
|
|
|
|
LexerKeyword * res =
|
2007-04-24 10:52:22 +00:00
|
|
|
lower_bound(table, table + no_items,
|
2008-01-12 21:49:48 +00:00
|
|
|
search_tag, CompareTags());
|
2007-04-24 10:52:22 +00:00
|
|
|
// use the compare_ascii_no_case instead of compare_no_case,
|
|
|
|
// because in turkish, 'i' is not the lowercase version of 'I',
|
|
|
|
// and thus turkish locale breaks parsing of tags.
|
|
|
|
if (res != table + no_items
|
|
|
|
&& !compare_ascii_no_case(res->tag, tag))
|
|
|
|
return res->code;
|
|
|
|
return LEX_UNDEF;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
int Lexer::Pimpl::lex()
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
//NOTE: possible bug.
|
2007-05-28 22:27:45 +00:00
|
|
|
if (next() && status == LEX_TOKEN)
|
2008-04-03 20:55:09 +00:00
|
|
|
return searchKeyword(getString().c_str());
|
2007-04-24 10:52:22 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::Pimpl::eatLine()
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
buff.clear();
|
|
|
|
|
|
|
|
unsigned char c = '\0';
|
|
|
|
char cc = 0;
|
|
|
|
while (is && c != '\n') {
|
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
2007-11-15 20:04:51 +00:00
|
|
|
//LYXERR(Debug::LYXLEX, "Lexer::EatLine read char: `" << c << '\'');
|
2010-08-28 16:46:54 +00:00
|
|
|
if (c != '\r' && is)
|
2007-04-24 10:52:22 +00:00
|
|
|
buff.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\n') {
|
|
|
|
++lineno;
|
|
|
|
buff.resize(buff.size() - 1);
|
|
|
|
status = LEX_DATA;
|
|
|
|
return true;
|
|
|
|
} else if (buff.length() > 0) { // last line
|
|
|
|
status = LEX_DATA;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::Pimpl::nextToken()
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
if (!pushTok.empty()) {
|
|
|
|
// There can have been a whole line pushed so
|
|
|
|
// we extract the first word and leaves the rest
|
|
|
|
// in pushTok. (Lgb)
|
|
|
|
if (pushTok[0] == '\\' && pushTok.find(' ') != string::npos) {
|
|
|
|
buff.clear();
|
|
|
|
pushTok = split(pushTok, buff, ' ');
|
|
|
|
} else {
|
|
|
|
buff = pushTok;
|
|
|
|
pushTok.clear();
|
|
|
|
}
|
|
|
|
status = LEX_TOKEN;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
while (is && !status) {
|
|
|
|
unsigned char c = 0;
|
|
|
|
char cc = 0;
|
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
2008-09-30 18:00:02 +00:00
|
|
|
if ((c >= ' ' || c == '\t') && is) {
|
2007-04-24 10:52:22 +00:00
|
|
|
buff.clear();
|
|
|
|
|
|
|
|
if (c == '\\') { // first char == '\\'
|
|
|
|
do {
|
|
|
|
buff.push_back(c);
|
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
|
|
|
} while (c > ' ' && c != '\\' && is);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
buff.push_back(c);
|
|
|
|
is.get(cc);
|
|
|
|
c = cc;
|
2008-09-30 18:00:02 +00:00
|
|
|
} while ((c >= ' ' || c == '\t') && c != '\\' && is);
|
2007-04-24 10:52:22 +00:00
|
|
|
}
|
|
|
|
|
2008-01-12 21:49:48 +00:00
|
|
|
if (c == '\\')
|
|
|
|
is.putback(c); // put it back
|
2007-04-24 10:52:22 +00:00
|
|
|
status = LEX_TOKEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (status)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
status = is.eof() ? LEX_FEOF: LEX_UNDEF;
|
|
|
|
buff.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::Pimpl::inputAvailable()
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
2007-05-28 22:27:45 +00:00
|
|
|
return is.good();
|
2007-04-24 10:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::Pimpl::pushToken(string const & pt)
|
2007-04-24 10:52:22 +00:00
|
|
|
{
|
|
|
|
pushTok = pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2007-04-26 11:30:54 +00:00
|
|
|
// Lexer
|
2007-04-24 10:52:22 +00:00
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
Lexer::Lexer()
|
2014-05-19 20:41:51 +00:00
|
|
|
: pimpl_(new Pimpl(0, 0)), lastReadOk_(false)
|
2000-04-08 17:02:02 +00:00
|
|
|
{}
|
2000-03-09 03:36:48 +00:00
|
|
|
|
2000-01-06 11:35:29 +00:00
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
void Lexer::init(LexerKeyword * tab, int num)
|
|
|
|
{
|
|
|
|
pimpl_ = new Pimpl(tab, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer::~Lexer()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
delete pimpl_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
void Lexer::setLineNumber(int l)
|
2000-04-08 17:02:02 +00:00
|
|
|
{
|
|
|
|
pimpl_->lineno = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
int Lexer::lineNumber() const
|
2000-04-08 17:02:02 +00:00
|
|
|
{
|
|
|
|
return pimpl_->lineno;
|
|
|
|
}
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
istream & Lexer::getStream()
|
2000-04-08 17:02:02 +00:00
|
|
|
{
|
|
|
|
return pimpl_->is;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
void Lexer::pushTable(LexerKeyword * 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::popTable()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
pimpl_->popTable();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-12 19:57:42 +00:00
|
|
|
bool Lexer::setFile(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::setCommentChar(char c)
|
2000-11-08 15:19:55 +00:00
|
|
|
{
|
|
|
|
pimpl_->setCommentChar(c);
|
|
|
|
}
|
|
|
|
|
2008-05-05 13:53:28 +00:00
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
int Lexer::lex()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
return pimpl_->lex();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
int Lexer::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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
double Lexer::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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-19 08:15:34 +00:00
|
|
|
string const Lexer::getString(bool trim) 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_)
|
2015-05-19 08:15:34 +00:00
|
|
|
return trim ? support::trim(pimpl_->getString(), "\t ") : pimpl_->getString();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-19 08:15:34 +00:00
|
|
|
docstring const Lexer::getDocString(bool trim) 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;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-03-26 13:43:49 +00:00
|
|
|
if (lastReadOk_)
|
2015-05-19 08:15:34 +00:00
|
|
|
return trim ? support::trim(pimpl_->getDocString(), "\t ") : pimpl_->getDocString();
|
2007-03-26 13:43:49 +00:00
|
|
|
|
|
|
|
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)
|
2016-09-25 10:37:40 +00:00
|
|
|
docstring Lexer::getLongString(docstring const & endtoken)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2016-09-25 10:37:40 +00:00
|
|
|
docstring str;
|
|
|
|
docstring 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;
|
2016-09-25 10:37:40 +00:00
|
|
|
docstring tmpstr = getDocString();
|
|
|
|
docstring const token = trim(tmpstr, " \t");
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2016-09-25 10:37:40 +00:00
|
|
|
LYXERR(Debug::PARSER, "LongString: `" << tmpstr << '\'');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-04-03 20:55:09 +00:00
|
|
|
// We do a case independent comparison, like searchKeyword does.
|
2016-09-25 10:37:40 +00:00
|
|
|
if (compare_no_case(token, endtoken) == 0)
|
2002-08-02 16:18:15 +00:00
|
|
|
break;
|
2002-08-04 23:11:50 +00:00
|
|
|
|
|
|
|
if (firstline) {
|
2016-09-25 10:37:40 +00:00
|
|
|
size_t i = tmpstr.find_first_not_of(char_type(' '));
|
2002-08-04 23:11:50 +00:00
|
|
|
if (i != string::npos)
|
|
|
|
prefix = tmpstr.substr(0, i);
|
|
|
|
firstline = false;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::PARSER, "Prefix = `" << prefix << "\'");
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// further lines in long strings may have the same
|
|
|
|
// whitespace prefix as the first line. Remove it.
|
2008-03-21 21:08:57 +00:00
|
|
|
if (prefix.length() && prefixIs(tmpstr, prefix))
|
2002-08-04 23:11:50 +00:00
|
|
|
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
|
|
|
|
2008-03-21 21:08:57 +00:00
|
|
|
if (!pimpl_->is)
|
2016-09-25 10:37:40 +00:00
|
|
|
printError("Long string not ended by `" + to_utf8(endtoken) + '\'');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::getBool() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2008-04-05 10:34:29 +00:00
|
|
|
string const s = pimpl_->getString();
|
|
|
|
if (s == "false" || s == "0") {
|
|
|
|
lastReadOk_ = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (s == "true" || s == "1") {
|
2007-03-26 13:43:49 +00:00
|
|
|
lastReadOk_ = true;
|
2000-01-06 02:44:26 +00:00
|
|
|
return true;
|
2001-12-05 23:04:07 +00:00
|
|
|
}
|
2008-04-05 10:34:29 +00:00
|
|
|
pimpl_->printError("Bad boolean `$$Token'. "
|
|
|
|
"Use \"false\" or \"true\"");
|
|
|
|
lastReadOk_ = false;
|
2008-04-05 21:24:57 +00:00
|
|
|
return false;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::next(bool esc)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
return pimpl_->next(esc);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::nextToken()
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-04-08 17:02:02 +00:00
|
|
|
return pimpl_->nextToken();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
void Lexer::pushToken(string const & pt)
|
2000-05-17 14:43:09 +00:00
|
|
|
{
|
|
|
|
pimpl_->pushToken(pt);
|
|
|
|
}
|
|
|
|
|
2007-04-24 10:52:22 +00:00
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer::operator void const *() const
|
2003-12-02 10:19:51 +00:00
|
|
|
{
|
2007-12-12 19:28:07 +00:00
|
|
|
// This behaviour is NOT the same as the streams which would
|
2003-12-02 12:27:07 +00:00
|
|
|
// 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-04-24 10:52:22 +00:00
|
|
|
return lastReadOk_? this : 0;
|
2003-12-02 10:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
bool Lexer::operator!() const
|
2003-12-02 10:19:51 +00:00
|
|
|
{
|
2007-03-26 13:43:49 +00:00
|
|
|
return !lastReadOk_;
|
2003-12-02 10:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
Lexer & Lexer::operator>>(string & s)
|
2003-12-02 10:19:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & Lexer::operator>>(docstring & s)
|
2006-11-03 15:16:45 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & Lexer::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & Lexer::operator>>(int & s)
|
2003-12-02 10:19:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & Lexer::operator>>(unsigned int & s)
|
2003-12-02 10:19:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & Lexer::operator>>(bool & s)
|
2003-12-02 10:19:51 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
|
2008-04-05 10:34:29 +00:00
|
|
|
Lexer & Lexer::operator>>(char & c)
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
operator>>(s);
|
|
|
|
if (!s.empty())
|
|
|
|
c = s[0];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-21 21:08:57 +00:00
|
|
|
// quotes a string, e.g. for use in preferences files or as an argument
|
|
|
|
// of the "log" dialog
|
2008-04-03 22:38:14 +00:00
|
|
|
string Lexer::quoteString(string const & arg)
|
2006-08-12 10:39:12 +00:00
|
|
|
{
|
2008-03-21 21:08:57 +00:00
|
|
|
string res;
|
|
|
|
res += '"';
|
|
|
|
res += subst(subst(arg, "\\", "\\\\"), "\"", "\\\"");
|
|
|
|
res += '"';
|
|
|
|
return res;
|
2006-08-12 10:39:12 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2008-11-25 14:44:17 +00:00
|
|
|
// same for docstring
|
|
|
|
docstring Lexer::quoteString(docstring const & arg)
|
|
|
|
{
|
|
|
|
docstring res;
|
|
|
|
res += '"';
|
|
|
|
res += subst(subst(arg, from_ascii("\\"), from_ascii("\\\\")),
|
|
|
|
from_ascii("\""), from_ascii("\\\""));
|
|
|
|
res += '"';
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-03 22:38:14 +00:00
|
|
|
Lexer & Lexer::operator>>(char const * required)
|
|
|
|
{
|
|
|
|
string token;
|
|
|
|
*this >> token;
|
|
|
|
if (token != required) {
|
2008-04-05 10:34:29 +00:00
|
|
|
LYXERR0("Missing '" << required << "'-tag in " << pimpl_->context
|
|
|
|
<< ". Got " << token << " instead. Line: " << lineNumber());
|
2008-04-03 22:38:14 +00:00
|
|
|
pushToken(token);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-05 10:34:29 +00:00
|
|
|
bool Lexer::checkFor(char const * required)
|
|
|
|
{
|
|
|
|
string token;
|
|
|
|
*this >> token;
|
|
|
|
if (token == required)
|
|
|
|
return true;
|
|
|
|
pushToken(token);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-03 22:38:14 +00:00
|
|
|
void Lexer::setContext(std::string const & str)
|
|
|
|
{
|
|
|
|
pimpl_->context = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|