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