]> git.lyx.org Git - lyx.git/blobdiff - src/Lexer.cpp
* GuiDocument.cpp: before accessing the buffer() in paramsToDialog(), check
[lyx.git] / src / Lexer.cpp
index 008b0cc9620417395a933b23d46fe0a0358c1514..e99875b28660286882eea04638e53b0848df2b3a 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Alejandro Aguilar Sierra
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  * \author Jean-Marc Lasgouttes
  * \author John Levon
  *
@@ -20,6 +20,7 @@
 #include "support/FileName.h"
 #include "support/filetools.h"
 #include "support/gzstream.h"
+#include "support/lassert.h"
 #include "support/lstrings.h"
 #include "support/lyxalgo.h"
 #include "support/types.h"
@@ -173,7 +174,9 @@ void Lexer::Pimpl::printError(string const & message) const
 {
        string const tmpmsg = subst(message, "$$Token", getString());
        lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
-               << " of file " << to_utf8(makeDisplayPath(name)) << ']' << endl;
+               << " of file " << to_utf8(makeDisplayPath(name))
+               << " current token: '" << getString() << "'"
+               << " context: '" << context << "']" << endl;
 }
 
 
@@ -249,7 +252,8 @@ bool Lexer::Pimpl::setFile(FileName const & filename)
                is.rdbuf(&gz_);
                name = filename.absFilename();
                lineno = 0;
-               return gz_.is_open() && is.good();
+               if (!gz_.is_open() || !is.good())
+                       return false;
        } else {
                LYXERR(Debug::LYXLEX, "lyxlex: UNcompressed");
 
@@ -264,8 +268,21 @@ bool Lexer::Pimpl::setFile(FileName const & filename)
                is.rdbuf(&fb_);
                name = filename.absFilename();
                lineno = 0;
-               return fb_.is_open() && is.good();
+               if (!fb_.is_open() || !is.good())
+                       return false;
        }
+
+       // Skip byte order mark.
+       if (is.peek() == 0xef) {
+               int c = is.get();
+               if (is.peek() == 0xbb) {
+                       c = is.get();
+                       LASSERT(is.get() == 0xbf, /**/);
+               } else
+                       is.unget();
+       }
+
+       return true;
 }
 
 
@@ -497,7 +514,7 @@ bool Lexer::Pimpl::nextToken()
                char cc = 0;
                is.get(cc);
                c = cc;
-               if (c >= ' ' && is) {
+               if ((c >= ' ' || c == '\t') && is) {
                        buff.clear();
 
                        if (c == '\\') { // first char == '\\'
@@ -511,7 +528,7 @@ bool Lexer::Pimpl::nextToken()
                                        buff.push_back(c);
                                        is.get(cc);
                                        c = cc;
-                               } while (c >= ' ' && c != '\\' && is);
+                               } while ((c >= ' ' || c == '\t') && c != '\\' && is);
                        }
 
                        if (c == '\\')
@@ -634,6 +651,7 @@ void Lexer::setCommentChar(char c)
        pimpl_->setCommentChar(c);
 }
 
+
 int Lexer::lex()
 {
        return pimpl_->lex();
@@ -760,6 +778,7 @@ bool Lexer::getBool() const
        pimpl_->printError("Bad boolean `$$Token'. "
                                 "Use \"false\" or \"true\"");
        lastReadOk_ = false;
+       return false;
 }
 
 
@@ -897,6 +916,18 @@ string Lexer::quoteString(string const & arg)
 }
 
 
+// same for docstring
+docstring Lexer::quoteString(docstring const & arg)
+{
+       docstring res;
+       res += '"';
+       res += subst(subst(arg, from_ascii("\\"), from_ascii("\\\\")), 
+                    from_ascii("\""), from_ascii("\\\""));
+       res += '"';
+       return res;
+}
+
+
 Lexer & Lexer::operator>>(char const * required)
 {
        string token;