4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
23 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE, MATHTEXT_MODE, TABLE_MODE};
25 mode_type asMode(mode_type oldmode, std::string const & str);
28 // These are TeX's catcodes
30 catEscape, // 0 backslash
41 catLetter, // 11 a-zA-Z
42 catOther, // 12 none of the above
45 catInvalid // 15 <delete>
49 CatCode catcode(unsigned char c);
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
72 // Helper class for parsing
78 Token() : cs_(), char_(0), cat_(catIgnore) {}
80 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
82 Token(std::string const & cs, CatCode cat) : cs_(cs), char_(0), cat_(cat) {}
85 std::string const & cs() const { return cs_; }
86 /// Returns the catcode of the token
87 CatCode cat() const { return cat_; }
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;
104 std::ostream & operator<<(std::ostream & os, Token const & t);
108 * Actual parser class
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
122 Parser(std::istream & is);
124 Parser(std::string const & s);
127 int lineno() const { return lineno_; }
130 /// dump contents to screen
134 typedef std::pair<bool, std::string> Arg;
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().
140 Arg getFullArg(char left, char right);
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.
148 std::string getArg(char left, char right);
150 * \returns getFullArg('[', ']') including the brackets or the
151 * empty string if no argument was found.
153 std::string getFullOpt();
154 /// \returns getArg('[', ']') including the brackets
155 std::string getOpt();
157 * \returns the contents of the environment \p name.
158 * <tt>\begin{name}</tt> must be parsed already, <tt>\end{name}</tt>
159 * is parsed but not returned.
161 std::string const verbatimEnvironment(std::string const & name);
162 /// Returns the character of the current token and increments the token position.
165 void error(std::string const & msg);
166 /// Parses \p is into tokens
167 void tokenize(std::istream & is);
169 void push_back(Token const & t);
172 /// The previous token.
173 Token const & prev_token() const;
174 /// The current token.
175 Token const & curr_token() const;
177 Token const & next_token() const;
178 /// Make the next token current and return that.
179 Token const & get_token();
180 /// \return whether the current token starts a new paragraph
181 bool isParagraph() const;
182 /// skips spaces (and comments if \p skip_comments is true)
183 void skip_spaces(bool skip_comments = false);
184 /// puts back spaces (and comments if \p skip_comments is true)
185 void unskip_spaces(bool skip_comments = false);
187 void lex(std::string const & s);
191 std::string verbatim_item();
193 std::string verbatimOption();
194 /// resets the parser to initial state
197 void setCatCode(char c, CatCode cat);
199 CatCode getCatCode(char c) const;
205 std::vector<Token> tokens_;