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.
20 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE, MATHTEXT_MODE, TABLE_MODE};
22 mode_type asMode(mode_type oldmode, std::string const & str);
25 // These are TeX's catcodes
27 catEscape, // 0 backslash
38 catLetter, // 11 a-zA-Z
39 catOther, // 12 none of the above
42 catInvalid // 15 <delete>
46 CatCode catcode(unsigned char c);
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
69 // Helper class for parsing
75 Token() : cs_(), char_(0), cat_(catIgnore) {}
77 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
79 Token(std::string const & cs, CatCode cat) : cs_(cs), char_(0), cat_(cat) {}
82 std::string const & cs() const { return cs_; }
83 /// Returns the catcode of the token
84 CatCode cat() const { return cat_; }
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;
101 std::ostream & operator<<(std::ostream & os, Token const & t);
105 * Actual parser class
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
119 Parser(std::istream & is);
121 Parser(std::string const & s);
124 int lineno() const { return lineno_; }
127 /// dump contents to screen
131 typedef std::pair<bool, std::string> Arg;
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().
137 Arg getFullArg(char left, char right);
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.
145 std::string getArg(char left, char right);
147 * \returns getFullArg('[', ']') including the brackets or the
148 * empty string if no argument was found.
150 std::string getFullOpt();
151 /// \returns getArg('[', ']') including the brackets
152 std::string getOpt();
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.
158 std::string const verbatimEnvironment(std::string const & name);
159 /// Returns the character of the current token and increments the token position.
162 void error(std::string const & msg);
163 /// Parses \p is into tokens
164 void tokenize(std::istream & is);
166 void push_back(Token const & t);
169 /// The previous token.
170 Token const & prev_token() const;
171 /// The current token.
172 Token const & curr_token() const;
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);
184 void lex(std::string const & s);
188 std::string verbatim_item();
190 std::string verbatimOption();
191 /// resets the parser to initial state
194 void setCatCode(char c, CatCode cat);
196 CatCode getCatCode(char c) const;
202 std::vector<Token> tokens_;