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