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