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.
13 #include "texparser.h"
22 using std::istringstream;
29 CatCode theCatcode[256];
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[10] = 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[13] = catIgnore;
50 theCatcode[int('~')] = catActive;
51 theCatcode[int('%')] = catComment;
54 theCatcode[int('@')] = catLetter;
64 mode_type asMode(mode_type oldmode, string const & str)
66 if (str == "mathmode")
68 if (str == "textmode" || str == "forcetext")
74 CatCode catcode(unsigned char c)
85 ostream & operator<<(ostream & os, Token const & t)
87 if (t.cat() == catComment)
88 os << '%' << t.cs() << '\n';
89 else if (t.cat() == catSpace)
91 else if (t.cat() == catEscape)
92 os << '\\' << t.cs() << ' ';
93 else if (t.cat() == catLetter)
95 else if (t.cat() == catNewline)
96 os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n";
98 os << '[' << t.character() << ',' << t.cat() << ']';
103 string Token::asString() const
105 return cs_.size() ? cs_ : string(1, char_);
109 string Token::asInput() const
111 if (cat_ == catComment)
112 return '%' + cs_ + '\n';
113 if (cat_ == catSpace || cat_ == catNewline)
115 return char_ ? string(1, char_) : '\\' + cs_;
124 Parser::Parser(istream & is)
125 : lineno_(0), pos_(0)
131 Parser::Parser(string const & s)
132 : lineno_(0), pos_(0)
139 void Parser::push_back(Token const & t)
141 tokens_.push_back(t);
145 void Parser::pop_back()
151 Token const & Parser::prev_token() const
153 static const Token dummy;
154 return pos_ > 1 ? tokens_[pos_ - 2] : dummy;
158 Token const & Parser::curr_token() const
160 static const Token dummy;
161 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
165 Token const & Parser::next_token() const
167 static const Token dummy;
168 return good() ? tokens_[pos_] : dummy;
172 Token const & Parser::get_token()
174 static const Token dummy;
175 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
176 return good() ? tokens_[pos_++] : dummy;
180 void Parser::skip_spaces(bool skip_comments)
182 // We just silently return if we have no more tokens.
183 // skip_spaces() should be callable at any time,
184 // the caller must check p::good() anyway.
186 if ( next_token().cat() == catSpace ||
187 (next_token().cat() == catNewline && next_token().cs().size() == 1) ||
188 next_token().cat() == catComment && next_token().cs().empty())
190 else if (skip_comments && next_token().cat() == catComment)
191 cerr << " Ignoring comment: " << get_token().asInput();
198 void Parser::unskip_spaces(bool skip_comments)
201 if ( curr_token().cat() == catSpace ||
202 (curr_token().cat() == catNewline && curr_token().cs().size() == 1))
204 else if (skip_comments && curr_token().cat() == catComment) {
205 // TODO: Get rid of this
206 cerr << "Unignoring comment: " << curr_token().asInput();
215 void Parser::putback()
221 bool Parser::good() const
223 return pos_ < tokens_.size();
227 char Parser::getChar()
230 error("The input stream is not well...");
231 return tokens_[pos_++].character();
235 string Parser::getArg(char left, char right)
239 // This is needed if a partial file ends with a command without arguments,
250 while ((c = getChar()) != right && good()) {
252 if (curr_token().cat() == catComment) {
253 if (!curr_token().cs().empty())
254 cerr << "Ignoring comment: " << curr_token().asInput();
256 else if (curr_token().cat() == catSpace || curr_token().cat() == catNewline)
257 result += curr_token().cs();
266 string Parser::getOpt()
268 string const res = getArg('[', ']');
269 return res.size() ? '[' + res + ']' : string();
273 void Parser::tokenize(istream & is)
275 static bool init_done = false;
284 //cerr << "reading c: " << c << "\n";
286 switch (catcode(c)) {
289 while (is.get(c) && catcode(c) == catSpace)
291 if (catcode(c) != catSpace)
293 push_back(Token(s, catSpace));
300 while (is.get(c) && catcode(c) == catNewline) {
304 if (catcode(c) != catNewline)
306 push_back(Token(s, catNewline));
311 // We don't treat "%\n" combinations here specially because
312 // we want to preserve them in the preamble
314 while (is.get(c) && catcode(c) != catNewline)
316 // Note: The '%' at the beginning and the '\n' at the end
317 // of the comment are not stored.
319 push_back(Token(s, catComment));
326 error("unexpected end of input");
329 if (catcode(c) == catLetter) {
331 while (is.get(c) && catcode(c) == catLetter)
333 if (catcode(c) != catLetter)
336 push_back(Token(s, catEscape));
343 cerr << "ignoring a char: " << int(c) << "\n";
348 push_back(Token(c, catcode(c)));
354 void Parser::dump() const
356 cerr << "\nTokens: ";
357 for (unsigned i = 0; i < tokens_.size(); ++i) {
362 cerr << " pos: " << pos_ << "\n";
366 void Parser::error(string const & msg)
368 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
374 string Parser::verbatimOption()
377 if (next_token().character() == '[') {
378 Token t = get_token();
379 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
380 if (t.cat() == catBegin) {
382 res += '{' + verbatim_item() + '}';
391 string Parser::verbatim_item()
396 if (next_token().cat() == catBegin) {
397 Token t = get_token(); // skip brace
399 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
400 if (t.cat() == catBegin) {
402 res += '{' + verbatim_item() + '}';
409 return get_token().asInput();
419 void Parser::setCatCode(char c, CatCode cat)
421 theCatcode[(unsigned char)c] = cat;
425 CatCode Parser::getCatCode(char c) const
427 return theCatcode[(unsigned char)c];