]> git.lyx.org Git - lyx.git/blob - src/lyxlex.h
42714c3af0dd4d056e28bb8ce9eb05c293b6de6f
[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 "support/utility.hpp"
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 : public noncopyable { 
34 public:
35         ///
36         LyXLex (keyword_item *, int);
37         ///
38         ~LyXLex();
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(std::istream & i);
58         ///
59         std::istream & getStream();
60         /// Danger! Don't use it unless you know what you are doing.
61         void setLineNo(int l);
62         
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         /// Push a token, that next token got from lyxlex.
76         void pushToken(string const &);
77         
78         /// 
79         int GetLineNo() const;
80         
81         ///
82         int GetInteger() const;
83         ///
84         bool GetBool() const;
85         ///
86         float GetFloat() const;
87         ///
88         string const GetString() const;
89         
90         /**
91          * Get a long string, ended by the tag `endtag'
92          * This string can span several lines. The first line
93          * serves as a template for how many spaces the lines
94          * are indented. This much white space is skipped from
95          * each following line. This mechanism does not work
96          * perfectly if you use tabs.
97          */
98         string const getLongString(string const & endtag);
99         
100         ///
101         bool EatLine();
102         ///
103         int FindToken(char const * str[]);
104         ///
105         int CheckToken(char const * str[], int print_error);
106
107         ///
108         char const * const text() const;
109
110         /** Pushes a token list on a stack and replaces it with a new one.
111          */
112         void pushTable(keyword_item *, int);
113
114         /** Pops a token list into void and replaces it with the one now
115           on top of the stack.
116           */
117         void popTable();
118
119         /** Prints an error message with the corresponding line number
120           and file name. If message contains the substring `$$Token',
121           it is replaced with the value of GetString()
122           */
123         void printError(string const & message) const;
124
125         /**
126           Prints the current token table on the supplied ostream.
127           */
128         void printTable(std::ostream &);
129 private:
130         struct Pimpl;
131         Pimpl * pimpl_;
132 };
133
134
135 // This is needed to ensure that the pop is done upon exit from methods
136 // with more than one exit point or that can return as a response to
137 // exceptions. (Lgb)
138 struct pushpophelper {
139         pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
140                 lex.pushTable(i, s);
141         }
142         ~pushpophelper() {
143                 lex.popTable();
144         }
145         LyXLex & lex;
146 };
147 // To avoid wrong usage:
148 // pushpophelper(...); // wrong
149 // pushpophelper pph(...); // right
150 // we add this macro:
151 #define pushpophelper(x, y, z) unnamed_pushpophelper;
152 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
153
154 #endif