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.
23 CatCode theCatcode[256];
27 static bool init_done = false;
32 fill(theCatcode, theCatcode + 256, catOther);
33 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
34 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
36 theCatcode[int('\\')] = catEscape;
37 theCatcode[int('{')] = catBegin;
38 theCatcode[int('}')] = catEnd;
39 theCatcode[int('$')] = catMath;
40 theCatcode[int('&')] = catAlign;
41 theCatcode[int('\n')] = catNewline;
42 theCatcode[int('#')] = catParameter;
43 theCatcode[int('^')] = catSuper;
44 theCatcode[int('_')] = catSub;
45 theCatcode[0x7f] = catIgnore;
46 theCatcode[int(' ')] = catSpace;
47 theCatcode[int('\t')] = catSpace;
48 theCatcode[int('\r')] = catNewline;
49 theCatcode[int('~')] = catActive;
50 theCatcode[int('%')] = catComment;
53 theCatcode[int('@')] = catLetter;
57 * Translate a line ending to '\n'.
58 * \p c must have catcode catNewline, and it must be the last character read
61 char getNewline(idocstream & is, char c)
63 // we have to handle 3 different line endings:
70 if (is.get(wc) && wc != '\n') {
80 CatCode catcode(char_type c)
83 return theCatcode[(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.cs() << ',' << t.cat() << ']';
112 string Token::asString() const
118 string Token::asInput() const
120 if (cat_ == catComment)
121 return '%' + cs_ + '\n';
122 if (cat_ == catEscape)
133 Parser::Parser(idocstream & is)
134 : lineno_(0), pos_(0), iss_(0), is_(is)
139 Parser::Parser(string const & s)
140 : lineno_(0), pos_(0),
141 iss_(new idocstringstream(from_utf8(s))), is_(*iss_)
152 void Parser::push_back(Token const & t)
154 tokens_.push_back(t);
158 Token const & Parser::prev_token() const
160 static const Token dummy;
161 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
165 Token const & Parser::curr_token() const
167 static const Token dummy;
168 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
172 Token const & Parser::next_token()
174 static const Token dummy;
175 return good() ? tokens_[pos_] : dummy;
179 Token const & Parser::get_token()
181 static const Token dummy;
182 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
183 return good() ? tokens_[pos_++] : dummy;
187 bool Parser::isParagraph()
189 // A new paragraph in TeX ist started
190 // - either by a newline, following any amount of whitespace
191 // characters (including zero), and another newline
192 // - or the token \par
193 if (curr_token().cat() == catNewline &&
194 (curr_token().cs().size() > 1 ||
195 (next_token().cat() == catSpace &&
196 pos_ < tokens_.size() - 1 &&
197 tokens_[pos_ + 1].cat() == catNewline)))
199 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
205 void Parser::skip_spaces(bool skip_comments)
207 // We just silently return if we have no more tokens.
208 // skip_spaces() should be callable at any time,
209 // the caller must check p::good() anyway.
216 if ( curr_token().cat() == catSpace ||
217 curr_token().cat() == catNewline ||
218 (curr_token().cat() == catComment && curr_token().cs().empty()))
220 if (skip_comments && curr_token().cat() == catComment)
221 cerr << " Ignoring comment: " << curr_token().asInput();
230 void Parser::unskip_spaces(bool skip_comments)
233 if ( curr_token().cat() == catSpace ||
234 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
236 else if (skip_comments && curr_token().cat() == catComment) {
237 // TODO: Get rid of this
238 cerr << "Unignoring comment: " << curr_token().asInput();
247 void Parser::putback()
255 if (pos_ < tokens_.size())
258 return pos_ < tokens_.size();
262 char Parser::getChar()
265 error("The input stream is not well...");
266 return get_token().character();
270 Parser::Arg Parser::getFullArg(char left, char right)
274 // This is needed if a partial file ends with a command without arguments,
277 return make_pair(false, string());
284 return make_pair(false, string());
286 while ((c = getChar()) != right && good()) {
288 if (curr_token().cat() == catComment) {
289 if (!curr_token().cs().empty())
290 cerr << "Ignoring comment: " << curr_token().asInput();
293 result += curr_token().asInput();
296 return make_pair(true, result);
300 string Parser::getArg(char left, char right)
302 return getFullArg(left, right).second;
306 string Parser::getFullOpt()
308 Arg arg = getFullArg('[', ']');
310 return '[' + arg.second + ']';
315 string Parser::getOpt()
317 string const res = getArg('[', ']');
318 return res.empty() ? string() : '[' + res + ']';
322 string Parser::getFullParentheseArg()
324 Arg arg = getFullArg('(', ')');
326 return '(' + arg.second + ')';
331 string const Parser::verbatimEnvironment(string const & name)
337 for (Token t = get_token(); good(); t = get_token()) {
338 if (t.cat() == catBegin) {
340 os << '{' << verbatim_item() << '}';
341 } else if (t.asInput() == "\\begin") {
342 string const env = getArg('{', '}');
343 os << "\\begin{" << env << '}'
344 << verbatimEnvironment(env)
345 << "\\end{" << env << '}';
346 } else if (t.asInput() == "\\end") {
347 string const end = getArg('{', '}');
349 cerr << "\\end{" << end
350 << "} does not match \\begin{" << name
356 cerr << "unexpected end of input" << endl;
361 void Parser::tokenize_one()
368 switch (catcode(c)) {
371 while (is_.get(c) && catcode(c) == catSpace)
373 if (catcode(c) != catSpace)
375 push_back(Token(s, catSpace));
381 docstring s(1, getNewline(is_, c));
382 while (is_.get(c) && catcode(c) == catNewline) {
384 s += getNewline(is_, c);
386 if (catcode(c) != catNewline)
388 push_back(Token(s, catNewline));
393 // We don't treat "%\n" combinations here specially because
394 // we want to preserve them in the preamble
396 while (is_.get(c) && catcode(c) != catNewline)
398 // handle possible DOS line ending
399 if (catcode(c) == catNewline)
400 c = getNewline(is_, c);
401 // Note: The '%' at the beginning and the '\n' at the end
402 // of the comment are not stored.
404 push_back(Token(s, catComment));
411 error("unexpected end of input");
414 if (catcode(c) == catLetter) {
416 while (is_.get(c) && catcode(c) == catLetter)
418 if (catcode(c) != catLetter)
421 push_back(Token(s, catEscape));
427 cerr << "ignoring a char: " << c << "\n";
432 push_back(Token(docstring(1, c), catcode(c)));
434 //cerr << tokens_.back();
438 void Parser::dump() const
440 cerr << "\nTokens: ";
441 for (unsigned i = 0; i < tokens_.size(); ++i) {
446 cerr << " pos: " << pos_ << "\n";
450 void Parser::error(string const & msg)
452 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
458 string Parser::verbatimOption()
461 if (next_token().character() == '[') {
462 Token t = get_token();
463 for (t = get_token(); t.character() != ']' && good(); t = get_token()) {
464 if (t.cat() == catBegin) {
466 res += '{' + verbatim_item() + '}';
475 string Parser::verbatim_item()
480 if (next_token().cat() == catBegin) {
481 Token t = get_token(); // skip brace
483 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
484 if (t.cat() == catBegin) {
486 res += '{' + verbatim_item() + '}';
493 return get_token().asInput();
503 void Parser::setCatCode(char c, CatCode cat)
505 theCatcode[(unsigned char)c] = cat;
509 CatCode Parser::getCatCode(char c) const
511 return theCatcode[(unsigned char)c];