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