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 getNewline(idocstream & is, char 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::asString() const
119 string Token::asInput() const
121 if (cat_ == catComment)
122 return '%' + cs_ + '\n';
123 if (cat_ == catEscape)
134 Parser::Parser(idocstream & is)
135 : lineno_(0), pos_(0), iss_(0), is_(is)
140 Parser::Parser(string const & s)
141 : lineno_(0), pos_(0),
142 iss_(new idocstringstream(from_utf8(s))), is_(*iss_)
153 void Parser::setEncoding(std::string const & e)
155 Encoding const * enc = encodings.fromLaTeXName(e);
156 cerr << "setting encoding to " << enc->iconvName();
157 is_ << lyx::setEncoding(enc->iconvName());
161 void Parser::push_back(Token const & t)
163 tokens_.push_back(t);
167 Token const & Parser::prev_token() const
169 static const Token dummy;
170 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
174 Token const & Parser::curr_token() const
176 static const Token dummy;
177 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
181 Token const & Parser::next_token()
183 static const Token dummy;
184 return good() ? tokens_[pos_] : dummy;
188 Token const & Parser::get_token()
190 static const Token dummy;
191 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
192 return good() ? tokens_[pos_++] : dummy;
196 bool Parser::isParagraph()
198 // A new paragraph in TeX ist started
199 // - either by a newline, following any amount of whitespace
200 // characters (including zero), and another newline
201 // - or the token \par
202 if (curr_token().cat() == catNewline &&
203 (curr_token().cs().size() > 1 ||
204 (next_token().cat() == catSpace &&
205 pos_ < tokens_.size() - 1 &&
206 tokens_[pos_ + 1].cat() == catNewline)))
208 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
214 void Parser::skip_spaces(bool skip_comments)
216 // We just silently return if we have no more tokens.
217 // skip_spaces() should be callable at any time,
218 // the caller must check p::good() anyway.
225 if ( curr_token().cat() == catSpace ||
226 curr_token().cat() == catNewline ||
227 (curr_token().cat() == catComment && curr_token().cs().empty()))
229 if (skip_comments && curr_token().cat() == catComment)
230 cerr << " Ignoring comment: " << curr_token().asInput();
239 void Parser::unskip_spaces(bool skip_comments)
242 if ( curr_token().cat() == catSpace ||
243 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
245 else if (skip_comments && curr_token().cat() == catComment) {
246 // TODO: Get rid of this
247 cerr << "Unignoring comment: " << curr_token().asInput();
256 void Parser::putback()
264 if (pos_ < tokens_.size())
267 return pos_ < tokens_.size();
271 char Parser::getChar()
274 error("The input stream is not well...");
275 return get_token().character();
279 Parser::Arg Parser::getFullArg(char left, char right)
283 // This is needed if a partial file ends with a command without arguments,
286 return make_pair(false, string());
293 return make_pair(false, string());
295 while ((c = getChar()) != right && good()) {
297 if (curr_token().cat() == catComment) {
298 if (!curr_token().cs().empty())
299 cerr << "Ignoring comment: " << curr_token().asInput();
302 result += curr_token().asInput();
305 return make_pair(true, result);
309 string Parser::getArg(char left, char right)
311 return getFullArg(left, right).second;
315 string Parser::getFullOpt()
317 Arg arg = getFullArg('[', ']');
319 return '[' + arg.second + ']';
324 string Parser::getOpt()
326 string const res = getArg('[', ']');
327 return res.empty() ? string() : '[' + res + ']';
331 string Parser::getFullParentheseArg()
333 Arg arg = getFullArg('(', ')');
335 return '(' + arg.second + ')';
340 string const Parser::verbatimEnvironment(string const & name)
346 for (Token t = get_token(); good(); t = get_token()) {
347 if (t.cat() == catBegin) {
349 os << '{' << verbatim_item() << '}';
350 } else if (t.asInput() == "\\begin") {
351 string const env = getArg('{', '}');
352 os << "\\begin{" << env << '}'
353 << verbatimEnvironment(env)
354 << "\\end{" << env << '}';
355 } else if (t.asInput() == "\\end") {
356 string const end = getArg('{', '}');
358 cerr << "\\end{" << end
359 << "} does not match \\begin{" << name
365 cerr << "unexpected end of input" << endl;
370 void Parser::tokenize_one()
377 switch (catcode(c)) {
380 while (is_.get(c) && catcode(c) == catSpace)
382 if (catcode(c) != catSpace)
384 push_back(Token(s, catSpace));
390 docstring s(1, getNewline(is_, c));
391 while (is_.get(c) && catcode(c) == catNewline) {
393 s += getNewline(is_, c);
395 if (catcode(c) != catNewline)
397 push_back(Token(s, catNewline));
402 // We don't treat "%\n" combinations here specially because
403 // we want to preserve them in the preamble
405 while (is_.get(c) && catcode(c) != catNewline)
407 // handle possible DOS line ending
408 if (catcode(c) == catNewline)
409 c = getNewline(is_, c);
410 // Note: The '%' at the beginning and the '\n' at the end
411 // of the comment are not stored.
413 push_back(Token(s, catComment));
420 error("unexpected end of input");
423 if (catcode(c) == catLetter) {
425 while (is_.get(c) && catcode(c) == catLetter)
427 if (catcode(c) != catLetter)
430 push_back(Token(s, catEscape));
436 cerr << "ignoring a char: " << c << "\n";
441 push_back(Token(docstring(1, c), catcode(c)));
443 //cerr << tokens_.back();
447 void Parser::dump() const
449 cerr << "\nTokens: ";
450 for (unsigned i = 0; i < tokens_.size(); ++i) {
455 cerr << " pos: " << pos_ << "\n";
459 void Parser::error(string const & msg)
461 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
467 string Parser::verbatimOption()
470 if (next_token().character() == '[') {
471 Token t = get_token();
472 for (t = get_token(); t.character() != ']' && good(); t = get_token()) {
473 if (t.cat() == catBegin) {
475 res += '{' + verbatim_item() + '}';
484 string Parser::verbatim_item()
489 if (next_token().cat() == catBegin) {
490 Token t = get_token(); // skip brace
492 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
493 if (t.cat() == catBegin) {
495 res += '{' + verbatim_item() + '}';
502 return get_token().asInput();
512 void Parser::setCatCode(char c, CatCode cat)
514 theCatcode[(unsigned char)c] = cat;
518 CatCode Parser::getCatCode(char c) const
520 return theCatcode[(unsigned char)c];