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 string Parser::getArg(char left, char right)
278 // This is needed if a partial file ends with a command without arguments,
289 while ((c = getChar()) != right && good()) {
291 if (curr_token().cat() == catComment) {
292 if (!curr_token().cs().empty())
293 cerr << "Ignoring comment: " << curr_token().asInput();
296 result += curr_token().asInput();
303 string Parser::getOpt()
305 string const res = getArg('[', ']');
306 return res.size() ? '[' + res + ']' : string();
310 void Parser::tokenize(istream & is)
312 static bool init_done = false;
321 //cerr << "reading c: " << c << "\n";
323 switch (catcode(c)) {
326 while (is.get(c) && catcode(c) == catSpace)
328 if (catcode(c) != catSpace)
330 push_back(Token(s, catSpace));
336 string s(1, getNewline(is, c));
337 while (is.get(c) && catcode(c) == catNewline) {
339 s += getNewline(is, c);
341 if (catcode(c) != catNewline)
343 push_back(Token(s, catNewline));
348 // We don't treat "%\n" combinations here specially because
349 // we want to preserve them in the preamble
351 while (is.get(c) && catcode(c) != catNewline)
353 // handle possible DOS line ending
354 if (catcode(c) == catNewline)
355 c = getNewline(is, c);
356 // Note: The '%' at the beginning and the '\n' at the end
357 // of the comment are not stored.
359 push_back(Token(s, catComment));
366 error("unexpected end of input");
369 if (catcode(c) == catLetter) {
371 while (is.get(c) && catcode(c) == catLetter)
373 if (catcode(c) != catLetter)
376 push_back(Token(s, catEscape));
382 cerr << "ignoring a char: " << int(c) << "\n";
387 push_back(Token(c, catcode(c)));
393 void Parser::dump() const
395 cerr << "\nTokens: ";
396 for (unsigned i = 0; i < tokens_.size(); ++i) {
401 cerr << " pos: " << pos_ << "\n";
405 void Parser::error(string const & msg)
407 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
413 string Parser::verbatimOption()
416 if (next_token().character() == '[') {
417 Token t = get_token();
418 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
419 if (t.cat() == catBegin) {
421 res += '{' + verbatim_item() + '}';
430 string Parser::verbatim_item()
435 if (next_token().cat() == catBegin) {
436 Token t = get_token(); // skip brace
438 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
439 if (t.cat() == catBegin) {
441 res += '{' + verbatim_item() + '}';
448 return get_token().asInput();
458 void Parser::setCatCode(char c, CatCode cat)
460 theCatcode[(unsigned char)c] = cat;
464 CatCode Parser::getCatCode(char c) const
466 return theCatcode[(unsigned char)c];