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