diff --git a/test/parse_yaml_test.cpp b/test/parse_yaml_test.cpp index 1f9de563..4eb2601e 100644 --- a/test/parse_yaml_test.cpp +++ b/test/parse_yaml_test.cpp @@ -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 -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 - iterator_type; - iterator_type first(sfirst, slast); - iterator_type last; - first.set_tabchars(1); - - omd::yaml::parser::yaml 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; diff --git a/yaml/ast.hpp b/yaml/ast.hpp index 22efb1dc..a3cd17f6 100644 --- a/yaml/ast.hpp +++ b/yaml/ast.hpp @@ -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 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 diff --git a/yaml/detail/parse_impl.hpp b/yaml/detail/parse_impl.hpp new file mode 100644 index 00000000..f7632efe --- /dev/null +++ b/yaml/detail/parse_impl.hpp @@ -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 + +#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 \ + iterator_type; \ + iterator_type first(sfirst, slast); \ + iterator_type last; \ + first.set_tabchars(1); \ + \ + omd::yaml::parser::yaml 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 diff --git a/yaml/parser/yaml.hpp b/yaml/parser/yaml.hpp index 9423e968..17611936 100644 --- a/yaml/parser/yaml.hpp +++ b/yaml/parser/yaml.hpp @@ -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 #include -namespace omd { namespace yaml { namespace parser -{ + +#ifndef YAML_HEADER_ONLY +#define YAML_HEADER_ONLY 1 +#endif + + +namespace omd { namespace yaml { namespace parser { + template struct yaml : qi::grammar { @@ -37,6 +44,14 @@ namespace omd { namespace yaml { namespace parser typedef omd::yaml::parser::error_handler error_handler_t; boost::phoenix::function 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