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