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;
29 CatCode theCatcode[256];
33 fill(theCatcode, theCatcode + 256, catOther);
34 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
35 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
37 theCatcode[int('\\')] = catEscape;
38 theCatcode[int('{')] = catBegin;
39 theCatcode[int('}')] = catEnd;
40 theCatcode[int('$')] = catMath;
41 theCatcode[int('&')] = catAlign;
42 theCatcode[int('\n')] = catNewline;
43 theCatcode[int('#')] = catParameter;
44 theCatcode[int('^')] = catSuper;
45 theCatcode[int('_')] = catSub;
46 theCatcode[0x7f] = catIgnore;
47 theCatcode[int(' ')] = catSpace;
48 theCatcode[int('\t')] = catSpace;
49 theCatcode[int('\r')] = catNewline;
50 theCatcode[int('~')] = catActive;
51 theCatcode[int('%')] = catComment;
54 theCatcode[int('@')] = catLetter;
59 * Translate a line ending to '\n'.
60 * \p c must have catcode catNewline, and it must be the last character read
63 char getNewline(istream & is, char c)
65 // we have to handle 3 different line endings:
71 if (is.get(c) && c != '\n') {
88 CatCode catcode(unsigned char c)
99 ostream & operator<<(ostream & os, Token const & t)
101 if (t.cat() == catComment)
102 os << '%' << t.cs() << '\n';
103 else if (t.cat() == catSpace)
105 else if (t.cat() == catEscape)
106 os << '\\' << t.cs() << ' ';
107 else if (t.cat() == catLetter)
109 else if (t.cat() == catNewline)
110 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
112 os << '[' << t.character() << ',' << t.cat() << ']';
117 string Token::asString() const
119 return cs_.size() ? cs_ : string(1, char_);
123 string Token::asInput() const
125 if (cat_ == catComment)
126 return '%' + cs_ + '\n';
127 if (cat_ == catSpace || cat_ == catNewline)
129 return char_ ? string(1, char_) : '\\' + cs_;
138 Parser::Parser(istream & is)
139 : lineno_(0), pos_(0)
145 Parser::Parser(string const & s)
146 : lineno_(0), pos_(0)
153 void Parser::push_back(Token const & t)
155 tokens_.push_back(t);
159 void Parser::pop_back()
165 Token const & Parser::prev_token() const
167 static const Token dummy;
168 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
172 Token const & Parser::curr_token() const
174 static const Token dummy;
175 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
179 Token const & Parser::next_token() const
181 static const Token dummy;
182 return good() ? tokens_[pos_] : dummy;
186 Token const & Parser::get_token()
188 static const Token dummy;
189 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
190 return good() ? tokens_[pos_++] : dummy;
194 bool Parser::isParagraph() const
196 // A new paragraph in TeX ist started
197 // - either by a newline, following any amount of whitespace
198 // characters (including zero), and another newline
199 // - or the token \par
200 if (curr_token().cat() == catNewline &&
201 (curr_token().cs().size() > 1 ||
202 (next_token().cat() == catSpace &&
203 pos_ < tokens_.size() - 1 &&
204 tokens_[pos_ + 1].cat() == catNewline)))
206 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
212 void Parser::skip_spaces(bool skip_comments)
214 // We just silently return if we have no more tokens.
215 // skip_spaces() should be callable at any time,
216 // the caller must check p::good() anyway.
223 if ( curr_token().cat() == catSpace ||
224 curr_token().cat() == catNewline ||
225 (curr_token().cat() == catComment && curr_token().cs().empty()))
227 if (skip_comments && curr_token().cat() == catComment)
228 cerr << " Ignoring comment: " << curr_token().asInput();
237 void Parser::unskip_spaces(bool skip_comments)
240 if ( curr_token().cat() == catSpace ||
241 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
243 else if (skip_comments && curr_token().cat() == catComment) {
244 // TODO: Get rid of this
245 cerr << "Unignoring comment: " << curr_token().asInput();
254 void Parser::putback()
260 bool Parser::good() const
262 return pos_ < tokens_.size();
266 char Parser::getChar()
269 error("The input stream is not well...");
270 return tokens_[pos_++].character();
274 Parser::Arg Parser::getFullArg(char left, char right)
278 // This is needed if a partial file ends with a command without arguments,
281 return std::make_pair(false, string());
288 return std::make_pair(false, string());
290 while ((c = getChar()) != right && good()) {
292 if (curr_token().cat() == catComment) {
293 if (!curr_token().cs().empty())
294 cerr << "Ignoring comment: " << curr_token().asInput();
297 result += curr_token().asInput();
300 return std::make_pair(true, result);
304 string Parser::getArg(char left, char right)
306 return getFullArg(left, right).second;
310 string Parser::getFullOpt()
312 Arg arg = getFullArg('[', ']');
314 return '[' + arg.second + ']';
319 string Parser::getOpt()
321 string const res = getArg('[', ']');
322 return res.empty() ? string() : '[' + res + ']';
326 void Parser::tokenize(istream & is)
328 static bool init_done = false;
337 //cerr << "reading c: " << c << "\n";
339 switch (catcode(c)) {
342 while (is.get(c) && catcode(c) == catSpace)
344 if (catcode(c) != catSpace)
346 push_back(Token(s, catSpace));
352 string s(1, getNewline(is, c));
353 while (is.get(c) && catcode(c) == catNewline) {
355 s += getNewline(is, c);
357 if (catcode(c) != catNewline)
359 push_back(Token(s, catNewline));
364 // We don't treat "%\n" combinations here specially because
365 // we want to preserve them in the preamble
367 while (is.get(c) && catcode(c) != catNewline)
369 // handle possible DOS line ending
370 if (catcode(c) == catNewline)
371 c = getNewline(is, c);
372 // Note: The '%' at the beginning and the '\n' at the end
373 // of the comment are not stored.
375 push_back(Token(s, catComment));
382 error("unexpected end of input");
385 if (catcode(c) == catLetter) {
387 while (is.get(c) && catcode(c) == catLetter)
389 if (catcode(c) != catLetter)
392 push_back(Token(s, catEscape));
398 cerr << "ignoring a char: " << int(c) << "\n";
403 push_back(Token(c, catcode(c)));
409 void Parser::dump() const
411 cerr << "\nTokens: ";
412 for (unsigned i = 0; i < tokens_.size(); ++i) {
417 cerr << " pos: " << pos_ << "\n";
421 void Parser::error(string const & msg)
423 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
429 string Parser::verbatimOption()
432 if (next_token().character() == '[') {
433 Token t = get_token();
434 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
435 if (t.cat() == catBegin) {
437 res += '{' + verbatim_item() + '}';
446 string Parser::verbatim_item()
451 if (next_token().cat() == catBegin) {
452 Token t = get_token(); // skip brace
454 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
455 if (t.cat() == catBegin) {
457 res += '{' + verbatim_item() + '}';
464 return get_token().asInput();
474 void Parser::setCatCode(char c, CatCode cat)
476 theCatcode[(unsigned char)c] = cat;
480 CatCode Parser::getCatCode(char c) const
482 return theCatcode[(unsigned char)c];