From 26b3d93293afc4363fa9e715c217bc8e3dbf5ca5 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Tue, 27 Aug 2013 18:58:23 +0200 Subject: [PATCH] streamlined filter syntax --- apps/opencs/model/filter/parser.cpp | 68 +++++++++++++++-------------- apps/opencs/model/filter/parser.hpp | 2 +- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/apps/opencs/model/filter/parser.cpp b/apps/opencs/model/filter/parser.cpp index abdc3bab3..d334a7f63 100644 --- a/apps/opencs/model/filter/parser.cpp +++ b/apps/opencs/model/filter/parser.cpp @@ -174,14 +174,14 @@ CSMFilter::Token CSMFilter::Parser::checkKeywords (const Token& token) { "true", "false", "and", "or", "not", - "text", "value", + "string", "value", 0 }; std::string string = Misc::StringUtils::lowerCase (token.mString); for (int i=0; sKeywords[i]; ++i) - if (sKeywords[i]==string) + if (sKeywords[i]==string || (string.size()==1 && sKeywords[i][0]==string[0])) return Token (static_cast (i+Token::Type_Keyword_True)); return token; @@ -211,7 +211,7 @@ CSMFilter::Token CSMFilter::Parser::getNextToken() case '[': ++mIndex; return Token (Token::Type_OpenSquare); case ']': ++mIndex; return Token (Token::Type_CloseSquare); case ',': ++mIndex; return Token (Token::Type_Comma); - case '?': ++mIndex; return Token (Token::Type_OneShot); + case '!': ++mIndex; return Token (Token::Type_OneShot); } if (c=='"' || c=='_' || std::isalpha (c) || c==':') @@ -224,54 +224,58 @@ CSMFilter::Token CSMFilter::Parser::getNextToken() return Token (Token::Type_None); } -boost::shared_ptr CSMFilter::Parser::parseImp (bool allowEmpty) +boost::shared_ptr CSMFilter::Parser::parseImp (bool allowEmpty, bool ignoreOneShot) { if (Token token = getNextToken()) { - switch (token.mType) - { - case Token::Type_Keyword_True: + if (token==Token (Token::Type_OneShot)) + token = getNextToken(); - return boost::shared_ptr (new BooleanNode (true)); + if (token) + switch (token.mType) + { + case Token::Type_Keyword_True: - case Token::Type_Keyword_False: + return boost::shared_ptr (new BooleanNode (true)); - return boost::shared_ptr (new BooleanNode (false)); + case Token::Type_Keyword_False: - case Token::Type_Keyword_And: - case Token::Type_Keyword_Or: + return boost::shared_ptr (new BooleanNode (false)); - return parseNAry (token); + case Token::Type_Keyword_And: + case Token::Type_Keyword_Or: - case Token::Type_Keyword_Not: - { - boost::shared_ptr node = parseImp(); + return parseNAry (token); - if (mError) - return boost::shared_ptr(); + case Token::Type_Keyword_Not: + { + boost::shared_ptr node = parseImp(); - return boost::shared_ptr (new NotNode (node)); - } + if (mError) + return boost::shared_ptr(); - case Token::Type_Keyword_Text: + return boost::shared_ptr (new NotNode (node)); + } - return parseText(); + case Token::Type_Keyword_Text: - case Token::Type_Keyword_Value: + return parseText(); - return parseValue(); + case Token::Type_Keyword_Value: - case Token::Type_EOS: + return parseValue(); - if (!allowEmpty) - error(); + case Token::Type_EOS: - return boost::shared_ptr(); + if (!allowEmpty) + error(); - default: + return boost::shared_ptr(); - error(); - } + default: + + error(); + } } return boost::shared_ptr(); @@ -528,7 +532,7 @@ bool CSMFilter::Parser::parse (const std::string& filter, bool allowPredefined) if (!allowPredefined || token==Token (Token::Type_OneShot)) { - boost::shared_ptr node = parseImp (true); + boost::shared_ptr node = parseImp (true, token!=Token (Token::Type_OneShot)); if (mError) return false; diff --git a/apps/opencs/model/filter/parser.hpp b/apps/opencs/model/filter/parser.hpp index fbaf6972e..5700102cf 100644 --- a/apps/opencs/model/filter/parser.hpp +++ b/apps/opencs/model/filter/parser.hpp @@ -31,7 +31,7 @@ namespace CSMFilter Token checkKeywords (const Token& token); ///< Turn string token into keyword token, if possible. - boost::shared_ptr parseImp (bool allowEmpty = false); + boost::shared_ptr parseImp (bool allowEmpty = false, bool ignoreOneShot = false); ///< Will return a null-pointer, if there is nothing more to parse. boost::shared_ptr parseNAry (const Token& keyword);