]> git.lyx.org Git - lyx.git/blob - src/lyxlex.h
* Painter.h:
[lyx.git] / src / lyxlex.h
1 // -*- C++ -*-
2 /**
3  * \file lyxlex.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 //  Generalized simple lexical analizer.
14 //  It can be used for simple syntax parsers, like lyxrc,
15 //  texclass and others to come.
16
17 #ifndef LYXLEX_H
18 #define LYXLEX_H
19
20 #include "support/docstring.h"
21
22 #include <boost/utility.hpp>
23
24 #include <iosfwd>
25
26
27 namespace lyx {
28
29
30 ///
31 struct keyword_item {
32         ///
33         char const * tag;
34         ///
35         int code;
36 };
37
38 /** Generalized simple lexical analizer.
39     It can be used for simple syntax parsers, like lyxrc,
40     texclass and others to come.
41     @see lyxrc.C for an example of usage.
42   */
43 class LyXLex : boost::noncopyable {
44 public:
45         ///
46         LyXLex(keyword_item *, int);
47         ///
48         ~LyXLex();
49
50         /// Lex basic codes
51         enum {
52                 ///
53                 LEX_UNDEF = -1,
54                 ///
55                 LEX_FEOF  = -2,
56                 ///
57                 LEX_DATA  = -3,
58                 ///
59                 LEX_TOKEN = -4
60         };
61
62         /// stream is open and end of stream is not reached
63         bool isOK() const;
64         /// stream is ok
65         operator void const *() const;
66         /// stream is not ok
67         bool operator!() const;
68         /// return true if able to open file, else false
69         bool setFile(std::string const & filename);
70         ///
71         void setStream(std::istream & is);
72         ///
73         std::istream & getStream();
74         /// Danger! Don't use it unless you know what you are doing.
75         void setLineNo(int l);
76         /// Change the character that begins a comment. Default is '#'
77         void setCommentChar(char c);
78
79         /// returns a lex code
80         int lex();
81
82         /** Just read athe next word. If esc is true remember that
83             some chars might be escaped: "\ atleast
84         */
85         bool next(bool esc = false);
86
87         /** Read next token. This one is almost the same as next,
88             but it will consider " as a regular character and always
89             split a word if it contains a backslash.
90         */
91         bool nextToken();
92         /// Push a token, that next token got from lyxlex.
93         void pushToken(std::string const &);
94
95         ///
96         int getLineNo() const;
97
98         ///
99         int getInteger() const;
100         ///
101         bool getBool() const;
102         ///
103         double getFloat() const;
104         ///
105         std::string const getString() const;
106
107         ///
108         docstring const getDocString() const;
109
110         /** Get a long string, ended by the tag `endtag'.
111             This string can span several lines. The first line
112             serves as a template for how many spaces the lines
113             are indented. This much white space is skipped from
114             each following line. This mechanism does not work
115             perfectly if you use tabs.
116         */
117         std::string const getLongString(std::string const & endtag);
118
119         ///
120         bool eatLine();
121
122         /// Pushes a token list on a stack and replaces it with a new one.
123         void pushTable(keyword_item *, int);
124
125         /** Pops a token list into void and replaces it with the one now
126             on top of the stack.
127         */
128         void popTable();
129
130         /** Prints an error message with the corresponding line number
131             and file name. If message contains the substring `$$Token',
132             it is replaced with the value of GetString()
133         */
134         void printError(std::string const & message) const;
135
136         /// Prints the current token table on the supplied ostream.
137         void printTable(std::ostream &);
138
139         /// extract string
140         LyXLex & operator>>(std::string &);
141         /// extract double
142         LyXLex & operator>>(double &);
143         /// extract integer
144         LyXLex & operator>>(int &);
145         /// extract unsigned integer
146         LyXLex & operator>>(unsigned int &);
147         /// extract bool
148         LyXLex & operator>>(bool &);
149
150         /// Quotes a string so that reading it again with LyXLex::next(true)
151         /// gets the original string
152         static std::string const quoteString(std::string const &);
153
154 private:
155         class Pimpl;
156         ///
157         Pimpl * pimpl_;
158 };
159
160
161 /** Use to enable multiple exit points.
162     This is needed to ensure that the pop is done upon exit from methods
163     with more than one exit point or that can return as a response to
164     exceptions.
165     @author Lgb
166 */
167 class pushpophelper {
168 public:
169         ///
170         pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
171                 lex.pushTable(i, s);
172         }
173         ///
174         ~pushpophelper() {
175                 lex.popTable();
176         }
177         ///
178         LyXLex & lex;
179 };
180 /** Avoid wrong usage of pushpophelper.
181     To avoid wrong usage:
182     pushpophelper(...); // wrong
183     pushpophelper pph(...); // right
184 */
185 #define pushpophelper(x, y, z) unnamed_pushpophelper;
186 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
187
188
189 } // namespace lyx
190
191 #endif