]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/tex2lyx.C
Fix bug 2667
[lyx.git] / src / tex2lyx / tex2lyx.C
index 165ec5a38e71ce8560558f06c1ec066a8b1ab77d..09dcf7cbbcf35c7678056ef9b73df038005e3d31 100644 (file)
-
-/** The .tex to .lyx converter
-    \author André Pönitz (2003)
+/**
+ * \file tex2lyx.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
+// {[(
+
 #include <config.h>
 
-#include <algorithm>
+#include "tex2lyx.h"
+#include "context.h"
+
+#include "debug.h"
+#include "lyxtextclass.h"
+
+#include "support/convert.h"
+#include "support/filetools.h"
+#include "support/fs_extras.h"
+#include "support/lstrings.h"
+#include "support/lyxlib.h"
+#include "support/os.h"
+#include "support/package.h"
+
+#include <boost/function.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+
 #include <cctype>
 #include <fstream>
 #include <iostream>
-#include <sstream>
-#include <stack>
 #include <string>
+#include <sstream>
 #include <vector>
+#include <map>
 
-using std::atoi;
+using std::endl;
 using std::cout;
 using std::cerr;
-using std::endl;
-using std::fill;
 using std::getline;
-using std::ios;
+
 using std::ifstream;
-using std::istream;
+using std::ofstream;
 using std::istringstream;
-using std::ostream;
 using std::ostringstream;
-using std::stack;
+using std::stringstream;
 using std::string;
 using std::vector;
-using std::string;
-using std::stack;
-using std::istringstream;
-using std::ostringstream;
-using std::ifstream;
+using std::map;
 
+using lyx::support::changeExtension;
+using lyx::support::isStrUnsignedInt;
+using lyx::support::ltrim;
+using lyx::support::makeAbsPath;
+using lyx::support::onlyPath;
+using lyx::support::rtrim;
+using lyx::support::isFileReadable;
 
-namespace {
+namespace fs = boost::filesystem;
 
-char const OPEN = '<';
-char const CLOSE = '>';
-
-const char * known_languages[] = { "austrian", "babel", "bahasa",
-"basque", "breton", "bulgarian", "catalan", "croatian", "czech", "danish",
-"dutch", "english", "esperanto", "estonian", "finnish", "francais",
-"frenchb", "galician", "germanb", "greek", "hebcal", "hebfont", "hebrew",
-"hebrew_newcode", "hebrew_oldcode", "hebrew_p", "hyphen", "icelandic",
-"irish", "italian", "latin", "lgrcmr", "lgrcmro", "lgrcmss", "lgrcmtt",
-"lgrenc", "lgrlcmss", "lgrlcmtt", "lheclas", "lhecmr", "lhecmss",
-"lhecmtt", "lhecrml", "lheenc", "lhefr", "lheredis", "lheshold",
-"lheshscr", "lheshstk", "lsorbian", "magyar", "naustrian", "ngermanb",
-"ngerman", "norsk", "polish", "portuges", "rlbabel", "romanian",
-"russianb", "samin", "scottish", "serbian", "slovak", "slovene", "spanish",
-"swedish", "turkish", "ukraineb", "usorbian", "welsh", 0};
-
-const char * known_fontsizes[] = { "10pt", "11pt", "12pt", 0 };
-
-
-// some ugly stuff
-string h_preamble;
-string h_textclass               = "FIXME";
-string h_options                 = "FIXME";
-string h_language                = "FIXME";
-string h_inputencoding           = "FIXME";
-string h_fontscheme              = "FIXME";
-string h_graphics                = "default";
-string h_paperfontsize           = "FIXME";
-string h_spacing                 = "single";
-string h_papersize               = "FIXME";
-string h_paperpackage            = "FIXME";
-string h_use_geometry            = "0";
-string h_use_amsmath             = "0";
-string h_use_natbib              = "0";
-string h_use_numerical_citations = "0";
-string h_paperorientation        = "portrait";
-string h_secnumdepth             = "3";
-string h_tocdepth                = "3";
-string h_paragraph_separation    = "indent";
-string h_defskip                 = "medskip";
-string h_quotes_language         = "2";
-string h_quotes_times            = "1";
-string h_papercolumns            = "1";
-string h_papersides              = "1";
-string h_paperpagestyle          = "default";
-string h_tracking_changes        = "0";
-
-// indicates whether we are in the preamble
-bool in_preamble = true;
 
-// current stack of nested environments
-stack<string> active_environments;
+// Hacks to allow the thing to link in the lyxlayout stuff
+LyXErr lyxerr(std::cerr.rdbuf());
+
+
+string const trim(string const & a, char const * p)
+{
+       // BOOST_ASSERT(p);
 
+       if (a.empty() || !*p)
+               return a;
+
+       string::size_type r = a.find_last_not_of(p);
+       string::size_type l = a.find_first_not_of(p);
+
+       // Is this the minimal test? (lgb)
+       if (r == string::npos && l == string::npos)
+               return string();
+
+       return a.substr(l, r - l + 1);
+}
 
 
 void split(string const & s, vector<string> & result, char delim)
 {
-       istringstream is(s);    
+       //cerr << "split 1: '" << s << "'\n";
+       istringstream is(s);
        string t;
        while (getline(is, t, delim))
                result.push_back(t);
+       //cerr << "split 2\n";
 }
 
 
-string join(vector<string> const & input, char delim)
+string join(vector<string> const & input, char const * delim)
 {
        ostringstream os;
-       for (size_t i = 0; i != input.size(); ++i) {
+       for (size_t i = 0; i < input.size(); ++i) {
                if (i)
-                       os << delim;    
-               os << input[i]; 
+                       os << delim;
+               os << input[i];
        }
        return os.str();
 }
 
 
-void handle_opt(vector<string> & opts, char const ** what, string & target)
+char const * const * is_known(string const & str, char const * const * what)
 {
-       for ( ; what; ++what) {
-               vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
-               if (it != opts.end()) {
-                       //cerr << "### found option '" << *what << "'\n";
-                       target = *what;
-                       opts.erase(it);
-                       return;
-               }
-       }
+       for ( ; *what; ++what)
+               if (str == *what)
+                       return what;
+       return 0;
 }
 
 
-void handle_ert(ostream & os, string const & s)
-{
-       os << "\n\\begin_inset ERT\nstatus Collapsed\n\n\\layout Standard\n\n";
-       os << s;
-       os << "\n\\end_inset\n";
-}
 
-
-string wrap(string const & cmd, string const & str)
-{
-       return OPEN + cmd + ' ' + str + CLOSE;
-}
+// current stack of nested environments
+vector<string> active_environments;
 
 
-string wrap(string const & cmd, string const & str, string const & str2)
+string active_environment()
 {
-       return OPEN + cmd + ' ' + str + ' ' + str2 + CLOSE;
+       return active_environments.empty() ? string() : active_environments.back();
 }
 
 
-enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
-
-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;
-}
+CommandMap known_commands;
+CommandMap known_environments;
+CommandMap known_math_environments;
 
 
-// These are TeX's catcodes
-enum CatCode {
-       catEscape,     // 0    backslash
-       catBegin,      // 1    {
-       catEnd,        // 2    }
-       cat,       // 3    $
-       catAlign,      // 4    &
-       catNewline,    // 5    ^^M
-       catParameter,  // 6    #
-       catSuper,      // 7    ^
-       catSub,        // 8    _
-       catIgnore,     // 9
-       catSpace,      // 10   space
-       catLetter,     // 11   a-zA-Z
-       catOther,      // 12   none of the above
-       catActive,     // 13   ~
-       catComment,    // 14   %
-       catInvalid     // 15   <delete>
-};
-
-CatCode theCatcode[256];
-
-
-inline CatCode catcode(unsigned char c)
+void add_known_command(string const & command, string const & o1,
+                      bool o2)
 {
-       return theCatcode[c];
+       // We have to handle the following cases:
+       // definition                      o1    o2    invocation result
+       // \newcommand{\foo}{bar}          ""    false \foo       bar
+       // \newcommand{\foo}[1]{bar #1}    "[1]" false \foo{x}    bar x
+       // \newcommand{\foo}[1][]{bar #1}  "[1]" true  \foo       bar
+       // \newcommand{\foo}[1][]{bar #1}  "[1]" true  \foo[x]    bar x
+       // \newcommand{\foo}[1][x]{bar #1} "[1]" true  \foo[x]    bar x
+       unsigned int nargs = 0;
+       vector<ArgumentType> arguments;
+       string const opt1 = rtrim(ltrim(o1, "["), "]");
+       if (isStrUnsignedInt(opt1)) {
+               // The command has arguments
+               nargs = convert<unsigned int>(opt1);
+               if (nargs > 0 && o2) {
+                       // The first argument is optional
+                       arguments.push_back(optional);
+                       --nargs;
+               }
+       }
+       for (unsigned int i = 0; i < nargs; ++i)
+               arguments.push_back(required);
+       known_commands[command] = arguments;
 }
 
 
-enum {
-       FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing
-       FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
-       FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
-       FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
-       FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
-       FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
-       FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
-       FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
-       FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
-       FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
-       FLAG_OPTION     = 1 << 11, //  read [...] style option
-       FLAG_BRACED     = 1 << 12  //  read {...} style argument
-};
+namespace {
 
 
-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;
-       theCatcode['$']  = cat;
-       theCatcode['&']  = catAlign;
-       theCatcode['\n'] = catNewline;
-       theCatcode['#']  = catParameter;
-       theCatcode['^']  = catSuper;
-       theCatcode['_']  = catSub;
-       theCatcode['\7f'] = catIgnore;
-       theCatcode[' ']  = catSpace;
-       theCatcode['\t'] = catSpace;
-       theCatcode['\r'] = catNewline;
-       theCatcode['~']  = catActive;
-       theCatcode['%']  = catComment;
+/*!
+ * Read one command definition from the syntax file
+ */
+void read_command(Parser & p, string command, CommandMap & commands) {
+       if (p.next_token().asInput() == "*") {
+               p.get_token();
+               command += '*';
+       }
+       vector<ArgumentType> arguments;
+       while (p.next_token().cat() == catBegin ||
+              p.next_token().asInput() == "[") {
+               if (p.next_token().cat() == catBegin) {
+                       string const arg = p.getArg('{', '}');
+                       if (arg == "translate")
+                               arguments.push_back(required);
+                       else
+                               arguments.push_back(verbatim);
+               } else {
+                       p.getArg('[', ']');
+                       arguments.push_back(optional);
+               }
+       }
+       commands[command] = arguments;
 }
 
 
-
-//
-// Helper class for parsing
-//
-
-class Token {
-public:
-       ///
-       Token() : cs_(), char_(0), cat_(catIgnore) {}
-       ///
-       Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
-       ///
-       Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
-
-       ///
-       string const & cs() const { return cs_; }
-       ///
-       CatCode cat() const { return cat_; }
-       ///
-       char character() const { return char_; }
-       ///
-       string asString() const { return cs_.size() ? cs_ : string(1, char_); }
-
-private:
-       ///
-       string cs_;
-       ///
-       char char_;
-       ///
-       CatCode cat_;
-};
-
-ostream & operator<<(ostream & os, Token const & t)
+/*!
+ * Read a class of environments from the syntax file
+ */
+void read_environment(Parser & p, string const & begin,
+                     CommandMap & environments)
 {
-       if (t.cs().size())
-               os << '\\' << t.cs();
-       else
-               os << '[' << t.character() << ',' << t.cat() << ']';
-       return os;
+       string environment;
+       while (p.good()) {
+               Token const & t = p.get_token();
+               if (t.cat() == catLetter)
+                       environment += t.asInput();
+               else if (!environment.empty()) {
+                       p.putback();
+                       read_command(p, environment, environments);
+                       environment.erase();
+               }
+               if (t.cat() == catEscape && t.asInput() == "\\end") {
+                       string const end = p.getArg('{', '}');
+                       if (end == begin)
+                               return;
+               }
+       }
 }
 
 
-class Parser {
-
-public:
-       ///
-       Parser(istream & is);
-
-       ///
-       string parse();
-       ///
-       string parse(unsigned flags, mode_type mode);
-       ///
-       int lineno() const { return lineno_; }
-       ///
-       void putback();
-       /// dump contents to screen
-       void dump() const;
-
-private:
-       ///
-       string getArg(char left, char right);
-       ///
-       char getChar();
-       ///
-       void error(string const & msg);
-       ///
-       void tokenize(istream & is);
-       ///
-       void tokenize(string const & s);
-       ///
-       void skipSpaceTokens(istream & is, char c);
-       ///
-       void push_back(Token const & t);
-       ///
-       void pop_back();
-       ///
-       Token const & prevToken() const;
-       ///
-       Token const & nextToken() const;
-       ///
-       Token const & getToken();
-       /// skips spaces if any
-       void skipSpaces();
-       ///
-       void lex(string const & s);
-       ///
-       bool good() const;
-       ///
-       string parse_verbatim_item();
-       ///
-       string parse_verbatim_option();
-
-       ///
-       int lineno_;
-       ///
-       vector<Token> tokens_;
-       ///
-       unsigned pos_;
-};
-
-
-Parser::Parser(istream & is)
-       : lineno_(0), pos_(0)
+/*!
+ * Read a list of TeX commands from a reLyX compatible syntax file.
+ * Since this list is used after all commands that have a LyX counterpart
+ * are handled, it does not matter that the "syntax.default" file
+ * has almost all of them listed. For the same reason the reLyX-specific
+ * reLyXre environment is ignored.
+ */
+void read_syntaxfile(string const & file_name)
 {
-       tokenize(is);
+       ifstream is(file_name.c_str());
+       if (!is.good()) {
+               cerr << "Could not open syntax file \"" << file_name
+                    << "\" for reading." << endl;
+               exit(2);
+       }
+       // We can use our TeX parser, since the syntax of the layout file is
+       // modeled after TeX.
+       // Unknown tokens are just silently ignored, this helps us to skip some
+       // reLyX specific things.
+       Parser p(is);
+       while (p.good()) {
+               Token const & t = p.get_token();
+               if (t.cat() == catEscape) {
+                       string const command = t.asInput();
+                       if (command == "\\begin") {
+                               string const name = p.getArg('{', '}');
+                               if (name == "environments" || name == "reLyXre")
+                                       // We understand "reLyXre", but it is
+                                       // not as powerful as "environments".
+                                       read_environment(p, name,
+                                               known_environments);
+                               else if (name == "mathenvironments")
+                                       read_environment(p, name,
+                                               known_math_environments);
+                       } else {
+                               read_command(p, command, known_commands);
+                       }
+               }
+       }
 }
 
 
-void Parser::push_back(Token const & t)
-{
-       tokens_.push_back(t);
-}
+string documentclass;
+string syntaxfile;
+bool overwrite_files = false;
 
 
-void Parser::pop_back()
-{
-       tokens_.pop_back();
-}
+/// return the number of arguments consumed
+typedef boost::function<int(string const &, string const &)> cmd_helper;
 
 
-Token const & Parser::prevToken() const
+int parse_help(string const &, string const &)
 {
-       static const Token dummy;
-       return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
+       cerr << "Usage: tex2lyx [ command line switches ] <infile.tex> [<outfile.lyx>]\n"
+               "Command line switches (case sensitive):\n"
+               "\t-help              summarize tex2lyx usage\n"
+               "\t-f                 Force creation of .lyx files even if they exist already\n"
+               "\t-userdir dir       try to set user directory to dir\n"
+               "\t-sysdir dir        try to set system directory to dir\n"
+               "\t-c textclass       declare the textclass\n"
+               "\t-s syntaxfile      read additional syntax file" << endl;
+       exit(0);
 }
 
 
-Token const & Parser::nextToken() const
+int parse_class(string const & arg, string const &)
 {
-       static const Token dummy;
-       return good() ? tokens_[pos_] : dummy;
+       if (arg.empty()) {
+               cerr << "Missing textclass string after -c switch" << endl;
+               exit(1);
+       }
+       documentclass = arg;
+       return 1;
 }
 
 
-Token const & Parser::getToken()
+int parse_syntaxfile(string const & arg, string const &)
 {
-       static const Token dummy;
-       //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
-       return good() ? tokens_[pos_++] : dummy;
+       if (arg.empty()) {
+               cerr << "Missing syntaxfile string after -s switch" << endl;
+               exit(1);
+       }
+       syntaxfile = arg;
+       return 1;
 }
 
 
-void Parser::skipSpaces()
-{
-       while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
-               getToken();
-}
+// Filled with the command line arguments "foo" of "-sysdir foo" or
+// "-userdir foo".
+string cl_system_support;
+string cl_user_support;
 
 
-void Parser::putback()
+int parse_sysdir(string const & arg, string const &)
 {
-       --pos_;
+       if (arg.empty()) {
+               cerr << "Missing directory for -sysdir switch" << endl;
+               exit(1);
+       }
+       cl_system_support = arg;
+       return 1;
 }
 
 
-bool Parser::good() const
+int parse_userdir(string const & arg, string const &)
 {
-       return pos_ < tokens_.size();
+       if (arg.empty()) {
+               cerr << "Missing directory for -userdir switch" << endl;
+               exit(1);
+       }
+       cl_user_support = arg;
+       return 1;
 }
 
 
-char Parser::getChar()
+int parse_force(string const &, string const &)
 {
-       if (!good())
-               error("The input stream is not well...");
-       return tokens_[pos_++].character();
+       overwrite_files = true;
+       return 0;
 }
 
 
-string Parser::getArg(char left, char right)
+void easyParse(int & argc, char * argv[])
 {
-       skipSpaces();
+       map<string, cmd_helper> cmdmap;
+
+       cmdmap["-c"] = parse_class;
+       cmdmap["-f"] = parse_force;
+       cmdmap["-s"] = parse_syntaxfile;
+       cmdmap["-help"] = parse_help;
+       cmdmap["--help"] = parse_help;
+       cmdmap["-sysdir"] = parse_sysdir;
+       cmdmap["-userdir"] = parse_userdir;
+
+       for (int i = 1; i < argc; ++i) {
+               std::map<string, cmd_helper>::const_iterator it
+                       = cmdmap.find(argv[i]);
+
+               // don't complain if not found - may be parsed later
+               if (it == cmdmap.end())
+                       continue;
+
+               string arg((i + 1 < argc) ? argv[i + 1] : "");
+               string arg2((i + 2 < argc) ? argv[i + 2] : "");
+
+               int const remove = 1 + it->second(arg, arg2);
+
+               // Now, remove used arguments by shifting
+               // the following ones remove places down.
+               argc -= remove;
+               for (int j = i; j < argc; ++j)
+                       argv[j] = argv[j + remove];
+               --i;
+       }
+}
 
-       string result;
-       char c = getChar();
 
-       if (c != left)
-               putback();
-       else
-               while ((c = getChar()) != right && good())
-                       result += c;
+// path of the first parsed file
+string masterFilePath;
+// path of the currently parsed file
+string parentFilePath;
 
-       return result;
-}
+} // anonymous namespace
 
 
-void Parser::skipSpaceTokens(istream & is, char c)
+string getMasterFilePath()
 {
-       // skip trailing spaces
-       while (catcode(c) == catSpace || catcode(c) == catNewline)
-               if (!is.get(c))
-                       break;
-       //cerr << "putting back: " << c << "\n";
-       is.putback(c);
+       return masterFilePath;
 }
 
-
-void Parser::tokenize(istream & is)
+string getParentFilePath()
 {
-       // eat everything up to the next \end_inset or end of stream
-       // and store it in s for further tokenization
-       string s;
-       char c;
-       while (is.get(c)) {
-               s += c;
-               if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
-                       s = s.substr(0, s.size() - 10);
-                       break;
-               }
-       }
-       // Remove the space after \end_inset
-       if (is.get(c) && c != ' ')
-               is.unget();
-
-       // tokenize buffer
-       tokenize(s);
+       return parentFilePath;
 }
 
 
-void Parser::tokenize(string const & buffer)
-{
-       static bool init_done = false;
-
-       if (!init_done) {
-               catInit();
-               init_done = true;
-       }
-
-       istringstream is(buffer.c_str(), ios::in | ios::binary);
-
-       char c;
-       while (is.get(c)) {
-               //cerr << "reading c: " << c << "\n";
-
-               switch (catcode(c)) {
-                       case catNewline: {
-                               ++lineno_;
-                               is.get(c);
-                               if (catcode(c) == catNewline)
-                                       push_back(Token("par"));
-                               else {
-                                       push_back(Token('\n', catNewline));
-                                       is.putback(c);
-                               }
-                               break;
-                       }
-
-/*
-                       case catComment: {
-                               while (is.get(c) && catcode(c) != catNewline)
-                                       ;
-                               ++lineno_;
-                               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: {
-                               cerr << "ignoring a char: " << int(c) << "\n";
-                               break;
-                       }
+namespace {
 
-                       default:
-                               push_back(Token(c, catcode(c)));
-               }
-       }
+/*!
+ *  Reads tex input from \a is and writes lyx output to \a os.
+ *  Uses some common settings for the preamble, so this should only
+ *  be used more than once for included documents.
+ *  Caution: Overwrites the existing preamble settings if the new document
+ *  contains a preamble.
+ *  You must ensure that \p parentFilePath is properly set before calling
+ *  this function!
+ */
+void tex2lyx(std::istream &is, std::ostream &os)
+{
+       Parser p(is);
+       //p.dump();
 
-#ifdef FILEDEBUG
-       dump();
+       stringstream ss;
+       LyXTextClass textclass = parse_preamble(p, ss, documentclass);
+
+       active_environments.push_back("document");
+       Context context(true, textclass);
+       parse_text(p, ss, FLAG_END, true, context);
+       if (Context::empty)
+               // Empty document body. LyX needs at least one paragraph.
+               context.check_layout(ss);
+       context.check_end_layout(ss);
+       ss << "\n\\end_body\n\\end_document\n";
+       active_environments.pop_back();
+       ss.seekg(0);
+       os << ss.str();
+#ifdef TEST_PARSER
+       p.reset();
+       ofstream parsertest("parsertest.tex");
+       while (p.good())
+               parsertest << p.get_token().asInput();
+       // <origfile> and parsertest.tex should now have identical content
 #endif
 }
 
 
-void Parser::dump() const
+/// convert TeX from \p infilename to LyX and write it to \p os
+bool tex2lyx(string const &infilename, std::ostream &os)
 {
-       cerr << "\nTokens: ";
-       for (unsigned i = 0; i < tokens_.size(); ++i) {
-               if (i == pos_)
-                       cerr << " <#> ";
-               cerr << tokens_[i];
+       BOOST_ASSERT(lyx::support::absolutePath(infilename));
+       ifstream is(infilename.c_str());
+       if (!is.good()) {
+               cerr << "Could not open input file \"" << infilename
+                    << "\" for reading." << endl;
+               return false;
        }
-       cerr << " pos: " << pos_ << "\n";
-}
-
-
-void Parser::error(string const & msg)
-{
-       cerr << "Line ~" << lineno_ << ":  parse error: " << msg << endl;
-       dump();
-       //exit(1);
+       string const oldParentFilePath = parentFilePath;
+       parentFilePath = onlyPath(infilename);
+       tex2lyx(is, os);
+       parentFilePath = oldParentFilePath;
+       return true;
 }
 
-
-string Parser::parse()
-{
-       skipSpaces();
-       return parse(0, UNDECIDED_MODE);
-}
+} // anonymous namespace
 
 
-string Parser::parse_verbatim_option()
+bool tex2lyx(string const &infilename, string const &outfilename)
 {
-       string res;
-       if (nextToken().character() == '[') {
-               Token t = getToken();
-               for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
-                       if (t.cat() == catBegin) {
-                               putback();
-                               res += '{' + parse_verbatim_item() + '}';
-                       } else
-                               res += t.asString();
+       if (isFileReadable(outfilename)) {
+               if (overwrite_files) {
+                       cerr << "Overwriting existing file "
+                            << outfilename << endl;
+               } else {
+                       cerr << "Not overwriting existing file "
+                            << outfilename << endl;
+                       return false;
                }
+       } else {
+               cerr << "Creating file " << outfilename << endl;
        }
-       return res;
-}
-
-
-string Parser::parse_verbatim_item()
-{
-       string res;
-       if (nextToken().cat() == catBegin) {
-               Token t = getToken();
-               for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
-                       if (t.cat() == catBegin) {
-                               putback();
-                               res += '{' + parse_verbatim_item() + '}';
-                       }
-                       else
-                               res += t.asString();
-               }
+       ofstream os(outfilename.c_str());
+       if (!os.good()) {
+               cerr << "Could not open output file \"" << outfilename
+                    << "\" for writing." << endl;
+               return false;
        }
-       return res;
-}
-
-
-string Parser::parse(unsigned flags, mode_type mode)
-{
-       //int limits = 0;
-
-       ostringstream result;
-       while (good()) {
-               Token const & t = getToken();
-
 #ifdef FILEDEBUG
-               cerr << "t: " << t << " flags: " << flags << "\n";
-               cell->dump();
-               cerr << "\n";
+       cerr << "Input file: " << infilename << "\n";
+       cerr << "Output file: " << outfilename << "\n";
 #endif
-
-               if (flags & FLAG_ITEM) {
-                       if (t.cat() == catSpace)
-                               continue;
-
-                       flags &= ~FLAG_ITEM;
-                       if (t.cat() == catBegin) {
-                               // skip the brace and collect everything to the next matching
-                               // closing brace
-                               flags |= FLAG_BRACE_LAST;
-                               continue;
-                       }
-
-                       // handle only this single token, leave the loop if done
-                       flags |= FLAG_LEAVE;
-               }
-
-
-               if (flags & FLAG_BRACED) {
-                       if (t.cat() == catSpace)
-                               continue;
-
-                       if (t.cat() != catBegin) {
-                               error("opening brace expected");
-                               return result.str();
-                       }
-
-                       // skip the brace and collect everything to the next matching
-                       // closing brace
-                       flags = FLAG_BRACE_LAST;
-               }
-
-
-               if (flags & FLAG_OPTION) {
-                       if (t.cat() == catOther && t.character() == '[') {
-                               result << parse(FLAG_BRACK_LAST, mode);
-                       } else {
-                               // no option found, put back token and we are done
-                               putback();
-                       }
-                       return result.str();
-               }
-
-               //
-               // cat codes
-               //
-               if (t.cat() == cat) {
-                       if (mode != MATH_MODE) {
-                               // we are inside some text mode thingy, so opening new math is allowed
-                               Token const & n = getToken();
-                               if (n.cat() == cat) {
-                                       // TeX's $$...$$ syntax for displayed math
-                                       result << wrap("equation", parse(FLAG_SIMPLE, MATH_MODE));
-                                       getToken(); // skip the second '$' token
-                               } else {
-                                       // simple $...$  stuff
-                                       putback();
-                                       result << wrap("simple", parse(FLAG_SIMPLE, MATH_MODE));
-                               }
-                       }
-
-                       else if (flags & FLAG_SIMPLE) {
-                               // this is the end of the formula
-                               return result.str();
-                       }
-
-                       else {
-                               error("something strange in the parser\n");
-                               break;
-                       }
-               }
-
-               else if (t.cat() == catLetter)
-                       result << t.character();
-
-               else if (t.cat() == catSpace && mode != MATH_MODE) {
-                       //if (result.empty() || result[result.size() - 1] != ' ')
-                               result << t.character();
-               }
-
-               else if (t.cat() == catNewline && mode != MATH_MODE)
-                       result << t.character();
-
-               else if (t.cat() == catParameter) {
-                       Token const & n = getToken();
-                       result << wrap("macroarg", string(1, n.character()));
-               }
-
-               else if (t.cat() == catActive)
-                       result << wrap("active", string(1, t.character()));
-
-               else if (t.cat() == catBegin)
-                       result << wrap("braced", parse(FLAG_BRACE_LAST, mode));
-
-               else if (t.cat() == catEnd) {
-                       if (flags & FLAG_BRACE_LAST)
-                               return result.str();
-                       error("found '}' unexpectedly");
-                       //lyx::Assert(0);
-                       //add(cell, '}', LM_TC_TEX);
-               }
-
-/*
-               else if (t.cat() == catAlign) {
-                       ++cellcol;
-                       //cerr << " column now " << cellcol << " max: " << grid.ncols() << "\n";
-                       if (cellcol == grid.ncols()) {
-                               //cerr << "adding column " << cellcol << "\n";
-                               grid.addCol(cellcol - 1);
-                       }
-                       cell = &grid.cell(grid.index(cellrow, cellcol));
-               }
-*/
-
-               else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
-                       //cerr << "finished reading option\n";
-                       return result.str();
-               }
-
-               else if (t.cat() == catOther)
-                       result << string(1, t.character());
-
-               else if (t.cat() == catComment) {
-                       string s;
-                       while (good()) {
-                               Token const & t = getToken();
-                               if (t.cat() == catNewline)
-                                       break;
-                               s += t.asString();
-                       }
-                       //result << wrap("comment", s);
-                       skipSpaces();
-               }
-
-               //
-               // control sequences
-               //
-
-               else if (t.cs() == "lyxlock") {
-                       // ignored;
-               }
-
-               else if (t.cs() == "newcommand" || t.cs() == "providecommand") {
-                       string const name = parse_verbatim_item();
-                       string const opts = getArg('[', ']');
-                       string const body = parse_verbatim_item();
-                       // only non-lyxspecific stuff
-                       if (name != "noun" && name != "tabularnewline") {
-                               h_preamble += "\\" + t.cs() + "{" + name + "}";
-                               if (opts.size()) 
-                                       h_preamble += "[" + opts + "]";
-                               h_preamble += "{" + body + "}\n";
-                       }
-               }
-
-               else if (t.cs() == "(") 
-                       result << wrap("simple", parse(FLAG_SIMPLE2, MATH_MODE));
-
-               else if (t.cs() == "[")
-                       result << wrap("equation", parse(FLAG_EQUATION, MATH_MODE));
-
-               else if (t.cs() == "protect")
-                       // ignore \\protect, will hopefully be re-added during output
-                       ;
-
-               else if (t.cs() == "end") {
-                       if (flags & FLAG_END) {
-                               // eat environment name
-                               string const name = getArg('{', '}');
-                               if (name != active_environments.top())
-                                       error("\\end{" + name + "} does not match \\begin{"
-                                               + active_environments.top() + "}");
-                               active_environments.pop();
-                               return result.str();
-                       }
-                       error("found 'end' unexpectedly");
-               }
-
-               else if (t.cs() == ")") {
-                       if (flags & FLAG_SIMPLE2)
-                               return result.str();
-                       error("found '\\)' unexpectedly");
-               }
-
-               else if (t.cs() == "]") {
-                       if (flags & FLAG_EQUATION)
-                               return result.str();
-                       error("found '\\]' unexpectedly");
-               }
-
-/*
-               else if (t.cs() == "\\") {
-                       grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
-                       ++cellrow;
-                       cellcol = 0;
-                       if (cellrow == grid.nrows())
-                               grid.addRow(cellrow - 1);
-                       if (grid.asHullstring())
-                               grid.asHullstring()->numbered(cellrow, numbered);
-                       cell = &grid.cell(grid.index(cellrow, cellcol));
-               }
-*/
-               else if (t.cs() == "documentclass") {
-                       vector<string> opts;
-                       split(getArg('[', ']'), opts, ',');
-                       handle_opt(opts, known_languages, h_language); 
-                       handle_opt(opts, known_fontsizes, h_paperfontsize); 
-                       h_options = join(opts, ',');
-                       h_textclass = getArg('{', '}');
-               }
-
-               else if (t.cs() == "usepackage") {
-                       string const options = getArg('[', ']');
-                       string const name = getArg('{', '}');
-                       if (name == "a4wide") {
-                               h_papersize = "a4";
-                               h_paperpackage = "widemarginsa4";
-                       } else if (name == "ae") 
-                               h_fontscheme = "ae";
-                       else if (name == "aecompl") 
-                               h_fontscheme = "ae";
-                       else if (name == "amsmath") 
-                               h_use_amsmath = "1";
-                       else if (name == "amssymb") 
-                               h_use_amsmath = "1";
-                       else if (name == "babel") 
-                               ; // ignore this
-                       else if (name == "fontenc") 
-                               ; // ignore this
-                       else if (name == "inputenc") 
-                               h_inputencoding = options;
-                       else if (name == "makeidx") 
-                               ; // ignore this
-                       else if (name == "verbatim") 
-                               ; // ignore this
-                       else {
-                               if (options.size())
-                                       h_preamble += "\\usepackage[" + options + "]{" + name + "}\n";
-                               else
-                                       h_preamble += "\\usepackage{" + name + "}\n";
-                       }
-               }
-
-               else if (t.cs() == "newenvironment") {
-                       string const name = getArg('{', '}');
-                       skipSpaces();
-                       string const begin = parse_verbatim_item();
-                       skipSpaces();
-                       string const end = parse_verbatim_item();
-                       // ignore out mess
-                       if (name != "lyxcode") 
-                               result << wrap("newenvironment", begin + end); 
-               }
-
-               else if (t.cs() == "def") {
-                       string const name = getToken().cs();
-                       string res;
-                       while (nextToken().cat() != catBegin)
-                               res += getToken().asString();
-                       handle_ert(result, "\\def" + res + '{' + parse_verbatim_item() + '}');
-               }
-
-               else if (t.cs() == "setcounter") {
-                       string const name = getArg('{', '}');
-                       string const content = getArg('{', '}');
-                       if (name == "secnumdepth") 
-                               h_secnumdepth = content;
-                       else if (name == "tocdepth") 
-                               h_tocdepth = content;
-                       else
-                               h_preamble += "\\setcounter{" + name + "}{" + content + "}\n";
-               }
-
-               else if (t.cs() == "setlength") {
-                       string const name = getToken().cs();
-                       string const content = getArg('{', '}');
-                       if (name == "parskip")
-                               h_paragraph_separation = "skip";
-                       else if (name == "parindent")
-                               h_paragraph_separation = "skip";
-                       else
-                               h_preamble += "\\setcounter{" + name + "}{" + content + "}\n";
-               }
-       
-               else if (t.cs() == "par") {
-                       if (!active_environments.empty())       
-                               result << "\n\\layout " << active_environments.top() << "\n\n";
-               }
-
-               else if (t.cs() == "title")
-                       result << "\\layout Title\n\n" + parse_verbatim_item();
-
-               else if (t.cs() == "author")
-                       result << "\\layout Author\n\n" + parse_verbatim_item();
-
-               else if (t.cs() == "abstract")
-                       result << "\\layout Abstract\n\n" + parse_verbatim_item();
-
-               else if (t.cs() == "begin") {
-                       string const name = getArg('{', '}');
-                       active_environments.push(name);
-                       result << parse(FLAG_END, mode);
-               }
-
-               if (flags & FLAG_LEAVE) {
-                       flags &= ~FLAG_LEAVE;
-                       break;
-               }
-       }
-
-       return result.str();
+       return tex2lyx(infilename, os);
 }
 
 
-} // anonymous namespace
-
-
 int main(int argc, char * argv[])
 {
+       fs::path::default_name_check(fs::no_check);
+
+       easyParse(argc, argv);
+
        if (argc <= 1) {
-               cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
+               cerr << "Usage: tex2lyx [ command line switches ] <infile.tex> [<outfile.lyx>]\n"
+                         "See tex2lyx -help." << endl;
                return 2;
        }
 
-       string t;
-       ifstream is(argv[1]);
-       Parser p(is);
-       //p.dump();
-       string s = p.parse();
-       cout << "# tex2lyx 0.0.2 created this file\n"
-            << "\\lyxformat 222\n"
-            << "\\textclass " << h_textclass << "\n"
-            << "\\begin_preamble\n" << h_preamble << "\\end_preamble\n"
-            << "\\options " << h_options << "\n"
-            << "\\language " << h_language << "\n"
-            << "\\inputencoding " << h_inputencoding << "\n"
-            << "\\fontscheme " << h_fontscheme << "\n"
-            << "\\graphics " << h_graphics << "\n"
-            << "\\paperfontsize " << h_paperfontsize << "\n"
-            << "\\spacing " << h_spacing << "\n"
-            << "\\papersize " << h_papersize << "\n"
-            << "\\paperpackage " << h_paperpackage << "\n"
-            << "\\use_geometry " << h_use_geometry << "\n"
-            << "\\use_amsmath " << h_use_amsmath << "\n"
-            << "\\use_natbib " << h_use_natbib << "\n"
-            << "\\use_numerical_citations " << h_use_numerical_citations << "\n"
-            << "\\paperorientation " << h_paperorientation << "\n"
-            << "\\secnumdepth " << h_secnumdepth << "\n"
-            << "\\tocdepth " << h_tocdepth << "\n"
-            << "\\paragraph_separation " << h_paragraph_separation << "\n"
-            << "\\defskip " << h_defskip << "\n"
-            << "\\quotes_language " << h_quotes_language << "\n"
-            << "\\quotes_times " << h_quotes_times << "\n"
-            << "\\papercolumns " << h_papercolumns << "\n"
-            << "\\papersides " << h_papersides << "\n"
-            << "\\paperpagestyle " << h_paperpagestyle << "\n"
-            << "\\tracking_changes " << h_tracking_changes << "\n"
-            << s << "\n"
-            << "\\the_end";
-
-       return 0;       
-}      
+       lyx::support::os::init(argc, argv);
+       lyx::support::init_package(argv[0], cl_system_support, cl_user_support,
+                                  lyx::support::top_build_dir_is_two_levels_up);
+
+       // Now every known option is parsed. Look for input and output
+       // file name (the latter is optional).
+       string const infilename = makeAbsPath(argv[1]);
+       string outfilename;
+       if (argc > 2) {
+               outfilename = argv[2];
+               if (outfilename != "-")
+                       outfilename = makeAbsPath(argv[2]);
+       } else
+               outfilename = changeExtension(infilename, ".lyx");
+
+       string const system_syntaxfile = lyx::support::libFileSearch("", "syntax.default");
+       if (system_syntaxfile.empty()) {
+               cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
+               exit(1);
+       }
+       read_syntaxfile(system_syntaxfile);
+       if (!syntaxfile.empty())
+               read_syntaxfile(syntaxfile);
+
+       masterFilePath = onlyPath(infilename);
+       parentFilePath = masterFilePath;
+
+       if (outfilename == "-") {
+               if (tex2lyx(infilename, cout))
+                       return EXIT_SUCCESS;
+               else
+                       return EXIT_FAILURE;
+       } else {
+               if (tex2lyx(infilename, outfilename))
+                       return EXIT_SUCCESS;
+               else
+                       return EXIT_FAILURE;
+       }
+}
+
+// }])