2
0
mirror of https://github.com/boostorg/parser.git synced 2026-01-19 04:22:13 +00:00

error handling tweaks

This commit is contained in:
Joel de Guzman
2011-11-09 21:37:48 +08:00
parent e1316604bd
commit d9cf84a342
8 changed files with 41 additions and 32 deletions

View File

@@ -2,6 +2,8 @@
* Copyright (C) 2010, 2011 Object Modeling Designs
*/
//~ #define BOOST_SPIRIT_DEBUG
#include "../yaml/parser/flow_def.hpp"
#include "../yaml/parser/scalar_def.hpp"

View File

@@ -9,6 +9,6 @@
12345,
[
92,
["another string", "apple", "Sîne"]
["another string", apple, Sîne]
]
]

View File

@@ -9,6 +9,7 @@
#include <string>
#include <map>
#include <vector>
#include <ostream>
#include <boost/spirit/include/support_extended_variant.hpp>
namespace omd { namespace ast
@@ -26,6 +27,9 @@ namespace omd { namespace ast
inline bool operator==(null_t a, null_t b) { return true; }
inline bool operator!=(null_t a, null_t b) { return false; }
inline std::ostream& operator<<(std::ostream& out, null_t)
{ out << "<null>"; return out; }
struct value_t;
typedef std::map<value_t, value_t> object_t;
typedef std::vector<value_t> array_t;

View File

@@ -22,33 +22,47 @@ namespace omd { namespace parser
std::string source_file;
error_handler(std::string const& source_file = "")
: source_file(source_file) {}
: source_file(source_file)
{
}
void operator()(
Iterator first, Iterator last,
Iterator err_pos, boost::spirit::info const& what) const
{
Iterator eol = err_pos;
int line = boost::spirit::get_line(err_pos);
Iterator line_start = boost::spirit::get_line_start(first, err_pos);
if (source_file != "")
std::cerr << source_file;
std::cerr << "In file " << source_file << ", ";
else
std::cerr << "In ";
if (line != -1)
std::cerr << '(' << line << ')';
std::cerr << "line " << line << ':' << std::endl;
std::cerr << " : Error! Expecting " << what;
std::cerr << "Error! Expecting " << what;
std::cerr << " got:\"";
for (Iterator i = err_pos; i != last; ++i)
std::cerr << " here:" << std::endl;
int ci = 0;
int col;
for (Iterator i = ++line_start; i != last; ++i, ++ci)
{
Iterator::value_type c = *i;
typename Iterator::value_type c = *i;
if (i == err_pos)
col = ci;
if (c == '\r' || c == '\n')
break;
std::cerr << c;
}
std::cerr << "\"" << std::endl;
std::cerr << std::endl;
for (int i = 0; i != col; ++i)
{
std::cerr << '_';
}
std::cerr << "^_" << std::endl;
}
};
}}

View File

@@ -25,12 +25,13 @@ namespace omd { namespace parser
flow(std::string const& source_file = "");
typedef std::pair<ast::value_t, ast::value_t> element_t;
typedef white_space<Iterator> white_space;
typedef white_space<Iterator> white_space_t;
qi::rule<Iterator, ast::value_t(), white_space> value;
qi::rule<Iterator, ast::object_t(), white_space> object;
qi::rule< Iterator, element_t(), white_space > member_pair;
qi::rule<Iterator, ast::array_t(), white_space> array;
qi::rule<Iterator, ast::value_t(), white_space_t> start;
qi::rule<Iterator, ast::value_t(), white_space_t> value;
qi::rule<Iterator, ast::object_t(), white_space_t> object;
qi::rule< Iterator, element_t(), white_space_t> member_pair;
qi::rule<Iterator, ast::array_t(), white_space_t> array;
scalar<Iterator> scalar_value;
typedef omd::parser::error_handler<Iterator> error_handler_t;

View File

@@ -28,7 +28,6 @@ namespace omd { namespace parser
template <typename Iterator>
flow<Iterator>::flow(std::string const& source_file)
: flow::base_type(value),
scalar_value(source_file),
error_handler(error_handler_t(source_file))
{
value =
@@ -39,7 +38,7 @@ namespace omd { namespace parser
object =
'{'
> -(member_pair % ',')
> -(member_pair >> *(',' > member_pair))
> '}'
;
@@ -51,7 +50,7 @@ namespace omd { namespace parser
array =
'['
> -(value % ',')
> -(value >> *(',' > value))
> ']'
;

View File

@@ -36,16 +36,13 @@ namespace omd { namespace parser
template <typename Iterator>
struct scalar : qi::grammar<Iterator, ast::value_t()>
{
scalar(std::string const& source_file = "");
scalar();
qi::rule<Iterator, ast::value_t()> value;
unicode_string<Iterator> string_value;
qi::rule<Iterator, int()> integer_value;
qi::symbols<char, bool> bool_value;
qi::rule<Iterator, ast::null_t() > null_value;
typedef omd::parser::error_handler<Iterator> error_handler_t;
boost::phoenix::function<error_handler_t> const error_handler;
};
}}

View File

@@ -101,9 +101,8 @@ namespace omd { namespace parser
}
template <typename Iterator>
scalar<Iterator>::scalar(std::string const& source_file)
: scalar::base_type(value),
error_handler(error_handler_t(source_file))
scalar<Iterator>::scalar()
: scalar::base_type(value)
{
qi::_val_type _val;
qi::lit_type lit;
@@ -147,15 +146,8 @@ namespace omd { namespace parser
BOOST_SPIRIT_DEBUG_NODES(
(value)
(integer_value)
(bool_value)
(null_value)
);
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::on_error<qi::fail>(value, error_handler(_1, _2, _3, _4));
}
}}