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.
19 #include "support/docstream.h"
24 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE, MATHTEXT_MODE, TABLE_MODE};
26 mode_type asMode(mode_type oldmode, std::string const & str);
29 // These are TeX's catcodes
31 catEscape, // 0 backslash
42 catLetter, // 11 a-zA-Z
43 catOther, // 12 none of the above
46 catInvalid // 15 <delete>
57 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
58 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
59 FLAG_END = 1 << 3, // next \\end ends the parsing process
60 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
61 FLAG_TEXTMODE = 1 << 5, // we are in a box
62 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
63 FLAG_LEAVE = 1 << 7, // leave the loop at the end
64 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
65 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
66 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
67 FLAG_OPTION = 1 << 11, // read [...] style option
68 FLAG_BRACED = 1 << 12, // read {...} style argument
69 FLAG_CELL = 1 << 13, // read table cell
70 FLAG_TABBING = 1 << 14 // We are inside a tabbing environment
76 // Helper class for parsing
82 Token() : cs_(), cat_(catIgnore) {}
84 Token(docstring const & cs, CatCode cat) : cs_(to_utf8(cs)), cat_(cat) {}
86 /// Returns the token as string
87 std::string const & cs() const { return cs_; }
88 /// Returns the catcode of the token
89 CatCode cat() const { return cat_; }
90 /** Get the character of tokens that were constructed from a single
91 * character input or a two character input and cat_ == catEscape.
92 * FIXME: The intended usage is not clear. The Token class in
93 * ../mathed/MathParser.cpp (which is the anchestor of this
94 * class) uses a separate char member for this method. I
95 * believe that the intended usage is to not cover tokens with
96 * catEscape or catComment, e.g. \code
97 * return (cs_.empty() || cat_ == catEscape || cat_ == catComment) ? 0 : cs_[0];
99 * All usages of this method should be checked. gb 2011-01-05
101 char character() const { return cs_.empty() ? 0 : cs_[0]; }
102 /// Returns the token verbatim
103 std::string asInput() const;
104 /// Is the token an alphanumerical character?
105 bool isAlnumASCII() const;
114 std::ostream & operator<<(std::ostream & os, Token const & t);
117 extern void debugToken(std::ostream & os, Token const & t, unsigned int flags);
120 /// A docstream version that supports putback even when not buffered
121 class iparserdocstream
124 typedef idocstream::int_type int_type;
126 iparserdocstream(idocstream & is) : is_(is) {}
128 /// Like std::istream::operator bool()
129 /// Do not convert is_ implicitly to bool, since that is forbidden in C++11.
130 explicit operator bool() const { return s_.empty() ? !is_.fail() : true; }
132 /// change the encoding of the input stream to \p e (iconv name)
133 void setEncoding(std::string const & e);
135 // add to the list of characters to read before actually reading
137 void putback(char_type c);
139 // add to the list of characters to read before actually reading
141 void putback(docstring s);
143 /// Like std::istream::get()
144 iparserdocstream & get(char_type &c);
146 /// Like std::istream::good()
147 bool good() const { return s_.empty() ? is_.good() : true; }
149 /// Like std::istream::peek()
150 int_type peek() const { return s_.empty() ? is_.peek() : s_[0]; }
154 /// characters to read before actually reading the stream
160 * Actual parser class
162 * The parser parses every character of the inputstream into a token
163 * and classifies the token.
164 * The following transformations are done:
165 * - Consecutive spaces are combined into one single token with CatCode catSpace
166 * - Consecutive newlines are combined into one single token with CatCode catNewline
167 * - Comments and %\n combinations are parsed into one token with CatCode catComment
172 Parser(Parser const & p);
173 Parser & operator=(Parser const & p);
176 Parser(idocstream & is, std::string const & fixedenc);
178 Parser(std::string const & s);
182 /** forget already parsed next tokens and put the
183 * corresponding characters into the input stream for
184 * re-reading. Useful when changing catcodes. */
187 /// change the encoding of the input stream according to \p encoding
188 /// (latex name) and package \p package
189 bool setEncoding(std::string const & encoding, int const & package);
190 /// change the encoding of the input stream to \p encoding (iconv name)
191 bool setEncoding(std::string const & encoding);
192 /// get the current iconv encoding of the input stream
193 std::string getEncoding() const { return encoding_iconv_; }
196 CatCode catcode(char_type c) const;
198 void setCatcode(char c, CatCode cat);
199 /// set parser to normal or verbatim mode
200 void setCatcodes(cat_type t);
203 int lineno() const { return lineno_; }
206 /// store current position
208 /// restore previous position
210 /// forget last saved position
212 /// dump contents to screen
215 /// Does an optional argument follow after the current token?
218 typedef std::pair<bool, std::string> Arg;
220 * Get an argument enclosed by \p left and \p right.
221 * If \p allow_escaping is true, a right delimiter escaped by a
222 * backslash does not count as delimiter, but is included in the
224 * \returns wether an argument was found in \p Arg.first and the
225 * argument in \p Arg.second. \see getArg().
227 Arg getFullArg(char left, char right, bool allow_escaping = true);
229 * Get an argument enclosed by \p left and \p right.
230 * If \p allow_escaping is true, a right delimiter escaped by a
231 * backslash does not count as delimiter, but is included in the
233 * \returns the argument (without \p left and \p right) or the empty
234 * string if the next non-space token is not \p left. Use
235 * getFullArg() if you need to know wether there was an empty
236 * argument or no argument at all.
238 std::string getArg(char left, char right, bool allow_escaping = true);
240 * Like getOpt(), but distinguishes between a missing argument ""
241 * and an empty argument "[]".
243 std::string getFullOpt(bool keepws = false,
244 char left = '[', char right = ']');
246 * \returns getArg('[', ']') including the brackets or the
247 * empty string if there is no such argument.
248 * No whitespace is eaten if \p keepws is true and no optional
249 * argument exists. This is important if an optional argument is
250 * parsed that would go after a command in ERT: In this case the
251 * whitespace is needed to separate the ERT from the subsequent
252 * word. Without it, the ERT and the next word would be concatenated
253 * during .tex export, thus creating an invalid command.
255 std::string getOpt(bool keepws = false);
257 * \returns getFullArg('(', ')') including the parentheses or the
258 * empty string if there is no such argument.
260 std::string getFullParentheseArg();
262 * \returns the contents of the environment \p name.
263 * <tt>\begin{name}</tt> must be parsed already, <tt>\end{name}</tt>
264 * is parsed but not returned. This parses nested environments properly.
266 std::string const ertEnvironment(std::string const & name);
268 * The same as ertEnvironment(std::string const & name) but
269 * \begin and \end commands inside the name environment are not parsed.
270 * This function is designed to parse verbatim environments.
272 std::string const plainEnvironment(std::string const & name);
274 * Basically the same as plainEnvironment(std::string const & name) but
275 * instead of \begin and \end commands the parsing is started/stopped
276 * at given characters.
277 * This function is designed to parse verbatim commands.
279 std::string const plainCommand(char left, char right, std::string const & name);
281 * Basically the same as plainEnvironment() but the parsing is
282 * stopped at string \p end_string. Contrary to the other
283 * methods, this uses proper catcode setting. This function is
284 * designed to parse verbatim environments and command. The
285 * intention is to eventually replace all of its siblings. the
286 * member \p first of the result tells whether the arg was
287 * found and the member \p second is the value. If \p
288 * allow_linebreak is false, then the parsing is limited to one line
290 Arg verbatimStuff(std::string const & end_string,
291 bool allow_linebreak = true);
293 * \returns the contents of the environment \p name.
294 * <tt>\begin{name}</tt> must be parsed already,
295 * <tt>\end{name}</tt> is parsed but not returned. The string
296 * is parsed with proper verbatim catcodes and one newline is
297 * removed from head and tail of the string if applicable.
299 std::string const verbatimEnvironment(std::string const & end_string);
301 std::string verbatim_item();
303 std::string verbatimOption();
305 void error(std::string const & msg);
306 /// The previous token.
307 Token const prev_token() const;
308 /// The current token.
309 Token const curr_token() const;
310 /// The next token. Caution: If this is called, an encoding change is
311 /// only possible again after get_token() has been called.
312 Token const next_token();
313 /// The next but one token. Caution: If this is called, an encoding
314 /// change is only possible again after get_token() has been called
316 Token const next_next_token();
317 /// Make the next token current and return that.
318 Token const get_token();
319 /// \return whether the current token starts a new paragraph
321 /// skips spaces (and comments if \p skip_comments is true)
322 /// \return whether whitespace was skipped (not comments)
323 bool skip_spaces(bool skip_comments = false);
324 /// puts back spaces (and comments if \p skip_comments is true)
325 void unskip_spaces(bool skip_comments = false);
326 /// Is any further input pending()? This is not like
327 /// std::istream::good(), which returns true if all available input
328 /// was read, and the next attempt to read would return EOF.
330 /// resets the parser to initial state
334 /// Setup catcode table
336 /// Parses one token from \p is
339 void push_back(Token const & t);
343 std::vector<Token> tokens_;
347 std::vector<unsigned> positions_;
349 idocstringstream * iss_;
351 iparserdocstream is_;
352 /// iconv name of the current encoding
353 std::string encoding_iconv_;
355 CatCode theCatcode_[256];
357 cat_type theCatcodesType_;