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>
51 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
52 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
53 FLAG_END = 1 << 3, // next \\end ends the parsing process
54 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
55 FLAG_TEXTMODE = 1 << 5, // we are in a box
56 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
57 FLAG_LEAVE = 1 << 7, // leave the loop at the end
58 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
59 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
60 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
61 FLAG_OPTION = 1 << 11, // read [...] style option
62 FLAG_BRACED = 1 << 12, // read {...} style argument
63 FLAG_CELL = 1 << 13, // read table cell
64 FLAG_TABBING = 1 << 14 // We are inside a tabbing environment
70 // Helper class for parsing
76 Token() : cs_(), cat_(catIgnore) {}
78 Token(docstring const & cs, CatCode cat) : cs_(to_utf8(cs)), cat_(cat) {}
80 /// Returns the token as string
81 std::string const & cs() const { return cs_; }
82 /// Returns the catcode of the token
83 CatCode cat() const { return cat_; }
85 char character() const { return cs_.empty() ? 0 : cs_[0]; }
86 /// Returns the token verbatim
87 std::string asInput() const;
88 /// Is the token an alphanumerical character?
89 bool isAlnumASCII() const;
98 std::ostream & operator<<(std::ostream & os, Token const & t);
102 * Actual parser class
104 * The parser parses every character of the inputstream into a token
105 * and classifies the token.
106 * The following transformations are done:
107 * - Consecutive spaces are combined into one single token with CatCode catSpace
108 * - Consecutive newlines are combined into one single token with CatCode catNewline
109 * - Comments and %\n combinations are parsed into one token with CatCode catComment
114 Parser(Parser const & p);
115 Parser & operator=(Parser const & p);
118 Parser(idocstream & is);
120 Parser(std::string const & s);
124 /// change the latex encoding of the input stream
125 void setEncoding(std::string const & encoding);
126 /// get the current latex encoding of the input stream
127 std::string getEncoding() const { return encoding_latex_; }
130 int lineno() const { return lineno_; }
133 /// store current position
135 /// restore previous position
137 /// dump contents to screen
140 /// Does an optional argument follow after the current token?
143 typedef std::pair<bool, std::string> Arg;
145 * Get an argument enclosed by \p left and \p right.
146 * \returns wether an argument was found in \p Arg.first and the
147 * argument in \p Arg.second. \see getArg().
149 Arg getFullArg(char left, char right);
151 * Get an argument enclosed by \p left and \p right.
152 * \returns the argument (without \p left and \p right) or the empty
153 * string if the next non-space token is not \p left. Use
154 * getFullArg() if you need to know wether there was an empty
155 * argument or no argument at all.
157 std::string getArg(char left, char right);
159 * \returns getFullArg('[', ']') including the brackets or the
160 * empty string if there is no such argument.
162 std::string getFullOpt();
164 * \returns getArg('[', ']') including the brackets or the
165 * empty string if there is no such argument.
166 * No whitespace is eaten if \p keepws is true and no optional
167 * argument exists. This is important if an optional argument is
168 * parsed that would go after a command in ERT: In this case the
169 * whitespace is needed to separate the ERT from the subsequent
170 * word. Without it, the ERT and the next word would be concatenated
171 * during .tex export, thus creating an invalid command.
173 std::string getOpt(bool keepws = false);
175 * the same as getOpt but without the brackets
177 std::string getOptContent();
179 * \returns getFullArg('(', ')') including the parentheses or the
180 * empty string if there is no such argument.
182 std::string getFullParentheseArg();
184 * \returns the contents of the environment \p name.
185 * <tt>\begin{name}</tt> must be parsed already, <tt>\end{name}</tt>
186 * is parsed but not returned.
188 std::string const verbatimEnvironment(std::string const & name);
190 * Returns the character of the current token and increments
191 * the token position.
195 void error(std::string const & msg);
196 /// Parses one token from \p is
199 void push_back(Token const & t);
200 /// The previous token.
201 Token const prev_token() const;
202 /// The current token.
203 Token const curr_token() const;
205 Token const next_token();
206 /// Make the next token current and return that.
207 Token const get_token();
208 /// \return whether the current token starts a new paragraph
210 /// skips spaces (and comments if \p skip_comments is true)
211 /// \return whether whitespace was skipped (not comments)
212 bool skip_spaces(bool skip_comments = false);
213 /// puts back spaces (and comments if \p skip_comments is true)
214 void unskip_spaces(bool skip_comments = false);
216 void lex(std::string const & s);
220 std::string verbatim_item();
222 std::string verbatimOption();
223 /// resets the parser to initial state
226 void setCatCode(char c, CatCode cat);
228 CatCode getCatCode(char c) const;
234 std::vector<Token> tokens_;
238 std::vector<unsigned> positions_;
240 idocstringstream * iss_;
243 /// latex name of the current encoding
244 std::string encoding_latex_;