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.
15 #include "support/textutils.h"
25 CatCode theCatcode[256];
29 static bool init_done = false;
34 fill(theCatcode, theCatcode + 256, catOther);
35 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
36 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
38 theCatcode[int('\\')] = catEscape;
39 theCatcode[int('{')] = catBegin;
40 theCatcode[int('}')] = catEnd;
41 theCatcode[int('$')] = catMath;
42 theCatcode[int('&')] = catAlign;
43 theCatcode[int('\n')] = catNewline;
44 theCatcode[int('#')] = catParameter;
45 theCatcode[int('^')] = catSuper;
46 theCatcode[int('_')] = catSub;
47 theCatcode[0x7f] = catIgnore;
48 theCatcode[int(' ')] = catSpace;
49 theCatcode[int('\t')] = catSpace;
50 theCatcode[int('\r')] = catNewline;
51 theCatcode[int('~')] = catActive;
52 theCatcode[int('%')] = catComment;
55 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_type getNewline(idocstream & is, char_type c)
65 // we have to handle 3 different line endings:
72 if (is.get(wc) && wc != '\n') {
82 CatCode catcode(char_type c)
85 return theCatcode[(unsigned char)c];
96 ostream & operator<<(ostream & os, Token const & t)
98 if (t.cat() == catComment)
99 os << '%' << t.cs() << '\n';
100 else if (t.cat() == catSpace)
102 else if (t.cat() == catEscape)
103 os << '\\' << t.cs() << ' ';
104 else if (t.cat() == catLetter)
106 else if (t.cat() == catNewline)
107 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
109 os << '[' << t.cs() << ',' << t.cat() << ']';
114 string Token::asInput() const
116 if (cat_ == catComment)
117 return '%' + cs_ + '\n';
118 if (cat_ == catEscape)
124 bool Token::isAlnumASCII() const
126 return cat_ == catLetter ||
127 (cat_ == catOther && cs_.length() == 1 && isDigitASCII(cs_[0]));
136 Parser::Parser(idocstream & is)
137 : lineno_(0), pos_(0), iss_(0), is_(is), encoding_latex_("utf8")
142 Parser::Parser(string const & s)
143 : lineno_(0), pos_(0),
144 iss_(new idocstringstream(from_utf8(s))), is_(*iss_),
145 encoding_latex_("utf8")
156 void Parser::setEncoding(std::string const & e)
158 Encoding const * enc = encodings.fromLaTeXName(e);
160 cerr << "Unknown encoding " << e << ". Ignoring." << std::endl;
163 //cerr << "setting encoding to " << enc->iconvName() << std::endl;
164 is_ << lyx::setEncoding(enc->iconvName());
169 void Parser::push_back(Token const & t)
171 tokens_.push_back(t);
175 // We return a copy here because the tokens_ vector may get reallocated
176 Token const Parser::prev_token() const
178 static const Token dummy;
179 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
183 // We return a copy here because the tokens_ vector may get reallocated
184 Token const Parser::curr_token() const
186 static const Token dummy;
187 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
191 // We return a copy here because the tokens_ vector may get reallocated
192 Token const Parser::next_token()
194 static const Token dummy;
195 return good() ? tokens_[pos_] : dummy;
199 // We return a copy here because the tokens_ vector may get reallocated
200 Token const Parser::get_token()
202 static const Token dummy;
203 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
204 return good() ? tokens_[pos_++] : dummy;
208 bool Parser::isParagraph()
210 // A new paragraph in TeX ist started
211 // - either by a newline, following any amount of whitespace
212 // characters (including zero), and another newline
213 // - or the token \par
214 if (curr_token().cat() == catNewline &&
215 (curr_token().cs().size() > 1 ||
216 (next_token().cat() == catSpace &&
217 pos_ < tokens_.size() - 1 &&
218 tokens_[pos_ + 1].cat() == catNewline)))
220 if (curr_token().cat() == catEscape && curr_token().cs() == "par")
226 bool Parser::skip_spaces(bool skip_comments)
228 // We just silently return if we have no more tokens.
229 // skip_spaces() should be callable at any time,
230 // the caller must check p::good() anyway.
231 bool skipped = false;
238 if (curr_token().cat() == catSpace ||
239 curr_token().cat() == catNewline) {
243 if ((curr_token().cat() == catComment && curr_token().cs().empty()))
245 if (skip_comments && curr_token().cat() == catComment)
246 cerr << " Ignoring comment: " << curr_token().asInput();
256 void Parser::unskip_spaces(bool skip_comments)
259 if ( curr_token().cat() == catSpace ||
260 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
262 else if (skip_comments && curr_token().cat() == catComment) {
263 // TODO: Get rid of this
264 cerr << "Unignoring comment: " << curr_token().asInput();
273 void Parser::putback()
281 if (pos_ < tokens_.size())
284 return pos_ < tokens_.size();
288 char Parser::getChar()
291 error("The input stream is not well...");
292 return get_token().character();
296 bool Parser::hasOpt()
298 // An optional argument can occur in any of the following forms:
306 // remember current position
307 unsigned int oldpos = pos_;
308 // skip spaces and comments
315 if (curr_token().cat() == catSpace ||
316 curr_token().cat() == catNewline ||
317 curr_token().cat() == catComment)
322 bool const retval = (next_token().asInput() == "[");
328 Parser::Arg Parser::getFullArg(char left, char right)
332 // This is needed if a partial file ends with a command without arguments,
335 return make_pair(false, string());
342 return make_pair(false, string());
344 while ((c = getChar()) != right && good()) {
346 if (curr_token().cat() == catComment) {
347 if (!curr_token().cs().empty())
348 cerr << "Ignoring comment: " << curr_token().asInput();
351 result += curr_token().asInput();
354 return make_pair(true, result);
358 string Parser::getArg(char left, char right)
360 return getFullArg(left, right).second;
364 string Parser::getFullOpt()
366 Arg arg = getFullArg('[', ']');
368 return '[' + arg.second + ']';
373 string Parser::getOpt(bool keepws)
375 string const res = getArg('[', ']');
381 return '[' + res + ']';
385 string Parser::getOptContent()
386 // the same as getOpt but without the brackets
388 string const res = getArg('[', ']');
389 return res.empty() ? string() : res;
393 string Parser::getFullParentheseArg()
395 Arg arg = getFullArg('(', ')');
397 return '(' + arg.second + ')';
402 string const Parser::verbatimEnvironment(string const & name)
408 for (Token t = get_token(); good(); t = get_token()) {
409 if (t.cat() == catBegin) {
411 os << '{' << verbatim_item() << '}';
412 } else if (t.asInput() == "\\begin") {
413 string const env = getArg('{', '}');
414 os << "\\begin{" << env << '}'
415 << verbatimEnvironment(env)
416 << "\\end{" << env << '}';
417 } else if (t.asInput() == "\\end") {
418 string const end = getArg('{', '}');
420 cerr << "\\end{" << end
421 << "} does not match \\begin{" << name
427 cerr << "unexpected end of input" << endl;
432 void Parser::tokenize_one()
439 switch (catcode(c)) {
442 while (is_.get(c) && catcode(c) == catSpace)
444 if (catcode(c) != catSpace)
446 push_back(Token(s, catSpace));
452 docstring s(1, getNewline(is_, c));
453 while (is_.get(c) && catcode(c) == catNewline) {
455 s += getNewline(is_, c);
457 if (catcode(c) != catNewline)
459 push_back(Token(s, catNewline));
464 // We don't treat "%\n" combinations here specially because
465 // we want to preserve them in the preamble
467 while (is_.get(c) && catcode(c) != catNewline)
469 // handle possible DOS line ending
470 if (catcode(c) == catNewline)
471 c = getNewline(is_, c);
472 // Note: The '%' at the beginning and the '\n' at the end
473 // of the comment are not stored.
475 push_back(Token(s, catComment));
482 error("unexpected end of input");
485 if (catcode(c) == catLetter) {
487 while (is_.get(c) && catcode(c) == catLetter)
489 if (catcode(c) != catLetter)
492 push_back(Token(s, catEscape));
498 cerr << "ignoring a char: " << c << "\n";
503 push_back(Token(docstring(1, c), catcode(c)));
505 //cerr << tokens_.back();
509 void Parser::dump() const
511 cerr << "\nTokens: ";
512 for (unsigned i = 0; i < tokens_.size(); ++i) {
517 cerr << " pos: " << pos_ << "\n";
521 void Parser::error(string const & msg)
523 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
529 string Parser::verbatimOption()
532 if (next_token().character() == '[') {
533 Token t = get_token();
534 for (t = get_token(); t.character() != ']' && good(); t = get_token()) {
535 if (t.cat() == catBegin) {
537 res += '{' + verbatim_item() + '}';
546 string Parser::verbatim_item()
551 if (next_token().cat() == catBegin) {
552 Token t = get_token(); // skip brace
554 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
555 if (t.cat() == catBegin) {
557 res += '{' + verbatim_item() + '}';
564 return get_token().asInput();
574 void Parser::setCatCode(char c, CatCode cat)
576 theCatcode[(unsigned char)c] = cat;
580 CatCode Parser::getCatCode(char c) const
582 return theCatcode[(unsigned char)c];