14 using std::istringstream;
21 CatCode theCatcode[256];
23 void skipSpaceTokens(istream & is, char c)
25 // skip trailing spaces
26 while (catcode(c) == catSpace || catcode(c) == catNewline)
29 //cerr << "putting back: " << c << "\n";
36 fill(theCatcode, theCatcode + 256, catOther);
37 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
38 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
40 theCatcode['\\'] = catEscape;
41 theCatcode['{'] = catBegin;
42 theCatcode['}'] = catEnd;
43 theCatcode['$'] = catMath;
44 theCatcode['&'] = catAlign;
45 theCatcode[10] = catNewline;
46 theCatcode['#'] = catParameter;
47 theCatcode['^'] = catSuper;
48 theCatcode['_'] = catSub;
49 theCatcode['
\7f'] = catIgnore;
50 theCatcode[' '] = catSpace;
51 theCatcode['\t'] = catSpace;
52 theCatcode[13] = catIgnore;
53 theCatcode['~'] = catActive;
54 theCatcode['%'] = catComment;
57 theCatcode['@'] = catLetter;
67 mode_type asMode(mode_type oldmode, string const & str)
69 if (str == "mathmode")
71 if (str == "textmode" || str == "forcetext")
77 CatCode catcode(unsigned char c)
88 ostream & operator<<(ostream & os, Token const & t)
91 os << '\\' << t.cs() << ' ';
92 else if (t.cat() == catLetter)
94 else if (t.cat() == catNewline)
95 os << "[\\n," << t.cat() << "]\n";
97 os << '[' << t.character() << ',' << t.cat() << ']';
102 string Token::asString() const
104 return cs_.size() ? cs_ : string(1, char_);
108 string Token::asInput() const
110 return char_ ? string(1, char_) : '\\' + cs_ + ' ';
119 Parser::Parser(istream & is)
120 : lineno_(0), pos_(0)
126 Parser::Parser(string const & s)
127 : lineno_(0), pos_(0)
129 istringstream is(STRCONV(s));
134 void Parser::push_back(Token const & t)
136 tokens_.push_back(t);
140 void Parser::pop_back()
146 Token const & Parser::prev_token() const
148 static const Token dummy;
149 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
153 Token const & Parser::next_token() const
155 static const Token dummy;
156 return good() ? tokens_[pos_] : dummy;
160 Token const & Parser::get_token()
162 static const Token dummy;
163 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
164 return good() ? tokens_[pos_++] : dummy;
168 void Parser::skip_spaces()
171 if (next_token().cat() == catSpace || next_token().cat() == catNewline)
173 else if (next_token().cat() == catComment)
174 while (next_token().cat() != catNewline)
182 void Parser::putback()
188 bool Parser::good() const
190 return pos_ < tokens_.size();
194 char Parser::getChar()
197 error("The input stream is not well...");
198 return tokens_[pos_++].character();
202 string Parser::getArg(char left, char right)
212 while ((c = getChar()) != right && good())
219 string Parser::getOpt()
221 string const res = getArg('[', ']');
222 return res.size() ? '[' + res + ']' : string();
226 void Parser::tokenize(istream & is)
228 static bool init_done = false;
237 //cerr << "reading c: " << c << "\n";
239 switch (catcode(c)) {
243 if (catcode(c) == catNewline) {
246 //} while (catcode(c) == catNewline);
247 push_back(Token("par"));
249 push_back(Token('\n', catNewline));
256 push_back(Token(c, catComment));
257 while (is.get(c) && catcode(c) != catNewline)
258 push_back(Token(c, catLetter));
259 push_back(Token(c, catNewline));
262 if (catcode(c) == catNewline) {
263 push_back(Token("par"));
274 error("unexpected end of input");
277 if (catcode(c) == catLetter) {
279 while (is.get(c) && catcode(c) == catLetter)
281 skipSpaceTokens(is, c);
290 push_back(Token(c, catcode(c)));
292 skipSpaceTokens(is, c);
298 cerr << "ignoring a char: " << int(c) << "\n";
303 push_back(Token(c, catcode(c)));
309 void Parser::dump() const
311 cerr << "\nTokens: ";
312 for (unsigned i = 0; i < tokens_.size(); ++i) {
317 cerr << " pos: " << pos_ << "\n";
321 void Parser::error(string const & msg)
323 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
329 string Parser::verbatimOption()
332 if (next_token().character() == '[') {
333 Token t = get_token();
334 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
335 if (t.cat() == catBegin) {
337 res += '{' + verbatim_item() + '}';
346 string Parser::verbatim_item()
351 if (next_token().cat() == catBegin) {
352 Token t = get_token(); // skip brace
354 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
355 if (t.cat() == catBegin) {
357 res += '{' + verbatim_item() + '}';
364 return get_token().asInput();
368 void Parser::setCatCode(char c, CatCode cat)
374 CatCode Parser::getCatCode(char c) const
376 return theCatcode[c];