4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alejandro Aguilar Sierra
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
13 // Generalized simple lexical analizer.
14 // It can be used for simple syntax parsers, like lyxrc,
15 // texclass and others to come.
20 #include "support/types.h"
22 #include <boost/utility.hpp>
36 /** Generalized simple lexical analizer.
37 It can be used for simple syntax parsers, like lyxrc,
38 texclass and others to come.
39 @see lyxrc.C for an example of usage.
41 class LyXLex : boost::noncopyable {
44 LyXLex(keyword_item *, int);
60 /// stream is open and end of stream is not reached
63 operator void const *() const;
65 bool operator!() const;
66 /// return true if able to open file, else false
67 bool setFile(std::string const & filename);
69 void setStream(std::istream & is);
71 std::istream & getStream();
72 /// Danger! Don't use it unless you know what you are doing.
73 void setLineNo(int l);
74 /// Change the character that begins a comment. Default is '#'
75 void setCommentChar(char c);
77 /// returns a lex code
80 /** Just read athe next word. If esc is true remember that
81 some chars might be escaped: "\ atleast
83 bool next(bool esc = false);
85 /** Read next token. This one is almost the same as next,
86 but it will consider " as a regular character and always
87 split a word if it contains a backslash.
90 /// Push a token, that next token got from lyxlex.
91 void pushToken(std::string const &);
94 int getLineNo() const;
97 int getInteger() const;
101 double getFloat() const;
103 std::string const getString() const;
106 lyx::docstring const getDocString() const;
108 /** Get a long string, ended by the tag `endtag'.
109 This string can span several lines. The first line
110 serves as a template for how many spaces the lines
111 are indented. This much white space is skipped from
112 each following line. This mechanism does not work
113 perfectly if you use tabs.
115 std::string const getLongString(std::string const & endtag);
120 /// Pushes a token list on a stack and replaces it with a new one.
121 void pushTable(keyword_item *, int);
123 /** Pops a token list into void and replaces it with the one now
128 /** Prints an error message with the corresponding line number
129 and file name. If message contains the substring `$$Token',
130 it is replaced with the value of GetString()
132 void printError(std::string const & message) const;
134 /// Prints the current token table on the supplied ostream.
135 void printTable(std::ostream &);
138 LyXLex & operator>>(std::string &);
140 LyXLex & operator>>(double &);
142 LyXLex & operator>>(int &);
143 /// extract unsigned integer
144 LyXLex & operator>>(unsigned int &);
146 LyXLex & operator>>(bool &);
148 /// Quotes a string so that reading it again with LyXLex::next(true)
149 /// gets the original string
150 static std::string const quoteString(std::string const &);
159 /** Use to enable multiple exit points.
160 This is needed to ensure that the pop is done upon exit from methods
161 with more than one exit point or that can return as a response to
165 class pushpophelper {
168 pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
178 /** Avoid wrong usage of pushpophelper.
179 To avoid wrong usage:
180 pushpophelper(...); // wrong
181 pushpophelper pph(...); // right
183 #define pushpophelper(x, y, z) unnamed_pushpophelper;
184 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal