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