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), iss_(0), is_(is)
140 Parser::Parser(string const & s)
141 : lineno_(0), pos_(0), iss_(new istringstream(s)), is_(*iss_)
153 void Parser::push_back(Token const & t)
155 tokens_.push_back(t);
159 Token const & Parser::prev_token() const
161 static const Token dummy;
162 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
166 Token const & Parser::curr_token() const
168 static const Token dummy;
169 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
173 Token const & Parser::next_token() const
175 static const Token dummy;
176 return good() ? tokens_[pos_] : dummy;
180 Token const & Parser::get_token()
182 static const Token dummy;
183 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
184 return good() ? tokens_[pos_++] : dummy;
188 bool Parser::isParagraph() const
190 // A new paragraph in TeX ist started
191 // - either by a newline, following any amount of whitespace
192 // characters (including zero), and another newline
193 // - or the token \par
194 if (curr_token().cat() == catNewline &&
195 (curr_token().cs().size() > 1 ||
196 (next_token().cat() == catSpace &&
197 pos_ < tokens_.size() - 1 &&
198 tokens_[pos_ + 1].cat() == catNewline)))
200 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
206 void Parser::skip_spaces(bool skip_comments)
208 // We just silently return if we have no more tokens.
209 // skip_spaces() should be callable at any time,
210 // the caller must check p::good() anyway.
217 if ( curr_token().cat() == catSpace ||
218 curr_token().cat() == catNewline ||
219 (curr_token().cat() == catComment && curr_token().cs().empty()))
221 if (skip_comments && curr_token().cat() == catComment)
222 cerr << " Ignoring comment: " << curr_token().asInput();
231 void Parser::unskip_spaces(bool skip_comments)
234 if ( curr_token().cat() == catSpace ||
235 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
237 else if (skip_comments && curr_token().cat() == catComment) {
238 // TODO: Get rid of this
239 cerr << "Unignoring comment: " << curr_token().asInput();
248 void Parser::putback()
254 bool Parser::good() const
256 return pos_ < tokens_.size();
260 char Parser::getChar()
263 error("The input stream is not well...");
264 return tokens_[pos_++].character();
268 Parser::Arg Parser::getFullArg(char left, char right)
272 // This is needed if a partial file ends with a command without arguments,
275 return make_pair(false, string());
282 return make_pair(false, string());
284 while ((c = getChar()) != right && good()) {
286 if (curr_token().cat() == catComment) {
287 if (!curr_token().cs().empty())
288 cerr << "Ignoring comment: " << curr_token().asInput();
291 result += curr_token().asInput();
294 return make_pair(true, result);
298 string Parser::getArg(char left, char right)
300 return getFullArg(left, right).second;
304 string Parser::getFullOpt()
306 Arg arg = getFullArg('[', ']');
308 return '[' + arg.second + ']';
313 string Parser::getOpt()
315 string const res = getArg('[', ']');
316 return res.empty() ? string() : '[' + res + ']';
320 string Parser::getFullParentheseArg()
322 Arg arg = getFullArg('(', ')');
324 return '(' + arg.second + ')';
329 string const Parser::verbatimEnvironment(string const & name)
335 for (Token t = get_token(); good(); t = get_token()) {
336 if (t.cat() == catBegin) {
338 os << '{' << verbatim_item() << '}';
339 } else if (t.asInput() == "\\begin") {
340 string const env = getArg('{', '}');
341 os << "\\begin{" << env << '}'
342 << verbatimEnvironment(env)
343 << "\\end{" << env << '}';
344 } else if (t.asInput() == "\\end") {
345 string const end = getArg('{', '}');
347 cerr << "\\end{" << end
348 << "} does not match \\begin{" << name
354 cerr << "unexpected end of input" << endl;
359 void Parser::tokenize_one()
364 //cerr << "reading c: " << c << "\n";
366 switch (catcode(c)) {
369 while (is_.get(c) && catcode(c) == catSpace)
371 if (catcode(c) != catSpace)
373 push_back(Token(s, catSpace));
379 string s(1, getNewline(is_, c));
380 while (is_.get(c) && catcode(c) == catNewline) {
382 s += getNewline(is_, c);
384 if (catcode(c) != catNewline)
386 push_back(Token(s, catNewline));
391 // We don't treat "%\n" combinations here specially because
392 // we want to preserve them in the preamble
394 while (is_.get(c) && catcode(c) != catNewline)
396 // handle possible DOS line ending
397 if (catcode(c) == catNewline)
398 c = getNewline(is_, c);
399 // Note: The '%' at the beginning and the '\n' at the end
400 // of the comment are not stored.
402 push_back(Token(s, catComment));
409 error("unexpected end of input");
412 if (catcode(c) == catLetter) {
414 while (is_.get(c) && catcode(c) == catLetter)
416 if (catcode(c) != catLetter)
419 push_back(Token(s, catEscape));
425 cerr << "ignoring a char: " << int(c) << "\n";
430 push_back(Token(c, catcode(c)));
435 void Parser::tokenize()
437 static bool init_done = false;
449 void Parser::dump() const
451 cerr << "\nTokens: ";
452 for (unsigned i = 0; i < tokens_.size(); ++i) {
457 cerr << " pos: " << pos_ << "\n";
461 void Parser::error(string const & msg)
463 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
469 string Parser::verbatimOption()
472 if (next_token().character() == '[') {
473 Token t = get_token();
474 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
475 if (t.cat() == catBegin) {
477 res += '{' + verbatim_item() + '}';
486 string Parser::verbatim_item()
491 if (next_token().cat() == catBegin) {
492 Token t = get_token(); // skip brace
494 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
495 if (t.cat() == catBegin) {
497 res += '{' + verbatim_item() + '}';
504 return get_token().asInput();
514 void Parser::setCatCode(char c, CatCode cat)
516 theCatcode[(unsigned char)c] = cat;
520 CatCode Parser::getCatCode(char c) const
522 return theCatcode[(unsigned char)c];