Author: baby-guest Date: 2008-02-29 11:44:16 +0000 (Fri, 29 Feb 2008) New Revision: 5956
Added: software/gofind/boolparser.cpp software/gofind/boolparser.h Removed: software/gofind/BoolExprParser.cpp software/gofind/BoolExprParser.h Log: Fixed parser class Deleted: software/gofind/BoolExprParser.cpp =================================================================== --- software/gofind/BoolExprParser.cpp 2008-02-29 11:42:51 UTC (rev 5955) +++ software/gofind/BoolExprParser.cpp 2008-02-29 11:44:16 UTC (rev 5956) @@ -1,180 +0,0 @@ -/* $Id: BoolExprParser.cpp,v 1.11 2005/05/09 02:43:19 sarrazip Exp $ - BoolExprParser.cpp - Boolean expression parser and syntax tree builder - - boolstuff - Disjunctive Normal Form boolean expression library - Copyright (C) 2002-2005 Pierre Sarrazin <http://sarrazip.com/> - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. -*/ - -#include <boolstuff/BoolExprParser.h> - -#include <cassert> -#include <memory> - -using namespace std; -using namespace boolstuff; - - -BoolExprParser::BoolExprParser() - : curInput(), - curIndex(0) -{ -} - - -BoolExprParser::~BoolExprParser() -{ -} - - -BoolExpr<string> * -BoolExprParser::parse(const std::string &expr) throw(Error) -{ - curInput = expr; - curIndex = 0; - - auto_ptr< BoolExpr<string> > root(parseExpr()); - if (!atEnd()) - throw Error(curIndex, Error::GARBAGE_AT_END); - return root.release(); -} - - -BoolExpr<string> * -BoolExprParser::parseExpr() throw(Error) -{ - auto_ptr< BoolExpr<string> > left(parseTerm()); - - if (!tokenSeen("|")) - return left.release(); - - skipToken("|"); - BoolExpr<string> *right = parseExpr(); // may throw - return new BoolExpr<string>(BoolExpr<string>::OR, left.release(), right); -} - - -BoolExpr<string> * -BoolExprParser::parseTerm() throw(Error) -{ - auto_ptr< BoolExpr<string> > left(parseFactor()); - - if (!tokenSeen("&")) - return left.release(); - - skipToken("&"); - BoolExpr<string> *right = parseTerm(); // may throw - return new BoolExpr<string>(BoolExpr<string>::AND, left.release(), right); -} - - -BoolExpr<string> * -BoolExprParser::parseFactor() throw(Error) -{ - bool v = true; - while (tokenSeen("!")) - { - skipToken("!"); - v = !v; - } - - BoolExpr<string> *atom = parseAtom(); // may throw - if (v) - return atom; - - return new BoolExpr<string>(BoolExpr<string>::NOT, NULL, atom); -} - - -BoolExpr<string> * -BoolExprParser::parseAtom() throw(Error) -{ - skipSpaces(); - size_t startIndex = curIndex; - if (tokenSeen("(")) - { - skipToken("("); - auto_ptr< BoolExpr<string> > expr(parseExpr()); // may throw - - if (!tokenSeen(")")) - throw Error(startIndex, Error::RUNAWAY_PARENTHESIS); - skipToken(")"); - - return expr.release(); - } - - return parseString(); // may throw -} - - -BoolExpr<string> * -BoolExprParser::parseString() throw(Error) -{ - skipSpaces(); - size_t inputLen = curInput.length(); - if (curIndex == inputLen) - throw Error(curIndex, Error::STRING_EXPECTED); - size_t startIndex = curIndex; - while (curIndex < inputLen && isStringChar(curInput[curIndex])) - curIndex++; - if (curIndex == startIndex) - throw Error(startIndex, Error::STRING_EXPECTED); - string s(curInput, startIndex, curIndex - startIndex); - return new BoolExpr<string>(s); -} - - -bool -BoolExprParser::atEnd() -{ - skipSpaces(); - return curIndex == curInput.length(); -} - - -bool -BoolExprParser::tokenSeen(const char *s) -{ - if (s == NULL) - return false; - - skipSpaces(); - return strncmp(curInput.c_str() + curIndex, s, strlen(s)) == 0; -} - - -void -BoolExprParser::skipToken(const char *s) -{ - curIndex += strlen(s); -} - - -void -BoolExprParser::skipSpaces() -{ - size_t inputLen = curInput.length(); - while (curIndex < inputLen && isspace(curInput[curIndex])) - curIndex++; -} - - -bool -BoolExprParser::isStringChar(char c) const -{ - return isalnum(c) || c == '_'; -} Deleted: software/gofind/BoolExprParser.h =================================================================== --- software/gofind/BoolExprParser.h 2008-02-29 11:42:51 UTC (rev 5955) +++ software/gofind/BoolExprParser.h 2008-02-29 11:44:16 UTC (rev 5956) @@ -1,116 +0,0 @@ -/* $Id: BoolExprParser.h,v 1.12 2005/05/09 02:43:19 sarrazip Exp $ - BoolExprParser.h - Boolean expression parser and syntax tree builder - - boolstuff - Disjunctive Normal Form boolean expression library - Copyright (C) 2002-2005 Pierre Sarrazin <http://sarrazip.com/> - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. -*/ - -#ifndef _H_BoolExprParser -#define _H_BoolExprParser - -#include <boolstuff/BoolExpr.h> - -#include <string> - - -namespace boolstuff { - - -/** - Parser for a language of boolean expressions. - The parse() method dynamically allocates a binary tree of nodes that - represents the syntactic structure of a textual boolean expression. -*/ -class BoolExprParser -{ -public: - - /** Error descriptor. */ - class Error - { - public: - /** Possible error codes returned by the parser. */ - enum Code - { - GARBAGE_AT_END, - RUNAWAY_PARENTHESIS, - STRING_EXPECTED - }; - - /** Index (>=0) in the input string where the error was detected. */ - size_t index; - /** Code that gives the type of the error */ - Code code; - - /** - Initializes an error object with the given index and error. - */ - Error(size_t i, Code c) : index(i), code(c) {} - }; - - - /** - Initializes the parser. - */ - BoolExprParser(); - - /** - Destroys the parser and frees the associated resources. - */ - ~BoolExprParser(); - - /** - Parses a textual boolean expression and creates a binary syntax tree. - Dynamically allocates a tree of nodes that represents the - syntactic structure of 'expr'. - The returned tree must eventually be destroyed with operator delete. - - @param expr text of the boolean expression to parse - @returns the root of the created tree - @throws Error describes a parsing error - */ - BoolExpr<std::string> *parse(const std::string &expr) throw(Error); - -private: - - std::string curInput; - size_t curIndex; - - // Implementation methods: - BoolExpr<std::string> *parseExpr() throw(Error); - BoolExpr<std::string> *parseTerm() throw(Error); - BoolExpr<std::string> *parseFactor() throw(Error); - BoolExpr<std::string> *parseAtom() throw(Error); - BoolExpr<std::string> *parseString() throw(Error); - - bool atEnd(); - bool tokenSeen(const char *s); - void skipToken(const char *s); - void skipSpaces(); - bool isStringChar(char c) const; - - // Forbidden operations: - BoolExprParser(const BoolExprParser &); - BoolExprParser &operator = (const BoolExprParser &); -}; - - -} // namespace boolstuff - - -#endif /* _H_BoolExprParser */ Copied: software/gofind/boolparser.cpp (from rev 5955, software/gofind/BoolExprParser.cpp) =================================================================== --- software/gofind/boolparser.cpp (rev 0) +++ software/gofind/boolparser.cpp 2008-02-29 11:44:16 UTC (rev 5956) @@ -0,0 +1,162 @@ +/* + BoolParser.cpp - Boolean expression parser and syntax tree builder + + boolstuff - Disjunctive Normal Form boolean expression library + Copyright (C) 2002-2005 Pierre Sarrazin <http://sarrazip.com/> + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +#include "common.h" +#include "boolparser.h" + +#ifdef USE_UTF8 +#include "utf8.h" +#endif + +#include <cassert> +#include <memory> + +using namespace std; +using namespace boolstuff; + +BoolParser::BoolParser() : curInput(), curIndex(0) +{ +} + +BoolParser::~BoolParser() +{ +} + +BoolExpr<string> * BoolParser::parse(const std::string &expr) throw(Error) +{ + curInput = expr; + curIndex = 0; + + auto_ptr< BoolExpr<string> > root(parseExpr()); + if (!atEnd()) + throw Error(curIndex, Error::GARBAGE_AT_END); + return root.release(); +} + +BoolExpr<string> * BoolParser::parseExpr() throw(Error) +{ + auto_ptr< BoolExpr<string> > left(parseTerm()); + + if (!tokenSeen("|")) + return left.release(); + + skipToken("|"); + BoolExpr<string> *right = parseExpr(); // may throw + return new BoolExpr<string>(BoolExpr<string>::OR, left.release(), right); +} + +BoolExpr<string> * BoolParser::parseTerm() throw(Error) +{ + auto_ptr< BoolExpr<string> > left(parseFactor()); + + if (!tokenSeen("&")) + return left.release(); + + skipToken("&"); + BoolExpr<string> *right = parseTerm(); // may throw + return new BoolExpr<string>(BoolExpr<string>::AND, left.release(), right); +} + +BoolExpr<string> * BoolParser::parseFactor() throw(Error) +{ + bool v = true; + while (tokenSeen("!")) + { + skipToken("!"); + v = !v; + } + + BoolExpr<string> *atom = parseAtom(); // may throw + if (v) + return atom; + + return new BoolExpr<string>(BoolExpr<string>::NOT, NULL, atom); +} + +BoolExpr<string> * BoolParser::parseAtom() throw(Error) +{ + skipSpaces(); + size_t startIndex = curIndex; + if (tokenSeen("(")) + { + skipToken("("); + auto_ptr< BoolExpr<string> > expr(parseExpr()); // may throw + + if (!tokenSeen(")")) + throw Error(startIndex, Error::RUNAWAY_PARENTHESIS); + skipToken(")"); + + return expr.release(); + } + + return parseString(); // may throw +} + +// TODO: support UTF8 +BoolExpr<string> * BoolParser::parseString() throw(Error) +{ + skipSpaces(); + size_t inputLen = curInput.length(); + if (curIndex == inputLen) + throw Error(curIndex, Error::STRING_EXPECTED); + size_t startIndex = curIndex; + while (curIndex < inputLen && isStringChar(curInput[curIndex])) + curIndex++; + if (curIndex == startIndex) + throw Error(startIndex, Error::STRING_EXPECTED); + string s(curInput, startIndex, curIndex - startIndex); + return new BoolExpr<string>(s); +} + +bool BoolParser::atEnd() +{ + skipSpaces(); + return curIndex == curInput.length(); +} + +bool BoolParser::tokenSeen(const char *s) +{ + if (s == NULL) + return false; + + skipSpaces(); + return strncmp(curInput.c_str() + curIndex, s, strlen(s)) == 0; +} + +void BoolParser::skipToken(const char *s) +{ + curIndex += strlen(s); +} + +// TODO: support UTF8 +void BoolParser::skipSpaces() +{ + size_t inputLen = curInput.length(); + while (curIndex < inputLen && isspace(curInput[curIndex])) + curIndex++; +} + +// TODO: support UTF8 +bool BoolParser::isStringChar(char c) const +{ + return isalnum(c) || c == ':' || c == '_'; +} Copied: software/gofind/boolparser.h (from rev 5955, software/gofind/BoolExprParser.h) =================================================================== --- software/gofind/boolparser.h (rev 0) +++ software/gofind/boolparser.h 2008-02-29 11:44:16 UTC (rev 5956) @@ -0,0 +1,108 @@ +/* + BoolParser.h - Boolean expression parser and syntax tree builder + + boolstuff - Disjunctive Normal Form boolean expression library + Copyright (C) 2002-2005 Pierre Sarrazin <http://sarrazip.com/> + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +#ifndef _games_boolparser_h +#define _games_boolparser_h + +#include <boolstuff/BoolExpr.h> + +#include <string> + +/** + Parser for a language of boolean expressions. + The parse() method dynamically allocates a binary tree of nodes that + represents the syntactic structure of a textual boolean expression. +*/ +class BoolParser +{ +public: + + /** Error descriptor. */ + class Error + { + public: + /** Possible error codes returned by the parser. */ + enum Code + { + GARBAGE_AT_END, + RUNAWAY_PARENTHESIS, + STRING_EXPECTED + }; + + /** Index (>=0) in the input string where the error was detected. */ + size_t index; + /** Code that gives the type of the error */ + Code code; + + /** + Initializes an error object with the given index and error. + */ + Error(size_t i, Code c) : index(i), code(c) {} + }; + + + /** + Initializes the parser. + */ + BoolParser(); + + /** + Destroys the parser and frees the associated resources. + */ + ~BoolParser(); + + /** + Parses a textual boolean expression and creates a binary syntax tree. + Dynamically allocates a tree of nodes that represents the + syntactic structure of 'expr'. + The returned tree must eventually be destroyed with operator delete. + + @param expr text of the boolean expression to parse + @returns the root of the created tree + @throws Error describes a parsing error + */ + boolstuff::BoolExpr<std::string> *parse(const std::string &expr) throw(Error); + +private: + + std::string curInput; + size_t curIndex; + + // Implementation methods: + boolstuff::BoolExpr<std::string> *parseExpr() throw(Error); + boolstuff::BoolExpr<std::string> *parseTerm() throw(Error); + boolstuff::BoolExpr<std::string> *parseFactor() throw(Error); + boolstuff::BoolExpr<std::string> *parseAtom() throw(Error); + boolstuff::BoolExpr<std::string> *parseString() throw(Error); + + bool atEnd(); + bool tokenSeen(const char *s); + void skipToken(const char *s); + void skipSpaces(); + bool isStringChar(char c) const; + + // Forbidden operations: + BoolParser(const BoolParser &); + BoolParser &operator = (const BoolParser &); +}; + +#endif /* _games_boolparser_h */ _______________________________________________ Pkg-games-commits mailing list [email protected] http://lists.alioth.debian.org/mailman/listinfo/pkg-games-commits

