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[10] = 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[13] = catIgnore;
50 theCatcode[int('~')] = catActive;
51 theCatcode[int('%')] = catComment;
54 theCatcode[int('@')] = catLetter;
64 CatCode catcode(unsigned char c)
75 ostream & operator<<(ostream & os, Token const & t)
77 if (t.cat() == catComment)
78 os << '%' << t.cs() << '\n';
79 else if (t.cat() == catSpace)
81 else if (t.cat() == catEscape)
82 os << '\\' << t.cs() << ' ';
83 else if (t.cat() == catLetter)
85 else if (t.cat() == catNewline)
86 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
88 os << '[' << t.character() << ',' << t.cat() << ']';
93 string Token::asString() const
95 return cs_.size() ? cs_ : string(1, char_);
99 string Token::asInput() const
101 if (cat_ == catComment)
102 return '%' + cs_ + '\n';
103 if (cat_ == catSpace || cat_ == catNewline)
105 return char_ ? string(1, char_) : '\\' + cs_;
114 Parser::Parser(istream & is)
115 : lineno_(0), pos_(0)
121 Parser::Parser(string const & s)
122 : lineno_(0), pos_(0)
129 void Parser::push_back(Token const & t)
131 tokens_.push_back(t);
135 void Parser::pop_back()
141 Token const & Parser::prev_token() const
143 static const Token dummy;
144 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
148 Token const & Parser::curr_token() const
150 static const Token dummy;
151 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
155 Token const & Parser::next_token() const
157 static const Token dummy;
158 return good() ? tokens_[pos_] : dummy;
162 Token const & Parser::get_token()
164 static const Token dummy;
165 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
166 return good() ? tokens_[pos_++] : dummy;
170 bool Parser::isParagraph() const
172 // A new paragraph in TeX ist started
173 // - either by a newline, following any amount of whitespace
174 // characters (including zero), and another newline
175 // - or the token \par
176 if (curr_token().cat() == catNewline &&
177 (curr_token().cs().size() > 1 ||
178 (next_token().cat() == catSpace &&
179 pos_ < tokens_.size() - 1 &&
180 tokens_[pos_ + 1].cat() == catNewline)))
182 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
188 void Parser::skip_spaces(bool skip_comments)
190 // We just silently return if we have no more tokens.
191 // skip_spaces() should be callable at any time,
192 // the caller must check p::good() anyway.
199 if ( curr_token().cat() == catSpace ||
200 curr_token().cat() == catNewline ||
201 (curr_token().cat() == catComment && curr_token().cs().empty()))
203 if (skip_comments && curr_token().cat() == catComment)
204 cerr << " Ignoring comment: " << curr_token().asInput();
213 void Parser::unskip_spaces(bool skip_comments)
216 if ( curr_token().cat() == catSpace ||
217 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
219 else if (skip_comments && curr_token().cat() == catComment) {
220 // TODO: Get rid of this
221 cerr << "Unignoring comment: " << curr_token().asInput();
230 void Parser::putback()
236 bool Parser::good() const
238 return pos_ < tokens_.size();
242 char Parser::getChar()
245 error("The input stream is not well...");
246 return tokens_[pos_++].character();
250 string Parser::getArg(char left, char right)
254 // This is needed if a partial file ends with a command without arguments,
265 while ((c = getChar()) != right && good()) {
267 if (curr_token().cat() == catComment) {
268 if (!curr_token().cs().empty())
269 cerr << "Ignoring comment: " << curr_token().asInput();
272 result += curr_token().asInput();
279 string Parser::getOpt()
281 string const res = getArg('[', ']');
282 return res.size() ? '[' + res + ']' : string();
286 void Parser::tokenize(istream & is)
288 static bool init_done = false;
297 //cerr << "reading c: " << c << "\n";
299 switch (catcode(c)) {
302 while (is.get(c) && catcode(c) == catSpace)
304 if (catcode(c) != catSpace)
306 push_back(Token(s, catSpace));
313 while (is.get(c) && catcode(c) == catNewline) {
317 if (catcode(c) != catNewline)
319 push_back(Token(s, catNewline));
324 // We don't treat "%\n" combinations here specially because
325 // we want to preserve them in the preamble
327 while (is.get(c) && catcode(c) != catNewline)
329 // Note: The '%' at the beginning and the '\n' at the end
330 // of the comment are not stored.
332 push_back(Token(s, catComment));
339 error("unexpected end of input");
342 if (catcode(c) == catLetter) {
344 while (is.get(c) && catcode(c) == catLetter)
346 if (catcode(c) != catLetter)
349 push_back(Token(s, catEscape));
356 cerr << "ignoring a char: " << int(c) << "\n";
361 push_back(Token(c, catcode(c)));
367 void Parser::dump() const
369 cerr << "\nTokens: ";
370 for (unsigned i = 0; i < tokens_.size(); ++i) {
375 cerr << " pos: " << pos_ << "\n";
379 void Parser::error(string const & msg)
381 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
387 string Parser::verbatimOption()
390 if (next_token().character() == '[') {
391 Token t = get_token();
392 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
393 if (t.cat() == catBegin) {
395 res += '{' + verbatim_item() + '}';
404 string Parser::verbatim_item()
409 if (next_token().cat() == catBegin) {
410 Token t = get_token(); // skip brace
412 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
413 if (t.cat() == catBegin) {
415 res += '{' + verbatim_item() + '}';
422 return get_token().asInput();
432 void Parser::setCatCode(char c, CatCode cat)
434 theCatcode[(unsigned char)c] = cat;
438 CatCode Parser::getCatCode(char c) const
440 return theCatcode[(unsigned char)c];