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.
13 #include "texparser.h"
25 using std::istringstream;
26 using std::ostringstream;
33 CatCode theCatcode[256];
37 fill(theCatcode, theCatcode + 256, catOther);
38 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
39 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
41 theCatcode[int('\\')] = catEscape;
42 theCatcode[int('{')] = catBegin;
43 theCatcode[int('}')] = catEnd;
44 theCatcode[int('$')] = catMath;
45 theCatcode[int('&')] = catAlign;
46 theCatcode[int('\n')] = catNewline;
47 theCatcode[int('#')] = catParameter;
48 theCatcode[int('^')] = catSuper;
49 theCatcode[int('_')] = catSub;
50 theCatcode[0x7f] = catIgnore;
51 theCatcode[int(' ')] = catSpace;
52 theCatcode[int('\t')] = catSpace;
53 theCatcode[int('\r')] = catNewline;
54 theCatcode[int('~')] = catActive;
55 theCatcode[int('%')] = catComment;
58 theCatcode[int('@')] = catLetter;
63 * Translate a line ending to '\n'.
64 * \p c must have catcode catNewline, and it must be the last character read
67 char getNewline(istream & is, char c)
69 // we have to handle 3 different line endings:
75 if (is.get(c) && c != '\n') {
92 CatCode catcode(unsigned char c)
103 ostream & operator<<(ostream & os, Token const & t)
105 if (t.cat() == catComment)
106 os << '%' << t.cs() << '\n';
107 else if (t.cat() == catSpace)
109 else if (t.cat() == catEscape)
110 os << '\\' << t.cs() << ' ';
111 else if (t.cat() == catLetter)
113 else if (t.cat() == catNewline)
114 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
116 os << '[' << t.character() << ',' << t.cat() << ']';
121 string Token::asString() const
123 return cs_.size() ? cs_ : string(1, char_);
127 string Token::asInput() const
129 if (cat_ == catComment)
130 return '%' + cs_ + '\n';
131 if (cat_ == catSpace || cat_ == catNewline)
133 return char_ ? string(1, char_) : '\\' + cs_;
142 Parser::Parser(istream & is)
143 : lineno_(0), pos_(0)
149 Parser::Parser(string const & s)
150 : lineno_(0), pos_(0)
157 void Parser::push_back(Token const & t)
159 tokens_.push_back(t);
163 void Parser::pop_back()
169 Token const & Parser::prev_token() const
171 static const Token dummy;
172 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
176 Token const & Parser::curr_token() const
178 static const Token dummy;
179 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
183 Token const & Parser::next_token() const
185 static const Token dummy;
186 return good() ? tokens_[pos_] : dummy;
190 Token const & Parser::get_token()
192 static const Token dummy;
193 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
194 return good() ? tokens_[pos_++] : dummy;
198 bool Parser::isParagraph() const
200 // A new paragraph in TeX ist started
201 // - either by a newline, following any amount of whitespace
202 // characters (including zero), and another newline
203 // - or the token \par
204 if (curr_token().cat() == catNewline &&
205 (curr_token().cs().size() > 1 ||
206 (next_token().cat() == catSpace &&
207 pos_ < tokens_.size() - 1 &&
208 tokens_[pos_ + 1].cat() == catNewline)))
210 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
216 void Parser::skip_spaces(bool skip_comments)
218 // We just silently return if we have no more tokens.
219 // skip_spaces() should be callable at any time,
220 // the caller must check p::good() anyway.
227 if ( curr_token().cat() == catSpace ||
228 curr_token().cat() == catNewline ||
229 (curr_token().cat() == catComment && curr_token().cs().empty()))
231 if (skip_comments && curr_token().cat() == catComment)
232 cerr << " Ignoring comment: " << curr_token().asInput();
241 void Parser::unskip_spaces(bool skip_comments)
244 if ( curr_token().cat() == catSpace ||
245 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
247 else if (skip_comments && curr_token().cat() == catComment) {
248 // TODO: Get rid of this
249 cerr << "Unignoring comment: " << curr_token().asInput();
258 void Parser::putback()
264 bool Parser::good() const
266 return pos_ < tokens_.size();
270 char Parser::getChar()
273 error("The input stream is not well...");
274 return tokens_[pos_++].character();
278 Parser::Arg Parser::getFullArg(char left, char right)
282 // This is needed if a partial file ends with a command without arguments,
285 return std::make_pair(false, string());
292 return std::make_pair(false, string());
294 while ((c = getChar()) != right && good()) {
296 if (curr_token().cat() == catComment) {
297 if (!curr_token().cs().empty())
298 cerr << "Ignoring comment: " << curr_token().asInput();
301 result += curr_token().asInput();
304 return std::make_pair(true, result);
308 string Parser::getArg(char left, char right)
310 return getFullArg(left, right).second;
314 string Parser::getFullOpt()
316 Arg arg = getFullArg('[', ']');
318 return '[' + arg.second + ']';
323 string Parser::getOpt()
325 string const res = getArg('[', ']');
326 return res.empty() ? string() : '[' + res + ']';
330 string const Parser::verbatimEnvironment(string const & name)
336 for (Token t = get_token(); good(); t = get_token()) {
337 if (t.cat() == catBegin) {
339 os << '{' << verbatim_item() << '}';
340 } else if (t.asInput() == "\\begin") {
341 string const env = getArg('{', '}');
342 os << "\\begin{" << env << '}'
343 << verbatimEnvironment(env)
344 << "\\end{" << env << '}';
345 } else if (t.asInput() == "\\end") {
346 string const end = getArg('{', '}');
348 cerr << "\\end{" << end
349 << "} does not match \\begin{" << name
355 cerr << "unexpected end of input" << endl;
360 void Parser::tokenize(istream & is)
362 static bool init_done = false;
371 //cerr << "reading c: " << c << "\n";
373 switch (catcode(c)) {
376 while (is.get(c) && catcode(c) == catSpace)
378 if (catcode(c) != catSpace)
380 push_back(Token(s, catSpace));
386 string s(1, getNewline(is, c));
387 while (is.get(c) && catcode(c) == catNewline) {
389 s += getNewline(is, c);
391 if (catcode(c) != catNewline)
393 push_back(Token(s, catNewline));
398 // We don't treat "%\n" combinations here specially because
399 // we want to preserve them in the preamble
401 while (is.get(c) && catcode(c) != catNewline)
403 // handle possible DOS line ending
404 if (catcode(c) == catNewline)
405 c = getNewline(is, c);
406 // Note: The '%' at the beginning and the '\n' at the end
407 // of the comment are not stored.
409 push_back(Token(s, catComment));
416 error("unexpected end of input");
419 if (catcode(c) == catLetter) {
421 while (is.get(c) && catcode(c) == catLetter)
423 if (catcode(c) != catLetter)
426 push_back(Token(s, catEscape));
432 cerr << "ignoring a char: " << int(c) << "\n";
437 push_back(Token(c, catcode(c)));
443 void Parser::dump() const
445 cerr << "\nTokens: ";
446 for (unsigned i = 0; i < tokens_.size(); ++i) {
451 cerr << " pos: " << pos_ << "\n";
455 void Parser::error(string const & msg)
457 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
463 string Parser::verbatimOption()
466 if (next_token().character() == '[') {
467 Token t = get_token();
468 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
469 if (t.cat() == catBegin) {
471 res += '{' + verbatim_item() + '}';
480 string Parser::verbatim_item()
485 if (next_token().cat() == catBegin) {
486 Token t = get_token(); // skip brace
488 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
489 if (t.cat() == catBegin) {
491 res += '{' + verbatim_item() + '}';
498 return get_token().asInput();
508 void Parser::setCatCode(char c, CatCode cat)
510 theCatcode[(unsigned char)c] = cat;
514 CatCode Parser::getCatCode(char c) const
516 return theCatcode[(unsigned char)c];