mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2024-11-29 01:33:46 +00:00
Throw if glob parsing fails.
This also fixes the warning about an unused boolean variable.
This commit is contained in:
parent
217b41ace2
commit
253af1a3ad
6 changed files with 106 additions and 0 deletions
|
@ -7,6 +7,7 @@ add_library(${PROJECT_NAME}
|
|||
src/glob2regex.cpp
|
||||
src/glob_ast.cpp
|
||||
src/render_ast.cpp
|
||||
src/exceptions.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} SYSTEM
|
||||
|
|
40
lib/glob2regex/include/glob2regex/exceptions.hpp
Normal file
40
lib/glob2regex/include/glob2regex/exceptions.hpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
/* Copyright 2015, 2016, Michele Santullo
|
||||
* This file is part of "dindexer".
|
||||
*
|
||||
* "dindexer" 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef id998F0978CB064D728DDD7BE77F1FD9DC
|
||||
#define id998F0978CB064D728DDD7BE77F1FD9DC
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace g2r {
|
||||
class ParsingError : public std::runtime_error {
|
||||
public:
|
||||
ParsingError ( void ) = delete;
|
||||
~ParsingError ( void ) noexcept = default;
|
||||
ParsingError ( const std::string& parGlob, unsigned int parPosition );
|
||||
|
||||
const std::string& glob ( void ) const noexcept;
|
||||
unsigned int position ( void ) const noexcept;
|
||||
|
||||
private:
|
||||
std::string m_glob;
|
||||
unsigned int m_position;
|
||||
};
|
||||
} //namespace g2r
|
||||
|
||||
#endif
|
|
@ -18,6 +18,7 @@
|
|||
#ifndef id98DC1C17239B4DD38C8697EC91BC3DA4
|
||||
#define id98DC1C17239B4DD38C8697EC91BC3DA4
|
||||
|
||||
#include "glob2regex/exceptions.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace g2r {
|
||||
|
|
49
lib/glob2regex/src/exceptions.cpp
Normal file
49
lib/glob2regex/src/exceptions.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
/* Copyright 2015, 2016, Michele Santullo
|
||||
* This file is part of "dindexer".
|
||||
*
|
||||
* "dindexer" 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "glob2regex/exceptions.hpp"
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
|
||||
namespace g2r {
|
||||
namespace {
|
||||
std::string compose_err_msg (const std::string& parGlob, unsigned int parPosition) {
|
||||
assert(parPosition <= parGlob.size());
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << "Error parsing glob \"" << parGlob << "\" at character " << (parPosition + 1);
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
ParsingError::ParsingError (const std::string& parGlob, unsigned int parPosition) :
|
||||
std::runtime_error(compose_err_msg(parGlob, parPosition)),
|
||||
m_glob(parGlob),
|
||||
m_position(parPosition)
|
||||
{
|
||||
}
|
||||
|
||||
const std::string& ParsingError::glob() const noexcept {
|
||||
return m_glob;
|
||||
}
|
||||
|
||||
unsigned int ParsingError::position() const noexcept {
|
||||
return m_position;
|
||||
}
|
||||
} //namespace g2r
|
||||
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
|
||||
#include "glob_ast.hpp"
|
||||
#include "glob2regex/exceptions.hpp"
|
||||
#include <boost/spirit/include/qi.hpp>
|
||||
//#include <boost/spirit/include/qi_core.hpp>
|
||||
//#include <boost/spirit/include/qi_parse.hpp>
|
||||
|
@ -30,6 +31,7 @@
|
|||
#include <boost/phoenix/stl/container.hpp>
|
||||
#include <boost/phoenix/bind/bind_member_function.hpp>
|
||||
#include <boost/phoenix/operator.hpp>
|
||||
#include <ciso646>
|
||||
|
||||
//#define AST_DEBUG_OUTPUT
|
||||
|
||||
|
@ -111,6 +113,9 @@ namespace g2r {
|
|||
std::cout << ", glob_ast.size() = " << glob_ast.size() << std::endl;
|
||||
#endif
|
||||
|
||||
if (not parse_ret or parGlob.end() != glob_beg)
|
||||
throw ParsingError(parGlob, parGlob.end() - glob_beg);
|
||||
|
||||
return glob_ast;
|
||||
}
|
||||
} //namespace g2r
|
||||
|
|
|
@ -61,4 +61,14 @@ TEST(glob2regex, convert) {
|
|||
const auto auto_regex = g2r::convert(glob);
|
||||
EXPECT_EQ(expected_regex, auto_regex);
|
||||
}
|
||||
|
||||
{
|
||||
const std::string glob = "nested_{example,*sample,test";
|
||||
EXPECT_THROW(g2r::convert(glob), g2r::ParsingError);
|
||||
}
|
||||
|
||||
{
|
||||
const std::string glob = "]";
|
||||
EXPECT_THROW(g2r::convert(glob), g2r::ParsingError);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue