]> git.lyx.org Git - lyx.git/blob - src/lyxlex.h
minimal effort implementation of:
[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/types.h"
21
22 #include <boost/utility.hpp>
23
24 #include <iosfwd>
25 #include <string>
26
27
28 ///
29 struct keyword_item {
30         ///
31         char const * tag;
32         ///
33         int code;
34 };
35
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.
40   */
41 class LyXLex : boost::noncopyable {
42 public:
43         ///
44         LyXLex(keyword_item *, int);
45         ///
46         ~LyXLex();
47
48         /// Lex basic codes
49         enum {
50                 ///
51                 LEX_UNDEF = -1,
52                 ///
53                 LEX_FEOF  = -2,
54                 ///
55                 LEX_DATA  = -3,
56                 ///
57                 LEX_TOKEN = -4
58         };
59
60         /// stream is open and end of stream is not reached
61         bool isOK() const;
62         /// stream is ok
63         operator void const *() const;
64         /// stream is not ok
65         bool operator!() const;
66         /// return true if able to open file, else false
67         bool setFile(std::string const & filename);
68         ///
69         void setStream(std::istream & is);
70         ///
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);
76
77         /// returns a lex code
78         int lex();
79
80         /** Just read athe next word. If esc is true remember that
81             some chars might be escaped: "\ atleast
82         */
83         bool next(bool esc = false);
84
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.
88         */
89         bool nextToken();
90         /// Push a token, that next token got from lyxlex.
91         void pushToken(std::string const &);
92
93         ///
94         int getLineNo() const;
95
96         ///
97         int getInteger() const;
98         ///
99         bool getBool() const;
100         ///
101         double getFloat() const;
102         ///
103         std::string const getString() const;
104
105         ///
106         lyx::docstring const getDocString() const;
107
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.
114         */
115         std::string const getLongString(std::string const & endtag);
116
117         ///
118         bool eatLine();
119
120         /// Pushes a token list on a stack and replaces it with a new one.
121         void pushTable(keyword_item *, int);
122
123         /** Pops a token list into void and replaces it with the one now
124             on top of the stack.
125         */
126         void popTable();
127
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()
131         */
132         void printError(std::string const & message) const;
133
134         /// Prints the current token table on the supplied ostream.
135         void printTable(std::ostream &);
136
137         /// extract string
138         LyXLex & operator>>(std::string &);
139         /// extract double
140         LyXLex & operator>>(double &);
141         /// extract integer
142         LyXLex & operator>>(int &);
143         /// extract unsigned integer
144         LyXLex & operator>>(unsigned int &);
145         /// extract bool
146         LyXLex & operator>>(bool &);
147
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 &);
151
152 private:
153         class Pimpl;
154         ///
155         Pimpl * pimpl_;
156 };
157
158
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
162     exceptions.
163     @author Lgb
164 */
165 class pushpophelper {
166 public:
167         ///
168         pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
169                 lex.pushTable(i, s);
170         }
171         ///
172         ~pushpophelper() {
173                 lex.popTable();
174         }
175         ///
176         LyXLex & lex;
177 };
178 /** Avoid wrong usage of pushpophelper.
179     To avoid wrong usage:
180     pushpophelper(...); // wrong
181     pushpophelper pph(...); // right
182 */
183 #define pushpophelper(x, y, z) unnamed_pushpophelper;
184 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
185
186 #endif