This changes the semantics of isOK() and operator(), comments from Bernhard below:
With the old version of lyxlex it was _impossible_ to check whether reading an integer, float, ... succeeded or not. The current solution to check for is.bad() in some cases and in other cases use is.good() does not give the desired information. Moreover the result of is.bad() depends on the stl implementation and behaves different for linux and windows.
the bug was introduced by the patch that fixed the bug that crashed lyx when "inset-insert ert" was executed from the command buffer.
The lexer has the method isOK() which reflects the status of the stream is.
The operators void* and ! are not really well defined (they depend on the value of is.bad()). What is missing is a test if the last reading operation was successful and thus the returned value is valid.
That's what i implemented in this patch.
The new rule for using the lexer:
if you want to know if the lexer still has data to read (either from the stream or from the pushed token) then use "lex.isOK()".
If you want to test if the last reading operation was successful then use eg. "if (lex) {...}" or unsuccessful then use eg. "if (!lex) {...}"
an example:
int readParam(LyxLex &lex) {
int param = 1; // default value
if (lex.isOK()) { // the lexer has data to read
int p; // temporary variable
lex >> p;
if (lex) param = p; // only use the input if the reading operation was successful
}
return param;
}
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@17569
a592a061-630c-0410-9148-
cb99ea01b6c8
Toolbar tb;
tb.name = lex.getString();
lex.next(true);
- if (!lex.isOK()) {
+ tb.gui_name = lex.getString();
+ if (!lex) {
lyxerr << "ToolbarBackend::read: Malformed toolbar "
"description " << lex.getString() << endl;
return;
}
- tb.gui_name = lex.getString();
bool quit = false;
lex.next();
string const token(lex.getString());
- if (!lex.isOK()) {
+ if (!lex) {
Alert::error(_("Document could not be read"),
bformat(_("%1$s could not be read."), from_utf8(filename.absFilename())));
return failure;
MathArray const & DocIterator::cell() const
{
- BOOST_ASSERT(inMathed());
+// BOOST_ASSERT(inMathed());
return top().cell();
}
MathArray & DocIterator::cell()
{
- BOOST_ASSERT(inMathed());
+// BOOST_ASSERT(inMathed());
return top().cell();
}
string logtype, logfile;
lex >> logtype;
- if (lex.isOK()) {
+ if (lex) {
lex.next(true);
logfile = lex.getString();
}
} else if (token == "update") {
lex.next();
bool const accept = lex.getBool();
- action = accept ? 1 : 2;
+ if (lex) {
+ action = accept ? 1 : 2;
+ } else {
+ // Unrecognised update option
+ return false;
+ }
} else if (!token.empty()) {
// Unrecognised token
return false;
if (!lex.isOK())
return;
- if (lex.isOK()) {
- lex.next();
- type = lex.getString();
- }
- if (!lex.isOK())
+ lex.next();
+ type = lex.getString();
+
+ if (!lex)
return;
+
lex.next();
string token;
token = lex.getString();
+ if (!lex)
+ return;
if (token == "position") {
lex.next();
// The [0] is needed. We need the first and only char in
lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
lex.pushToken(token);
}
- if (!lex.isOK())
- return;
+
lex.next();
token = lex.getString();
+ if (!lex)
+ return;
if (token == "hor_pos") {
lex.next();
hor_pos = lex.getString()[0];
lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
lex.pushToken(token);
}
- if (!lex.isOK())
- return;
+
lex.next();
token = lex.getString();
+ if (!lex)
+ return;
if (token == "has_inner_box") {
lex.next();
inner_box = lex.getInteger();
lex.pushToken(token);
}
- if (!lex.isOK())
- return;
lex.next();
token = lex.getString();
+ if (!lex)
+ return;
if (token == "inner_pos") {
lex.next();
inner_pos = lex.getString()[0];
<< token << endl;
lex.pushToken(token);
}
- if (!lex.isOK())
- return;
+
lex.next();
token = lex.getString();
+ if (!lex)
+ return;
if (token == "use_parbox") {
lex.next();
use_parbox = lex.getInteger();
lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
lex.pushToken(token);
}
- if (!lex.isOK())
- return;
+
lex.next();
token = lex.getString();
+ if (!lex)
+ return;
if (token == "width") {
lex.next();
width = LyXLength(lex.getString());
lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
lex.pushToken(token);
}
- if (!lex.isOK())
- return;
+
lex.next();
token = lex.getString();
+ if (!lex)
+ return;
if (token == "special") {
lex.next();
special = lex.getString();
lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
lex.pushToken(token);
}
- if (!lex.isOK())
- return;
+
lex.next();
token = lex.getString();
+ if (!lex)
+ return;
if (token == "height") {
lex.next();
height = LyXLength(lex.getString());
lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
lex.pushToken(token);
}
- if (!lex.isOK())
- return;
+
lex.next();
token = lex.getString();
+ if (!lex)
+ return;
if (token == "height_special") {
lex.next();
height_special = lex.getString();
int s;
lex >> s;
- if (lex.isOK())
+ if (lex)
status = static_cast<InsetCollapsable::CollapseStatus>(s);
}
{
string label;
lex >> label;
- if (lex.isOK())
+ if (lex)
type = notetranslator().find(label);
}
lex.next();
string token = lex.getString();
- while (lex.isOK() && (token != "\\end_inset")) {
+ while (lex && token != "\\end_inset") {
lex.next();
token = lex.getString();
}
- if (token != "\\end_inset") {
+ if (!lex) {
lex.printError("Missing \\end_inset at this point. "
"Read: `$$Token'");
}
BOOST_ASSERT(lex.isOK());
string vsp;
lex >> vsp;
- if (lex.isOK())
+ if (lex)
space_ = VSpace(vsp);
string end_token;
string vsp;
lex >> vsp;
- if (lex.isOK())
+ if (lex)
vspace = VSpace(vsp);
}
bool LyXLex::isOK() const
{
- return pimpl_->is.good();
+ return pimpl_->inputAvailable();
}
int LyXLex::getInteger() const
{
+ lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
+ if (!lastReadOk_) {
+ pimpl_->printError("integer token missing");
+ return -1;
+ }
+
if (isStrInt(pimpl_->getString()))
return convert<int>(pimpl_->getString());
+
+ lastReadOk_ = false;
pimpl_->printError("Bad integer `$$Token'");
return -1;
}
// replace comma with dot in case the file was written with
// the wrong locale (should be rare, but is easy enough to
// avoid).
+ lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
+ if (!lastReadOk_) {
+ pimpl_->printError("float token missing");
+ return -1;
+ }
+
string const str = subst(pimpl_->getString(), ",", ".");
if (isStrDbl(str))
return convert<double>(str);
+
+ lastReadOk_ = false;
pimpl_->printError("Bad float `$$Token'");
return -1;
}
string const LyXLex::getString() const
{
+ lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
+
+ if (lastReadOk_)
return pimpl_->getString();
+
+ return string();
}
docstring const LyXLex::getDocString() const
{
- return pimpl_->getDocString();
+ lastReadOk_ = pimpl_->status == LEX_DATA || pimpl_->status == LEX_TOKEN;
+
+ if (lastReadOk_)
+ return pimpl_->getDocString();
+
+ return docstring();
}
string str, prefix;
bool firstline = true;
- while (isOK()) {
+ while (pimpl_->is) { //< eatLine only reads from is, not from pushTok
if (!eatLine())
// blank line in the file being read
continue;
str += ltrim(tmpstr, "\t") + '\n';
}
- if (!isOK()) {
+ if (!pimpl_->is) {
printError("Long string not ended by `" + endtoken + '\'');
}
bool LyXLex::getBool() const
{
if (pimpl_->getString() == "true") {
+ lastReadOk_ = true;
return true;
} else if (pimpl_->getString() != "false") {
pimpl_->printError("Bad boolean `$$Token'. "
"Use \"false\" or \"true\"");
+ lastReadOk_ = false;
}
+ lastReadOk_ = true;
return false;
}
// 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.
- return pimpl_->is.bad() ? 0 : this;
+ return lastReadOk_? this: 0;
}
bool LyXLex::operator!() const
{
- return pimpl_->is.bad();
+ return !lastReadOk_;
}
if (isOK()) {
next();
s = getString();
+ } else {
+ lastReadOk_ = false;
}
return *this;
}
if (isOK()) {
next();
s = getDocString();
+ } else {
+ lastReadOk_ = false;
}
return *this;
}
if (isOK()) {
next();
s = getFloat();
+ } else {
+ lastReadOk_ = false;
}
return *this;
}
if (isOK()) {
next();
s = getInteger();
+ } else {
+ lastReadOk_ = false;
}
return *this;
}
if (isOK()) {
next();
s = getInteger();
+ } else {
+ lastReadOk_ = false;
}
return *this;
}
if (isOK()) {
next();
s = getBool();
+ } else {
+ lastReadOk_ = false;
}
return *this;
}
};
/** Generalized simple lexical analizer.
- It can be used for simple syntax parsers, like lyxrc,
- texclass and others to come.
+ Use the method isOK() to check if there is still data available
+ for lexing. Use one of the the operators void* or ! to test if
+ the last reading operation was successful.
+
+ Example:
+
+ int readParam(LyxLex &lex) {
+ int param = 1; // default value
+ if (lex.isOK()) { // the lexer has data to read
+ int p; // temporary variable
+ lex >> p;
+ if (lex) param = p; // only use the input if reading was successful
+ }
+ return param;
+ }
+
@see lyxrc.C for an example of usage.
*/
class LyXLex : boost::noncopyable {
};
/// stream is open and end of stream is not reached
- /// FIXME: Rename to good() since this is the name of the
- /// corresponding std::stream method.
+ /// FIXME: test also if pushTok is not empty
+ /// FIXME: the method should be renamed to something like
+ /// dataAvailable(), in order to reflect the real behavior
bool isOK() const;
- /// stream is ok
- /// FIXME: This does not behave like the std::stream counterpart.
+ /// FIXME: The next two operators should be replaced by one method
+ /// called e.g. lastReadOk(), in order to reflect the real
+ /// behavior
+ /// last read operation was successful.
operator void const *() const;
- /// stream is not ok
- /// FIXME: This does not behave like the std::stream counterpart.
+ /// last read operation was not successful
bool operator!() const;
/// return true if able to open file, else false
bool setFile(support::FileName const & filename);
/// returns a lex code
int lex();
- /** Just read athe next word. If esc is true remember that
+ /** Just read the next word. If esc is true remember that
some chars might be escaped: "\ atleast
*/
bool next(bool esc = false);
class Pimpl;
///
Pimpl * pimpl_;
+ ///
+ mutable bool lastReadOk_;
};
// There can have been a whole line pushed so
// we extract the first word and leaves the rest
// in pushTok. (Lgb)
- if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
+ if (pushTok[0] == '\\' && pushTok.find(' ') != string::npos) {
buff.clear();
pushTok = split(pushTok, buff, ' ');
- return true;
} else {
buff = pushTok;
pushTok.clear();
- return true;
}
+ status = LEX_TOKEN;
+ return true;
}
if (!esc) {
unsigned char c = 0; // getc() returns an int
c = cc;
// skip ','s
- if (c == ',') continue;
-
- if (c == '\\') {
- // escape
- buff.clear();
-
- do {
- if (c == '\\') {
- // escape the next char
- is.get(cc);
- c = cc;
- }
- buff.push_back(c);
- is.get(cc);
- c = cc;
- } while (c > ' ' && c != ',' && is);
-
- status = LEX_TOKEN;
+ if (c == ',')
continue;
- }
if (c == commentChar) {
// Read rest of line (fast :-)
buff.resize(buff.size() - 1);
status = LEX_DATA;
return true;
+ } else if (buff.length() > 0) { // last line
+ status = LEX_DATA;
+ return true;
} else {
return false;
}
// There can have been a whole line pushed so
// we extract the first word and leaves the rest
// in pushTok. (Lgb)
- if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
+ if (pushTok[0] == '\\' && pushTok.find(' ') != string::npos) {
buff.clear();
pushTok = split(pushTok, buff, ' ');
- return true;
} else {
buff = pushTok;
pushTok.clear();
- return true;
}
+ status = LEX_TOKEN;
+ return true;
}
status = 0;
}
+bool LyXLex::Pimpl::inputAvailable() {
+ return is.good();
+}
+
+
void LyXLex::Pimpl::pushToken(string const & pt)
{
pushTok = pt;
bool eatLine();
///
bool nextToken();
+ /// test if there is a pushed token or the stream is ok
+ bool inputAvailable();
///
void pushToken(std::string const &);
/// fb_ is only used to open files, the stream is accessed through is.