]> git.lyx.org Git - lyx.git/blob - src/lyxlex.h
f2cab3d0938010bf12ed046a783e13e5ff0a491c
[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
18 ///
19 struct keyword_item {
20         ///
21         char const * tag;
22         ///
23         short code;
24 };
25
26 /*@Doc:
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 { 
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         
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         /// Push a token, that next token got from lyxlex.
75         void pushToken(string const &);
76         
77         /// 
78         int GetLineNo() const;
79         
80         ///
81         int GetInteger() const;
82         ///
83         bool GetBool() const;
84         ///
85         float GetFloat() const;
86         ///
87         string const GetString() const;
88         
89         /// get a long string, ended by the tag `endtag'
90         string getLongString(string const & endtag);
91         
92         ///
93         bool EatLine();
94         ///
95         int FindToken(char const * str[]);
96         ///
97         int CheckToken(char const * str[], int print_error);
98
99         ///
100         char const * text() const;
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         Pimpl * pimpl_;
124 };
125
126
127 // This is needed to ensure that the pop is done upon exit from methods
128 // with more than one exit point or that can return as a response to
129 // exceptions. (Lgb)
130 struct pushpophelper {
131         pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
132                 lex.pushTable(i, s);
133         }
134         ~pushpophelper() {
135                 lex.popTable();
136         }
137         LyXLex & lex;
138 };
139 // To avoid wrong usage:
140 // pushpophelper(...); // wrong
141 // pushpophelper pph(...); // right
142 // we add this macro:
143 #define pushpophelper(x, y, z) unnamed_pushpophelper;
144 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
145
146 #endif