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/docstring.h"
22 #include <boost/utility.hpp>
29 namespace support { class FileName; }
39 /** Generalized simple lexical analizer.
40 It can be used for simple syntax parsers, like lyxrc,
41 texclass and others to come.
42 @see lyxrc.C for an example of usage.
44 class LyXLex : boost::noncopyable {
47 LyXLex(keyword_item *, int);
63 /// stream is open and end of stream is not reached
66 operator void const *() const;
68 bool operator!() const;
69 /// return true if able to open file, else false
70 bool setFile(support::FileName const & filename);
72 void setStream(std::istream & is);
74 std::istream & getStream();
75 /// Danger! Don't use it unless you know what you are doing.
76 void setLineNo(int l);
77 /// Change the character that begins a comment. Default is '#'
78 void setCommentChar(char c);
80 /// returns a lex code
83 /** Just read athe next word. If esc is true remember that
84 some chars might be escaped: "\ atleast
86 bool next(bool esc = false);
88 /** Read next token. This one is almost the same as next,
89 but it will consider " as a regular character and always
90 split a word if it contains a backslash.
93 /// Push a token, that next token got from lyxlex.
94 void pushToken(std::string const &);
97 int getLineNo() const;
100 int getInteger() const;
102 bool getBool() const;
104 double getFloat() const;
106 std::string const getString() const;
109 docstring const getDocString() const;
111 /** Get a long string, ended by the tag `endtag'.
112 This string can span several lines. The first line
113 serves as a template for how many spaces the lines
114 are indented. This much white space is skipped from
115 each following line. This mechanism does not work
116 perfectly if you use tabs.
118 std::string const getLongString(std::string const & endtag);
123 /// Pushes a token list on a stack and replaces it with a new one.
124 void pushTable(keyword_item *, int);
126 /** Pops a token list into void and replaces it with the one now
131 /** Prints an error message with the corresponding line number
132 and file name. If message contains the substring `$$Token',
133 it is replaced with the value of GetString()
135 void printError(std::string const & message) const;
137 /// Prints the current token table on the supplied ostream.
138 void printTable(std::ostream &);
141 LyXLex & operator>>(std::string &);
142 /// extract docstring
143 LyXLex & operator>>(docstring &);
145 LyXLex & operator>>(double &);
147 LyXLex & operator>>(int &);
148 /// extract unsigned integer
149 LyXLex & operator>>(unsigned int &);
151 LyXLex & operator>>(bool &);
153 /// Quotes a string so that reading it again with LyXLex::next(true)
154 /// gets the original string
155 static std::string const quoteString(std::string const &);
164 /** Use to enable multiple exit points.
165 This is needed to ensure that the pop is done upon exit from methods
166 with more than one exit point or that can return as a response to
170 class pushpophelper {
173 pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
183 /** Avoid wrong usage of pushpophelper.
184 To avoid wrong usage:
185 pushpophelper(...); // wrong
186 pushpophelper pph(...); // right
188 #define pushpophelper(x, y, z) unnamed_pushpophelper;
189 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal