]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/Parser.h
Fix typo.
[lyx.git] / src / tex2lyx / Parser.h
1 // -*- C++ -*-
2 /**
3  * \file Parser.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef PARSER_H
13 #define PARSER_H
14
15 #include <string>
16 #include <utility>
17 #include <vector>
18
19 #include "support/docstream.h"
20
21 namespace lyx {
22
23
24 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE, MATHTEXT_MODE, TABLE_MODE};
25
26 mode_type asMode(mode_type oldmode, std::string const & str);
27
28
29 // These are TeX's catcodes
30 enum CatCode {
31         catEscape,     // 0    backslash
32         catBegin,      // 1    {
33         catEnd,        // 2    }
34         catMath,       // 3    $
35         catAlign,      // 4    &
36         catNewline,    // 5    ^^M
37         catParameter,  // 6    #
38         catSuper,      // 7    ^
39         catSub,        // 8    _
40         catIgnore,     // 9
41         catSpace,      // 10   space
42         catLetter,     // 11   a-zA-Z
43         catOther,      // 12   none of the above
44         catActive,     // 13   ~
45         catComment,    // 14   %
46         catInvalid     // 15   <delete>
47 };
48
49
50 enum {
51         FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing
52         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
53         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
54         FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
55         FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
56         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
57         FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
58         FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
59         FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
60         FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
61         FLAG_OPTION     = 1 << 11, //  read [...] style option
62         FLAG_BRACED     = 1 << 12, //  read {...} style argument
63         FLAG_CELL       = 1 << 13, //  read table cell
64         FLAG_TABBING    = 1 << 14  //  We are inside a tabbing environment
65 };
66
67
68
69 //
70 // Helper class for parsing
71 //
72
73 class Token {
74 public:
75         ///
76         Token() : cs_(), cat_(catIgnore) {}
77         ///
78         Token(docstring const & cs, CatCode cat) : cs_(to_utf8(cs)), cat_(cat) {}
79
80         ///
81         std::string const & cs() const { return cs_; }
82         /// Returns the catcode of the token
83         CatCode cat() const { return cat_; }
84         ///
85         char character() const { return cs_.empty() ? 0 : cs_[0]; }
86         /// Returns the token as string
87         std::string asString() const;
88         /// Returns the token verbatim
89         std::string asInput() const;
90
91 private:
92         ///
93         std::string cs_;
94         ///
95         CatCode cat_;
96 };
97
98 std::ostream & operator<<(std::ostream & os, Token const & t);
99
100
101 /*!
102  * Actual parser class
103  *
104  * The parser parses every character of the inputstream into a token
105  * and classifies the token.
106  * The following transformations are done:
107  * - Consecutive spaces are combined into one single token with CatCode catSpace
108  * - Consecutive newlines are combined into one single token with CatCode catNewline
109  * - Comments and %\n combinations are parsed into one token with CatCode catComment
110  */
111
112 class Parser {
113
114 public:
115         ///
116         Parser(idocstream & is);
117         ///
118         Parser(std::string const & s);
119         ///
120         ~Parser();
121
122         /// change the latex encoding of the input stream
123         void setEncoding(std::string const & encoding);
124         /// get the current latex encoding of the input stream
125         std::string getEncoding() const { return encoding_latex_; }
126
127         ///
128         int lineno() const { return lineno_; }
129         ///
130         void putback();
131         /// dump contents to screen
132         void dump() const;
133
134         ///
135         typedef std::pair<bool, std::string> Arg;
136         /*!
137          * Get an argument enclosed by \p left and \p right.
138          * \returns wether an argument was found in \p Arg.first and the
139          * argument in \p Arg.second. \see getArg().
140          */
141         Arg getFullArg(char left, char right);
142         /*!
143          * Get an argument enclosed by \p left and \p right.
144          * \returns the argument (without \p left and \p right) or the empty
145          * string if the next non-space token is not \p left. Use
146          * getFullArg() if you need to know wether there was an empty
147          * argument or no argument at all.
148          */
149         std::string getArg(char left, char right);
150         /*!
151          * \returns getFullArg('[', ']') including the brackets or the
152          * empty string if there is no such argument.
153          */
154         std::string getFullOpt();
155         /*!
156          * \returns getArg('[', ']') including the brackets or the
157          * empty string if there is no such argument.
158          */
159         std::string getOpt();
160         /*!
161          * \returns getFullArg('[', ']') including the parentheses or the
162          * empty string if there is no such argument.
163          */
164         std::string getOptContent();
165         /*!
166          * the same as getOpt but without the brackets
167          */
168         std::string getFullParentheseArg();
169         /*!
170          * \returns the contents of the environment \p name.
171          * <tt>\begin{name}</tt> must be parsed already, <tt>\end{name}</tt>
172          * is parsed but not returned.
173          */
174         std::string const verbatimEnvironment(std::string const & name);
175         /*!
176          * Returns the character of the current token and increments
177          * the token position.
178          */
179         char getChar();
180         ///
181         void error(std::string const & msg);
182         /// Parses one token from \p is 
183         void tokenize_one();
184         ///
185         void push_back(Token const & t);
186         /// The previous token.
187         Token const prev_token() const;
188         /// The current token.
189         Token const curr_token() const;
190         /// The next token.
191         Token const next_token();
192         /// Make the next token current and return that.
193         Token const get_token();
194         /// \return whether the current token starts a new paragraph
195         bool isParagraph();
196         /// skips spaces (and comments if \p skip_comments is true)
197         void skip_spaces(bool skip_comments = false);
198         /// puts back spaces (and comments if \p skip_comments is true)
199         void unskip_spaces(bool skip_comments = false);
200         ///
201         void lex(std::string const & s);
202         ///
203         bool good();
204         ///
205         std::string verbatim_item();
206         ///
207         std::string verbatimOption();
208         /// resets the parser to initial state
209         void reset();
210         ///
211         void setCatCode(char c, CatCode cat);
212         ///
213         CatCode getCatCode(char c) const;
214
215 private:
216         ///
217         int lineno_;
218         ///
219         std::vector<Token> tokens_;
220         ///
221         unsigned pos_;
222         ///
223         idocstringstream * iss_;
224         ///
225         idocstream & is_;
226         /// latex name of the current encoding
227         std::string encoding_latex_;
228 };
229
230
231
232 } // namespace lyx
233
234 #endif