mirror of
https://github.com/boostorg/parser.git
synced 2026-01-26 06:42:25 +00:00
Promote testing parse() to a part of the parser interface; make it header-only
or compiled via a macro.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Copyright (C) 2010, 2011, 2012 Object Modeling Designs
|
||||
* consultomd.com
|
||||
* Copyright (C) 2017 Zach Laine
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
@@ -13,31 +14,6 @@
|
||||
|
||||
#include <boost/spirit/include/classic_position_iterator.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
bool parse(
|
||||
std::istream& is,
|
||||
omd::yaml::ast::value_t& result,
|
||||
std::string const& source_file = "")
|
||||
{
|
||||
std::string file;
|
||||
std::getline(is, file, '\0');
|
||||
|
||||
typedef std::string::const_iterator base_iterator_type;
|
||||
base_iterator_type sfirst(file.begin());
|
||||
base_iterator_type slast(file.end());
|
||||
|
||||
typedef boost::spirit::classic::position_iterator<base_iterator_type>
|
||||
iterator_type;
|
||||
iterator_type first(sfirst, slast);
|
||||
iterator_type last;
|
||||
first.set_tabchars(1);
|
||||
|
||||
omd::yaml::parser::yaml<iterator_type> p(source_file);
|
||||
|
||||
return boost::spirit::qi::parse(first, last, p, result);
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Main program
|
||||
@@ -83,13 +59,10 @@ int main(int argc, char **argv)
|
||||
namespace qi = boost::spirit::qi;
|
||||
|
||||
value_t result;
|
||||
if (parse(in, result, filename))
|
||||
if (omd::yaml::parser::parse_yaml(in, result, filename))
|
||||
{
|
||||
std::cout << "success: \n";
|
||||
|
||||
// link the aliases
|
||||
omd::yaml::ast::link_yaml(result);
|
||||
|
||||
// print the result (2-spaces indent with all aliases expanded)
|
||||
omd::yaml::ast::print_yaml<2, true>(std::cout, result);
|
||||
std::cout << std::endl;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Copyright (C) 2010, 2011, 2012 Michael Caisse, Object Modeling Designs
|
||||
* consultomd.com
|
||||
* Copyright (C) 2017 Zach Laine
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
@@ -79,8 +80,12 @@ namespace omd { namespace yaml { namespace ast
|
||||
template <int Spaces, bool ExpandAliases>
|
||||
std::ostream& print_yaml(std::ostream& out, value_t const& val);
|
||||
|
||||
inline std::ostream& print_yaml(std::ostream& out, value_t const& val)
|
||||
{ return print_yaml<2, false>(out, val); }
|
||||
|
||||
// ---------------------------------------------------
|
||||
}}}
|
||||
|
||||
#include "detail/ast_impl.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
46
yaml/detail/parse_impl.hpp
Normal file
46
yaml/detail/parse_impl.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (C) 2017 Zach Laine
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
|
||||
#if !defined(OMD_DETAIL_PARSE_IMPL_HPP)
|
||||
#define OMD_DETAIL_PARSE_IMPL_HPP
|
||||
|
||||
#include <boost/spirit/include/classic_position_iterator.hpp>
|
||||
|
||||
#define PARSE_YAML_IMPLEMENTATION() \
|
||||
namespace omd { namespace yaml { namespace parser { \
|
||||
\
|
||||
bool parse_yaml(std::istream& is, ast::value_t& result, std::string const& source_file) \
|
||||
{ \
|
||||
std::string file; \
|
||||
std::getline(is, file, '\0'); \
|
||||
\
|
||||
typedef std::string::const_iterator base_iterator_type; \
|
||||
base_iterator_type sfirst(file.begin()); \
|
||||
base_iterator_type slast(file.end()); \
|
||||
\
|
||||
typedef boost::spirit::classic::position_iterator<base_iterator_type> \
|
||||
iterator_type; \
|
||||
iterator_type first(sfirst, slast); \
|
||||
iterator_type last; \
|
||||
first.set_tabchars(1); \
|
||||
\
|
||||
omd::yaml::parser::yaml<iterator_type> p(source_file); \
|
||||
\
|
||||
bool const retval = \
|
||||
boost::spirit::qi::parse(first, last, p, result); \
|
||||
if (retval) \
|
||||
ast::link_yaml(result); \
|
||||
return retval; \
|
||||
} \
|
||||
\
|
||||
} } }
|
||||
|
||||
#if YAML_HEADER_ONLY
|
||||
PARSE_YAML_IMPLEMENTATION()
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Copyright (C) 2010, 2011, 2012 Object Modeling Designs
|
||||
* consultomd.com
|
||||
* Copyright (C) 2017 Zach Laine
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
@@ -16,8 +17,14 @@
|
||||
#include <string>
|
||||
#include <boost/fusion/adapted/std_pair.hpp>
|
||||
|
||||
namespace omd { namespace yaml { namespace parser
|
||||
{
|
||||
|
||||
#ifndef YAML_HEADER_ONLY
|
||||
#define YAML_HEADER_ONLY 1
|
||||
#endif
|
||||
|
||||
|
||||
namespace omd { namespace yaml { namespace parser {
|
||||
|
||||
template <typename Iterator>
|
||||
struct yaml : qi::grammar<Iterator, ast::value_t()>
|
||||
{
|
||||
@@ -37,6 +44,14 @@ namespace omd { namespace yaml { namespace parser
|
||||
typedef omd::yaml::parser::error_handler<Iterator> error_handler_t;
|
||||
boost::phoenix::function<error_handler_t> const error_handler;
|
||||
};
|
||||
}}}
|
||||
|
||||
#if YAML_HEADER_ONLY
|
||||
inline
|
||||
#endif
|
||||
bool parse_yaml(std::istream& is, ast::value_t& result, std::string const& source_file = "");
|
||||
|
||||
} } }
|
||||
|
||||
#include "../detail/parse_impl.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user