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