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::skipSpaces()
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 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));
258 while (is.get(c) && catcode(c) != catNewline)
268 error("unexpected end of input");
271 if (catcode(c) == catLetter) {
273 while (is.get(c) && catcode(c) == catLetter)
275 skipSpaceTokens(is, c);
284 push_back(Token(c, catcode(c)));
286 skipSpaceTokens(is, c);
292 cerr << "ignoring a char: " << int(c) << "\n";
297 push_back(Token(c, catcode(c)));
303 void Parser::dump() const
305 cerr << "\nTokens: ";
306 for (unsigned i = 0; i < tokens_.size(); ++i) {
311 cerr << " pos: " << pos_ << "\n";
315 void Parser::error(string const & msg)
317 cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
323 string Parser::verbatimOption()
326 if (next_token().character() == '[') {
327 Token t = get_token();
328 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
329 if (t.cat() == catBegin) {
331 res += '{' + verbatim_item() + '}';
340 string Parser::verbatim_item()
345 if (next_token().cat() == catBegin) {
346 Token t = get_token(); // skip brace
348 for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
349 if (t.cat() == catBegin) {
351 res += '{' + verbatim_item() + '}';
358 return get_token().asInput();
362 void Parser::setCatCode(char c, CatCode cat)
368 CatCode Parser::getCatCode(char c) const
370 return theCatcode[c];