1999-09-27 18:44:28 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
// Generalized simple lexical analizer.
|
|
|
|
// It can be used for simple syntax parsers, like lyxrc,
|
|
|
|
// texclass and others to come. [asierra30/03/96]
|
|
|
|
//
|
2000-03-09 03:36:48 +00:00
|
|
|
// Copyright 1996 Lyx Team.
|
1999-12-01 00:57:31 +00:00
|
|
|
#ifndef LYXLEX_H
|
|
|
|
#define LYXLEX_H
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma interface
|
|
|
|
#endif
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
#include <iosfwd>
|
2000-01-10 16:28:29 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "LString.h"
|
2000-08-05 05:17:18 +00:00
|
|
|
#include "support/utility.hpp"
|
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
|
|
|
///
|
|
|
|
short code;
|
|
|
|
};
|
|
|
|
|
2000-08-07 20:58:24 +00:00
|
|
|
/** Generalized simple lexical analizer.
|
|
|
|
It can be used for simple syntax parsers, like lyxrc,
|
|
|
|
texclass and others to come.
|
|
|
|
@see lyxrc.C for an example of usage.
|
1999-09-27 18:44:28 +00:00
|
|
|
*/
|
2000-08-05 05:17:18 +00:00
|
|
|
class LyXLex : public noncopyable {
|
1999-09-27 18:44:28 +00:00
|
|
|
public:
|
|
|
|
///
|
1999-12-01 00:57:31 +00:00
|
|
|
LyXLex (keyword_item *, int);
|
2000-04-08 17:02:02 +00:00
|
|
|
///
|
|
|
|
~LyXLex();
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Lex basic codes
|
|
|
|
enum {
|
|
|
|
///
|
|
|
|
LEX_UNDEF = -1,
|
|
|
|
///
|
|
|
|
LEX_FEOF = -2,
|
|
|
|
///
|
|
|
|
LEX_DATA = -3,
|
|
|
|
///
|
|
|
|
LEX_TOKEN = -4
|
|
|
|
};
|
|
|
|
|
|
|
|
/// file is open and end of file is not reached
|
1999-12-01 00:57:31 +00:00
|
|
|
bool IsOK() const;
|
1999-09-27 18:44:28 +00:00
|
|
|
/// return true if able to open file, else false
|
1999-10-02 16:21:10 +00:00
|
|
|
bool setFile(string const & filename);
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2000-04-04 00:19:15 +00:00
|
|
|
void setStream(std::istream & i);
|
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);
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// returns a lex code
|
|
|
|
int lex();
|
|
|
|
|
|
|
|
/** Just read athe 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.
|
|
|
|
void pushToken(string const &);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
///
|
2000-04-08 17:02:02 +00:00
|
|
|
int GetLineNo() const;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
1999-12-01 00:57:31 +00:00
|
|
|
int GetInteger() const;
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
1999-12-01 00:57:31 +00:00
|
|
|
bool GetBool() const;
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
1999-12-01 00:57:31 +00:00
|
|
|
float GetFloat() const;
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2000-08-05 05:17:18 +00:00
|
|
|
string const GetString() const;
|
1999-09-27 18:44:28 +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.
|
|
|
|
*/
|
2000-08-05 05:17:18 +00:00
|
|
|
string const getLongString(string const & endtag);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
bool EatLine();
|
|
|
|
///
|
2000-03-09 03:36:48 +00:00
|
|
|
int FindToken(char const * str[]);
|
1999-09-27 18:44:28 +00:00
|
|
|
///
|
2000-03-09 03:36:48 +00:00
|
|
|
int CheckToken(char const * str[], int print_error);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
///
|
2000-09-14 17:53:12 +00:00
|
|
|
string const text() const;
|
1999-09-27 18:44:28 +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);
|
2000-08-07 20:58:24 +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()
|
|
|
|
*/
|
1999-12-01 00:57:31 +00:00
|
|
|
void printError(string const & message) const;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/**
|
2000-08-07 20:58:24 +00:00
|
|
|
Prints the current token table on the supplied ostream.
|
|
|
|
*/
|
2000-04-04 00:19:15 +00:00
|
|
|
void printTable(std::ostream &);
|
2000-04-08 17:02:02 +00:00
|
|
|
private:
|
|
|
|
struct Pimpl;
|
2000-08-07 20:58:24 +00:00
|
|
|
///
|
2000-04-08 17:02:02 +00:00
|
|
|
Pimpl * pimpl_;
|
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.
|
|
|
|
@autor Lgb
|
|
|
|
*/
|
1999-12-01 00:57:31 +00:00
|
|
|
struct pushpophelper {
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
1999-12-01 00:57:31 +00:00
|
|
|
pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
|
|
|
|
lex.pushTable(i, s);
|
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
1999-12-01 00:57:31 +00:00
|
|
|
~pushpophelper() {
|
|
|
|
lex.popTable();
|
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
1999-12-01 00:57:31 +00:00
|
|
|
LyXLex & lex;
|
|
|
|
};
|
2000-08-07 20:58:24 +00:00
|
|
|
/** Avoid wrong usage of pushpophelper.
|
|
|
|
To avoid wrong usage:
|
|
|
|
pushpophelper(...); // wrong
|
|
|
|
pushpophelper pph(...); // right
|
|
|
|
*/
|
2000-02-22 00:36:17 +00:00
|
|
|
#define pushpophelper(x, y, z) unnamed_pushpophelper;
|
|
|
|
// Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
|
1999-12-01 00:57:31 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|