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;
58 * Translate a line ending to '\n'.
59 * \p c must have catcode catNewline, and it must be the last character read
62 char_type getNewline(idocstream & is, char_type c)
64 // we have to handle 3 different line endings:
71 if (is.get(wc) && wc != '\n') {
81 CatCode catcode(char_type c)
84 return theCatcode[(unsigned char)c];
95 ostream & operator<<(ostream & os, Token const & t)
97 if (t.cat() == catComment)
98 os << '%' << t.cs() << '\n';
99 else if (t.cat() == catSpace)
101 else if (t.cat() == catEscape)
102 os << '\\' << t.cs() << ' ';
103 else if (t.cat() == catLetter)
105 else if (t.cat() == catNewline)
106 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
108 os << '[' << t.cs() << ',' << t.cat() << ']';
113 string Token::asInput() const
115 if (cat_ == catComment)
116 return '%' + cs_ + '\n';
117 if (cat_ == catEscape)
128 Parser::Parser(idocstream & is)
129 : lineno_(0), pos_(0), iss_(0), is_(is), encoding_latex_("utf8")
134 Parser::Parser(string const & s)
135 : lineno_(0), pos_(0),
136 iss_(new idocstringstream(from_utf8(s))), is_(*iss_),
137 encoding_latex_("utf8")
148 void Parser::setEncoding(std::string const & e)
150 Encoding const * enc = encodings.fromLaTeXName(e);
152 cerr << "Unknown encoding " << e << ". Ignoring." << std::endl;
155 //cerr << "setting encoding to " << enc->iconvName() << std::endl;
156 is_ << lyx::setEncoding(enc->iconvName());
161 void Parser::push_back(Token const & t)
163 tokens_.push_back(t);
167 // We return a copy here because the tokens_ vector may get reallocated
168 Token const Parser::prev_token() const
170 static const Token dummy;
171 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
175 // We return a copy here because the tokens_ vector may get reallocated
176 Token const Parser::curr_token() const
178 static const Token dummy;
179 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
183 // We return a copy here because the tokens_ vector may get reallocated
184 Token const Parser::next_token()
186 static const Token dummy;
187 return good() ? tokens_[pos_] : dummy;
191 // We return a copy here because the tokens_ vector may get reallocated
192 Token const Parser::get_token()
194 static const Token dummy;
195 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
196 return good() ? tokens_[pos_++] : dummy;
200 bool Parser::isParagraph()
202 // A new paragraph in TeX ist started
203 // - either by a newline, following any amount of whitespace
204 // characters (including zero), and another newline
205 // - or the token \par
206 if (curr_token().cat() == catNewline &&
207 (curr_token().cs().size() > 1 ||
208 (next_token().cat() == catSpace &&
209 pos_ < tokens_.size() - 1 &&
210 tokens_[pos_ + 1].cat() == catNewline)))
212 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
218 void Parser::skip_spaces(bool skip_comments)
220 // We just silently return if we have no more tokens.
221 // skip_spaces() should be callable at any time,
222 // the caller must check p::good() anyway.
229 if ( curr_token().cat() == catSpace ||
230 curr_token().cat() == catNewline ||
231 (curr_token().cat() == catComment && curr_token().cs().empty()))
233 if (skip_comments && curr_token().cat() == catComment)
234 cerr << " Ignoring comment: " << curr_token().asInput();
243 void Parser::unskip_spaces(bool skip_comments)
246 if ( curr_token().cat() == catSpace ||
247 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
249 else if (skip_comments && curr_token().cat() == catComment) {
250 // TODO: Get rid of this
251 cerr << "Unignoring comment: " << curr_token().asInput();
260 void Parser::putback()
268 if (pos_ < tokens_.size())
271 return pos_ < tokens_.size();
275 char Parser::getChar()
278 error("The input stream is not well...");
279 return get_token().character();
283 Parser::Arg Parser::getFullArg(char left, char right)
287 // This is needed if a partial file ends with a command without arguments,
290 return make_pair(false, string());
297 return make_pair(false, string());
299 while ((c = getChar()) != right && good()) {
301 if (curr_token().cat() == catComment) {
302 if (!curr_token().cs().empty())
303 cerr << "Ignoring comment: " << curr_token().asInput();
306 result += curr_token().asInput();
309 return make_pair(true, result);
313 string Parser::getArg(char left, char right)
315 return getFullArg(left, right).second;
319 string Parser::getFullOpt()
321 Arg arg = getFullArg('[', ']');
323 return '[' + arg.second + ']';
328 string Parser::getOpt()
330 string const res = getArg('[', ']');
331 return res.empty() ? string() : '[' + res + ']';
335 string Parser::getOptContent()
336 // the same as getOpt but without the brackets
338 string const res = getArg('[', ']');
339 return res.empty() ? string() : res;
343 string Parser::getFullParentheseArg()
345 Arg arg = getFullArg('(', ')');
347 return '(' + arg.second + ')';
352 string const Parser::verbatimEnvironment(string const & name)
358 for (Token t = get_token(); good(); t = get_token()) {
359 if (t.cat() == catBegin) {
361 os << '{' << verbatim_item() << '}';
362 } else if (t.asInput() == "\\begin") {
363 string const env = getArg('{', '}');
364 os << "\\begin{" << env << '}'
365 << verbatimEnvironment(env)
366 << "\\end{" << env << '}';
367 } else if (t.asInput() == "\\end") {
368 string const end = getArg('{', '}');
370 cerr << "\\end{" << end
371 << "} does not match \\begin{" << name
377 cerr << "unexpected end of input" << endl;
382 void Parser::tokenize_one()
389 switch (catcode(c)) {
392 while (is_.get(c) && catcode(c) == catSpace)
394 if (catcode(c) != catSpace)
396 push_back(Token(s, catSpace));
402 docstring s(1, getNewline(is_, c));
403 while (is_.get(c) && catcode(c) == catNewline) {
405 s += getNewline(is_, c);
407 if (catcode(c) != catNewline)
409 push_back(Token(s, catNewline));
414 // We don't treat "%\n" combinations here specially because
415 // we want to preserve them in the preamble
417 while (is_.get(c) && catcode(c) != catNewline)
419 // handle possible DOS line ending
420 if (catcode(c) == catNewline)
421 c = getNewline(is_, c);
422 // Note: The '%' at the beginning and the '\n' at the end
423 // of the comment are not stored.
425 push_back(Token(s, catComment));
432 error("unexpected end of input");
435 if (catcode(c) == catLetter) {
437 while (is_.get(c) && catcode(c) == catLetter)
439 if (catcode(c) != catLetter)
442 push_back(Token(s, catEscape));
448 cerr << "ignoring a char: " << c << "\n";
453 push_back(Token(docstring(1, c), catcode(c)));
455 //cerr << tokens_.back();
459 void Parser::dump() const
461 cerr << "\nTokens: ";
462 for (unsigned i = 0; i < tokens_.size(); ++i) {
467 cerr << " pos: " << pos_ << "\n";
471 void Parser::error(string const & msg)
473 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
479 string Parser::verbatimOption()
482 if (next_token().character() == '[') {
483 Token t = get_token();
484 for (t = get_token(); t.character() != ']' && good(); t = get_token()) {
485 if (t.cat() == catBegin) {
487 res += '{' + verbatim_item() + '}';
496 string Parser::verbatim_item()
501 if (next_token().cat() == catBegin) {
502 Token t = get_token(); // skip brace
504 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
505 if (t.cat() == catBegin) {
507 res += '{' + verbatim_item() + '}';
514 return get_token().asInput();
524 void Parser::setCatCode(char c, CatCode cat)
526 theCatcode[(unsigned char)c] = cat;
530 CatCode Parser::getCatCode(char c) const
532 return theCatcode[(unsigned char)c];