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"
22 using std::istringstream;
23 using std::ostringstream;
30 CatCode theCatcode[256];
34 fill(theCatcode, theCatcode + 256, catOther);
35 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
36 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
38 theCatcode[int('\\')] = catEscape;
39 theCatcode[int('{')] = catBegin;
40 theCatcode[int('}')] = catEnd;
41 theCatcode[int('$')] = catMath;
42 theCatcode[int('&')] = catAlign;
43 theCatcode[int('\n')] = catNewline;
44 theCatcode[int('#')] = catParameter;
45 theCatcode[int('^')] = catSuper;
46 theCatcode[int('_')] = catSub;
47 theCatcode[0x7f] = catIgnore;
48 theCatcode[int(' ')] = catSpace;
49 theCatcode[int('\t')] = catSpace;
50 theCatcode[int('\r')] = catNewline;
51 theCatcode[int('~')] = catActive;
52 theCatcode[int('%')] = catComment;
55 theCatcode[int('@')] = catLetter;
60 * Translate a line ending to '\n'.
61 * \p c must have catcode catNewline, and it must be the last character read
64 char getNewline(istream & is, char c)
66 // we have to handle 3 different line endings:
72 if (is.get(c) && c != '\n') {
89 CatCode catcode(unsigned char c)
100 ostream & operator<<(ostream & os, Token const & t)
102 if (t.cat() == catComment)
103 os << '%' << t.cs() << '\n';
104 else if (t.cat() == catSpace)
106 else if (t.cat() == catEscape)
107 os << '\\' << t.cs() << ' ';
108 else if (t.cat() == catLetter)
110 else if (t.cat() == catNewline)
111 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
113 os << '[' << t.character() << ',' << t.cat() << ']';
118 string Token::asString() const
120 return cs_.size() ? cs_ : string(1, char_);
124 string Token::asInput() const
126 if (cat_ == catComment)
127 return '%' + cs_ + '\n';
128 if (cat_ == catSpace || cat_ == catNewline)
130 return char_ ? string(1, char_) : '\\' + cs_;
139 Parser::Parser(istream & is)
140 : lineno_(0), pos_(0)
146 Parser::Parser(string const & s)
147 : lineno_(0), pos_(0)
154 void Parser::push_back(Token const & t)
156 tokens_.push_back(t);
160 void Parser::pop_back()
166 Token const & Parser::prev_token() const
168 static const Token dummy;
169 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
173 Token const & Parser::curr_token() const
175 static const Token dummy;
176 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
180 Token const & Parser::next_token() const
182 static const Token dummy;
183 return good() ? tokens_[pos_] : dummy;
187 Token const & Parser::get_token()
189 static const Token dummy;
190 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
191 return good() ? tokens_[pos_++] : dummy;
195 bool Parser::isParagraph() const
197 // A new paragraph in TeX ist started
198 // - either by a newline, following any amount of whitespace
199 // characters (including zero), and another newline
200 // - or the token \par
201 if (curr_token().cat() == catNewline &&
202 (curr_token().cs().size() > 1 ||
203 (next_token().cat() == catSpace &&
204 pos_ < tokens_.size() - 1 &&
205 tokens_[pos_ + 1].cat() == catNewline)))
207 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
213 void Parser::skip_spaces(bool skip_comments)
215 // We just silently return if we have no more tokens.
216 // skip_spaces() should be callable at any time,
217 // the caller must check p::good() anyway.
224 if ( curr_token().cat() == catSpace ||
225 curr_token().cat() == catNewline ||
226 (curr_token().cat() == catComment && curr_token().cs().empty()))
228 if (skip_comments && curr_token().cat() == catComment)
229 cerr << " Ignoring comment: " << curr_token().asInput();
238 void Parser::unskip_spaces(bool skip_comments)
241 if ( curr_token().cat() == catSpace ||
242 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
244 else if (skip_comments && curr_token().cat() == catComment) {
245 // TODO: Get rid of this
246 cerr << "Unignoring comment: " << curr_token().asInput();
255 void Parser::putback()
261 bool Parser::good() const
263 return pos_ < tokens_.size();
267 char Parser::getChar()
270 error("The input stream is not well...");
271 return tokens_[pos_++].character();
275 Parser::Arg Parser::getFullArg(char left, char right)
279 // This is needed if a partial file ends with a command without arguments,
282 return std::make_pair(false, string());
289 return std::make_pair(false, string());
291 while ((c = getChar()) != right && good()) {
293 if (curr_token().cat() == catComment) {
294 if (!curr_token().cs().empty())
295 cerr << "Ignoring comment: " << curr_token().asInput();
298 result += curr_token().asInput();
301 return std::make_pair(true, result);
305 string Parser::getArg(char left, char right)
307 return getFullArg(left, right).second;
311 string Parser::getFullOpt()
313 Arg arg = getFullArg('[', ']');
315 return '[' + arg.second + ']';
320 string Parser::getOpt()
322 string const res = getArg('[', ']');
323 return res.empty() ? string() : '[' + res + ']';
327 string const Parser::verbatimEnvironment(string const & name)
333 for (Token t = get_token(); good(); t = get_token()) {
334 if (t.cat() == catBegin) {
336 os << '{' << verbatim_item() << '}';
337 } else if (t.asInput() == "\\begin") {
338 string const env = getArg('{', '}');
339 os << "\\begin{" << env << '}'
340 << verbatimEnvironment(env)
341 << "\\end{" << env << '}';
342 } else if (t.asInput() == "\\end") {
343 string const end = getArg('{', '}');
345 cerr << "\\end{" << end
346 << "} does not match \\begin{" << name
352 cerr << "unexpected end of input" << endl;
357 void Parser::tokenize(istream & is)
359 static bool init_done = false;
368 //cerr << "reading c: " << c << "\n";
370 switch (catcode(c)) {
373 while (is.get(c) && catcode(c) == catSpace)
375 if (catcode(c) != catSpace)
377 push_back(Token(s, catSpace));
383 string s(1, getNewline(is, c));
384 while (is.get(c) && catcode(c) == catNewline) {
386 s += getNewline(is, c);
388 if (catcode(c) != catNewline)
390 push_back(Token(s, catNewline));
395 // We don't treat "%\n" combinations here specially because
396 // we want to preserve them in the preamble
398 while (is.get(c) && catcode(c) != catNewline)
400 // handle possible DOS line ending
401 if (catcode(c) == catNewline)
402 c = getNewline(is, c);
403 // Note: The '%' at the beginning and the '\n' at the end
404 // of the comment are not stored.
406 push_back(Token(s, catComment));
413 error("unexpected end of input");
416 if (catcode(c) == catLetter) {
418 while (is.get(c) && catcode(c) == catLetter)
420 if (catcode(c) != catLetter)
423 push_back(Token(s, catEscape));
429 cerr << "ignoring a char: " << int(c) << "\n";
434 push_back(Token(c, catcode(c)));
440 void Parser::dump() const
442 cerr << "\nTokens: ";
443 for (unsigned i = 0; i < tokens_.size(); ++i) {
448 cerr << " pos: " << pos_ << "\n";
452 void Parser::error(string const & msg)
454 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
460 string Parser::verbatimOption()
463 if (next_token().character() == '[') {
464 Token t = get_token();
465 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
466 if (t.cat() == catBegin) {
468 res += '{' + verbatim_item() + '}';
477 string Parser::verbatim_item()
482 if (next_token().cat() == catBegin) {
483 Token t = get_token(); // skip brace
485 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
486 if (t.cat() == catBegin) {
488 res += '{' + verbatim_item() + '}';
495 return get_token().asInput();
505 void Parser::setCatCode(char c, CatCode cat)
507 theCatcode[(unsigned char)c] = cat;
511 CatCode Parser::getCatCode(char c) const
513 return theCatcode[(unsigned char)c];