X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FLexer.cpp;h=6f5da00d2bbebcd1e6a7a3be8f330ff2cb60d427;hb=0362c6aae73c293d1c20277c12d362acfe0b2ef6;hp=4406d074977dbeb64ec4f02475a2eeb83104443f;hpb=39e79d8602920eefe36e898c9f415afb979521b2;p=lyx.git diff --git a/src/Lexer.cpp b/src/Lexer.cpp index 4406d07497..6f5da00d2b 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -15,21 +15,16 @@ #include "Lexer.h" -#include "debug.h" - #include "support/convert.h" +#include "support/debug.h" +#include "support/FileName.h" #include "support/filetools.h" +#include "support/gzstream.h" #include "support/lstrings.h" #include "support/lyxalgo.h" #include "support/types.h" -#include "support/unicode.h" - -#include -#include -#include -#include -namespace io = boost::iostreams; +#include #include #include @@ -37,31 +32,11 @@ namespace io = boost::iostreams; #include #include +using namespace std; +using namespace lyx::support; namespace lyx { -using support::compare_ascii_no_case; -using support::FileName; -using support::getFormatFromContents; -using support::isStrDbl; -using support::isStrInt; -using support::ltrim; -using support::makeDisplayPath; -using support::prefixIs; -using support::split; -using support::subst; -using support::trim; - -using std::endl; -using std::getline; -using std::lower_bound; -using std::sort; -using std::string; -using std::ios; -using std::istream; -using std::ostream; - - ////////////////////////////////////////////////////////////////////// // // Lexer::Pimpl @@ -75,21 +50,21 @@ public: /// Pimpl(keyword_item * tab, int num); /// - std::string const getString() const; + string const getString() const; /// docstring const getDocString() const; /// - void printError(std::string const & message) const; + void printError(string const & message) const; /// - void printTable(std::ostream & os); + void printTable(ostream & os); /// void pushTable(keyword_item * tab, int num); /// void popTable(); /// - bool setFile(support::FileName const & filename); + bool setFile(FileName const & filename); /// - void setStream(std::istream & i); + void setStream(istream & i); /// void setCommentChar(char c); /// @@ -105,42 +80,42 @@ public: /// test if there is a pushed token or the stream is ok bool inputAvailable(); /// - void pushToken(std::string const &); + void pushToken(string const &); /// fb_ is only used to open files, the stream is accessed through is. - std::filebuf fb_; + filebuf fb_; /// gz_ is only used to open files, the stream is accessed through is. - io::filtering_istreambuf gz_; + gz::gzstreambuf gz_; /// the stream that we use. - std::istream is; + istream is; /// - std::string name; + string name; /// keyword_item * table; /// int no_items; /// - std::string buff; + string buff; /// int status; /// int lineno; /// - std::string pushTok; + string pushTok; /// char commentChar; private: /// void verifyTable(); /// - class pushed_table { + class PushedTable { public: /// - pushed_table() + PushedTable() : table_elem(0), table_siz(0) {} /// - pushed_table(keyword_item * ki, int siz) + PushedTable(keyword_item * ki, int siz) : table_elem(ki), table_siz(siz) {} /// keyword_item * table_elem; @@ -148,15 +123,15 @@ private: int table_siz; }; /// - std::stack pushed; + stack pushed; }; namespace { -class compare_tags - : public std::binary_function { +class CompareTags + : public binary_function { public: // used by lower_bound, sort and sorted bool operator()(keyword_item const & a, keyword_item const & b) const @@ -214,14 +189,14 @@ void Lexer::Pimpl::verifyTable() { // Check if the table is sorted and if not, sort it. if (table - && !lyx::sorted(table, table + no_items, compare_tags())) { + && !lyx::sorted(table, table + no_items, CompareTags())) { lyxerr << "The table passed to Lexer is not sorted!\n" << "Tell the developers to fix it!" << endl; // We sort it anyway to avoid problems. lyxerr << "\nUnsorted:" << endl; printTable(lyxerr); - sort(table, table + no_items, compare_tags()); + sort(table, table + no_items, CompareTags()); lyxerr << "\nSorted:" << endl; printTable(lyxerr); } @@ -230,7 +205,7 @@ void Lexer::Pimpl::verifyTable() void Lexer::Pimpl::pushTable(keyword_item * tab, int num) { - pushed_table tmppu(table, no_items); + PushedTable tmppu(table, no_items); pushed.push(tmppu); table = tab; @@ -247,7 +222,7 @@ void Lexer::Pimpl::popTable() return; } - pushed_table tmp = pushed.top(); + PushedTable tmp = pushed.top(); pushed.pop(); table = tmp.table_elem; no_items = tmp.table_siz; @@ -257,32 +232,31 @@ void Lexer::Pimpl::popTable() bool Lexer::Pimpl::setFile(FileName const & filename) { // Check the format of the file. - string const format = getFormatFromContents(filename); + string const format = filename.guessFormatFromContents(); if (format == "gzip" || format == "zip" || format == "compress") { - LYXERR(Debug::LYXLEX) << "lyxlex: compressed" << endl; - + LYXERR(Debug::LYXLEX, "lyxlex: compressed"); // The check only outputs a debug message, because it triggers // a bug in compaq cxx 6.2, where is_open() returns 'true' for // a fresh new filebuf. (JMarc) - if (!gz_.empty() || istream::off_type(is.tellg()) > -1) - LYXERR(Debug::LYXLEX) << "Error in Lexer::setFile: " - "file or stream already set." << endl; - gz_.push(io::gzip_decompressor()); - gz_.push(io::file_source(filename.toFilesystemEncoding())); + if (gz_.is_open() || istream::off_type(is.tellg()) > -1) + LYXERR(Debug::LYXLEX, "Error in LyXLex::setFile: " + "file or stream already set."); + gz_.open(filename.toFilesystemEncoding().c_str(), ios::in); is.rdbuf(&gz_); name = filename.absFilename(); lineno = 0; - return gz_.component(1)->is_open() && is.good(); + return gz_.is_open() && is.good(); } else { - LYXERR(Debug::LYXLEX) << "lyxlex: UNcompressed" << endl; + LYXERR(Debug::LYXLEX, "lyxlex: UNcompressed"); // The check only outputs a debug message, because it triggers // a bug in compaq cxx 6.2, where is_open() returns 'true' for // a fresh new filebuf. (JMarc) - if (fb_.is_open() || istream::off_type(is.tellg()) > 0) - LYXERR(Debug::LYXLEX) << "Error in Lexer::setFile: " - "file or stream already set." << endl; + if (fb_.is_open() || istream::off_type(is.tellg()) > 0) { + LYXERR(Debug::LYXLEX, "Error in Lexer::setFile: " + "file or stream already set."); + } fb_.open(filename.toFilesystemEncoding().c_str(), ios::in); is.rdbuf(&fb_); name = filename.absFilename(); @@ -294,9 +268,10 @@ bool Lexer::Pimpl::setFile(FileName const & filename) void Lexer::Pimpl::setStream(istream & i) { - if (fb_.is_open() || istream::off_type(is.tellg()) > 0) - LYXERR(Debug::LYXLEX) << "Error in Lexer::setStream: " - "file or stream already set." << endl; + if (fb_.is_open() || istream::off_type(is.tellg()) > 0) { + LYXERR(Debug::LYXLEX, "Error in Lexer::setStream: " + "file or stream already set."); + } is.rdbuf(i.rdbuf()); lineno = 0; } @@ -338,8 +313,7 @@ bool Lexer::Pimpl::next(bool esc /* = false */) string dummy; getline(is, dummy); - LYXERR(Debug::LYXLEX) << "Comment read: `" << c - << dummy << '\'' << endl; + LYXERR(Debug::LYXLEX, "Comment read: `" << c << dummy << '\''); #else // unfortunately ignore is buggy (Lgb) is.ignore(100, '\n'); @@ -417,7 +391,7 @@ bool Lexer::Pimpl::next(bool esc /* = false */) c = cc; // skip ','s - if (c == ',') + if (c == ',') continue; if (c == commentChar) { @@ -427,8 +401,7 @@ bool Lexer::Pimpl::next(bool esc /* = false */) string dummy; getline(is, dummy); - LYXERR(Debug::LYXLEX) << "Comment read: `" << c - << dummy << '\'' << endl; + LYXERR(Debug::LYXLEX, "Comment read: `" << c << dummy << '\''); #else // but ignore is also still buggy (Lgb) // This is fast (Lgb) @@ -511,7 +484,7 @@ int Lexer::Pimpl::search_kw(char const * const tag) const keyword_item search_tag = { tag, 0 }; keyword_item * res = lower_bound(table, table + no_items, - search_tag, compare_tags()); + search_tag, CompareTags()); // use the compare_ascii_no_case instead of compare_no_case, // because in turkish, 'i' is not the lowercase version of 'I', // and thus turkish locale breaks parsing of tags. @@ -525,7 +498,7 @@ int Lexer::Pimpl::search_kw(char const * const tag) const int Lexer::Pimpl::lex() { //NOTE: possible bug. - if (next() && status == LEX_TOKEN) + if (next() && status == LEX_TOKEN) return search_kw(getString().c_str()); return status; } @@ -540,8 +513,7 @@ bool Lexer::Pimpl::eatLine() while (is && c != '\n') { is.get(cc); c = cc; - //LYXERR(Debug::LYXLEX) << "Lexer::EatLine read char: `" - // << c << '\'' << endl; + //LYXERR(Debug::LYXLEX, "Lexer::EatLine read char: `" << c << '\''); if (c != '\r') buff.push_back(c); } @@ -600,7 +572,8 @@ bool Lexer::Pimpl::nextToken() } while (c >= ' ' && c != '\\' && is); } - if (c == '\\') is.putback(c); // put it back + if (c == '\\') + is.putback(c); // put it back status = LEX_TOKEN; } @@ -619,7 +592,7 @@ bool Lexer::Pimpl::nextToken() bool Lexer::Pimpl::inputAvailable() { - return is.good(); + return is.good(); } @@ -696,7 +669,7 @@ void Lexer::printError(string const & message) const } -bool Lexer::setFile(support::FileName const & filename) +bool Lexer::setFile(FileName const & filename) { return pimpl_->setFile(filename); } @@ -771,7 +744,7 @@ string const Lexer::getString() const docstring const Lexer::getDocString() const { lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN; - + if (lastReadOk_) return pimpl_->getDocString(); @@ -794,8 +767,7 @@ string const Lexer::getLongString(string const & endtoken) string const token = trim(getString(), " \t"); - LYXERR(Debug::PARSER) << "LongString: `" - << getString() << '\'' << endl; + LYXERR(Debug::PARSER, "LongString: `" << getString() << '\''); // We do a case independent comparison, like search_kw does. if (compare_ascii_no_case(token, endtoken) == 0) @@ -807,8 +779,7 @@ string const Lexer::getLongString(string const & endtoken) if (i != string::npos) prefix = tmpstr.substr(0, i); firstline = false; - LYXERR(Debug::PARSER) - << "Prefix = `" << prefix << "\'" << endl; + LYXERR(Debug::PARSER, "Prefix = `" << prefix << "\'"); } // further lines in long strings may have the same @@ -869,7 +840,7 @@ void Lexer::pushToken(string const & pt) Lexer::operator void const *() const { - // This behaviour is NOT the same as the std::streams which would + // This behaviour is NOT the same as the streams which would // use fail() here. However, our implementation of getString() et al. // can cause the eof() and fail() bits to be set, even though we // haven't tried to read 'em. @@ -883,7 +854,7 @@ bool Lexer::operator!() const } -Lexer & Lexer::operator>>(std::string & s) +Lexer & Lexer::operator>>(string & s) { if (isOK()) { next(); @@ -958,7 +929,7 @@ Lexer & Lexer::operator>>(bool & s) /// quotes a string, e.g. for use in preferences files or as an argument of the "log" dialog string const Lexer::quoteString(string const & arg) { - std::ostringstream os; + ostringstream os; os << '"' << subst(subst(arg, "\\", "\\\\"), "\"", "\\\"") << '"'; return os.str(); }