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 bool 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.
223 bool skipped = false;
230 if (curr_token().cat() == catSpace ||
231 curr_token().cat() == catNewline) {
235 if ((curr_token().cat() == catComment && curr_token().cs().empty()))
237 if (skip_comments && curr_token().cat() == catComment)
238 cerr << " Ignoring comment: " << curr_token().asInput();
248 void Parser::unskip_spaces(bool skip_comments)
251 if ( curr_token().cat() == catSpace ||
252 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
254 else if (skip_comments && curr_token().cat() == catComment) {
255 // TODO: Get rid of this
256 cerr << "Unignoring comment: " << curr_token().asInput();
265 void Parser::putback()
273 if (pos_ < tokens_.size())
276 return pos_ < tokens_.size();
280 char Parser::getChar()
283 error("The input stream is not well...");
284 return get_token().character();
288 Parser::Arg Parser::getFullArg(char left, char right)
292 // This is needed if a partial file ends with a command without arguments,
295 return make_pair(false, string());
302 return make_pair(false, string());
304 while ((c = getChar()) != right && good()) {
306 if (curr_token().cat() == catComment) {
307 if (!curr_token().cs().empty())
308 cerr << "Ignoring comment: " << curr_token().asInput();
311 result += curr_token().asInput();
314 return make_pair(true, result);
318 string Parser::getArg(char left, char right)
320 return getFullArg(left, right).second;
324 string Parser::getFullOpt()
326 Arg arg = getFullArg('[', ']');
328 return '[' + arg.second + ']';
333 string Parser::getOpt(bool keepws)
335 string const res = getArg('[', ']');
341 return '[' + res + ']';
345 string Parser::getOptContent()
346 // the same as getOpt but without the brackets
348 string const res = getArg('[', ']');
349 return res.empty() ? string() : res;
353 string Parser::getFullParentheseArg()
355 Arg arg = getFullArg('(', ')');
357 return '(' + arg.second + ')';
362 string const Parser::verbatimEnvironment(string const & name)
368 for (Token t = get_token(); good(); t = get_token()) {
369 if (t.cat() == catBegin) {
371 os << '{' << verbatim_item() << '}';
372 } else if (t.asInput() == "\\begin") {
373 string const env = getArg('{', '}');
374 os << "\\begin{" << env << '}'
375 << verbatimEnvironment(env)
376 << "\\end{" << env << '}';
377 } else if (t.asInput() == "\\end") {
378 string const end = getArg('{', '}');
380 cerr << "\\end{" << end
381 << "} does not match \\begin{" << name
387 cerr << "unexpected end of input" << endl;
392 void Parser::tokenize_one()
399 switch (catcode(c)) {
402 while (is_.get(c) && catcode(c) == catSpace)
404 if (catcode(c) != catSpace)
406 push_back(Token(s, catSpace));
412 docstring s(1, getNewline(is_, c));
413 while (is_.get(c) && catcode(c) == catNewline) {
415 s += getNewline(is_, c);
417 if (catcode(c) != catNewline)
419 push_back(Token(s, catNewline));
424 // We don't treat "%\n" combinations here specially because
425 // we want to preserve them in the preamble
427 while (is_.get(c) && catcode(c) != catNewline)
429 // handle possible DOS line ending
430 if (catcode(c) == catNewline)
431 c = getNewline(is_, c);
432 // Note: The '%' at the beginning and the '\n' at the end
433 // of the comment are not stored.
435 push_back(Token(s, catComment));
442 error("unexpected end of input");
445 if (catcode(c) == catLetter) {
447 while (is_.get(c) && catcode(c) == catLetter)
449 if (catcode(c) != catLetter)
452 push_back(Token(s, catEscape));
458 cerr << "ignoring a char: " << c << "\n";
463 push_back(Token(docstring(1, c), catcode(c)));
465 //cerr << tokens_.back();
469 void Parser::dump() const
471 cerr << "\nTokens: ";
472 for (unsigned i = 0; i < tokens_.size(); ++i) {
477 cerr << " pos: " << pos_ << "\n";
481 void Parser::error(string const & msg)
483 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
489 string Parser::verbatimOption()
492 if (next_token().character() == '[') {
493 Token t = get_token();
494 for (t = get_token(); t.character() != ']' && good(); t = get_token()) {
495 if (t.cat() == catBegin) {
497 res += '{' + verbatim_item() + '}';
506 string Parser::verbatim_item()
511 if (next_token().cat() == catBegin) {
512 Token t = get_token(); // skip brace
514 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
515 if (t.cat() == catBegin) {
517 res += '{' + verbatim_item() + '}';
524 return get_token().asInput();
534 void Parser::setCatCode(char c, CatCode cat)
536 theCatcode[(unsigned char)c] = cat;
540 CatCode Parser::getCatCode(char c) const
542 return theCatcode[(unsigned char)c];