#include "Encoding.h"
#include "Parser.h"
+#include "support/foreach.h"
#include "support/lstrings.h"
#include "support/textutils.h"
* \p c must have catcode catNewline, and it must be the last character read
* from \p is.
*/
-char_type getNewline(idocstream & is, char_type c)
+char_type getNewline(iparserdocstream & is, char_type c)
{
// we have to handle 3 different line endings:
// - UNIX (\n)
}
+void Parser::deparse()
+{
+ for(size_type i = pos_ ; i < tokens_.size() ; ++i) {
+ docstring const s = from_utf8(tokens_[i].asInput());
+ //cerr << "deparsing [" << to_utf8(s) << "]" <<endl;
+ foreach(char_type c, s)
+ is_.putback(c);
+ }
+ tokens_.erase(tokens_.begin() + pos_, tokens_.end());
+ // make sure that next token is read
+ tokenize_one();
+}
+
+
void Parser::setEncoding(std::string const & e, int const & p)
{
// We may (and need to) use unsafe encodings here: Since the text is
void Parser::setCatcode(char c, CatCode cat)
{
theCatcode_[(unsigned char)c] = cat;
+ deparse();
}
void Parser::setCatcodes(cat_type t)
{
theCatcodesType_ = t;
+ deparse();
}
void Parser::setEncoding(std::string const & e)
{
//cerr << "setting encoding to " << e << std::endl;
- is_ << lyx::setEncoding(e);
+ is_.docstream() << lyx::setEncoding(e);
encoding_iconv_ = e;
}
Token const Parser::get_token()
{
static const Token dummy;
- //cerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << '\n';
+ // if (good())
+ // cerr << "looking at token " << tokens_[pos_]
+ // << " pos: " << pos_ << '\n';
return good() ? tokens_[pos_++] : dummy;
}
extern void debugToken(std::ostream & os, Token const & t, unsigned int flags);
#endif
+// A docstream version that supports putback even when not buffered
+class iparserdocstream
+{
+public:
+ iparserdocstream(idocstream & is) : is_(is) {};
+
+ operator bool() const { return is_; };
+
+ idocstream & docstream() { return is_; };
+
+ void putback(char_type c) { s_ += c; };
+
+ iparserdocstream & get(char_type &c)
+ {
+ if (s_.empty())
+ is_.get(c);
+ else {
+ c = s_[0];
+ s_.erase(0,1);
+ }
+ return *this;
+ };
+private:
+ ///
+ idocstream & is_;
+ ///
+ docstring s_;
+};
+
/*!
* Actual parser class
///
~Parser();
+ /** forget already parsed next tokens and put the
+ * corresponding characters into the input stream for
+ * re-reading. Useful when changing catcodes. */
+ void deparse();
+
///
CatCode catcode(char_type c) const;
///
///
idocstringstream * iss_;
///
- idocstream & is_;
+ iparserdocstream is_;
/// iconv name of the current encoding
std::string encoding_iconv_;
///