From 193c25946baa53f69e5dea54b9dc4e706cabf95e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 3 Apr 2008 22:38:14 +0000 Subject: [PATCH] make Lexer interface a bit more friendly git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@24108 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Lexer.cpp | 22 +++++++++++++++++++++- src/Lexer.h | 17 ++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Lexer.cpp b/src/Lexer.cpp index b5f72e51a2..109180e8f9 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -102,6 +102,8 @@ public: string pushTok; /// char commentChar; + /// used for error messages + string context; private: /// non-copyable Pimpl(Pimpl const &); @@ -878,7 +880,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) +string Lexer::quoteString(string const & arg) { string res; res += '"'; @@ -888,4 +890,22 @@ string const Lexer::quoteString(string const & arg) } +Lexer & Lexer::operator>>(char const * required) +{ + string token; + *this >> token; + if (token != required) { + LYXERR0("Missing '" << required << "'-tag in " << pimpl_->context); + pushToken(token); + } + return *this; +} + + +void Lexer::setContext(std::string const & str) +{ + pimpl_->context = str; +} + + } // namespace lyx diff --git a/src/Lexer.h b/src/Lexer.h index 5419fab778..3324bdcad6 100644 --- a/src/Lexer.h +++ b/src/Lexer.h @@ -167,6 +167,8 @@ public: /// Prints the current token table on the supplied ostream. void printTable(std::ostream &); + /// Used to dispaly context information in case of errors. + void setContext(std::string const & functionName); /// extract string Lexer & operator>>(std::string &); @@ -181,9 +183,12 @@ public: /// extract bool Lexer & operator>>(bool &); + /// read and check a required token + Lexer & operator>>(char const * required); + /// Quotes a string so that reading it again with Lexer::next(true) /// gets the original string - static std::string const quoteString(std::string const &); + static std::string quoteString(std::string const &); private: /// noncopyable @@ -206,6 +211,16 @@ private: }; +/// extract something constructable from a string, i.e. a LaTeX length +template +Lexer & operator>>(Lexer & lex, T & t) +{ + if (lex.next()) + t = T(lex.getString()); + return lex; +} + + /** Use to enable multiple exit points. This is needed to ensure that the pop is done upon exit from methods with more than one exit point or that can return as a response to -- 2.39.5