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