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.
24 CatCode theCatcode[256];
28 fill(theCatcode, theCatcode + 256, catOther);
29 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
30 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
32 theCatcode[int('\\')] = catEscape;
33 theCatcode[int('{')] = catBegin;
34 theCatcode[int('}')] = catEnd;
35 theCatcode[int('$')] = catMath;
36 theCatcode[int('&')] = catAlign;
37 theCatcode[int('\n')] = catNewline;
38 theCatcode[int('#')] = catParameter;
39 theCatcode[int('^')] = catSuper;
40 theCatcode[int('_')] = catSub;
41 theCatcode[0x7f] = catIgnore;
42 theCatcode[int(' ')] = catSpace;
43 theCatcode[int('\t')] = catSpace;
44 theCatcode[int('\r')] = catNewline;
45 theCatcode[int('~')] = catActive;
46 theCatcode[int('%')] = catComment;
49 theCatcode[int('@')] = catLetter;
54 * Translate a line ending to '\n'.
55 * \p c must have catcode catNewline, and it must be the last character read
58 char getNewline(istream & is, char c)
60 // we have to handle 3 different line endings:
66 if (is.get(c) && c != '\n') {
83 CatCode catcode(unsigned char c)
94 ostream & operator<<(ostream & os, Token const & t)
96 if (t.cat() == catComment)
97 os << '%' << t.cs() << '\n';
98 else if (t.cat() == catSpace)
100 else if (t.cat() == catEscape)
101 os << '\\' << t.cs() << ' ';
102 else if (t.cat() == catLetter)
104 else if (t.cat() == catNewline)
105 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
107 os << '[' << t.character() << ',' << t.cat() << ']';
112 string Token::asString() const
114 return cs_.size() ? cs_ : string(1, char_);
118 string Token::asInput() const
120 if (cat_ == catComment)
121 return '%' + cs_ + '\n';
122 if (cat_ == catSpace || cat_ == catNewline)
124 return char_ ? string(1, char_) : '\\' + cs_;
133 Parser::Parser(istream & is)
134 : lineno_(0), pos_(0)
140 Parser::Parser(string const & s)
141 : lineno_(0), pos_(0)
148 void Parser::push_back(Token const & t)
150 tokens_.push_back(t);
154 void Parser::pop_back()
160 Token const & Parser::prev_token() const
162 static const Token dummy;
163 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
167 Token const & Parser::curr_token() const
169 static const Token dummy;
170 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
174 Token const & Parser::next_token() const
176 static const Token dummy;
177 return good() ? tokens_[pos_] : dummy;
181 Token const & Parser::get_token()
183 static const Token dummy;
184 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
185 return good() ? tokens_[pos_++] : dummy;
189 bool Parser::isParagraph() const
191 // A new paragraph in TeX ist started
192 // - either by a newline, following any amount of whitespace
193 // characters (including zero), and another newline
194 // - or the token \par
195 if (curr_token().cat() == catNewline &&
196 (curr_token().cs().size() > 1 ||
197 (next_token().cat() == catSpace &&
198 pos_ < tokens_.size() - 1 &&
199 tokens_[pos_ + 1].cat() == catNewline)))
201 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
207 void Parser::skip_spaces(bool skip_comments)
209 // We just silently return if we have no more tokens.
210 // skip_spaces() should be callable at any time,
211 // the caller must check p::good() anyway.
218 if ( curr_token().cat() == catSpace ||
219 curr_token().cat() == catNewline ||
220 (curr_token().cat() == catComment && curr_token().cs().empty()))
222 if (skip_comments && curr_token().cat() == catComment)
223 cerr << " Ignoring comment: " << curr_token().asInput();
232 void Parser::unskip_spaces(bool skip_comments)
235 if ( curr_token().cat() == catSpace ||
236 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
238 else if (skip_comments && curr_token().cat() == catComment) {
239 // TODO: Get rid of this
240 cerr << "Unignoring comment: " << curr_token().asInput();
249 void Parser::putback()
255 bool Parser::good() const
257 return pos_ < tokens_.size();
261 char Parser::getChar()
264 error("The input stream is not well...");
265 return tokens_[pos_++].character();
269 Parser::Arg Parser::getFullArg(char left, char right)
273 // This is needed if a partial file ends with a command without arguments,
276 return make_pair(false, string());
283 return make_pair(false, string());
285 while ((c = getChar()) != right && good()) {
287 if (curr_token().cat() == catComment) {
288 if (!curr_token().cs().empty())
289 cerr << "Ignoring comment: " << curr_token().asInput();
292 result += curr_token().asInput();
295 return make_pair(true, result);
299 string Parser::getArg(char left, char right)
301 return getFullArg(left, right).second;
305 string Parser::getFullOpt()
307 Arg arg = getFullArg('[', ']');
309 return '[' + arg.second + ']';
314 string Parser::getOpt()
316 string const res = getArg('[', ']');
317 return res.empty() ? string() : '[' + res + ']';
321 string const Parser::verbatimEnvironment(string const & name)
327 for (Token t = get_token(); good(); t = get_token()) {
328 if (t.cat() == catBegin) {
330 os << '{' << verbatim_item() << '}';
331 } else if (t.asInput() == "\\begin") {
332 string const env = getArg('{', '}');
333 os << "\\begin{" << env << '}'
334 << verbatimEnvironment(env)
335 << "\\end{" << env << '}';
336 } else if (t.asInput() == "\\end") {
337 string const end = getArg('{', '}');
339 cerr << "\\end{" << end
340 << "} does not match \\begin{" << name
346 cerr << "unexpected end of input" << endl;
351 void Parser::tokenize(istream & is)
353 static bool init_done = false;
362 //cerr << "reading c: " << c << "\n";
364 switch (catcode(c)) {
367 while (is.get(c) && catcode(c) == catSpace)
369 if (catcode(c) != catSpace)
371 push_back(Token(s, catSpace));
377 string s(1, getNewline(is, c));
378 while (is.get(c) && catcode(c) == catNewline) {
380 s += getNewline(is, c);
382 if (catcode(c) != catNewline)
384 push_back(Token(s, catNewline));
389 // We don't treat "%\n" combinations here specially because
390 // we want to preserve them in the preamble
392 while (is.get(c) && catcode(c) != catNewline)
394 // handle possible DOS line ending
395 if (catcode(c) == catNewline)
396 c = getNewline(is, c);
397 // Note: The '%' at the beginning and the '\n' at the end
398 // of the comment are not stored.
400 push_back(Token(s, catComment));
407 error("unexpected end of input");
410 if (catcode(c) == catLetter) {
412 while (is.get(c) && catcode(c) == catLetter)
414 if (catcode(c) != catLetter)
417 push_back(Token(s, catEscape));
423 cerr << "ignoring a char: " << int(c) << "\n";
428 push_back(Token(c, catcode(c)));
434 void Parser::dump() const
436 cerr << "\nTokens: ";
437 for (unsigned i = 0; i < tokens_.size(); ++i) {
442 cerr << " pos: " << pos_ << "\n";
446 void Parser::error(string const & msg)
448 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
454 string Parser::verbatimOption()
457 if (next_token().character() == '[') {
458 Token t = get_token();
459 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
460 if (t.cat() == catBegin) {
462 res += '{' + verbatim_item() + '}';
471 string Parser::verbatim_item()
476 if (next_token().cat() == catBegin) {
477 Token t = get_token(); // skip brace
479 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
480 if (t.cat() == catBegin) {
482 res += '{' + verbatim_item() + '}';
489 return get_token().asInput();
499 void Parser::setCatCode(char c, CatCode cat)
501 theCatcode[(unsigned char)c] = cat;
505 CatCode Parser::getCatCode(char c) const
507 return theCatcode[(unsigned char)c];