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