]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/texparser.h
the exception safety patch
[lyx.git] / src / tex2lyx / texparser.h
1 // -*- C++ -*-
2 /**
3  * \file texparser.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef PARSER_H
13 #define PARSER_H
14
15 #include <vector>
16 #include <string>
17
18
19 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE, MATHTEXT_MODE, TABLE_MODE};
20
21 mode_type asMode(mode_type oldmode, std::string const & str);
22
23
24 // These are TeX's catcodes
25 enum CatCode {
26         catEscape,     // 0    backslash
27         catBegin,      // 1    {
28         catEnd,        // 2    }
29         catMath,       // 3    $
30         catAlign,      // 4    &
31         catNewline,    // 5    ^^M
32         catParameter,  // 6    #
33         catSuper,      // 7    ^
34         catSub,        // 8    _
35         catIgnore,     // 9
36         catSpace,      // 10   space
37         catLetter,     // 11   a-zA-Z
38         catOther,      // 12   none of the above
39         catActive,     // 13   ~
40         catComment,    // 14   %
41         catInvalid     // 15   <delete>
42 };
43
44
45 CatCode catcode(unsigned char c);
46
47
48 enum {
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
63 };
64
65
66
67 //
68 // Helper class for parsing
69 //
70
71 class Token {
72 public:
73         ///
74         Token() : cs_(), char_(0), cat_(catIgnore) {}
75         ///
76         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
77         ///
78         Token(std::string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
79
80         ///
81         std::string const & cs() const { return cs_; }
82         ///
83         CatCode cat() const { return cat_; }
84         ///
85         char character() const { return char_; }
86         ///
87         std::string asString() const;
88         ///
89         std::string asInput() const;
90
91 private:
92         ///
93         std::string cs_;
94         ///
95         char char_;
96         ///
97         CatCode cat_;
98 };
99
100 std::ostream & operator<<(std::ostream & os, Token const & t);
101
102
103 //
104 // Actual parser class
105 //
106
107 class Parser {
108
109 public:
110         ///
111         Parser(std::istream & is);
112         ///
113         Parser(std::string const & s);
114
115         ///
116         int lineno() const { return lineno_; }
117         ///
118         void putback();
119         /// dump contents to screen
120         void dump() const;
121
122         ///
123         std::string getArg(char left, char right);
124         /// getArg('[', ']') including the brackets
125         std::string getOpt();
126         ///
127         char getChar();
128         ///
129         void error(std::string const & msg);
130         ///
131         void tokenize(std::istream & is);
132         ///
133         void push_back(Token const & t);
134         ///
135         void pop_back();
136         ///
137         Token const & prev_token() const;
138         ///
139         Token const & next_token() const;
140         ///
141         Token const & get_token();
142         /// skips spaces if any
143         void skip_spaces();
144         ///
145         void lex(std::string const & s);
146         ///
147         bool good() const;
148         ///
149         std::string verbatim_item();
150         ///
151         std::string verbatimOption();
152         /// resets the parser to initial state
153         void reset();
154         ///
155         void setCatCode(char c, CatCode cat);
156         ///
157         CatCode getCatCode(char c) const;
158
159 //private:
160         ///
161         int lineno_;
162         ///
163         std::vector<Token> tokens_;
164         ///
165         unsigned pos_;
166 };
167
168
169 #endif