]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/Parser.h
tex2lyx/preamble.cpp: support Vietnamese (since format 291)
[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         /// Returns the token as string
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 verbatim
87         std::string asInput() const;
88
89 private:
90         ///
91         std::string cs_;
92         ///
93         CatCode cat_;
94 };
95
96 std::ostream & operator<<(std::ostream & os, Token const & t);
97
98
99 /*!
100  * Actual parser class
101  *
102  * The parser parses every character of the inputstream into a token
103  * and classifies the token.
104  * The following transformations are done:
105  * - Consecutive spaces are combined into one single token with CatCode catSpace
106  * - Consecutive newlines are combined into one single token with CatCode catNewline
107  * - Comments and %\n combinations are parsed into one token with CatCode catComment
108  */
109
110 class Parser {
111
112 public:
113         ///
114         Parser(idocstream & is);
115         ///
116         Parser(std::string const & s);
117         ///
118         ~Parser();
119
120         /// change the latex encoding of the input stream
121         void setEncoding(std::string const & encoding);
122         /// get the current latex encoding of the input stream
123         std::string getEncoding() const { return encoding_latex_; }
124
125         ///
126         int lineno() const { return lineno_; }
127         ///
128         void putback();
129         /// dump contents to screen
130         void dump() const;
131
132         ///
133         typedef std::pair<bool, std::string> Arg;
134         /*!
135          * Get an argument enclosed by \p left and \p right.
136          * \returns wether an argument was found in \p Arg.first and the
137          * argument in \p Arg.second. \see getArg().
138          */
139         Arg getFullArg(char left, char right);
140         /*!
141          * Get an argument enclosed by \p left and \p right.
142          * \returns the argument (without \p left and \p right) or the empty
143          * string if the next non-space token is not \p left. Use
144          * getFullArg() if you need to know wether there was an empty
145          * argument or no argument at all.
146          */
147         std::string getArg(char left, char right);
148         /*!
149          * \returns getFullArg('[', ']') including the brackets or the
150          * empty string if there is no such argument.
151          */
152         std::string getFullOpt();
153         /*!
154          * \returns getArg('[', ']') including the brackets or the
155          * empty string if there is no such argument.
156          * No whitespace is eaten if \p keepws is true and no optional
157          * argument exists. This is important if an optional argument is
158          * parsed that would go after a command in ERT: In this case the
159          * whitespace is needed to separate the ERT from the subsequent
160          * word. Without it, the ERT and the next word would be concatenated
161          * during .tex export, thus creating an invalid command.
162          */
163         std::string getOpt(bool keepws = false);
164         /*!
165          * the same as getOpt but without the brackets
166          */
167         std::string getOptContent();
168         /*!
169          * \returns getFullArg('(', ')') including the parentheses or the
170          * empty string if there is no such argument.
171          */
172         std::string getFullParentheseArg();
173         /*!
174          * \returns the contents of the environment \p name.
175          * <tt>\begin{name}</tt> must be parsed already, <tt>\end{name}</tt>
176          * is parsed but not returned.
177          */
178         std::string const verbatimEnvironment(std::string const & name);
179         /*!
180          * Returns the character of the current token and increments
181          * the token position.
182          */
183         char getChar();
184         ///
185         void error(std::string const & msg);
186         /// Parses one token from \p is 
187         void tokenize_one();
188         ///
189         void push_back(Token const & t);
190         /// The previous token.
191         Token const prev_token() const;
192         /// The current token.
193         Token const curr_token() const;
194         /// The next token.
195         Token const next_token();
196         /// Make the next token current and return that.
197         Token const get_token();
198         /// \return whether the current token starts a new paragraph
199         bool isParagraph();
200         /// skips spaces (and comments if \p skip_comments is true)
201         /// \return whether whitespace was skipped (not comments)
202         bool skip_spaces(bool skip_comments = false);
203         /// puts back spaces (and comments if \p skip_comments is true)
204         void unskip_spaces(bool skip_comments = false);
205         ///
206         void lex(std::string const & s);
207         ///
208         bool good();
209         ///
210         std::string verbatim_item();
211         ///
212         std::string verbatimOption();
213         /// resets the parser to initial state
214         void reset();
215         ///
216         void setCatCode(char c, CatCode cat);
217         ///
218         CatCode getCatCode(char c) const;
219
220 private:
221         ///
222         int lineno_;
223         ///
224         std::vector<Token> tokens_;
225         ///
226         unsigned pos_;
227         ///
228         idocstringstream * iss_;
229         ///
230         idocstream & is_;
231         /// latex name of the current encoding
232         std::string encoding_latex_;
233 };
234
235
236
237 } // namespace lyx
238
239 #endif