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 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE, MATHTEXT_MODE, TABLE_MODE};
21 mode_type asMode(mode_type oldmode, std::string const & str);
24 // These are TeX's catcodes
26 catEscape, // 0 backslash
37 catLetter, // 11 a-zA-Z
38 catOther, // 12 none of the above
41 catInvalid // 15 <delete>
45 CatCode catcode(unsigned char c);
49 FLAG_BRACE_LAST = 1 << 1, // last closing brace ends the parsing
50 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
51 FLAG_END = 1 << 3, // next \\end ends the parsing process
52 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
53 FLAG_TEXTMODE = 1 << 5, // we are in a box
54 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
55 FLAG_LEAVE = 1 << 7, // leave the loop at the end
56 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
57 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
58 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
59 FLAG_OPTION = 1 << 11, // read [...] style option
60 FLAG_BRACED = 1 << 12, // read {...} style argument
61 FLAG_CELL = 1 << 13, // read table cell
62 FLAG_TABBING = 1 << 14 // We are inside a tabbing environment
68 // Helper class for parsing
74 Token() : cs_(), char_(0), cat_(catIgnore) {}
76 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
78 Token(std::string const & cs, CatCode cat) : cs_(cs), char_(0), cat_(cat) {}
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 char_; }
86 /// Returns the token as string
87 std::string asString() const;
88 /// Returns the token verbatim
89 std::string asInput() const;
100 std::ostream & operator<<(std::ostream & os, Token const & t);
104 * Actual parser class
106 * The parser parses every character of the inputstream into a token
107 * and classifies the token.
108 * The following transformations are done:
109 * - Consecutive spaces are combined into one single token with CatCode catSpace
110 * - Consecutive newlines are combined into one single token with CatCode catNewline
111 * - Comments and %\n combinations are parsed into one token with CatCode catComment
118 Parser(std::istream & is);
120 Parser(std::string const & s);
123 int lineno() const { return lineno_; }
126 /// dump contents to screen
130 std::string getArg(char left, char right);
131 /// getArg('[', ']') including the brackets
132 std::string getOpt();
133 /// Returns the character of the current token and increments the token position.
136 void error(std::string const & msg);
137 /// Parses \p is into tokens
138 void tokenize(std::istream & is);
140 void push_back(Token const & t);
143 /// The previous token.
144 Token const & prev_token() const;
145 /// The current token.
146 Token const & curr_token() const;
148 Token const & next_token() const;
149 /// Make the next token current and return that.
150 Token const & get_token();
151 /// \return whether the current token starts a new paragraph
152 bool isParagraph() const;
153 /// skips spaces (and comments if \p skip_comments is true)
154 void skip_spaces(bool skip_comments = false);
155 /// puts back spaces (and comments if \p skip_comments is true)
156 void unskip_spaces(bool skip_comments = false);
158 void lex(std::string const & s);
162 std::string verbatim_item();
164 std::string verbatimOption();
165 /// resets the parser to initial state
168 void setCatCode(char c, CatCode cat);
170 CatCode getCatCode(char c) const;
176 std::vector<Token> tokens_;