]> git.lyx.org Git - lyx.git/blob - src/lyxlex.h
Move quoteArg to LyXLex so that it can be used e.g. for writing viewers to
[lyx.git] / src / lyxlex.h
1 // -*- C++ -*-
2 /**
3  * \file lyxlex.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 //  Generalized simple lexical analizer.
14 //  It can be used for simple syntax parsers, like lyxrc,
15 //  texclass and others to come.
16
17 #ifndef LYXLEX_H
18 #define LYXLEX_H
19
20 #include <boost/utility.hpp>
21
22 #include <iosfwd>
23 #include <string>
24
25
26 ///
27 struct keyword_item {
28         ///
29         char const * tag;
30         ///
31         int code;
32 };
33
34 /** Generalized simple lexical analizer.
35     It can be used for simple syntax parsers, like lyxrc,
36     texclass and others to come.
37     @see lyxrc.C for an example of usage.
38   */
39 class LyXLex : boost::noncopyable {
40 public:
41         ///
42         LyXLex(keyword_item *, int);
43         ///
44         ~LyXLex();
45
46         /// Lex basic codes
47         enum {
48                 ///
49                 LEX_UNDEF = -1,
50                 ///
51                 LEX_FEOF  = -2,
52                 ///
53                 LEX_DATA  = -3,
54                 ///
55                 LEX_TOKEN = -4
56         };
57
58         /// stream is open and end of stream is not reached
59         bool isOK() const;
60         /// stream is ok
61         operator void const *() const;
62         /// stream is not ok
63         bool operator!() const;
64         /// return true if able to open file, else false
65         bool setFile(std::string const & filename);
66         ///
67         void setStream(std::istream & is);
68         ///
69         std::istream & getStream();
70         /// Danger! Don't use it unless you know what you are doing.
71         void setLineNo(int l);
72         /// Change the character that begins a comment. Default is '#'
73         void setCommentChar(char c);
74
75         /// returns a lex code
76         int lex();
77
78         /** Just read athe next word. If esc is true remember that
79             some chars might be escaped: "\ atleast
80         */
81         bool next(bool esc = false);
82
83         /** Read next token. This one is almost the same as next,
84             but it will consider " as a regular character and always
85             split a word if it contains a backslash.
86         */
87         bool nextToken();
88         /// Push a token, that next token got from lyxlex.
89         void pushToken(std::string const &);
90
91         ///
92         int getLineNo() const;
93
94         ///
95         int getInteger() const;
96         ///
97         bool getBool() const;
98         ///
99         double getFloat() const;
100         ///
101         std::string const getString() const;
102
103         /** Get a long string, ended by the tag `endtag'.
104             This string can span several lines. The first line
105             serves as a template for how many spaces the lines
106             are indented. This much white space is skipped from
107             each following line. This mechanism does not work
108             perfectly if you use tabs.
109         */
110         std::string const getLongString(std::string const & endtag);
111
112         ///
113         bool eatLine();
114
115         /// Pushes a token list on a stack and replaces it with a new one.
116         void pushTable(keyword_item *, int);
117
118         /** Pops a token list into void and replaces it with the one now
119             on top of the stack.
120         */
121         void popTable();
122
123         /** Prints an error message with the corresponding line number
124             and file name. If message contains the substring `$$Token',
125             it is replaced with the value of GetString()
126         */
127         void printError(std::string const & message) const;
128
129         /// Prints the current token table on the supplied ostream.
130         void printTable(std::ostream &);
131
132         /// extract string
133         LyXLex & operator>>(std::string &);
134         /// extract double
135         LyXLex & operator>>(double &);
136         /// extract integer
137         LyXLex & operator>>(int &);
138         /// extract unsigned integer
139         LyXLex & operator>>(unsigned int &);
140         /// extract bool
141         LyXLex & operator>>(bool &);
142
143         /// Quotes a string so that reading it again with LyXLex::next(true)
144         /// gets the original string
145         static std::string const quoteString(std::string const &);
146
147 private:
148         class Pimpl;
149         ///
150         Pimpl * pimpl_;
151 };
152
153
154 /** Use to enable multiple exit points.
155     This is needed to ensure that the pop is done upon exit from methods
156     with more than one exit point or that can return as a response to
157     exceptions.
158     @author Lgb
159 */
160 class pushpophelper {
161 public:
162         ///
163         pushpophelper(LyXLex & lexrc, keyword_item * i, int s) : lex(lexrc) {
164                 lex.pushTable(i, s);
165         }
166         ///
167         ~pushpophelper() {
168                 lex.popTable();
169         }
170         ///
171         LyXLex & lex;
172 };
173 /** Avoid wrong usage of pushpophelper.
174     To avoid wrong usage:
175     pushpophelper(...); // wrong
176     pushpophelper pph(...); // right
177 */
178 #define pushpophelper(x, y, z) unnamed_pushpophelper;
179 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
180
181 #endif