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 static bool init_done = false;
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), iss_(0), is_(is)
144 Parser::Parser(string const & s)
145 : lineno_(0), pos_(0), iss_(new istringstream(s)), is_(*iss_)
156 void Parser::push_back(Token const & t)
158 tokens_.push_back(t);
162 Token const & Parser::prev_token() const
164 static const Token dummy;
165 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
169 Token const & Parser::curr_token() const
171 static const Token dummy;
172 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
176 Token const & Parser::next_token()
178 static const Token dummy;
179 return good() ? tokens_[pos_] : dummy;
183 Token const & Parser::get_token()
185 static const Token dummy;
186 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
187 return good() ? tokens_[pos_++] : dummy;
191 bool Parser::isParagraph()
193 // A new paragraph in TeX ist started
194 // - either by a newline, following any amount of whitespace
195 // characters (including zero), and another newline
196 // - or the token \par
197 if (curr_token().cat() == catNewline &&
198 (curr_token().cs().size() > 1 ||
199 (next_token().cat() == catSpace &&
200 pos_ < tokens_.size() - 1 &&
201 tokens_[pos_ + 1].cat() == catNewline)))
203 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
209 void Parser::skip_spaces(bool skip_comments)
211 // We just silently return if we have no more tokens.
212 // skip_spaces() should be callable at any time,
213 // the caller must check p::good() anyway.
220 if ( curr_token().cat() == catSpace ||
221 curr_token().cat() == catNewline ||
222 (curr_token().cat() == catComment && curr_token().cs().empty()))
224 if (skip_comments && curr_token().cat() == catComment)
225 cerr << " Ignoring comment: " << curr_token().asInput();
234 void Parser::unskip_spaces(bool skip_comments)
237 if ( curr_token().cat() == catSpace ||
238 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
240 else if (skip_comments && curr_token().cat() == catComment) {
241 // TODO: Get rid of this
242 cerr << "Unignoring comment: " << curr_token().asInput();
251 void Parser::putback()
259 if (pos_ < tokens_.size())
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 make_pair(false, string());
288 return 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 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 string Parser::getFullParentheseArg()
328 Arg arg = getFullArg('(', ')');
330 return '(' + arg.second + ')';
335 string const Parser::verbatimEnvironment(string const & name)
341 for (Token t = get_token(); good(); t = get_token()) {
342 if (t.cat() == catBegin) {
344 os << '{' << verbatim_item() << '}';
345 } else if (t.asInput() == "\\begin") {
346 string const env = getArg('{', '}');
347 os << "\\begin{" << env << '}'
348 << verbatimEnvironment(env)
349 << "\\end{" << env << '}';
350 } else if (t.asInput() == "\\end") {
351 string const end = getArg('{', '}');
353 cerr << "\\end{" << end
354 << "} does not match \\begin{" << name
360 cerr << "unexpected end of input" << endl;
365 void Parser::tokenize_one()
371 //cerr << "reading c: " << c << "\n";
373 switch (catcode(c)) {
376 while (is_.get(c) && catcode(c) == catSpace)
378 if (catcode(c) != catSpace)
380 push_back(Token(s, catSpace));
386 string s(1, getNewline(is_, c));
387 while (is_.get(c) && catcode(c) == catNewline) {
389 s += getNewline(is_, c);
391 if (catcode(c) != catNewline)
393 push_back(Token(s, catNewline));
398 // We don't treat "%\n" combinations here specially because
399 // we want to preserve them in the preamble
401 while (is_.get(c) && catcode(c) != catNewline)
403 // handle possible DOS line ending
404 if (catcode(c) == catNewline)
405 c = getNewline(is_, c);
406 // Note: The '%' at the beginning and the '\n' at the end
407 // of the comment are not stored.
409 push_back(Token(s, catComment));
416 error("unexpected end of input");
419 if (catcode(c) == catLetter) {
421 while (is_.get(c) && catcode(c) == catLetter)
423 if (catcode(c) != catLetter)
426 push_back(Token(s, catEscape));
432 cerr << "ignoring a char: " << int(c) << "\n";
437 push_back(Token(c, catcode(c)));
442 void Parser::dump() const
444 cerr << "\nTokens: ";
445 for (unsigned i = 0; i < tokens_.size(); ++i) {
450 cerr << " pos: " << pos_ << "\n";
454 void Parser::error(string const & msg)
456 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
462 string Parser::verbatimOption()
465 if (next_token().character() == '[') {
466 Token t = get_token();
467 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
468 if (t.cat() == catBegin) {
470 res += '{' + verbatim_item() + '}';
479 string Parser::verbatim_item()
484 if (next_token().cat() == catBegin) {
485 Token t = get_token(); // skip brace
487 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
488 if (t.cat() == catBegin) {
490 res += '{' + verbatim_item() + '}';
497 return get_token().asInput();
507 void Parser::setCatCode(char c, CatCode cat)
509 theCatcode[(unsigned char)c] = cat;
513 CatCode Parser::getCatCode(char c) const
515 return theCatcode[(unsigned char)c];