2003-02-12 08:36:49 +00:00
|
|
|
|
|
2003-04-17 06:22:07 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2003-02-12 08:36:49 +00:00
|
|
|
|
#include "texparser.h"
|
2003-04-17 06:22:07 +00:00
|
|
|
|
|
2003-02-12 21:07:47 +00:00
|
|
|
|
#include <iostream>
|
2003-06-30 11:36:08 +00:00
|
|
|
|
#include <sstream>
|
2003-02-12 07:53:03 +00:00
|
|
|
|
|
|
|
|
|
using std::cerr;
|
|
|
|
|
using std::endl;
|
|
|
|
|
using std::fill;
|
|
|
|
|
using std::ios;
|
|
|
|
|
using std::istream;
|
|
|
|
|
using std::istringstream;
|
|
|
|
|
using std::ostream;
|
2003-06-30 11:36:08 +00:00
|
|
|
|
using std::string;
|
2003-02-12 08:36:49 +00:00
|
|
|
|
using std::vector;
|
2003-02-12 07:53:03 +00:00
|
|
|
|
|
|
|
|
|
|
2003-02-12 11:09:22 +00:00
|
|
|
|
namespace {
|
2003-02-12 07:53:03 +00:00
|
|
|
|
|
|
|
|
|
CatCode theCatcode[256];
|
|
|
|
|
|
2003-02-12 11:09:22 +00:00
|
|
|
|
void skipSpaceTokens(istream & is, char c)
|
2003-02-12 07:53:03 +00:00
|
|
|
|
{
|
2003-02-12 11:09:22 +00:00
|
|
|
|
// skip trailing spaces
|
|
|
|
|
while (catcode(c) == catSpace || catcode(c) == catNewline)
|
|
|
|
|
if (!is.get(c))
|
|
|
|
|
break;
|
|
|
|
|
//cerr << "putting back: " << c << "\n";
|
|
|
|
|
is.putback(c);
|
2003-02-12 07:53:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void catInit()
|
|
|
|
|
{
|
|
|
|
|
fill(theCatcode, theCatcode + 256, catOther);
|
|
|
|
|
fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
|
|
|
|
|
fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
|
|
|
|
|
|
|
|
|
|
theCatcode['\\'] = catEscape;
|
|
|
|
|
theCatcode['{'] = catBegin;
|
|
|
|
|
theCatcode['}'] = catEnd;
|
2003-02-12 08:36:49 +00:00
|
|
|
|
theCatcode['$'] = catMath;
|
2003-02-12 07:53:03 +00:00
|
|
|
|
theCatcode['&'] = catAlign;
|
2003-02-28 13:37:43 +00:00
|
|
|
|
theCatcode[10] = catNewline;
|
2003-02-12 07:53:03 +00:00
|
|
|
|
theCatcode['#'] = catParameter;
|
|
|
|
|
theCatcode['^'] = catSuper;
|
|
|
|
|
theCatcode['_'] = catSub;
|
|
|
|
|
theCatcode[''] = catIgnore;
|
|
|
|
|
theCatcode[' '] = catSpace;
|
|
|
|
|
theCatcode['\t'] = catSpace;
|
2003-02-28 13:37:43 +00:00
|
|
|
|
theCatcode[13] = catIgnore;
|
2003-02-12 07:53:03 +00:00
|
|
|
|
theCatcode['~'] = catActive;
|
|
|
|
|
theCatcode['%'] = catComment;
|
2003-03-03 17:49:26 +00:00
|
|
|
|
|
|
|
|
|
// This is wrong!
|
|
|
|
|
theCatcode['@'] = catLetter;
|
2003-02-12 07:53:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-02-12 11:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// catcodes
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
mode_type asMode(mode_type oldmode, string const & str)
|
|
|
|
|
{
|
|
|
|
|
if (str == "mathmode")
|
|
|
|
|
return MATH_MODE;
|
|
|
|
|
if (str == "textmode" || str == "forcetext")
|
|
|
|
|
return TEXT_MODE;
|
|
|
|
|
return oldmode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CatCode catcode(unsigned char c)
|
|
|
|
|
{
|
|
|
|
|
return theCatcode[c];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-12 07:53:03 +00:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Token
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
ostream & operator<<(ostream & os, Token const & t)
|
|
|
|
|
{
|
|
|
|
|
if (t.cs().size())
|
2003-03-06 10:39:54 +00:00
|
|
|
|
os << '\\' << t.cs() << ' ';
|
|
|
|
|
else if (t.cat() == catLetter)
|
|
|
|
|
os << t.character();
|
2003-04-23 15:14:43 +00:00
|
|
|
|
else if (t.cat() == catNewline)
|
|
|
|
|
os << "[\\n," << t.cat() << "]\n";
|
2003-02-12 07:53:03 +00:00
|
|
|
|
else
|
|
|
|
|
os << '[' << t.character() << ',' << t.cat() << ']';
|
|
|
|
|
return os;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-12 12:05:31 +00:00
|
|
|
|
string Token::asString() const
|
|
|
|
|
{
|
|
|
|
|
return cs_.size() ? cs_ : string(1, char_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string Token::asInput() const
|
|
|
|
|
{
|
2003-02-12 13:21:32 +00:00
|
|
|
|
return char_ ? string(1, char_) : '\\' + cs_ + ' ';
|
2003-02-12 12:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-02-12 11:09:22 +00:00
|
|
|
|
|
2003-02-12 07:53:03 +00:00
|
|
|
|
//
|
|
|
|
|
// Parser
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parser::Parser(istream & is)
|
|
|
|
|
: lineno_(0), pos_(0)
|
|
|
|
|
{
|
|
|
|
|
tokenize(is);
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-23 15:14:43 +00:00
|
|
|
|
|
2003-04-16 12:52:49 +00:00
|
|
|
|
Parser::Parser(string const & s)
|
|
|
|
|
: lineno_(0), pos_(0)
|
|
|
|
|
{
|
2003-06-30 11:36:08 +00:00
|
|
|
|
istringstream is(s);
|
2003-04-16 12:52:49 +00:00
|
|
|
|
tokenize(is);
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-12 07:53:03 +00:00
|
|
|
|
|
|
|
|
|
void Parser::push_back(Token const & t)
|
|
|
|
|
{
|
|
|
|
|
tokens_.push_back(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::pop_back()
|
|
|
|
|
{
|
|
|
|
|
tokens_.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-04-23 15:14:43 +00:00
|
|
|
|
Token const & Parser::prev_token() const
|
2003-02-12 07:53:03 +00:00
|
|
|
|
{
|
|
|
|
|
static const Token dummy;
|
|
|
|
|
return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-04-23 15:14:43 +00:00
|
|
|
|
Token const & Parser::next_token() const
|
2003-02-12 07:53:03 +00:00
|
|
|
|
{
|
|
|
|
|
static const Token dummy;
|
|
|
|
|
return good() ? tokens_[pos_] : dummy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-04-23 15:14:43 +00:00
|
|
|
|
Token const & Parser::get_token()
|
2003-02-12 07:53:03 +00:00
|
|
|
|
{
|
|
|
|
|
static const Token dummy;
|
|
|
|
|
//cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
|
|
|
|
|
return good() ? tokens_[pos_++] : dummy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-04-25 15:54:29 +00:00
|
|
|
|
void Parser::skip_spaces()
|
2003-02-12 07:53:03 +00:00
|
|
|
|
{
|
2003-03-03 17:49:26 +00:00
|
|
|
|
while (1) {
|
2003-04-23 15:14:43 +00:00
|
|
|
|
if (next_token().cat() == catSpace || next_token().cat() == catNewline)
|
|
|
|
|
get_token();
|
|
|
|
|
else if (next_token().cat() == catComment)
|
|
|
|
|
while (next_token().cat() != catNewline)
|
|
|
|
|
get_token();
|
2003-03-03 17:49:26 +00:00
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
2003-02-12 07:53:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::putback()
|
|
|
|
|
{
|
|
|
|
|
--pos_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Parser::good() const
|
|
|
|
|
{
|
|
|
|
|
return pos_ < tokens_.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char Parser::getChar()
|
|
|
|
|
{
|
|
|
|
|
if (!good())
|
|
|
|
|
error("The input stream is not well...");
|
|
|
|
|
return tokens_[pos_++].character();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string Parser::getArg(char left, char right)
|
|
|
|
|
{
|
2003-04-25 15:54:29 +00:00
|
|
|
|
skip_spaces();
|
2003-02-12 07:53:03 +00:00
|
|
|
|
|
|
|
|
|
string result;
|
|
|
|
|
char c = getChar();
|
|
|
|
|
|
|
|
|
|
if (c != left)
|
|
|
|
|
putback();
|
|
|
|
|
else
|
|
|
|
|
while ((c = getChar()) != right && good())
|
|
|
|
|
result += c;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-28 13:37:43 +00:00
|
|
|
|
string Parser::getOpt()
|
|
|
|
|
{
|
2003-05-19 07:22:16 +00:00
|
|
|
|
string const res = getArg('[', ']');
|
2003-02-28 13:37:43 +00:00
|
|
|
|
return res.size() ? '[' + res + ']' : string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-12 08:36:49 +00:00
|
|
|
|
void Parser::tokenize(istream & is)
|
2003-02-12 07:53:03 +00:00
|
|
|
|
{
|
|
|
|
|
static bool init_done = false;
|
|
|
|
|
|
|
|
|
|
if (!init_done) {
|
|
|
|
|
catInit();
|
|
|
|
|
init_done = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char c;
|
|
|
|
|
while (is.get(c)) {
|
|
|
|
|
//cerr << "reading c: " << c << "\n";
|
|
|
|
|
|
|
|
|
|
switch (catcode(c)) {
|
|
|
|
|
case catNewline: {
|
|
|
|
|
++lineno_;
|
|
|
|
|
is.get(c);
|
2003-04-23 15:14:43 +00:00
|
|
|
|
if (catcode(c) == catNewline) {
|
|
|
|
|
//do {
|
|
|
|
|
is.get(c);
|
|
|
|
|
//} while (catcode(c) == catNewline);
|
2003-02-12 07:53:03 +00:00
|
|
|
|
push_back(Token("par"));
|
2003-04-23 15:14:43 +00:00
|
|
|
|
} else {
|
2003-02-12 07:53:03 +00:00
|
|
|
|
push_back(Token('\n', catNewline));
|
|
|
|
|
}
|
2003-04-23 15:14:43 +00:00
|
|
|
|
is.putback(c);
|
2003-02-12 07:53:03 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case catComment: {
|
2003-05-19 07:22:16 +00:00
|
|
|
|
push_back(Token(c, catComment));
|
2003-02-12 07:53:03 +00:00
|
|
|
|
while (is.get(c) && catcode(c) != catNewline)
|
2003-05-19 07:22:16 +00:00
|
|
|
|
push_back(Token(c, catLetter));
|
|
|
|
|
push_back(Token(c, catNewline));
|
2003-02-12 07:53:03 +00:00
|
|
|
|
++lineno_;
|
2003-05-19 07:22:16 +00:00
|
|
|
|
is.get(c);
|
|
|
|
|
if (catcode(c) == catNewline) {
|
|
|
|
|
push_back(Token("par"));
|
|
|
|
|
++lineno_;
|
|
|
|
|
} else {
|
|
|
|
|
is.putback(c);
|
|
|
|
|
}
|
2003-02-12 07:53:03 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case catEscape: {
|
|
|
|
|
is.get(c);
|
|
|
|
|
if (!is) {
|
|
|
|
|
error("unexpected end of input");
|
|
|
|
|
} else {
|
|
|
|
|
string s(1, c);
|
|
|
|
|
if (catcode(c) == catLetter) {
|
|
|
|
|
// collect letters
|
|
|
|
|
while (is.get(c) && catcode(c) == catLetter)
|
|
|
|
|
s += c;
|
|
|
|
|
skipSpaceTokens(is, c);
|
|
|
|
|
}
|
|
|
|
|
push_back(Token(s));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case catSuper:
|
|
|
|
|
case catSub: {
|
|
|
|
|
push_back(Token(c, catcode(c)));
|
|
|
|
|
is.get(c);
|
|
|
|
|
skipSpaceTokens(is, c);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case catIgnore: {
|
2003-02-28 13:37:43 +00:00
|
|
|
|
if (c != 13)
|
|
|
|
|
cerr << "ignoring a char: " << int(c) << "\n";
|
2003-02-12 07:53:03 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
push_back(Token(c, catcode(c)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::dump() const
|
|
|
|
|
{
|
|
|
|
|
cerr << "\nTokens: ";
|
|
|
|
|
for (unsigned i = 0; i < tokens_.size(); ++i) {
|
|
|
|
|
if (i == pos_)
|
|
|
|
|
cerr << " <#> ";
|
|
|
|
|
cerr << tokens_[i];
|
|
|
|
|
}
|
|
|
|
|
cerr << " pos: " << pos_ << "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::error(string const & msg)
|
|
|
|
|
{
|
|
|
|
|
cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
|
|
|
|
|
dump();
|
|
|
|
|
//exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string Parser::verbatimOption()
|
|
|
|
|
{
|
|
|
|
|
string res;
|
2003-04-23 15:14:43 +00:00
|
|
|
|
if (next_token().character() == '[') {
|
|
|
|
|
Token t = get_token();
|
|
|
|
|
for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) {
|
2003-02-12 07:53:03 +00:00
|
|
|
|
if (t.cat() == catBegin) {
|
|
|
|
|
putback();
|
2003-04-23 15:14:43 +00:00
|
|
|
|
res += '{' + verbatim_item() + '}';
|
2003-02-12 07:53:03 +00:00
|
|
|
|
} else
|
|
|
|
|
res += t.asString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-04-23 15:14:43 +00:00
|
|
|
|
string Parser::verbatim_item()
|
2003-02-12 07:53:03 +00:00
|
|
|
|
{
|
2003-02-12 14:32:17 +00:00
|
|
|
|
if (!good())
|
|
|
|
|
error("stream bad");
|
2003-04-25 15:54:29 +00:00
|
|
|
|
skip_spaces();
|
2003-04-23 15:14:43 +00:00
|
|
|
|
if (next_token().cat() == catBegin) {
|
|
|
|
|
Token t = get_token(); // skip brace
|
2003-02-12 14:32:17 +00:00
|
|
|
|
string res;
|
2003-04-23 15:14:43 +00:00
|
|
|
|
for (Token t = get_token(); t.cat() != catEnd && good(); t = get_token()) {
|
2003-02-12 07:53:03 +00:00
|
|
|
|
if (t.cat() == catBegin) {
|
|
|
|
|
putback();
|
2003-04-23 15:14:43 +00:00
|
|
|
|
res += '{' + verbatim_item() + '}';
|
2003-02-12 07:53:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2003-02-12 13:21:32 +00:00
|
|
|
|
res += t.asInput();
|
2003-02-12 07:53:03 +00:00
|
|
|
|
}
|
2003-02-12 14:32:17 +00:00
|
|
|
|
return res;
|
2003-02-12 07:53:03 +00:00
|
|
|
|
}
|
2003-04-23 15:14:43 +00:00
|
|
|
|
return get_token().asInput();
|
2003-02-12 07:53:03 +00:00
|
|
|
|
}
|
2003-02-12 14:32:17 +00:00
|
|
|
|
|
2003-02-28 13:37:43 +00:00
|
|
|
|
|
|
|
|
|
void Parser::setCatCode(char c, CatCode cat)
|
|
|
|
|
{
|
|
|
|
|
theCatcode[c] = cat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CatCode Parser::getCatCode(char c) const
|
|
|
|
|
{
|
|
|
|
|
return theCatcode[c];
|
|
|
|
|
}
|