]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/texparser.C
Collapse all those LFUN_XYZ_APPLY to a single LFUN_INSET_APPLY.
[lyx.git] / src / tex2lyx / texparser.C
1
2 #include "texparser.h"
3 #include <iostream>
4
5 using std::cerr;
6 using std::endl;
7 using std::fill;
8 using std::ios;
9 using std::istream;
10 using std::istringstream;
11 using std::ostream;
12 using std::string;
13 using std::vector;
14
15
16 namespace {
17
18 CatCode theCatcode[256];
19
20 void skipSpaceTokens(istream & is, char c)
21 {
22         // skip trailing spaces
23         while (catcode(c) == catSpace || catcode(c) == catNewline)
24                 if (!is.get(c))
25                         break;
26         //cerr << "putting back: " << c << "\n";
27         is.putback(c);
28 }
29
30
31 void catInit()
32 {
33         fill(theCatcode, theCatcode + 256, catOther);
34         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
35         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
36
37         theCatcode['\\'] = catEscape;
38         theCatcode['{']  = catBegin;
39         theCatcode['}']  = catEnd;
40         theCatcode['$']  = catMath;
41         theCatcode['&']  = catAlign;
42         theCatcode['\n'] = catNewline;
43         theCatcode['#']  = catParameter;
44         theCatcode['^']  = catSuper;
45         theCatcode['_']  = catSub;
46         theCatcode['\7f'] = catIgnore;
47         theCatcode[' ']  = catSpace;
48         theCatcode['\t'] = catSpace;
49         theCatcode['\r'] = catNewline;
50         theCatcode['~']  = catActive;
51         theCatcode['%']  = catComment;
52 }
53
54 }
55
56
57 // 
58 // catcodes
59 //
60
61 mode_type asMode(mode_type oldmode, string const & str)
62 {
63         if (str == "mathmode")
64                 return MATH_MODE;
65         if (str == "textmode" || str == "forcetext")
66                 return TEXT_MODE;
67         return oldmode;
68 }
69
70
71 CatCode catcode(unsigned char c)
72 {
73         return theCatcode[c];
74 }
75
76
77
78 //
79 // Token
80 //
81
82 ostream & operator<<(ostream & os, Token const & t)
83 {
84         if (t.cs().size())
85                 os << '\\' << t.cs();
86         else
87                 os << '[' << t.character() << ',' << t.cat() << ']';
88         return os;
89 }
90
91
92 string Token::asString() const
93 {
94         return cs_.size() ? cs_ : string(1, char_);
95 }
96
97
98 string Token::asInput() const
99 {
100         return char_ ? string(1, char_) : '\\' + cs_ + ' ';
101 }
102
103
104 //
105 // Parser
106 //
107
108
109 Parser::Parser(istream & is)
110         : lineno_(0), pos_(0)
111 {
112         tokenize(is);
113 }
114
115
116 void Parser::push_back(Token const & t)
117 {
118         tokens_.push_back(t);
119 }
120
121
122 void Parser::pop_back()
123 {
124         tokens_.pop_back();
125 }
126
127
128 Token const & Parser::prevToken() const
129 {
130         static const Token dummy;
131         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
132 }
133
134
135 Token const & Parser::nextToken() const
136 {
137         static const Token dummy;
138         return good() ? tokens_[pos_] : dummy;
139 }
140
141
142 Token const & Parser::getToken()
143 {
144         static const Token dummy;
145         //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
146         return good() ? tokens_[pos_++] : dummy;
147 }
148
149
150 void Parser::skipSpaces()
151 {
152         while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
153                 getToken();
154 }
155
156
157 void Parser::putback()
158 {
159         --pos_;
160 }
161
162
163 bool Parser::good() const
164 {
165         return pos_ < tokens_.size();
166 }
167
168
169 char Parser::getChar()
170 {
171         if (!good())
172                 error("The input stream is not well...");
173         return tokens_[pos_++].character();
174 }
175
176
177 string Parser::getArg(char left, char right)
178 {
179         skipSpaces();
180
181         string result;
182         char c = getChar();
183
184         if (c != left)
185                 putback();
186         else
187                 while ((c = getChar()) != right && good())
188                         result += c;
189
190         return result;
191 }
192
193
194 void Parser::tokenize(istream & is)
195 {
196         static bool init_done = false;
197
198         if (!init_done) {
199                 catInit();
200                 init_done = true;
201         }
202
203         char c;
204         while (is.get(c)) {
205                 //cerr << "reading c: " << c << "\n";
206
207                 switch (catcode(c)) {
208                         case catNewline: {
209                                 ++lineno_;
210                                 is.get(c);
211                                 if (catcode(c) == catNewline)
212                                         push_back(Token("par"));
213                                 else {
214                                         push_back(Token('\n', catNewline));
215                                         is.putback(c);
216                                 }
217                                 break;
218                         }
219
220 /*
221                         case catComment: {
222                                 while (is.get(c) && catcode(c) != catNewline)
223                                         ;
224                                 ++lineno_;
225                                 break;
226                         }
227 */
228
229                         case catEscape: {
230                                 is.get(c);
231                                 if (!is) {
232                                         error("unexpected end of input");
233                                 } else {
234                                         string s(1, c);
235                                         if (catcode(c) == catLetter) {
236                                                 // collect letters
237                                                 while (is.get(c) && catcode(c) == catLetter)
238                                                         s += c;
239                                                 skipSpaceTokens(is, c);
240                                         }
241                                         push_back(Token(s));
242                                 }
243                                 break;
244                         }
245
246                         case catSuper:
247                         case catSub: {
248                                 push_back(Token(c, catcode(c)));
249                                 is.get(c);
250                                 skipSpaceTokens(is, c);
251                                 break;
252                         }
253
254                         case catIgnore: {
255                                 cerr << "ignoring a char: " << int(c) << "\n";
256                                 break;
257                         }
258
259                         default:
260                                 push_back(Token(c, catcode(c)));
261                 }
262         }
263 }
264
265
266 void Parser::dump() const
267 {
268         cerr << "\nTokens: ";
269         for (unsigned i = 0; i < tokens_.size(); ++i) {
270                 if (i == pos_)
271                         cerr << " <#> ";
272                 cerr << tokens_[i];
273         }
274         cerr << " pos: " << pos_ << "\n";
275 }
276
277
278 void Parser::error(string const & msg)
279 {
280         cerr << "Line ~" << lineno_ << ":  parse error: " << msg << endl;
281         dump();
282         //exit(1);
283 }
284
285
286 string Parser::verbatimOption()
287 {
288         string res;
289         if (nextToken().character() == '[') {
290                 Token t = getToken();
291                 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
292                         if (t.cat() == catBegin) {
293                                 putback();
294                                 res += '{' + verbatimItem() + '}';
295                         } else
296                                 res += t.asString();
297                 }
298         }
299         return res;
300 }
301
302
303 string Parser::verbatimItem()
304 {
305         if (!good())
306                 error("stream bad");
307         if (nextToken().cat() == catBegin) {
308                 Token t = getToken(); // skip brace
309                 string res;
310                 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
311                         if (t.cat() == catBegin) {
312                                 putback();
313                                 res += '{' + verbatimItem() + '}';
314                         }
315                         else
316                                 res += t.asInput();
317                 }
318                 return res;
319         }
320         return getToken().asInput();
321 }
322