3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
24 CatCode theCatcode[256];
28 fill(theCatcode, theCatcode + 256, catOther);
29 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
30 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
32 theCatcode[int('\\')] = catEscape;
33 theCatcode[int('{')] = catBegin;
34 theCatcode[int('}')] = catEnd;
35 theCatcode[int('$')] = catMath;
36 theCatcode[int('&')] = catAlign;
37 theCatcode[int('\n')] = catNewline;
38 theCatcode[int('#')] = catParameter;
39 theCatcode[int('^')] = catSuper;
40 theCatcode[int('_')] = catSub;
41 theCatcode[0x7f] = catIgnore;
42 theCatcode[int(' ')] = catSpace;
43 theCatcode[int('\t')] = catSpace;
44 theCatcode[int('\r')] = catNewline;
45 theCatcode[int('~')] = catActive;
46 theCatcode[int('%')] = catComment;
49 theCatcode[int('@')] = catLetter;
54 * Translate a line ending to '\n'.
55 * \p c must have catcode catNewline, and it must be the last character read
58 char getNewline(istream & is, char c)
60 // we have to handle 3 different line endings:
66 if (is.get(c) && c != '\n') {
83 CatCode catcode(unsigned char c)
94 ostream & operator<<(ostream & os, Token const & t)
96 if (t.cat() == catComment)
97 os << '%' << t.cs() << '\n';
98 else if (t.cat() == catSpace)
100 else if (t.cat() == catEscape)
101 os << '\\' << t.cs() << ' ';
102 else if (t.cat() == catLetter)
104 else if (t.cat() == catNewline)
105 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
107 os << '[' << t.character() << ',' << t.cat() << ']';
112 string Token::asString() const
114 return cs_.size() ? cs_ : string(1, char_);
118 string Token::asInput() const
120 if (cat_ == catComment)
121 return '%' + cs_ + '\n';
122 if (cat_ == catSpace || cat_ == catNewline)
124 return char_ ? string(1, char_) : '\\' + cs_;
133 Parser::Parser(istream & is)
134 : lineno_(0), pos_(0)
140 Parser::Parser(string const & s)
141 : lineno_(0), pos_(0)
148 void Parser::push_back(Token const & t)
150 tokens_.push_back(t);
154 Token const & Parser::prev_token() const
156 static const Token dummy;
157 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
161 Token const & Parser::curr_token() const
163 static const Token dummy;
164 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
168 Token const & Parser::next_token() const
170 static const Token dummy;
171 return good() ? tokens_[pos_] : dummy;
175 Token const & Parser::get_token()
177 static const Token dummy;
178 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
179 return good() ? tokens_[pos_++] : dummy;
183 bool Parser::isParagraph() const
185 // A new paragraph in TeX ist started
186 // - either by a newline, following any amount of whitespace
187 // characters (including zero), and another newline
188 // - or the token \par
189 if (curr_token().cat() == catNewline &&
190 (curr_token().cs().size() > 1 ||
191 (next_token().cat() == catSpace &&
192 pos_ < tokens_.size() - 1 &&
193 tokens_[pos_ + 1].cat() == catNewline)))
195 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
201 void Parser::skip_spaces(bool skip_comments)
203 // We just silently return if we have no more tokens.
204 // skip_spaces() should be callable at any time,
205 // the caller must check p::good() anyway.
212 if ( curr_token().cat() == catSpace ||
213 curr_token().cat() == catNewline ||
214 (curr_token().cat() == catComment && curr_token().cs().empty()))
216 if (skip_comments && curr_token().cat() == catComment)
217 cerr << " Ignoring comment: " << curr_token().asInput();
226 void Parser::unskip_spaces(bool skip_comments)
229 if ( curr_token().cat() == catSpace ||
230 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
232 else if (skip_comments && curr_token().cat() == catComment) {
233 // TODO: Get rid of this
234 cerr << "Unignoring comment: " << curr_token().asInput();
243 void Parser::putback()
249 bool Parser::good() const
251 return pos_ < tokens_.size();
255 char Parser::getChar()
258 error("The input stream is not well...");
259 return tokens_[pos_++].character();
263 Parser::Arg Parser::getFullArg(char left, char right)
267 // This is needed if a partial file ends with a command without arguments,
270 return make_pair(false, string());
277 return make_pair(false, string());
279 while ((c = getChar()) != right && good()) {
281 if (curr_token().cat() == catComment) {
282 if (!curr_token().cs().empty())
283 cerr << "Ignoring comment: " << curr_token().asInput();
286 result += curr_token().asInput();
289 return make_pair(true, result);
293 string Parser::getArg(char left, char right)
295 return getFullArg(left, right).second;
299 string Parser::getFullOpt()
301 Arg arg = getFullArg('[', ']');
303 return '[' + arg.second + ']';
308 string Parser::getOpt()
310 string const res = getArg('[', ']');
311 return res.empty() ? string() : '[' + res + ']';
315 string Parser::getFullParentheseArg()
317 Arg arg = getFullArg('(', ')');
319 return '(' + arg.second + ')';
324 string const Parser::verbatimEnvironment(string const & name)
330 for (Token t = get_token(); good(); t = get_token()) {
331 if (t.cat() == catBegin) {
333 os << '{' << verbatim_item() << '}';
334 } else if (t.asInput() == "\\begin") {
335 string const env = getArg('{', '}');
336 os << "\\begin{" << env << '}'
337 << verbatimEnvironment(env)
338 << "\\end{" << env << '}';
339 } else if (t.asInput() == "\\end") {
340 string const end = getArg('{', '}');
342 cerr << "\\end{" << end
343 << "} does not match \\begin{" << name
349 cerr << "unexpected end of input" << endl;
354 void Parser::tokenize_one(istream & is)
359 //cerr << "reading c: " << c << "\n";
361 switch (catcode(c)) {
364 while (is.get(c) && catcode(c) == catSpace)
366 if (catcode(c) != catSpace)
368 push_back(Token(s, catSpace));
374 string s(1, getNewline(is, c));
375 while (is.get(c) && catcode(c) == catNewline) {
377 s += getNewline(is, c);
379 if (catcode(c) != catNewline)
381 push_back(Token(s, catNewline));
386 // We don't treat "%\n" combinations here specially because
387 // we want to preserve them in the preamble
389 while (is.get(c) && catcode(c) != catNewline)
391 // handle possible DOS line ending
392 if (catcode(c) == catNewline)
393 c = getNewline(is, c);
394 // Note: The '%' at the beginning and the '\n' at the end
395 // of the comment are not stored.
397 push_back(Token(s, catComment));
404 error("unexpected end of input");
407 if (catcode(c) == catLetter) {
409 while (is.get(c) && catcode(c) == catLetter)
411 if (catcode(c) != catLetter)
414 push_back(Token(s, catEscape));
420 cerr << "ignoring a char: " << int(c) << "\n";
425 push_back(Token(c, catcode(c)));
430 void Parser::tokenize(istream & is)
432 static bool init_done = false;
444 void Parser::dump() const
446 cerr << "\nTokens: ";
447 for (unsigned i = 0; i < tokens_.size(); ++i) {
452 cerr << " pos: " << pos_ << "\n";
456 void Parser::error(string const & msg)
458 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
464 string Parser::verbatimOption()
467 if (next_token().character() == '[') {
468 Token t = get_token();
469 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
470 if (t.cat() == catBegin) {
472 res += '{' + verbatim_item() + '}';
481 string Parser::verbatim_item()
486 if (next_token().cat() == catBegin) {
487 Token t = get_token(); // skip brace
489 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
490 if (t.cat() == catBegin) {
492 res += '{' + verbatim_item() + '}';
499 return get_token().asInput();
509 void Parser::setCatCode(char c, CatCode cat)
511 theCatcode[(unsigned char)c] = cat;
515 CatCode Parser::getCatCode(char c) const
517 return theCatcode[(unsigned char)c];