1999-09-27 18:44:28 +00:00
|
|
|
// -*- C++ -*-
|
2003-08-23 00:17:00 +00:00
|
|
|
/**
|
2007-04-26 11:30:54 +00:00
|
|
|
* \file Lexer.h
|
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.
|
|
|
|
*
|
|
|
|
* \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
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// Generalized simple lexical analizer.
|
|
|
|
// It can be used for simple syntax parsers, like lyxrc,
|
2003-08-23 00:17:00 +00:00
|
|
|
// texclass and others to come.
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
#ifndef LEXER_H
|
|
|
|
#define LEXER_H
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2007-10-31 22:40:34 +00:00
|
|
|
#include "support/strfwd.h"
|
2011-12-07 16:03:37 +00:00
|
|
|
#include <string>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
namespace support { class FileName; }
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
/** A helper structure to describe a keyword for the Lexer.
|
2017-07-03 17:45:58 +00:00
|
|
|
Usually used bundled in C style arrays and passed to the
|
2008-04-02 23:06:22 +00:00
|
|
|
Lexer using a LexerKeywordTable object.
|
|
|
|
*/
|
|
|
|
struct LexerKeyword
|
|
|
|
{
|
|
|
|
/// the string to be recognized
|
1999-12-01 00:57:31 +00:00
|
|
|
char const * tag;
|
2008-04-02 23:06:22 +00:00
|
|
|
/// a corresponding numerical id
|
2001-11-26 11:08:43 +00:00
|
|
|
int code;
|
1999-09-27 18:44:28 +00:00
|
|
|
};
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
|
2000-08-07 20:58:24 +00:00
|
|
|
/** Generalized simple lexical analizer.
|
2007-05-28 22:27:45 +00:00
|
|
|
Use the method isOK() to check if there is still data available
|
2007-03-26 13:43:49 +00:00
|
|
|
for lexing. Use one of the the operators void* or ! to test if
|
|
|
|
the last reading operation was successful.
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-03-26 13:43:49 +00:00
|
|
|
Example:
|
|
|
|
|
2008-03-20 20:36:30 +00:00
|
|
|
int readParam(LyxLex & lex)
|
|
|
|
{
|
|
|
|
int param = 1; // default value
|
2007-03-26 13:43:49 +00:00
|
|
|
if (lex.isOK()) { // the lexer has data to read
|
2008-03-20 20:36:30 +00:00
|
|
|
int p; // temporary variable
|
2007-03-26 13:43:49 +00:00
|
|
|
lex >> p;
|
2008-03-20 20:36:30 +00:00
|
|
|
if (lex)
|
|
|
|
param = p; // only use the input if reading was successful
|
2007-03-26 13:43:49 +00:00
|
|
|
}
|
|
|
|
return param;
|
2007-05-28 22:27:45 +00:00
|
|
|
}
|
2007-03-26 13:43:49 +00:00
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
@see LyXRC.cpp for an example of usage.
|
1999-09-27 18:44:28 +00:00
|
|
|
*/
|
2008-04-02 23:06:22 +00:00
|
|
|
class Lexer
|
|
|
|
{
|
1999-09-27 18:44:28 +00:00
|
|
|
public:
|
2008-04-02 23:06:22 +00:00
|
|
|
/// initialize Lexer with no special keywords.
|
2015-03-05 11:24:47 +00:00
|
|
|
Lexer();
|
2008-04-02 23:06:22 +00:00
|
|
|
/// initialize Lexer with a bunch of keywords
|
|
|
|
template<int N> Lexer(LexerKeyword (&table)[N])
|
2015-03-05 11:24:47 +00:00
|
|
|
: pimpl_(0), lastReadOk_(false) { init(table, N); }
|
2008-04-02 23:06:22 +00:00
|
|
|
|
2000-04-08 17:02:02 +00:00
|
|
|
///
|
2007-04-26 11:30:54 +00:00
|
|
|
~Lexer();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Lex basic codes
|
|
|
|
enum {
|
|
|
|
///
|
|
|
|
LEX_UNDEF = -1,
|
|
|
|
///
|
|
|
|
LEX_FEOF = -2,
|
|
|
|
///
|
|
|
|
LEX_DATA = -3,
|
|
|
|
///
|
|
|
|
LEX_TOKEN = -4
|
|
|
|
};
|
|
|
|
|
2003-12-02 12:27:07 +00:00
|
|
|
/// stream is open and end of stream is not reached
|
2008-04-02 23:06:22 +00:00
|
|
|
/// FIXME: test also if pushToken is not empty
|
2007-05-28 22:27:45 +00:00
|
|
|
/// FIXME: the method should be renamed to something like
|
2007-03-26 13:43:49 +00:00
|
|
|
/// dataAvailable(), in order to reflect the real behavior
|
2001-08-06 19:12:46 +00:00
|
|
|
bool isOK() const;
|
2007-03-26 13:43:49 +00:00
|
|
|
/// FIXME: The next two operators should be replaced by one method
|
2007-05-28 22:27:45 +00:00
|
|
|
/// called e.g. lastReadOk(), in order to reflect the real
|
2007-03-26 13:43:49 +00:00
|
|
|
/// behavior
|
|
|
|
/// last read operation was successful.
|
2003-12-02 12:27:07 +00:00
|
|
|
operator void const *() const;
|
2007-03-26 13:43:49 +00:00
|
|
|
/// last read operation was not successful
|
2003-12-02 10:19:51 +00:00
|
|
|
bool operator!() const;
|
1999-09-27 18:44:28 +00:00
|
|
|
/// return true if able to open file, else false
|
2006-11-26 21:30:39 +00:00
|
|
|
bool setFile(support::FileName const & filename);
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2003-02-26 18:03:48 +00:00
|
|
|
void setStream(std::istream & is);
|
2000-01-06 02:44:26 +00:00
|
|
|
///
|
2000-04-08 17:02:02 +00:00
|
|
|
std::istream & getStream();
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Danger! Don't use it unless you know what you are doing.
|
2008-04-02 23:06:22 +00:00
|
|
|
void setLineNumber(int l);
|
2000-11-08 15:19:55 +00:00
|
|
|
/// Change the character that begins a comment. Default is '#'
|
|
|
|
void setCommentChar(char c);
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// returns a lex code
|
|
|
|
int lex();
|
|
|
|
|
2017-07-03 17:45:58 +00:00
|
|
|
/// Read the next string, as delimited by double quotes or
|
|
|
|
/// whitespace. If esc is true, then we remember that some chars
|
2010-03-29 18:00:26 +00:00
|
|
|
/// might be escaped: \" at least.
|
1999-09-27 18:44:28 +00:00
|
|
|
bool next(bool esc = false);
|
|
|
|
|
2010-03-29 18:00:26 +00:00
|
|
|
/// Read next token. This one is almost the same as next(),
|
|
|
|
/// but it will consider " as a regular character and always
|
|
|
|
/// split a word if it contains a backslash.
|
1999-09-27 18:44:28 +00:00
|
|
|
bool nextToken();
|
2010-03-29 18:00:26 +00:00
|
|
|
|
2017-07-03 17:45:58 +00:00
|
|
|
/// Puts the rest of the line in the buffer, where it will
|
2010-03-29 18:00:26 +00:00
|
|
|
/// be available via getString() or getDocString().
|
|
|
|
bool eatLine();
|
|
|
|
|
2000-05-17 14:43:09 +00:00
|
|
|
/// Push a token, that next token got from lyxlex.
|
2003-10-06 15:43:21 +00:00
|
|
|
void pushToken(std::string const &);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
/// return the current line number
|
|
|
|
int lineNumber() const;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2001-08-06 19:12:46 +00:00
|
|
|
int getInteger() const;
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2001-08-06 19:12:46 +00:00
|
|
|
bool getBool() const;
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2005-01-27 21:05:44 +00:00
|
|
|
double getFloat() const;
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2015-05-19 08:15:34 +00:00
|
|
|
std::string const getString(bool trim = false) const;
|
2006-08-13 22:54:59 +00:00
|
|
|
///
|
2015-05-19 08:15:34 +00:00
|
|
|
docstring const getDocString(bool trim = false) const;
|
2000-08-07 20:58:24 +00:00
|
|
|
/** Get a long string, ended by the tag `endtag'.
|
|
|
|
This string can span several lines. The first line
|
|
|
|
serves as a template for how many spaces the lines
|
|
|
|
are indented. This much white space is skipped from
|
|
|
|
each following line. This mechanism does not work
|
|
|
|
perfectly if you use tabs.
|
|
|
|
*/
|
2016-09-25 10:37:40 +00:00
|
|
|
docstring getLongString(docstring const & endtag);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2003-12-02 10:19:51 +00:00
|
|
|
/// Pushes a token list on a stack and replaces it with a new one.
|
2008-04-02 23:06:22 +00:00
|
|
|
template<int N> void pushTable(LexerKeyword (&table)[N])
|
|
|
|
{ pushTable(table, N); }
|
2002-03-21 17:27:08 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/** Pops a token list into void and replaces it with the one now
|
2000-08-07 20:58:24 +00:00
|
|
|
on top of the stack.
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
void popTable();
|
|
|
|
|
|
|
|
/** Prints an error message with the corresponding line number
|
2000-08-07 20:58:24 +00:00
|
|
|
and file name. If message contains the substring `$$Token',
|
|
|
|
it is replaced with the value of GetString()
|
|
|
|
*/
|
2003-10-06 15:43:21 +00:00
|
|
|
void printError(std::string const & message) const;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2003-12-02 10:19:51 +00:00
|
|
|
/// Prints the current token table on the supplied ostream.
|
2000-04-04 00:19:15 +00:00
|
|
|
void printTable(std::ostream &);
|
2008-04-03 22:38:14 +00:00
|
|
|
/// Used to dispaly context information in case of errors.
|
|
|
|
void setContext(std::string const & functionName);
|
2003-12-02 10:19:51 +00:00
|
|
|
|
|
|
|
/// extract string
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & operator>>(std::string &);
|
2006-11-03 15:16:45 +00:00
|
|
|
/// extract docstring
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & operator>>(docstring &);
|
2003-12-02 10:19:51 +00:00
|
|
|
/// extract double
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & operator>>(double &);
|
2003-12-02 10:19:51 +00:00
|
|
|
/// extract integer
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & operator>>(int &);
|
2003-12-02 10:19:51 +00:00
|
|
|
/// extract unsigned integer
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & operator>>(unsigned int &);
|
2003-12-02 10:19:51 +00:00
|
|
|
/// extract bool
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & operator>>(bool &);
|
2008-04-05 10:34:29 +00:00
|
|
|
/// extract first char of the string
|
|
|
|
Lexer & operator>>(char &);
|
2003-12-02 10:19:51 +00:00
|
|
|
|
2008-04-03 22:38:14 +00:00
|
|
|
/// read and check a required token
|
|
|
|
Lexer & operator>>(char const * required);
|
2008-04-05 10:34:29 +00:00
|
|
|
/// check for an optional token and swallow it if present.
|
|
|
|
bool checkFor(char const * required);
|
2008-04-03 22:38:14 +00:00
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
/// Quotes a string so that reading it again with Lexer::next(true)
|
2006-08-12 10:39:12 +00:00
|
|
|
/// gets the original string
|
2008-04-03 22:38:14 +00:00
|
|
|
static std::string quoteString(std::string const &);
|
2008-11-25 14:44:17 +00:00
|
|
|
/// Quotes a docstring so that reading it again with Lexer::next(true)
|
|
|
|
/// gets the original string
|
|
|
|
static docstring quoteString(docstring const &);
|
2006-08-12 10:39:12 +00:00
|
|
|
|
2000-04-08 17:02:02 +00:00
|
|
|
private:
|
2008-04-02 23:06:22 +00:00
|
|
|
/// noncopyable
|
2007-10-31 22:40:34 +00:00
|
|
|
Lexer(Lexer const &);
|
|
|
|
void operator=(Lexer const &);
|
|
|
|
|
2008-04-02 23:06:22 +00:00
|
|
|
///
|
|
|
|
friend class PushPopHelper;
|
|
|
|
|
|
|
|
///
|
|
|
|
void init(LexerKeyword *, int);
|
|
|
|
void pushTable(LexerKeyword *, int);
|
|
|
|
|
2007-10-31 22:40:34 +00:00
|
|
|
///
|
2005-01-19 15:03:31 +00:00
|
|
|
class Pimpl;
|
2000-08-07 20:58:24 +00:00
|
|
|
///
|
2000-04-08 17:02:02 +00:00
|
|
|
Pimpl * pimpl_;
|
2007-03-26 13:43:49 +00:00
|
|
|
///
|
|
|
|
mutable bool lastReadOk_;
|
1999-09-27 18:44:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-04-03 22:38:14 +00:00
|
|
|
/// extract something constructable from a string, i.e. a LaTeX length
|
|
|
|
template <class T>
|
|
|
|
Lexer & operator>>(Lexer & lex, T & t)
|
|
|
|
{
|
|
|
|
if (lex.next())
|
|
|
|
t = T(lex.getString());
|
|
|
|
return lex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
/** Use to enable multiple exit points.
|
2000-08-07 20:58:24 +00:00
|
|
|
This is needed to ensure that the pop is done upon exit from methods
|
|
|
|
with more than one exit point or that can return as a response to
|
|
|
|
exceptions.
|
2004-02-25 12:00:53 +00:00
|
|
|
@author Lgb
|
2000-08-07 20:58:24 +00:00
|
|
|
*/
|
2008-04-02 23:06:22 +00:00
|
|
|
class PushPopHelper
|
|
|
|
{
|
2005-01-19 15:03:31 +00:00
|
|
|
public:
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
2008-04-03 20:55:09 +00:00
|
|
|
template<int N>
|
|
|
|
PushPopHelper(Lexer & l, LexerKeyword (&table)[N])
|
|
|
|
: lex(l)
|
2008-04-02 23:06:22 +00:00
|
|
|
{
|
2008-04-03 20:55:09 +00:00
|
|
|
lex.pushTable(table, N);
|
1999-12-01 00:57:31 +00:00
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
2008-04-02 23:06:22 +00:00
|
|
|
~PushPopHelper()
|
|
|
|
{
|
1999-12-01 00:57:31 +00:00
|
|
|
lex.popTable();
|
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
2007-04-26 11:30:54 +00:00
|
|
|
Lexer & lex;
|
1999-12-01 00:57:31 +00:00
|
|
|
};
|
2007-04-26 11:30:54 +00:00
|
|
|
/** Avoid wrong usage of PushPopHelper.
|
2000-08-07 20:58:24 +00:00
|
|
|
To avoid wrong usage:
|
2007-04-26 11:30:54 +00:00
|
|
|
PushPopHelper(...); // wrong
|
|
|
|
PushPopHelper pph(...); // right
|
2000-08-07 20:58:24 +00:00
|
|
|
*/
|
2007-04-26 11:30:54 +00:00
|
|
|
#define PushPopHelper(x, y, z) unnamed_PushPopHelper;
|
2000-02-22 00:36:17 +00:00
|
|
|
// Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
|
1999-12-01 00:57:31 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
} // namespace lyx
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|