]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/texparser.h
Removed all redundant using directives from the source.
[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 };
63
64
65
66 //
67 // Helper class for parsing
68 //
69
70 class Token {
71 public:
72         ///
73         Token() : cs_(), char_(0), cat_(catIgnore) {}
74         ///
75         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
76         ///
77         Token(std::string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
78
79         ///
80         std::string const & cs() const { return cs_; }
81         ///
82         CatCode cat() const { return cat_; }
83         ///
84         char character() const { return char_; }
85         ///
86         std::string asString() const;
87         ///
88         std::string asInput() const;
89
90 private:
91         ///
92         std::string cs_;
93         ///
94         char char_;
95         ///
96         CatCode cat_;
97 };
98
99 std::ostream & operator<<(std::ostream & os, Token const & t);
100
101
102 //
103 // Actual parser class
104 //
105
106 class Parser {
107
108 public:
109         ///
110         Parser(std::istream & is);
111         ///
112         Parser(std::string const & s);
113
114         ///
115         int lineno() const { return lineno_; }
116         ///
117         void putback();
118         /// dump contents to screen
119         void dump() const;
120
121         ///
122         std::string getArg(char left, char right);
123         /// getArg('[', ']') including the brackets
124         std::string getOpt();
125         ///
126         char getChar();
127         ///
128         void error(std::string const & msg);
129         ///
130         void tokenize(std::istream & is);
131         ///
132         void push_back(Token const & t);
133         ///
134         void pop_back();
135         ///
136         Token const & prev_token() const;
137         ///
138         Token const & next_token() const;
139         ///
140         Token const & get_token();
141         /// skips spaces if any
142         void skip_spaces();
143         ///
144         void lex(std::string const & s);
145         ///
146         bool good() const;
147         ///
148         std::string verbatim_item();
149         ///
150         std::string verbatimOption();
151
152         ///
153         void setCatCode(char c, CatCode cat);
154         ///
155         CatCode getCatCode(char c) const;
156
157 //private:
158         ///
159         int lineno_;
160         ///
161         std::vector<Token> tokens_;
162         ///
163         unsigned pos_;
164 };
165
166
167 #endif