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
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
*
|
|
|
|
|
* 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"
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
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
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
///
|
|
|
|
|
struct keyword_item {
|
|
|
|
|
///
|
1999-12-01 00:57:31 +00:00
|
|
|
|
char const * tag;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
///
|
2001-11-26 11:08:43 +00:00
|
|
|
|
int code;
|
1999-09-27 18:44:28 +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
|
|
|
|
*/
|
2007-10-31 22:40:34 +00:00
|
|
|
|
class Lexer {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
public:
|
|
|
|
|
///
|
2007-04-26 11:30:54 +00:00
|
|
|
|
Lexer(keyword_item *, int);
|
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
|
2007-03-26 13:43:49 +00:00
|
|
|
|
/// FIXME: test also if pushTok 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.
|
2000-04-08 17:02:02 +00:00
|
|
|
|
void setLineNo(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();
|
|
|
|
|
|
2007-03-26 13:43:49 +00:00
|
|
|
|
/** Just read the next word. If esc is true remember that
|
2000-08-07 20:58:24 +00:00
|
|
|
|
some chars might be escaped: "\ atleast
|
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
bool next(bool esc = false);
|
|
|
|
|
|
|
|
|
|
/** Read next token. This one is almost the same as next,
|
2000-08-07 20:58:24 +00:00
|
|
|
|
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();
|
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
|
|
|
|
|
|
|
|
|
///
|
2001-08-06 19:12:46 +00:00
|
|
|
|
int getLineNo() 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
|
|
|
|
///
|
2003-10-06 15:43:21 +00:00
|
|
|
|
std::string const getString() const;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
|
///
|
2006-10-21 00:16:43 +00:00
|
|
|
|
docstring const getDocString() const;
|
2006-08-13 22:54:59 +00:00
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2003-10-06 15:43:21 +00:00
|
|
|
|
std::string const getLongString(std::string const & endtag);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
///
|
2001-08-06 19:12:46 +00:00
|
|
|
|
bool eatLine();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-12-02 10:19:51 +00:00
|
|
|
|
/// Pushes a token list on a stack and replaces it with a new one.
|
1999-12-01 00:57:31 +00:00
|
|
|
|
void pushTable(keyword_item *, int);
|
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 &);
|
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 &);
|
2003-12-02 10:19:51 +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
|
|
|
|
|
static std::string const quoteString(std::string const &);
|
|
|
|
|
|
2000-04-08 17:02:02 +00:00
|
|
|
|
private:
|
2007-10-31 22:40:34 +00:00
|
|
|
|
/// noncopiable
|
|
|
|
|
Lexer(Lexer const &);
|
|
|
|
|
void operator=(Lexer const &);
|
|
|
|
|
|
|
|
|
|
///
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2007-04-26 11:30:54 +00:00
|
|
|
|
class PushPopHelper {
|
2005-01-19 15:03:31 +00:00
|
|
|
|
public:
|
2000-09-14 17:53:12 +00:00
|
|
|
|
///
|
2007-04-26 11:30:54 +00:00
|
|
|
|
PushPopHelper(Lexer & lexrc, keyword_item * i, int s) : lex(lexrc) {
|
1999-12-01 00:57:31 +00:00
|
|
|
|
lex.pushTable(i, s);
|
|
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
///
|
2007-04-26 11:30:54 +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
|