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 + ']';
320 string Parser::getFullParentheseArg()
322 Arg arg = getFullArg('(', ')');
324 return '(' + arg.second + ')';
328 string const Parser::verbatimEnvironment(string const & name)
334 for (Token t = get_token(); good(); t = get_token()) {
335 if (t.cat() == catBegin) {
337 os << '{' << verbatim_item() << '}';
338 } else if (t.asInput() == "\\begin") {
339 string const env = getArg('{', '}');
340 os << "\\begin{" << env << '}'
341 << verbatimEnvironment(env)
342 << "\\end{" << env << '}';
343 } else if (t.asInput() == "\\end") {
344 string const end = getArg('{', '}');
346 cerr << "\\end{" << end
347 << "} does not match \\begin{" << name
353 cerr << "unexpected end of input" << endl;
358 void Parser::tokenize(istream & is)
360 static bool init_done = false;
369 //cerr << "reading c: " << c << "\n";
371 switch (catcode(c)) {
374 while (is.get(c) && catcode(c) == catSpace)
376 if (catcode(c) != catSpace)
378 push_back(Token(s, catSpace));
384 string s(1, getNewline(is, c));
385 while (is.get(c) && catcode(c) == catNewline) {
387 s += getNewline(is, c);
389 if (catcode(c) != catNewline)
391 push_back(Token(s, catNewline));
396 // We don't treat "%\n" combinations here specially because
397 // we want to preserve them in the preamble
399 while (is.get(c) && catcode(c) != catNewline)
401 // handle possible DOS line ending
402 if (catcode(c) == catNewline)
403 c = getNewline(is, c);
404 // Note: The '%' at the beginning and the '\n' at the end
405 // of the comment are not stored.
407 push_back(Token(s, catComment));
414 error("unexpected end of input");
417 if (catcode(c) == catLetter) {
419 while (is.get(c) && catcode(c) == catLetter)
421 if (catcode(c) != catLetter)
424 push_back(Token(s, catEscape));
430 cerr << "ignoring a char: " << int(c) << "\n";
435 push_back(Token(c, catcode(c)));
441 void Parser::dump() const
443 cerr << "\nTokens: ";
444 for (unsigned i = 0; i < tokens_.size(); ++i) {
449 cerr << " pos: " << pos_ << "\n";
453 void Parser::error(string const & msg)
455 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
461 string Parser::verbatimOption()
464 if (next_token().character() == '[') {
465 Token t = get_token();
466 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
467 if (t.cat() == catBegin) {
469 res += '{' + verbatim_item() + '}';
478 string Parser::verbatim_item()
483 if (next_token().cat() == catBegin) {
484 Token t = get_token(); // skip brace
486 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
487 if (t.cat() == catBegin) {
489 res += '{' + verbatim_item() + '}';
496 return get_token().asInput();
506 void Parser::setCatCode(char c, CatCode cat)
508 theCatcode[(unsigned char)c] = cat;
512 CatCode Parser::getCatCode(char c) const
514 return theCatcode[(unsigned char)c];