]> git.lyx.org Git - lyx.git/blob - src/lyxlex.h
0bd9d6f414ac41f10da6a08588076ac3f6e4c8d8
[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 <fstream>
15 using std::filebuf;
16
17 #include "support/LIstream.h"
18 #include "support/LOstream.h"
19
20 #include "LString.h"
21
22 ///
23 struct keyword_item {
24         ///
25         char const * tag;
26         ///
27         short code;
28 };
29
30 /*@Doc:
31   Generalized simple lexical analizer.
32   It can be used for simple syntax parsers, like lyxrc,
33   texclass and others to come.
34   See lyxrc.C for an example of usage.
35   */
36 class LyXLex { 
37 public:
38         ///
39         LyXLex (keyword_item *, int);
40
41         /// Lex basic codes
42         enum {
43                 ///
44                 LEX_UNDEF = -1,
45                 ///
46                 LEX_FEOF  = -2,
47                 ///
48                 LEX_DATA  = -3,
49                 ///
50                 LEX_TOKEN = -4
51         };
52
53         /// file is open and end of file is not reached
54         bool IsOK() const;
55         /// return true if able to open file, else false
56         bool setFile(string const & filename);
57         ///
58         void setStream(istream & i);
59         ///
60         istream & getStream() { return is; }
61         /// Danger! Don't use it unless you know what you are doing.
62         void setLineNo(int l) { lineno = l; }
63         /// returns a lex code
64         int lex();
65
66         /** Just read athe next word. If esc is true remember that
67           some chars might be escaped: "\ atleast */
68         bool next(bool esc = false);
69
70         /** Read next token. This one is almost the same as next,
71           but it will consider " as a regular character and always
72           split a word if it contains a backslash.
73           */
74         bool nextToken();
75         
76         /// 
77         int GetLineNo() const { return lineno; }
78         ///
79         int GetInteger() const;
80         ///
81         bool GetBool() const;
82         ///
83         float GetFloat() const;
84         ///
85         string GetString() const;
86         
87         /// get a long string, ended by the tag `endtag'
88         string getLongString(string const & endtag);
89         
90         ///
91         bool EatLine();
92         ///
93         int FindToken(char const * str[]);
94         ///
95         int CheckToken(char const * str[], int print_error);
96
97         ///
98         char const * text() const { return &buff[0]; }
99
100         /** Pushes a token list on a stack and replaces it with a new one.
101          */
102         void pushTable(keyword_item *, int);
103
104         /** Pops a token list into void and replaces it with the one now
105           on top of the stack.
106           */
107         void popTable();
108
109         /** Prints an error message with the corresponding line number
110           and file name. If message contains the substring `$$Token',
111           it is replaced with the value of GetString()
112           */
113         void printError(string const & message) const;
114
115         /**
116           Prints the current token table on the supplied ostream.
117           */
118         void printTable(ostream &);
119 protected:
120         ///
121         enum {
122                 ///
123                 LEX_MAX_BUFF = 2048
124         };
125
126         ///
127         struct pushed_table {
128                 ///
129                 pushed_table(){
130                         next= 0;
131                         table_elem= 0;
132                 }
133                 ///
134                 pushed_table * next;
135                 ///
136                 keyword_item * table_elem;
137                 ///
138                 int table_siz;
139         };
140
141         /// fb__ is only used to open files, the stream is accessed through is
142         filebuf fb__;
143         /// the stream that we use.
144         istream is;
145         /// 
146         string name;
147         ///
148         int lineno;
149         ///
150         keyword_item * table;
151         ///
152         int no_items;
153         ///
154         char buff[LEX_MAX_BUFF];
155         ///
156         pushed_table * pushed;
157         ///
158         int search_kw(char const * const) const;
159         ///
160         short status;
161 };
162
163
164 inline
165 bool LyXLex::IsOK() const
166 {
167         return is.good();
168 }
169
170
171 // This is needed to ensure that the pop is done upon exit from methods
172 // with more than one exit point or that can return as a response to
173 // exceptions. (Lgb)
174 struct pushpophelper {
175         pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
176                 lex.pushTable(i, s);
177         }
178         ~pushpophelper() {
179                 lex.popTable();
180         }
181         LyXLex & lex;
182 };
183 // To avoid wrong usage:
184 // pushpophelper(...); // wrong
185 // pushpophelper pph(...); // right
186 // we add this macro:
187 #define pushpophelper(x, y, z) unnamed_pushpophelper;
188 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
189
190 #endif