]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/texparser.h
Remove space at end of line, when superfluous.
[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, CatCode cat) : cs_(cs), char_(0), cat_(cat) {}
79
80         ///
81         std::string const & cs() const { return cs_; }
82         /// Returns the catcode of the token
83         CatCode cat() const { return cat_; }
84         ///
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;
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  * 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
112  */
113
114 class Parser {
115
116 public:
117         ///
118         Parser(std::istream & is);
119         ///
120         Parser(std::string const & s);
121
122         ///
123         int lineno() const { return lineno_; }
124         ///
125         void putback();
126         /// dump contents to screen
127         void dump() const;
128
129         ///
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.
134         char getChar();
135         ///
136         void error(std::string const & msg);
137         /// Parses \p is into tokens
138         void tokenize(std::istream & is);
139         ///
140         void push_back(Token const & t);
141         ///
142         void pop_back();
143         /// The previous token.
144         Token const & prev_token() const;
145         /// The current token.
146         Token const & curr_token() const;
147         /// The next token.
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);
157         ///
158         void lex(std::string const & s);
159         ///
160         bool good() const;
161         ///
162         std::string verbatim_item();
163         ///
164         std::string verbatimOption();
165         /// resets the parser to initial state
166         void reset();
167         ///
168         void setCatCode(char c, CatCode cat);
169         ///
170         CatCode getCatCode(char c) const;
171
172 private:
173         ///
174         int lineno_;
175         ///
176         std::vector<Token> tokens_;
177         ///
178         unsigned pos_;
179 };
180
181
182 #endif