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