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
|
|
|
|
|
* \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>
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
#include "Lexer.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
|
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
|
#include "support/convert.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"
|
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"
|
|
|
|
|
#include "support/unicode.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2007-09-30 15:47:00 +00:00
|
|
|
|
#include <boost/noncopyable.hpp>
|
2007-04-24 10:52:22 +00:00
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <istream>
|
|
|
|
|
#include <stack>
|
2006-08-12 10:39:12 +00:00
|
|
|
|
#include <sstream>
|
2007-04-24 10:52:22 +00:00
|
|
|
|
#include <vector>
|
2006-08-12 10:39:12 +00:00
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using support::compare_ascii_no_case;
|
2007-04-24 10:52:22 +00:00
|
|
|
|
using support::FileName;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
using support::isStrDbl;
|
|
|
|
|
using support::isStrInt;
|
|
|
|
|
using support::ltrim;
|
2007-04-24 10:52:22 +00:00
|
|
|
|
using support::makeDisplayPath;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
using support::prefixIs;
|
2007-04-24 10:52:22 +00:00
|
|
|
|
using support::split;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
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;
|
2007-04-24 10:52:22 +00:00
|
|
|
|
using std::getline;
|
|
|
|
|
using std::lower_bound;
|
|
|
|
|
using std::sort;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2007-04-24 10:52:22 +00:00
|
|
|
|
using std::ios;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using std::istream;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
|
2000-03-09 03:36:48 +00:00
|
|
|
|
|
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
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///
|
2007-04-26 11:30:54 +00:00
|
|
|
|
class Lexer::Pimpl : boost::noncopyable {
|
2007-04-24 10:52:22 +00:00
|
|
|
|
public:
|
|
|
|
|
///
|
|
|
|
|
Pimpl(keyword_item * tab, int num);
|
|
|
|
|
///
|
|
|
|
|
std::string const getString() const;
|
|
|
|
|
///
|
|
|
|
|
docstring const getDocString() const;
|
|
|
|
|
///
|
|
|
|
|
void printError(std::string const & message) const;
|
|
|
|
|
///
|
|
|
|
|
void printTable(std::ostream & os);
|
|
|
|
|
///
|
|
|
|
|
void pushTable(keyword_item * tab, int num);
|
|
|
|
|
///
|
|
|
|
|
void popTable();
|
|
|
|
|
///
|
|
|
|
|
bool setFile(support::FileName const & filename);
|
|
|
|
|
///
|
|
|
|
|
void setStream(std::istream & i);
|
|
|
|
|
///
|
|
|
|
|
void setCommentChar(char c);
|
|
|
|
|
///
|
|
|
|
|
bool next(bool esc = false);
|
|
|
|
|
///
|
|
|
|
|
int search_kw(char const * const tag) const;
|
|
|
|
|
///
|
|
|
|
|
int lex();
|
|
|
|
|
///
|
|
|
|
|
bool eatLine();
|
|
|
|
|
///
|
|
|
|
|
bool nextToken();
|
|
|
|
|
/// test if there is a pushed token or the stream is ok
|
|
|
|
|
bool inputAvailable();
|
|
|
|
|
///
|
|
|
|
|
void pushToken(std::string const &);
|
|
|
|
|
/// fb_ is only used to open files, the stream is accessed through is.
|
|
|
|
|
std::filebuf fb_;
|
|
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
|
std::istream is;
|
|
|
|
|
///
|
|
|
|
|
std::string name;
|
|
|
|
|
///
|
|
|
|
|
keyword_item * table;
|
|
|
|
|
///
|
|
|
|
|
int no_items;
|
|
|
|
|
///
|
|
|
|
|
std::string buff;
|
|
|
|
|
///
|
|
|
|
|
int status;
|
|
|
|
|
///
|
|
|
|
|
int lineno;
|
|
|
|
|
///
|
|
|
|
|
std::string pushTok;
|
|
|
|
|
///
|
|
|
|
|
char commentChar;
|
|
|
|
|
private:
|
|
|
|
|
///
|
|
|
|
|
void verifyTable();
|
|
|
|
|
///
|
|
|
|
|
class pushed_table {
|
|
|
|
|
public:
|
|
|
|
|
///
|
|
|
|
|
pushed_table()
|
|
|
|
|
: table_elem(0), table_siz(0) {}
|
|
|
|
|
///
|
|
|
|
|
pushed_table(keyword_item * ki, int siz)
|
|
|
|
|
: table_elem(ki), table_siz(siz) {}
|
|
|
|
|
///
|
|
|
|
|
keyword_item * table_elem;
|
|
|
|
|
///
|
|
|
|
|
int table_siz;
|
|
|
|
|
};
|
|
|
|
|
///
|
|
|
|
|
std::stack<pushed_table> pushed;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class compare_tags
|
|
|
|
|
: public std::binary_function<keyword_item, keyword_item, bool> {
|
|
|
|
|
public:
|
|
|
|
|
// used by lower_bound, sort and sorted
|
|
|
|
|
bool operator()(keyword_item const & a, keyword_item const & b) const
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
Lexer::Pimpl::Pimpl(keyword_item * 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
|
|
|
|
|
<< " of file " << to_utf8(makeDisplayPath(name)) << ']' << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
&& !lyx::sorted(table, table + no_items, compare_tags())) {
|
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);
|
|
|
|
|
|
|
|
|
|
sort(table, table + no_items, compare_tags());
|
|
|
|
|
lyxerr << "\nSorted:" << endl;
|
|
|
|
|
printTable(lyxerr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
void Lexer::Pimpl::pushTable(keyword_item * tab, int num)
|
2007-04-24 10:52:22 +00:00
|
|
|
|
{
|
|
|
|
|
pushed_table tmppu(table, no_items);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pushed_table tmp = pushed.top();
|
|
|
|
|
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.
|
2007-10-20 17:35:27 +00:00
|
|
|
|
string const format = filename.guessFormatFromContents();
|
2007-04-24 10:52:22 +00:00
|
|
|
|
|
|
|
|
|
if (format == "gzip" || format == "zip" || format == "compress") {
|
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_);
|
|
|
|
|
name = filename.absFilename();
|
|
|
|
|
lineno = 0;
|
2007-08-01 15:16:44 +00:00
|
|
|
|
return gz_.is_open() && is.good();
|
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
|
|
|
|
}
|
2007-04-24 10:52:22 +00:00
|
|
|
|
fb_.open(filename.toFilesystemEncoding().c_str(), ios::in);
|
|
|
|
|
is.rdbuf(&fb_);
|
|
|
|
|
name = filename.absFilename();
|
|
|
|
|
lineno = 0;
|
|
|
|
|
return fb_.is_open() && is.good();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
if (!esc) {
|
|
|
|
|
unsigned char c = 0; // getc() returns an int
|
|
|
|
|
char cc = 0;
|
|
|
|
|
status = 0;
|
|
|
|
|
while (is && !status) {
|
|
|
|
|
is.get(cc);
|
|
|
|
|
c = cc;
|
|
|
|
|
if (c == commentChar) {
|
|
|
|
|
// Read rest of line (fast :-)
|
|
|
|
|
#if 1
|
|
|
|
|
// That is not fast... (Lgb)
|
|
|
|
|
string dummy;
|
|
|
|
|
getline(is, dummy);
|
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::LYXLEX, "Comment read: `" << c << dummy << '\'');
|
2007-04-24 10:52:22 +00:00
|
|
|
|
#else
|
|
|
|
|
// unfortunately ignore is buggy (Lgb)
|
|
|
|
|
is.ignore(100, '\n');
|
|
|
|
|
#endif
|
|
|
|
|
++lineno;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == '\"') {
|
|
|
|
|
buff.clear();
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
is.get(cc);
|
|
|
|
|
c = cc;
|
|
|
|
|
if (c != '\r')
|
|
|
|
|
buff.push_back(c);
|
|
|
|
|
} while (c != '\"' && c != '\n' && is);
|
|
|
|
|
|
|
|
|
|
if (c != '\"') {
|
|
|
|
|
printError("Missing quote");
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
++lineno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buff.resize(buff.size()-1);
|
|
|
|
|
status = LEX_DATA;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == ',')
|
|
|
|
|
continue; /* Skip ','s */
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
if (c > ' ' && is) {
|
|
|
|
|
buff.clear();
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
buff.push_back(c);
|
|
|
|
|
is.get(cc);
|
|
|
|
|
c = cc;
|
|
|
|
|
} while (c > ' ' && c != ',' && is);
|
|
|
|
|
|
|
|
|
|
status = LEX_TOKEN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == '\r' && is) {
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (status)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
status = is.eof() ? LEX_FEOF: LEX_UNDEF;
|
|
|
|
|
buff.clear();
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
unsigned char c = 0; // getc() returns an int
|
|
|
|
|
char cc = 0;
|
|
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
|
while (is && !status) {
|
|
|
|
|
is.get(cc);
|
|
|
|
|
c = cc;
|
|
|
|
|
|
|
|
|
|
// skip ','s
|
2007-05-28 22:27:45 +00:00
|
|
|
|
if (c == ',')
|
2007-04-24 10:52:22 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (c == commentChar) {
|
|
|
|
|
// Read rest of line (fast :-)
|
|
|
|
|
#if 1
|
|
|
|
|
// That is still not fast... (Lgb)
|
|
|
|
|
string dummy;
|
|
|
|
|
getline(is, dummy);
|
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::LYXLEX, "Comment read: `" << c << dummy << '\'');
|
2007-04-24 10:52:22 +00:00
|
|
|
|
#else
|
|
|
|
|
// but ignore is also still buggy (Lgb)
|
|
|
|
|
// This is fast (Lgb)
|
|
|
|
|
is.ignore(100, '\n');
|
|
|
|
|
#endif
|
|
|
|
|
++lineno;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// string
|
|
|
|
|
if (c == '\"') {
|
|
|
|
|
buff.clear();
|
|
|
|
|
|
|
|
|
|
bool escaped = false;
|
|
|
|
|
do {
|
|
|
|
|
escaped = false;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (c != '\"') {
|
|
|
|
|
printError("Missing quote");
|
|
|
|
|
if (c == '\n')
|
|
|
|
|
++lineno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buff.resize(buff.size() -1);
|
|
|
|
|
status = LEX_DATA;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c > ' ' && is) {
|
|
|
|
|
buff.clear();
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
if (c == '\\') {
|
|
|
|
|
// escape the next char
|
|
|
|
|
is.get(cc);
|
|
|
|
|
c = cc;
|
|
|
|
|
//escaped = true;
|
|
|
|
|
}
|
|
|
|
|
buff.push_back(c);
|
|
|
|
|
is.get(cc);
|
|
|
|
|
c = cc;
|
|
|
|
|
} while (c > ' ' && c != ',' && is);
|
|
|
|
|
|
|
|
|
|
status = LEX_TOKEN;
|
|
|
|
|
}
|
|
|
|
|
// new line
|
|
|
|
|
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
|
|
|
|
int Lexer::Pimpl::search_kw(char const * const tag) const
|
2007-04-24 10:52:22 +00:00
|
|
|
|
{
|
|
|
|
|
keyword_item search_tag = { tag, 0 };
|
|
|
|
|
keyword_item * res =
|
|
|
|
|
lower_bound(table, table + no_items,
|
|
|
|
|
search_tag, compare_tags());
|
|
|
|
|
// 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)
|
2007-04-24 10:52:22 +00:00
|
|
|
|
return search_kw(getString().c_str());
|
|
|
|
|
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 << '\'');
|
2007-04-24 10:52:22 +00:00
|
|
|
|
if (c != '\r')
|
|
|
|
|
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;
|
|
|
|
|
if (c >= ' ' && is) {
|
|
|
|
|
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;
|
|
|
|
|
} while (c >= ' ' && c != '\\' && is);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == '\\') is.putback(c); // put it back
|
|
|
|
|
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
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
Lexer::Lexer(keyword_item * tab, int num)
|
2000-04-08 17:02:02 +00:00
|
|
|
|
: pimpl_(new Pimpl(tab, num))
|
|
|
|
|
{}
|
2000-03-09 03:36:48 +00:00
|
|
|
|
|
2000-01-06 11:35:29 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
void Lexer::setLineNo(int l)
|
2000-04-08 17:02:02 +00:00
|
|
|
|
{
|
|
|
|
|
pimpl_->lineno = l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
int Lexer::getLineNo() 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
void Lexer::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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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-04-26 11:30:54 +00:00
|
|
|
|
bool Lexer::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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
string const Lexer::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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
docstring const Lexer::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;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-03-26 13:43:49 +00:00
|
|
|
|
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)
|
2007-04-26 11:30:54 +00:00
|
|
|
|
string const Lexer::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
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::PARSER, "LongString: `" << getString() << '\'');
|
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;
|
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.
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
bool Lexer::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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
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-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-04-26 11:30:54 +00:00
|
|
|
|
Lexer & Lexer::operator>>(std::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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// quotes a string, e.g. for use in preferences files or as an argument of the "log" dialog
|
2007-04-26 11:30:54 +00:00
|
|
|
|
string const Lexer::quoteString(string const & arg)
|
2006-08-12 10:39:12 +00:00
|
|
|
|
{
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
os << '"' << subst(subst(arg, "\\", "\\\\"), "\"", "\\\"") << '"';
|
|
|
|
|
return os.str();
|
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|