]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/texparser.h
Alfredo's second patch
[lyx.git] / src / tex2lyx / texparser.h
1 #ifndef PARSER_H
2 #define PARSER_H
3
4 #include "LString.h"
5 #include <vector>
6
7 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE, MATHTEXT_MODE, TABLE_MODE};
8
9 mode_type asMode(mode_type oldmode, string const & str);
10
11
12 // These are TeX's catcodes
13 enum CatCode {
14         catEscape,     // 0    backslash
15         catBegin,      // 1    {
16         catEnd,        // 2    }
17         catMath,       // 3    $
18         catAlign,      // 4    &
19         catNewline,    // 5    ^^M
20         catParameter,  // 6    #
21         catSuper,      // 7    ^
22         catSub,        // 8    _
23         catIgnore,     // 9
24         catSpace,      // 10   space
25         catLetter,     // 11   a-zA-Z
26         catOther,      // 12   none of the above
27         catActive,     // 13   ~
28         catComment,    // 14   %
29         catInvalid     // 15   <delete>
30 };
31
32
33 CatCode catcode(unsigned char c);
34
35
36 enum {
37         FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing
38         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
39         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
40         FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
41         FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
42         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
43         FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
44         FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
45         FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
46         FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
47         FLAG_OPTION     = 1 << 11, //  read [...] style option
48         FLAG_BRACED     = 1 << 12, //  read {...} style argument
49         FLAG_CELL       = 1 << 13  //  read table cell
50 };
51
52
53
54 //
55 // Helper class for parsing
56 //
57
58 class Token {
59 public:
60         ///
61         Token() : cs_(), char_(0), cat_(catIgnore) {}
62         ///
63         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
64         ///
65         Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
66
67         ///
68         string const & cs() const { return cs_; }
69         ///
70         CatCode cat() const { return cat_; }
71         ///
72         char character() const { return char_; }
73         ///
74         string asString() const;
75         ///
76         string asInput() const;
77
78 private:
79         ///
80         string cs_;
81         ///
82         char char_;
83         ///
84         CatCode cat_;
85 };
86
87 std::ostream & operator<<(std::ostream & os, Token const & t);
88
89
90 //
91 // Actual parser class
92 //
93
94 class Parser {
95
96 public:
97         ///
98         Parser(std::istream & is);
99         ///
100         Parser(string const & s);
101
102         ///
103         int lineno() const { return lineno_; }
104         ///
105         void putback();
106         /// dump contents to screen
107         void dump() const;
108
109         ///
110         string getArg(char left, char right);
111         /// getArg('[', ']') including the brackets
112         string getOpt();
113         ///
114         char getChar();
115         ///
116         void error(string const & msg);
117         ///
118         void tokenize(std::istream & is);
119         ///
120         void push_back(Token const & t);
121         ///
122         void pop_back();
123         ///
124         Token const & prev_token() const;
125         ///
126         Token const & next_token() const;
127         ///
128         Token const & get_token();
129         /// skips spaces if any
130         void skip_spaces();
131         ///
132         void lex(string const & s);
133         ///
134         bool good() const;
135         ///
136         string verbatim_item();
137         ///
138         string verbatimOption();
139
140         ///
141         void setCatCode(char c, CatCode cat);
142         ///
143         CatCode getCatCode(char c) const;
144
145 //private:
146         ///
147         int lineno_;
148         ///
149         std::vector<Token> tokens_;
150         ///
151         unsigned pos_;
152 };
153
154 #endif