14 using std::istringstream;
22 CatCode theCatcode[256];
24 void skipSpaceTokens(istream & is, char c)
26 // skip trailing spaces
27 while (catcode(c) == catSpace || catcode(c) == catNewline)
30 //cerr << "putting back: " << c << "\n";
37 fill(theCatcode, theCatcode + 256, catOther);
38 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
39 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
41 theCatcode['\\'] = catEscape;
42 theCatcode['{'] = catBegin;
43 theCatcode['}'] = catEnd;
44 theCatcode['$'] = catMath;
45 theCatcode['&'] = catAlign;
46 theCatcode[10] = catNewline;
47 theCatcode['#'] = catParameter;
48 theCatcode['^'] = catSuper;
49 theCatcode['_'] = catSub;
50 theCatcode['
\7f'] = catIgnore;
51 theCatcode[' '] = catSpace;
52 theCatcode['\t'] = catSpace;
53 theCatcode[13] = catIgnore;
54 theCatcode['~'] = catActive;
55 theCatcode['%'] = catComment;
58 theCatcode['@'] = catLetter;
68 mode_type asMode(mode_type oldmode, string const & str)
70 if (str == "mathmode")
72 if (str == "textmode" || str == "forcetext")
78 CatCode catcode(unsigned char c)
89 ostream & operator<<(ostream & os, Token const & t)
92 os << '\\' << t.cs() << ' ';
93 else if (t.cat() == catLetter)
95 else if (t.cat() == catNewline)
96 os << "[\\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 return char_ ? string(1, char_) : '\\' + cs_ + ' ';
120 Parser::Parser(istream & is)
121 : lineno_(0), pos_(0)
127 Parser::Parser(string const & s)
128 : lineno_(0), pos_(0)
135 void Parser::push_back(Token const & t)
137 tokens_.push_back(t);
141 void Parser::pop_back()
147 Token const & Parser::prev_token() const
149 static const Token dummy;
150 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
154 Token const & Parser::next_token() const
156 static const Token dummy;
157 return good() ? tokens_[pos_] : dummy;
161 Token const & Parser::get_token()
163 static const Token dummy;
164 //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
165 return good() ? tokens_[pos_++] : dummy;
169 void Parser::skip_spaces()
172 if (next_token().cat() == catSpace || next_token().cat() == catNewline)
174 else if (next_token().cat() == catComment)
175 while (next_token().cat() != catNewline)
183 void Parser::putback()
189 bool Parser::good() const
191 return pos_ < tokens_.size();
195 char Parser::getChar()
198 error("The input stream is not well...");
199 return tokens_[pos_++].character();
203 string Parser::getArg(char left, char right)
213 while ((c = getChar()) != right && good())
220 string Parser::getOpt()
222 string const res = getArg('[', ']');
223 return res.size() ? '[' + res + ']' : string();
227 void Parser::tokenize(istream & is)
229 static bool init_done = false;
238 //cerr << "reading c: " << c << "\n";
240 switch (catcode(c)) {
244 if (catcode(c) == catNewline) {
247 //} while (catcode(c) == catNewline);
248 push_back(Token("par"));
250 push_back(Token('\n', catNewline));
257 push_back(Token(c, catComment));
258 while (is.get(c) && catcode(c) != catNewline)
259 push_back(Token(c, catLetter));
260 push_back(Token(c, catNewline));
263 if (catcode(c) == catNewline) {
264 push_back(Token("par"));
275 error("unexpected end of input");
278 if (catcode(c) == catLetter) {
280 while (is.get(c) && catcode(c) == catLetter)
282 skipSpaceTokens(is, c);
291 push_back(Token(c, catcode(c)));
293 skipSpaceTokens(is, c);
299 cerr << "ignoring a char: " << int(c) << "\n";
304 push_back(Token(c, catcode(c)));
310 void Parser::dump() const
312 cerr << "\nTokens: ";
313 for (unsigned i = 0; i < tokens_.size(); ++i) {
318 cerr << " pos: " << pos_ << "\n";
322 void Parser::error(string const & msg)
324 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
330 string Parser::verbatimOption()
333 if (next_token().character() == '[') {
334 Token t = get_token();
335 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
336 if (t.cat() == catBegin) {
338 res += '{' + verbatim_item() + '}';
347 string Parser::verbatim_item()
352 if (next_token().cat() == catBegin) {
353 Token t = get_token(); // skip brace
355 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
356 if (t.cat() == catBegin) {
358 res += '{' + verbatim_item() + '}';
365 return get_token().asInput();
369 void Parser::setCatCode(char c, CatCode cat)
375 CatCode Parser::getCatCode(char c) const
377 return theCatcode[c];