diff options
Diffstat (limited to 'src/boost/libs/spirit/example/x3')
96 files changed, 7632 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/example/x3/Jamfile b/src/boost/libs/spirit/example/x3/Jamfile new file mode 100644 index 00000000..1e8bb8f3 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/Jamfile @@ -0,0 +1,65 @@ +#============================================================================== +# Copyright (c) 2001-2014 Joel de Guzman +# +# 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) +#============================================================================== +project spirit-x3-example + : requirements + <include>. + <c++-template-depth>512 + <toolset>gcc:<cxxflags>-std=c++1y + <toolset>clang:<cxxflags>-std=c++1y + <toolset>darwin:<cxxflags>-std=c++1y + : + : + ; + +exe x3_num_list1 : num_list/num_list1.cpp ; +exe x3_num_list2 : num_list/num_list2.cpp ; +exe x3_num_list3 : num_list/num_list3.cpp ; +exe x3_num_list4 : num_list/num_list4.cpp ; + +exe x3_actions : actions.cpp ; +exe x3_complex_number : complex_number.cpp ; +exe x3_sum : sum.cpp ; +exe x3_roman : roman.cpp ; +exe x3_employee : employee.cpp ; + +exe x3_rexpr : rexpr/rexpr_min/rexpr.cpp ; + +build-project rexpr/rexpr_full ; + +exe x3_calc1 : calc/calc1.cpp ; +exe x3_calc2 : calc/calc2.cpp ; +exe x3_calc4 : calc/calc4.cpp ; +exe x3_calc4b : calc/calc4b.cpp ; +exe x3_calc5 : calc/calc5.cpp ; +exe x3_calc6 : calc/calc6.cpp ; + +exe x3_calc7 : + calc/calc7/vm.cpp + calc/calc7/compiler.cpp + calc/calc7/expression.cpp + calc/calc7/main.cpp +; + +exe x3_calc8 : + /boost//filesystem + calc/calc8/vm.cpp + calc/calc8/compiler.cpp + calc/calc8/expression.cpp + calc/calc8/statement.cpp + calc/calc8/main.cpp +; + +exe x3_calc9 : + /boost//filesystem + calc/calc9/vm.cpp + calc/calc9/compiler.cpp + calc/calc9/expression.cpp + calc/calc9/statement.cpp + calc/calc9/main.cpp +; + +exe x3_tuple : attributes/tuple.cpp ;
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/actions.cpp b/src/boost/libs/spirit/example/x3/actions.cpp new file mode 100644 index 00000000..7a80fe83 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/actions.cpp @@ -0,0 +1,52 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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) +=============================================================================*/ +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include <iostream> + +// Presented are various ways to attach semantic actions +// * Using plain function pointer +// * Using simple function object + +namespace client +{ + namespace x3 = boost::spirit::x3; + using x3::_attr; + + struct print_action + { + template <typename Context> + void operator()(Context const& ctx) const + { + std::cout << _attr(ctx) << std::endl; + } + }; +} + +int main() +{ + using boost::spirit::x3::int_; + using boost::spirit::x3::parse; + using client::print_action; + + { // example using function object + + char const *first = "{43}", *last = first + std::strlen(first); + parse(first, last, '{' >> int_[print_action()] >> '}'); + } + + { // example using C++14 lambda + + using boost::spirit::x3::_attr; + char const *first = "{44}", *last = first + std::strlen(first); + auto f = [](auto& ctx){ std::cout << _attr(ctx) << std::endl; }; + parse(first, last, '{' >> int_[f] >> '}'); + } + + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/annotation.cpp b/src/boost/libs/spirit/example/x3/annotation.cpp new file mode 100644 index 00000000..12dacfbb --- /dev/null +++ b/src/boost/libs/spirit/example/x3/annotation.cpp @@ -0,0 +1,246 @@ +/*============================================================================= + Copyright (c) 2002-2018 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Based on the employee parser (see employee.cpp), this example shows how +// to annotate the AST with the iterator positions for access to the source +// code when post processing. This example also shows how to "inject" client +// data, using the "with" directive, that the handlers can access. +// +// [ JDG May 9, 2007 ] +// [ JDG May 13, 2015 ] spirit X3 +// [ JDG Feb 22, 2018 ] Parser annotations for spirit X3 +// +// I would like to thank Rainbowverse, llc (https://primeorbial.com/) +// for sponsoring this work and donating it to the community. +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include <boost/fusion/include/adapt_struct.hpp> +#include <boost/fusion/include/io.hpp> + +#include <iostream> +#include <string> + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // Our AST (employee and person structs) + /////////////////////////////////////////////////////////////////////////// + namespace x3 = boost::spirit::x3; + + struct person : x3::position_tagged + { + person( + std::string const& first_name = "" + , std::string const& last_name = "" + ) + : first_name(first_name) + , last_name(last_name) + {} + + std::string first_name, last_name; + }; + + struct employee : x3::position_tagged + { + int age; + person who; + double salary; + }; + + using boost::fusion::operator<<; +}} + +// We need to tell fusion about our employee struct +// to make it a first-class fusion citizen. This has to +// be in global scope. + +BOOST_FUSION_ADAPT_STRUCT(client::ast::person, + first_name, last_name +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::employee, + age, who, salary +) + +namespace client +{ + namespace parser + { + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + /////////////////////////////////////////////////////////////////////// + // Our annotation handler + /////////////////////////////////////////////////////////////////////// + + // tag used to get the position cache from the context + struct position_cache_tag; + + struct annotate_position + { + template <typename T, typename Iterator, typename Context> + inline void on_success(Iterator const& first, Iterator const& last + , T& ast, Context const& context) + { + auto& position_cache = x3::get<position_cache_tag>(context).get(); + position_cache.annotate(ast, first, last); + } + }; + + /////////////////////////////////////////////////////////////////////// + // Our employee parser + /////////////////////////////////////////////////////////////////////// + + using x3::int_; + using x3::double_; + using x3::lexeme; + using ascii::char_; + + struct quoted_string_class; + struct person_class; + struct employee_class; + + x3::rule<quoted_string_class, std::string> const quoted_string = "quoted_string"; + x3::rule<person_class, ast::person> const person = "person"; + x3::rule<employee_class, ast::employee> const employee = "employee"; + + auto const quoted_string_def = lexeme['"' >> +(char_ - '"') >> '"']; + auto const person_def = quoted_string >> ',' >> quoted_string; + + auto const employee_def = + '{' + >> int_ >> ',' + >> person >> ',' + >> double_ + >> '}' + ; + + auto const employees = employee >> *(',' >> employee); + + BOOST_SPIRIT_DEFINE(quoted_string, person, employee); + + struct quoted_string_class {}; + struct person_class : annotate_position {}; + struct employee_class : annotate_position {}; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Our main parse entry point +/////////////////////////////////////////////////////////////////////////////// + +using iterator_type = std::string::const_iterator; +using position_cache = boost::spirit::x3::position_cache<std::vector<iterator_type>>; + +std::vector<client::ast::employee> +parse(std::string const& input, position_cache& positions) +{ + using boost::spirit::x3::ascii::space; + + std::vector<client::ast::employee> ast; + iterator_type iter = input.begin(); + iterator_type const end = input.end(); + + using boost::spirit::x3::with; + + // Our parser + using client::parser::employees; + using client::parser::position_cache_tag; + + auto const parser = + // we pass our position_cache to the parser so we can access + // it later in our on_sucess handlers + with<position_cache_tag>(std::ref(positions)) + [ + employees + ]; + + bool r = phrase_parse(iter, end, parser, space, ast); + + if (r && iter == end) + { + std::cout << boost::fusion::tuple_open('['); + std::cout << boost::fusion::tuple_close(']'); + std::cout << boost::fusion::tuple_delimiter(", "); + + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + + for (auto const& emp : ast) + { + std::cout << "got: " << emp << std::endl; + } + std::cout << "\n-------------------------\n"; + + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + ast.clear(); + } + return ast; +} + +// Sample input: + +std::string input = R"( +{ + 23, + "Amanda", + "Stefanski", + 1000.99 +}, +{ + 35, + "Angie", + "Chilcote", + 2000.99 +}, +{ + 43, + "Dannie", + "Dillinger", + 3000.99 +}, +{ + 22, + "Dorene", + "Dole", + 2500.99 +}, +{ + 38, + "Rossana", + "Rafferty", + 5000.99 +} +)"; + +int +main() +{ + position_cache positions{input.begin(), input.end()}; + auto ast = parse(input, positions); + + // Get the source of the 2nd employee and print it + auto pos = positions.position_of(ast[1]); // zero based of course! + std::cout << "Here's the 2nd employee:" << std::endl; + std::cout << std::string(pos.begin(), pos.end()) << std::endl; + std::cout << "-------------------------\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/attributes/tuple.cpp b/src/boost/libs/spirit/example/x3/attributes/tuple.cpp new file mode 100644 index 00000000..979127a7 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/attributes/tuple.cpp @@ -0,0 +1,51 @@ + +/*============================================================================= + Copyright (c) 2019 Tom Tan + + 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) +=============================================================================*/ +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include <boost/fusion/adapted/std_tuple.hpp> + +#include <iostream> +#include <tuple> +#include <string> + +// +// X3 does not support more than one attribute anymore in the parse function, +// this example show how to wrap multiple attributes into one leveraging std::tuple. +// + +std::tuple<uint32_t, uint32_t, uint32_t> parse_message_prefix_revision(const std::string &s) +{ + + namespace x3 = boost::spirit::x3; + + auto const uint_3_digits = x3::uint_parser<std::uint32_t, 10, 3, 3>{}; + auto const uint_4_digits = x3::uint_parser<std::uint32_t, 10, 4, 4>{}; + + auto iter = s.cbegin(); + auto end_iter = s.cend(); + + std::tuple<uint32_t, uint32_t, uint32_t> length_id_revision; + + x3::parse(iter, end_iter, + uint_4_digits >> uint_4_digits >> uint_3_digits, + length_id_revision); + + return length_id_revision; +} + +int main() +{ + std::string s = "00200060001"; + + std::cout << "parsing " << s << '\n'; + auto [len, id, rev] = parse_message_prefix_revision(s); + std::cout << "length = " << len << '\n' + << "id = " << id << '\n' + << "revision =" << rev; +}
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/calc/calc1.cpp b/src/boost/libs/spirit/example/x3/calc/calc1.cpp new file mode 100644 index 00000000..0a8e0c96 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc1.cpp @@ -0,0 +1,123 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Plain calculator example demonstrating the grammar. The parser is a +// syntax checker only and does not do any semantic evaluation. +// +// [ JDG May 10, 2002 ] spirit 1 +// [ JDG March 4, 2007 ] spirit 2 +// [ JDG February 21, 2011 ] spirit 2.5 +// [ JDG June 6, 2014 ] spirit x3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/fusion/include/adapt_struct.hpp> + +#include <iostream> +#include <string> +#include <list> +#include <numeric> + +namespace x3 = boost::spirit::x3; + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using x3::uint_; + using x3::char_; + + x3::rule<class expression> const expression("expression"); + x3::rule<class term> const term("term"); + x3::rule<class factor> const factor("factor"); + + auto const expression_def = + term + >> *( ('+' >> term) + | ('-' >> term) + ) + ; + + auto const term_def = + factor + >> *( ('*' >> factor) + | ('/' >> factor) + ) + ; + + auto const factor_def = + uint_ + | '(' >> expression >> ')' + | ('-' >> factor) + | ('+' >> factor) + ; + + BOOST_SPIRIT_DEFINE( + expression + , term + , factor + ); + + auto calculator = expression; + } + + using calculator_grammar::calculator; + +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + auto& calc = client::calculator; // Our grammar + + iterator_type iter = str.begin(); + iterator_type end = str.end(); + boost::spirit::x3::ascii::space_type space; + bool r = phrase_parse(iter, end, calc, space); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "stopped at: \"" << rest << "\"\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc2.cpp b/src/boost/libs/spirit/example/x3/calc/calc2.cpp new file mode 100644 index 00000000..6c0a2444 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc2.cpp @@ -0,0 +1,139 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A Calculator example demonstrating the grammar and semantic actions +// using lambda functions. The parser prints code suitable for a stack +// based virtual machine. +// +// [ JDG May 10, 2002 ] spirit 1 +// [ JDG March 4, 2007 ] spirit 2 +// [ JDG February 21, 2011 ] spirit 2.5 +// [ JDG June 6, 2014 ] spirit x3 (from qi calc2, but using lambda functions) +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/fusion/include/adapt_struct.hpp> + +#include <iostream> +#include <string> +#include <list> +#include <numeric> + +namespace x3 = boost::spirit::x3; + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // Semantic actions + ////////////////////////////////////////////////////////1/////////////////////// + namespace + { + using x3::_attr; + + auto do_int = [](auto& ctx) { std::cout << "push " << _attr(ctx) << std::endl; }; + auto do_add = []{ std::cout << "add\n"; }; + auto do_subt = []{ std::cout << "subtract\n"; }; + auto do_mult = []{ std::cout << "mult\n"; }; + auto do_div = []{ std::cout << "divide\n"; }; + auto do_neg = []{ std::cout << "negate\n"; }; + } + + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using x3::uint_; + using x3::char_; + + x3::rule<class expression> const expression("expression"); + x3::rule<class term> const term("term"); + x3::rule<class factor> const factor("factor"); + + auto const expression_def = + term + >> *( ('+' >> term [do_add]) + | ('-' >> term [do_subt]) + ) + ; + + auto const term_def = + factor + >> *( ('*' >> factor [do_mult]) + | ('/' >> factor [do_div]) + ) + ; + + auto const factor_def = + uint_ [do_int] + | '(' >> expression >> ')' + | ('-' >> factor [do_neg]) + | ('+' >> factor) + ; + + BOOST_SPIRIT_DEFINE( + expression + , term + , factor + ); + + auto calculator = expression; + } + + using calculator_grammar::calculator; + +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + auto& calc = client::calculator; // Our grammar + + iterator_type iter = str.begin(); + iterator_type end = str.end(); + boost::spirit::x3::ascii::space_type space; + bool r = phrase_parse(iter, end, calc, space); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "stopped at: \"" << rest << "\"\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc4.cpp b/src/boost/libs/spirit/example/x3/calc/calc4.cpp new file mode 100644 index 00000000..289e2cd9 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc4.cpp @@ -0,0 +1,273 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A Calculator example demonstrating generation of AST. The AST, +// once created, is traversed, 1) To print its contents and +// 2) To evaluate the result. +// +// [ JDG April 28, 2008 ] For BoostCon 2008 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG January 9, 2013 ] Spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/fusion/include/adapt_struct.hpp> + +#include <iostream> +#include <string> +#include <list> +#include <numeric> + +namespace x3 = boost::spirit::x3; + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + struct nil {}; + struct signed_; + struct program; + + struct operand : x3::variant< + nil + , unsigned int + , x3::forward_ast<signed_> + , x3::forward_ast<program> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + struct signed_ + { + char sign; + operand operand_; + }; + + struct operation + { + char operator_; + operand operand_; + }; + + struct program + { + operand first; + std::list<operation> rest; + }; +}} + +BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, + sign, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::program, + first, rest +) + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST Printer + /////////////////////////////////////////////////////////////////////////// + struct printer + { + typedef void result_type; + + void operator()(nil) const {} + void operator()(unsigned int n) const { std::cout << n; } + + void operator()(operation const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': std::cout << " add"; break; + case '-': std::cout << " subt"; break; + case '*': std::cout << " mult"; break; + case '/': std::cout << " div"; break; + } + } + + void operator()(signed_ const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': std::cout << " neg"; break; + case '+': std::cout << " pos"; break; + } + } + + void operator()(program const& x) const + { + boost::apply_visitor(*this, x.first); + for (operation const& oper: x.rest) + { + std::cout << ' '; + (*this)(oper); + } + } + }; + + /////////////////////////////////////////////////////////////////////////// + // The AST evaluator + /////////////////////////////////////////////////////////////////////////// + struct eval + { + typedef int result_type; + + int operator()(nil) const { BOOST_ASSERT(0); return 0; } + int operator()(unsigned int n) const { return n; } + + int operator()(int lhs, operation const& x) const + { + int rhs = boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': return lhs + rhs; + case '-': return lhs - rhs; + case '*': return lhs * rhs; + case '/': return lhs / rhs; + } + BOOST_ASSERT(0); + return 0; + } + + int operator()(signed_ const& x) const + { + int rhs = boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': return -rhs; + case '+': return +rhs; + } + BOOST_ASSERT(0); + return 0; + } + + int operator()(program const& x) const + { + return std::accumulate( + x.rest.begin(), x.rest.end() + , boost::apply_visitor(*this, x.first) + , *this); + } + }; +}} + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using x3::uint_; + using x3::char_; + + x3::rule<class expression, ast::program> const expression("expression"); + x3::rule<class term, ast::program> const term("term"); + x3::rule<class factor, ast::operand> const factor("factor"); + + auto const expression_def = + term + >> *( (char_('+') >> term) + | (char_('-') >> term) + ) + ; + + auto const term_def = + factor + >> *( (char_('*') >> factor) + | (char_('/') >> factor) + ) + ; + + auto const factor_def = + uint_ + | '(' >> expression >> ')' + | (char_('-') >> factor) + | (char_('+') >> factor) + ; + + BOOST_SPIRIT_DEFINE( + expression + , term + , factor + ); + + auto calculator = expression; + } + + using calculator_grammar::calculator; + +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + typedef client::ast::program ast_program; + typedef client::ast::printer ast_print; + typedef client::ast::eval ast_eval; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + auto& calc = client::calculator; // Our grammar + ast_program program; // Our program (AST) + ast_print print; // Prints the program + ast_eval eval; // Evaluates the program + + iterator_type iter = str.begin(); + iterator_type end = str.end(); + boost::spirit::x3::ascii::space_type space; + bool r = phrase_parse(iter, end, calc, space, program); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + print(program); + std::cout << "\nResult: " << eval(program) << std::endl; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "stopped at: \"" << rest << "\"\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc4b.cpp b/src/boost/libs/spirit/example/x3/calc/calc4b.cpp new file mode 100644 index 00000000..176359ab --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc4b.cpp @@ -0,0 +1,273 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A Calculator example demonstrating generation of AST. The AST, +// once created, is traversed, 1) To print its contents and +// 2) To evaluate the result. +// +// [ JDG April 28, 2008 ] For BoostCon 2008 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG January 9, 2013 ] Spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#if defined(_MSC_VER) +# pragma warning(disable: 4345) +#endif + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/fusion/include/adapt_struct.hpp> + +#include <iostream> +#include <string> +#include <list> +#include <numeric> + +namespace x3 = boost::spirit::x3; + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + struct nil {}; + struct signed_; + struct program; + + struct operand : x3::variant< + nil + , unsigned int + , x3::forward_ast<signed_> + , x3::forward_ast<program> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + struct signed_ + { + char sign; + operand operand_; + }; + + struct operation + { + char operator_; + operand operand_; + }; + + struct program + { + operand first; + std::list<operation> rest; + }; +}} + +BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, + sign, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::program, + first, rest +) + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST Printer + /////////////////////////////////////////////////////////////////////////// + struct printer + { + typedef void result_type; + + void operator()(nil) const {} + void operator()(unsigned int n) const { std::cout << n; } + + void operator()(operation const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': std::cout << " add"; break; + case '-': std::cout << " subt"; break; + case '*': std::cout << " mult"; break; + case '/': std::cout << " div"; break; + } + } + + void operator()(signed_ const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': std::cout << " neg"; break; + case '+': std::cout << " pos"; break; + } + } + + void operator()(program const& x) const + { + boost::apply_visitor(*this, x.first); + for (operation const& oper: x.rest) + { + std::cout << ' '; + (*this)(oper); + } + } + }; + + /////////////////////////////////////////////////////////////////////////// + // The AST evaluator + /////////////////////////////////////////////////////////////////////////// + struct eval + { + typedef int result_type; + + int operator()(nil) const { BOOST_ASSERT(0); return 0; } + int operator()(unsigned int n) const { return n; } + + int operator()(int lhs, operation const& x) const + { + int rhs = boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': return lhs + rhs; + case '-': return lhs - rhs; + case '*': return lhs * rhs; + case '/': return lhs / rhs; + } + BOOST_ASSERT(0); + return 0; + } + + int operator()(signed_ const& x) const + { + int rhs = boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': return -rhs; + case '+': return +rhs; + } + BOOST_ASSERT(0); + return 0; + } + + int operator()(program const& x) const + { + return std::accumulate( + x.rest.begin(), x.rest.end() + , boost::apply_visitor(*this, x.first) + , *this); + } + }; +}} + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using x3::uint_; + using x3::char_; + + x3::rule<class expression, ast::program> const expression("expression"); + x3::rule<class term, ast::program> const term("term"); + x3::rule<class factor, ast::operand> const factor("factor"); + + auto const expression_def = + term + >> *( (char_('+') >> term) + | (char_('-') >> term) + ) + ; + + auto const term_def = + factor + >> *( (char_('*') >> factor) + | (char_('/') >> factor) + ) + ; + + auto const factor_def = + uint_ + | '(' >> expression >> ')' + | (char_('-') >> factor) + | (char_('+') >> factor) + ; + + BOOST_SPIRIT_DEFINE(expression, term, factor); + + auto calculator = expression; + } + + using calculator_grammar::calculator; + +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + typedef client::ast::program ast_program; + typedef client::ast::printer ast_print; + typedef client::ast::eval ast_eval; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + auto& calc = client::calculator; // Our grammar + ast_program program; // Our program (AST) + ast_print print; // Prints the program + ast_eval eval; // Evaluates the program + + iterator_type iter = str.begin(); + iterator_type end = str.end(); + boost::spirit::x3::ascii::space_type space; + bool r = phrase_parse(iter, end, calc, space, program); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + print(program); + std::cout << "\nResult: " << eval(program) << std::endl; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "stopped at: \"" << rest << "\"\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc4c/calc4c.cpp b/src/boost/libs/spirit/example/x3/calc/calc4c/calc4c.cpp new file mode 100644 index 00000000..91d396b5 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc4c/calc4c.cpp @@ -0,0 +1,77 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + Copyright (c) 2013-2014 Agustin Berge + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A Calculator example demonstrating generation of AST. The AST, +// once created, is traversed, 1) To print its contents and +// 2) To evaluate the result. +// +// [ JDG April 28, 2008 ] For BoostCon 2008 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG January 9, 2013 ] Spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include "grammar.hpp" + +#include <iostream> +#include <string> + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + typedef client::ast::program ast_program; + typedef client::ast::printer ast_print; + typedef client::ast::eval ast_eval; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + auto& calc = client::calculator; // Our grammar + ast_program program; // Our program (AST) + ast_print print; // Prints the program + ast_eval eval; // Evaluates the program + + iterator_type iter = str.begin(); + iterator_type end = str.end(); + boost::spirit::x3::ascii::space_type space; + bool r = phrase_parse(iter, end, calc, space, program); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + print(program); + std::cout << "\nResult: " << eval(program) << std::endl; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "stopped at: \"" << rest << "\"\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc4c/grammar.cpp b/src/boost/libs/spirit/example/x3/calc/calc4c/grammar.cpp new file mode 100644 index 00000000..8d1526b2 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc4c/grammar.cpp @@ -0,0 +1,68 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + Copyright (c) 2013-2014 Agustin Berge + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A Calculator example demonstrating generation of AST. The AST, +// once created, is traversed, 1) To print its contents and +// 2) To evaluate the result. +// +// [ JDG April 28, 2008 ] For BoostCon 2008 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG January 9, 2013 ] Spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include "grammar.hpp" + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using x3::uint_; + using x3::char_; + + x3::rule<class expression, ast::program> const expression("expression"); + x3::rule<class term, ast::program> const term("term"); + x3::rule<class factor, ast::operand> const factor("factor"); + + auto const expression_def = + term + >> *( (char_('+') >> term) + | (char_('-') >> term) + ) + ; + + auto const term_def = + factor + >> *( (char_('*') >> factor) + | (char_('/') >> factor) + ) + ; + + auto const factor_def = + uint_ + | '(' >> expression >> ')' + | (char_('-') >> factor) + | (char_('+') >> factor) + ; + + BOOST_SPIRIT_DEFINE( + expression + , term + , factor + ); + + parser_type calculator() + { + return expression; + } + } +}
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/calc/calc4c/grammar.hpp b/src/boost/libs/spirit/example/x3/calc/calc4c/grammar.hpp new file mode 100644 index 00000000..89d867c6 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc4c/grammar.hpp @@ -0,0 +1,192 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + Copyright (c) 2013-2014 Agustin Berge + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A Calculator example demonstrating generation of AST. The AST, +// once created, is traversed, 1) To print its contents and +// 2) To evaluate the result. +// +// [ JDG April 28, 2008 ] For BoostCon 2008 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG January 9, 2013 ] Spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#if defined(_MSC_VER) +# pragma warning(disable: 4345) +#endif + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/variant/recursive_variant.hpp> +#include <boost/variant/apply_visitor.hpp> +#include <boost/fusion/include/adapt_struct.hpp> + +#include <list> +#include <numeric> + +namespace x3 = boost::spirit::x3; + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + struct nil {}; + struct signed_; + struct program; + + typedef x3::variant< + nil + , unsigned int + , x3::forward_ast<signed_> + , x3::forward_ast<program> + > + operand; + + struct signed_ + { + char sign; + operand operand_; + }; + + struct operation + { + char operator_; + operand operand_; + }; + + struct program + { + operand first; + std::list<operation> rest; + }; +}} + +BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, + sign, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::program, + first, rest +) + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST Printer + /////////////////////////////////////////////////////////////////////////// + struct printer + { + typedef void result_type; + + void operator()(nil) const {} + void operator()(unsigned int n) const { std::cout << n; } + + void operator()(operation const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': std::cout << " add"; break; + case '-': std::cout << " subt"; break; + case '*': std::cout << " mult"; break; + case '/': std::cout << " div"; break; + } + } + + void operator()(signed_ const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': std::cout << " neg"; break; + case '+': std::cout << " pos"; break; + } + } + + void operator()(program const& x) const + { + boost::apply_visitor(*this, x.first); + for (operation const& oper: x.rest) + { + std::cout << ' '; + (*this)(oper); + } + } + }; + + /////////////////////////////////////////////////////////////////////////// + // The AST evaluator + /////////////////////////////////////////////////////////////////////////// + struct eval + { + typedef int result_type; + + int operator()(nil) const { BOOST_ASSERT(0); return 0; } + int operator()(unsigned int n) const { return n; } + + int operator()(int lhs, operation const& x) const + { + int rhs = boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': return lhs + rhs; + case '-': return lhs - rhs; + case '*': return lhs * rhs; + case '/': return lhs / rhs; + } + BOOST_ASSERT(0); + return 0; + } + + int operator()(signed_ const& x) const + { + int rhs = boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': return -rhs; + case '+': return +rhs; + } + BOOST_ASSERT(0); + return 0; + } + + int operator()(program const& x) const + { + return std::accumulate( x.rest.begin(), x.rest.end() + , boost::apply_visitor(*this, x.first) + , *this); + } + }; +}} + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using parser_type = + x3::any_parser< + std::string::const_iterator + , ast::program + , decltype(x3::make_context<x3::skipper_tag>(x3::ascii::space)) + >; + + parser_type calculator(); + } + + auto const calculator = calculator_grammar::calculator(); +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc5.cpp b/src/boost/libs/spirit/example/x3/calc/calc5.cpp new file mode 100644 index 00000000..f6cd6a63 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc5.cpp @@ -0,0 +1,301 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Same as Calc4, this time, we'll incorporate debugging support, +// plus error handling and reporting. +// +// [ JDG April 28, 2008 ] For BoostCon 2008 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG April 9, 2014 ] Spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment this if you want to enable debugging +//#define BOOST_SPIRIT_X3_DEBUG + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/fusion/include/adapt_struct.hpp> + +#include <iostream> +#include <string> +#include <list> +#include <numeric> + +namespace x3 = boost::spirit::x3; + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + struct nil {}; + struct signed_; + struct program; + + struct operand : x3::variant< + nil + , unsigned int + , x3::forward_ast<signed_> + , x3::forward_ast<program> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + struct signed_ + { + char sign; + operand operand_; + }; + + struct operation + { + char operator_; + operand operand_; + }; + + struct program + { + operand first; + std::list<operation> rest; + }; + + // print function for debugging + inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; } +}} + +BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, + sign, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::program, + first, rest +) + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST Printer + /////////////////////////////////////////////////////////////////////////// + struct printer + { + typedef void result_type; + + void operator()(nil) const {} + void operator()(unsigned int n) const { std::cout << n; } + + void operator()(operation const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': std::cout << " add"; break; + case '-': std::cout << " subt"; break; + case '*': std::cout << " mult"; break; + case '/': std::cout << " div"; break; + } + } + + void operator()(signed_ const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': std::cout << " neg"; break; + case '+': std::cout << " pos"; break; + } + } + + void operator()(program const& x) const + { + boost::apply_visitor(*this, x.first); + for (operation const& oper : x.rest) + { + std::cout << ' '; + (*this)(oper); + } + } + }; + + /////////////////////////////////////////////////////////////////////////// + // The AST evaluator + /////////////////////////////////////////////////////////////////////////// + struct eval + { + typedef int result_type; + + int operator()(nil) const { BOOST_ASSERT(0); return 0; } + int operator()(unsigned int n) const { return n; } + + int operator()(operation const& x, int lhs) const + { + int rhs = boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': return lhs + rhs; + case '-': return lhs - rhs; + case '*': return lhs * rhs; + case '/': return lhs / rhs; + } + BOOST_ASSERT(0); + return 0; + } + + int operator()(signed_ const& x) const + { + int rhs = boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': return -rhs; + case '+': return +rhs; + } + BOOST_ASSERT(0); + return 0; + } + + int operator()(program const& x) const + { + int state = boost::apply_visitor(*this, x.first); + for (operation const& oper : x.rest) + { + state = (*this)(oper, state); + } + return state; + } + }; +}} + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using x3::uint_; + using x3::char_; + + struct expression_class; + struct term_class; + struct factor_class; + + x3::rule<expression_class, ast::program> const expression("expression"); + x3::rule<term_class, ast::program> const term("term"); + x3::rule<factor_class, ast::operand> const factor("factor"); + + auto const expression_def = + term + >> *( (char_('+') > term) + | (char_('-') > term) + ) + ; + + auto const term_def = + factor + >> *( (char_('*') > factor) + | (char_('/') > factor) + ) + ; + + auto const factor_def = + uint_ + | '(' > expression > ')' + | (char_('-') > factor) + | (char_('+') > factor) + ; + + BOOST_SPIRIT_DEFINE( + expression + , term + , factor + ); + + struct expression_class + { + // Our error handler + template <typename Iterator, typename Exception, typename Context> + x3::error_handler_result + on_error(Iterator&, Iterator const& last, Exception const& x, Context const& context) + { + std::cout + << "Error! Expecting: " + << x.which() + << " here: \"" + << std::string(x.where(), last) + << "\"" + << std::endl + ; + return x3::error_handler_result::fail; + } + }; + + auto calculator = expression; + } + + using calculator_grammar::calculator; +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + typedef client::ast::program ast_program; + typedef client::ast::printer ast_print; + typedef client::ast::eval ast_eval; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + auto& calc = client::calculator; // Our grammar + ast_program program; // Our program (AST) + ast_print print; // Prints the program + ast_eval eval; // Evaluates the program + + iterator_type iter = str.begin(); + iterator_type end = str.end(); + boost::spirit::x3::ascii::space_type space; + bool r = phrase_parse(iter, end, calc, space, program); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + print(program); + std::cout << "\nResult: " << eval(program) << std::endl; + std::cout << "-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc6.cpp b/src/boost/libs/spirit/example/x3/calc/calc6.cpp new file mode 100644 index 00000000..f9554aa6 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc6.cpp @@ -0,0 +1,341 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Yet another calculator example! This time, we will compile to a simple +// virtual machine. This is actually one of the very first Spirit example +// circa 2000. Now, it's ported to Spirit2 (and X3). +// +// [ JDG Sometime 2000 ] pre-boost +// [ JDG September 18, 2002 ] spirit1 +// [ JDG April 8, 2007 ] spirit2 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG April 9, 2014 ] Spirit X3 (from qi calc6) +// +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment this if you want to enable debugging +//#define BOOST_SPIRIT_X3_DEBUG + +#if defined(_MSC_VER) +# pragma warning(disable: 4345) +#endif + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/fusion/include/adapt_struct.hpp> + +#include <iostream> +#include <string> +#include <list> +#include <numeric> + +namespace x3 = boost::spirit::x3; + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + struct nil {}; + struct signed_; + struct expression; + + struct operand : x3::variant< + nil + , unsigned int + , x3::forward_ast<signed_> + , x3::forward_ast<expression> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + struct signed_ + { + char sign; + operand operand_; + }; + + struct operation + { + char operator_; + operand operand_; + }; + + struct expression + { + operand first; + std::list<operation> rest; + }; + + // print function for debugging + inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; } +}} + +BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, + sign, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::expression, + first, rest +) + +namespace client +{ + /////////////////////////////////////////////////////////////////////////// + // The Virtual Machine + /////////////////////////////////////////////////////////////////////////// + enum byte_code + { + op_neg, // negate the top stack entry + op_add, // add top two stack entries + op_sub, // subtract top two stack entries + op_mul, // multiply top two stack entries + op_div, // divide top two stack entries + op_int, // push constant integer into the stack + }; + + class vmachine + { + public: + + vmachine(unsigned stackSize = 4096) + : stack(stackSize) + , stack_ptr(stack.begin()) + { + } + + int top() const { return stack_ptr[-1]; }; + void execute(std::vector<int> const& code); + + private: + + std::vector<int> stack; + std::vector<int>::iterator stack_ptr; + }; + + void vmachine::execute(std::vector<int> const& code) + { + std::vector<int>::const_iterator pc = code.begin(); + stack_ptr = stack.begin(); + + while (pc != code.end()) + { + switch (*pc++) + { + case op_neg: + stack_ptr[-1] = -stack_ptr[-1]; + break; + + case op_add: + --stack_ptr; + stack_ptr[-1] += stack_ptr[0]; + break; + + case op_sub: + --stack_ptr; + stack_ptr[-1] -= stack_ptr[0]; + break; + + case op_mul: + --stack_ptr; + stack_ptr[-1] *= stack_ptr[0]; + break; + + case op_div: + --stack_ptr; + stack_ptr[-1] /= stack_ptr[0]; + break; + + case op_int: + *stack_ptr++ = *pc++; + break; + } + } + } + + /////////////////////////////////////////////////////////////////////////// + // The Compiler + /////////////////////////////////////////////////////////////////////////// + struct compiler + { + typedef void result_type; + + std::vector<int>& code; + compiler(std::vector<int>& code) + : code(code) {} + + void operator()(ast::nil) const { BOOST_ASSERT(0); } + void operator()(unsigned int n) const + { + code.push_back(op_int); + code.push_back(n); + } + + void operator()(ast::operation const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': code.push_back(op_add); break; + case '-': code.push_back(op_sub); break; + case '*': code.push_back(op_mul); break; + case '/': code.push_back(op_div); break; + default: BOOST_ASSERT(0); break; + } + } + + void operator()(ast::signed_ const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': code.push_back(op_neg); break; + case '+': break; + default: BOOST_ASSERT(0); break; + } + } + + void operator()(ast::expression const& x) const + { + boost::apply_visitor(*this, x.first); + for (ast::operation const& oper : x.rest) + { + (*this)(oper); + } + } + }; + + /////////////////////////////////////////////////////////////////////////////// + // The calculator grammar + /////////////////////////////////////////////////////////////////////////////// + namespace calculator_grammar + { + using x3::uint_; + using x3::char_; + + struct expression_class; + struct term_class; + struct factor_class; + + x3::rule<expression_class, ast::expression> const expression("expression"); + x3::rule<term_class, ast::expression> const term("term"); + x3::rule<factor_class, ast::operand> const factor("factor"); + + auto const expression_def = + term + >> *( (char_('+') > term) + | (char_('-') > term) + ) + ; + + auto const term_def = + factor + >> *( (char_('*') > factor) + | (char_('/') > factor) + ) + ; + + auto const factor_def = + uint_ + | '(' > expression > ')' + | (char_('-') > factor) + | (char_('+') > factor) + ; + + BOOST_SPIRIT_DEFINE( + expression + , term + , factor + ); + + struct expression_class + { + // Our error handler + template <typename Iterator, typename Exception, typename Context> + x3::error_handler_result + on_error(Iterator&, Iterator const& last, Exception const& x, Context const& context) + { + std::cout + << "Error! Expecting: " + << x.which() + << " here: \"" + << std::string(x.where(), last) + << "\"" + << std::endl + ; + return x3::error_handler_result::fail; + } + }; + + auto calculator = expression; + } + + using calculator_grammar::calculator; +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + typedef client::ast::expression ast_expression; + typedef client::compiler compiler; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + client::vmachine mach; // Our virtual machine + std::vector<int> code; // Our VM code + auto& calc = client::calculator; // Our grammar + ast_expression expression; // Our program (AST) + compiler compile(code); // Compiles the program + + iterator_type iter = str.begin(); + iterator_type const end = str.end(); + boost::spirit::x3::ascii::space_type space; + bool r = phrase_parse(iter, end, calc, space, expression); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + compile(expression); + mach.execute(code); + std::cout << "\nResult: " << mach.top() << std::endl; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/ast.hpp b/src/boost/libs/spirit/example/x3/calc/calc7/ast.hpp new file mode 100644 index 00000000..600350ec --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/ast.hpp @@ -0,0 +1,63 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC7_AST_HPP) +#define BOOST_SPIRIT_X3_CALC7_AST_HPP + +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include <boost/fusion/include/io.hpp> +#include <list> + +namespace client { namespace ast +{ + namespace x3 = boost::spirit::x3; + + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + struct nil {}; + struct signed_; + struct expression; + + struct operand : x3::variant< + nil + , unsigned int + , x3::forward_ast<signed_> + , x3::forward_ast<expression> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + struct signed_ + { + char sign; + operand operand_; + }; + + struct operation + { + char operator_; + operand operand_; + }; + + struct expression + { + operand first; + std::list<operation> rest; + }; + + // print function for debugging + inline std::ostream& operator<<(std::ostream& out, nil) + { + out << "nil"; + return out; + } +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/ast_adapted.hpp b/src/boost/libs/spirit/example/x3/calc/calc7/ast_adapted.hpp new file mode 100644 index 00000000..bd56cfc7 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/ast_adapted.hpp @@ -0,0 +1,25 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC7_AST_ADAPTED_HPP) +#define BOOST_SPIRIT_X3_CALC7_AST_ADAPTED_HPP + +#include "ast.hpp" +#include <boost/fusion/include/adapt_struct.hpp> + +BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, + sign, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::expression, + first, rest +) + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/compiler.cpp b/src/boost/libs/spirit/example/x3/calc/calc7/compiler.cpp new file mode 100644 index 00000000..8f6d6f09 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/compiler.cpp @@ -0,0 +1,55 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "compiler.hpp" +#include "vm.hpp" + +namespace client +{ + void compiler::operator()(ast::nil) const + { + BOOST_ASSERT(0); + } + + void compiler::operator()(unsigned int n) const + { + code.push_back(op_int); + code.push_back(n); + } + + void compiler::operator()(ast::operation const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.operator_) + { + case '+': code.push_back(op_add); break; + case '-': code.push_back(op_sub); break; + case '*': code.push_back(op_mul); break; + case '/': code.push_back(op_div); break; + default: BOOST_ASSERT(0); break; + } + } + + void compiler::operator()(ast::signed_ const& x) const + { + boost::apply_visitor(*this, x.operand_); + switch (x.sign) + { + case '-': code.push_back(op_neg); break; + case '+': break; + default: BOOST_ASSERT(0); break; + } + } + + void compiler::operator()(ast::expression const& x) const + { + boost::apply_visitor(*this, x.first); + for (ast::operation const& oper : x.rest) + { + (*this)(oper); + } + } +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/compiler.hpp b/src/boost/libs/spirit/example/x3/calc/calc7/compiler.hpp new file mode 100644 index 00000000..3cf0ed84 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/compiler.hpp @@ -0,0 +1,33 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC7_COMPILER_HPP) +#define BOOST_SPIRIT_X3_CALC7_COMPILER_HPP + +#include "ast.hpp" + +namespace client +{ + /////////////////////////////////////////////////////////////////////////// + // The Compiler + /////////////////////////////////////////////////////////////////////////// + struct compiler + { + typedef void result_type; + + std::vector<int>& code; + compiler(std::vector<int>& code) + : code(code) {} + + void operator()(ast::nil) const; + void operator()(unsigned int n) const; + void operator()(ast::operation const& x) const; + void operator()(ast::signed_ const& x) const; + void operator()(ast::expression const& x) const; + }; +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/error_handler.hpp b/src/boost/libs/spirit/example/x3/calc/calc7/error_handler.hpp new file mode 100644 index 00000000..9517220d --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/error_handler.hpp @@ -0,0 +1,40 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC7_ERROR_HANDLER_HPP) +#define BOOST_SPIRIT_X3_CALC7_ERROR_HANDLER_HPP + +#include <boost/spirit/home/x3.hpp> +#include <iostream> + +namespace client { namespace calculator_grammar +{ + //////////////////////////////////////////////////////////////////////////// + // Our error handler + //////////////////////////////////////////////////////////////////////////// + namespace x3 = boost::spirit::x3; + + struct error_handler + { + // Our error handler + template <typename Iterator, typename Exception, typename Context> + x3::error_handler_result + on_error(Iterator&, Iterator const& last, Exception const& x, Context const& context) + { + std::cout + << "Error! Expecting: " + << x.which() + << " here: \"" + << std::string(x.where(), last) + << "\"" + << std::endl + ; + return x3::error_handler_result::fail; + } + }; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/expression.cpp b/src/boost/libs/spirit/example/x3/calc/calc7/expression.cpp new file mode 100644 index 00000000..3dcb694e --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/expression.cpp @@ -0,0 +1,15 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "expression_def.hpp" + +namespace client { namespace calculator_grammar +{ + typedef std::string::const_iterator iterator_type; + typedef x3::phrase_parse_context<x3::ascii::space_type>::type context_type; + + BOOST_SPIRIT_INSTANTIATE(expression_type, iterator_type, context_type); +}} diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/expression.hpp b/src/boost/libs/spirit/example/x3/calc/calc7/expression.hpp new file mode 100644 index 00000000..93568eaf --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/expression.hpp @@ -0,0 +1,26 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC7_EXPRESSION_HPP) +#define BOOST_SPIRIT_X3_CALC7_EXPRESSION_HPP + +#include <boost/spirit/home/x3.hpp> +#include "ast.hpp" + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace calculator_grammar + { + struct expression_class; + typedef x3::rule<expression_class, ast::expression> expression_type; + BOOST_SPIRIT_DECLARE(expression_type); + } + + calculator_grammar::expression_type expression(); +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/expression_def.hpp b/src/boost/libs/spirit/example/x3/calc/calc7/expression_def.hpp new file mode 100644 index 00000000..dc73c288 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/expression_def.hpp @@ -0,0 +1,71 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC7_EXPRESSION_DEF_HPP) +#define BOOST_SPIRIT_X3_CALC7_EXPRESSION_DEF_HPP + +#include <boost/spirit/home/x3.hpp> +#include "ast.hpp" +#include "ast_adapted.hpp" +#include "expression.hpp" +#include "error_handler.hpp" + +namespace client { namespace calculator_grammar +{ + using x3::uint_; + using x3::char_; + + struct expression_class; + struct term_class; + struct factor_class; + + typedef x3::rule<expression_class, ast::expression> expression_type; + typedef x3::rule<term_class, ast::expression> term_type; + typedef x3::rule<factor_class, ast::operand> factor_type; + + expression_type const expression = "expression"; + term_type const term = "term"; + factor_type const factor = "factor"; + + auto const expression_def = + term + >> *( (char_('+') > term) + | (char_('-') > term) + ) + ; + + auto const term_def = + factor + >> *( (char_('*') > factor) + | (char_('/') > factor) + ) + ; + + auto const factor_def = + uint_ + | '(' > expression > ')' + | (char_('-') > factor) + | (char_('+') > factor) + ; + + BOOST_SPIRIT_DEFINE( + expression + , term + , factor + ); + + struct expression_class : error_handler {}; +}} + +namespace client +{ + calculator_grammar::expression_type expression() + { + return calculator_grammar::expression; + } +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/main.cpp b/src/boost/libs/spirit/example/x3/calc/calc7/main.cpp new file mode 100644 index 00000000..5f71230d --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/main.cpp @@ -0,0 +1,81 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Same as calc6, but this version also shows off grammar modularization. +// Here you will see how expressions is built as a modular grammars. +// +// [ JDG Sometime 2000 ] pre-boost +// [ JDG September 18, 2002 ] spirit1 +// [ JDG April 8, 2007 ] spirit2 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG April 9, 2014 ] Spirit X3 (from qi calc6) +// [ JDG May 2, 2014 ] Modular grammar using BOOST_SPIRIT_DEFINE. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "ast.hpp" +#include "vm.hpp" +#include "compiler.hpp" +#include "expression.hpp" +#include "error_handler.hpp" + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Expression parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type an expression...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + typedef client::ast::expression ast_expression; + typedef client::compiler compiler; + + std::string str; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + using boost::spirit::x3::ascii::space; + + client::vmachine mach; // Our virtual machine + std::vector<int> code; // Our VM code + auto calc = client::expression(); // grammar + + ast_expression ast; // Our program (AST) + compiler compile(code); // Compiles the program + + iterator_type iter = str.begin(); + iterator_type const end = str.end(); + bool r = phrase_parse(iter, end, calc, space, ast); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + compile(ast); + mach.execute(code); + std::cout << "\nResult: " << mach.top() << std::endl; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/vm.cpp b/src/boost/libs/spirit/example/x3/calc/calc7/vm.cpp new file mode 100644 index 00000000..323d7217 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/vm.cpp @@ -0,0 +1,50 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "vm.hpp" + +namespace client +{ + void vmachine::execute(std::vector<int> const& code) + { + std::vector<int>::const_iterator pc = code.begin(); + stack_ptr = stack.begin(); + + while (pc != code.end()) + { + switch (*pc++) + { + case op_neg: + stack_ptr[-1] = -stack_ptr[-1]; + break; + + case op_add: + --stack_ptr; + stack_ptr[-1] += stack_ptr[0]; + break; + + case op_sub: + --stack_ptr; + stack_ptr[-1] -= stack_ptr[0]; + break; + + case op_mul: + --stack_ptr; + stack_ptr[-1] *= stack_ptr[0]; + break; + + case op_div: + --stack_ptr; + stack_ptr[-1] /= stack_ptr[0]; + break; + + case op_int: + *stack_ptr++ = *pc++; + break; + } + } + } +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc7/vm.hpp b/src/boost/libs/spirit/example/x3/calc/calc7/vm.hpp new file mode 100644 index 00000000..2836efd3 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc7/vm.hpp @@ -0,0 +1,48 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC7_VM_HPP) +#define BOOST_SPIRIT_X3_CALC7_VM_HPP + +#include <vector> + +namespace client +{ + /////////////////////////////////////////////////////////////////////////// + // The Virtual Machine + /////////////////////////////////////////////////////////////////////////// + enum byte_code + { + op_neg, // negate the top stack entry + op_add, // add top two stack entries + op_sub, // subtract top two stack entries + op_mul, // multiply top two stack entries + op_div, // divide top two stack entries + op_int, // push constant integer into the stack + }; + + class vmachine + { + public: + + vmachine(unsigned stackSize = 4096) + : stack(stackSize) + , stack_ptr(stack.begin()) + { + } + + int top() const { return stack_ptr[-1]; }; + void execute(std::vector<int> const& code); + + private: + + std::vector<int> stack; + std::vector<int>::iterator stack_ptr; + }; + +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/ast.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/ast.hpp new file mode 100644 index 00000000..aacde561 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/ast.hpp @@ -0,0 +1,98 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_AST_HPP) +#define BOOST_SPIRIT_X3_CALC8_AST_HPP + +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include <boost/fusion/include/io.hpp> +#include <list> + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + namespace x3 = boost::spirit::x3; + + struct nil {}; + struct signed_; + struct expression; + + struct variable : x3::position_tagged + { + variable(std::string const& name = "") : name(name) {} + std::string name; + }; + + struct operand : + x3::variant< + nil + , unsigned int + , variable + , x3::forward_ast<signed_> + , x3::forward_ast<expression> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + struct signed_ + { + char sign; + operand operand_; + }; + + struct operation : x3::position_tagged + { + char operator_; + operand operand_; + }; + + struct expression : x3::position_tagged + { + operand first; + std::list<operation> rest; + }; + + struct assignment : x3::position_tagged + { + variable lhs; + expression rhs; + }; + + struct variable_declaration + { + assignment assign; + }; + + struct statement : + x3::variant< + variable_declaration + , assignment> + { + using base_type::base_type; + using base_type::operator=; + }; + + typedef std::list<statement> statement_list; + + // print functions for debugging + inline std::ostream& operator<<(std::ostream& out, nil) + { + out << "nil"; + return out; + } + + inline std::ostream& operator<<(std::ostream& out, variable const& var) + { + out << var.name; return out; + } +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/ast_adapted.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/ast_adapted.hpp new file mode 100644 index 00000000..ca776109 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/ast_adapted.hpp @@ -0,0 +1,33 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_AST_ADAPTED_HPP) +#define BOOST_SPIRIT_X3_CALC8_AST_ADAPTED_HPP + +#include "ast.hpp" +#include <boost/fusion/include/adapt_struct.hpp> + +BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_, + sign, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::expression, + first, rest +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::variable_declaration, + assign +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::assignment, + lhs, rhs +) + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/common.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/common.hpp new file mode 100644 index 00000000..0a559573 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/common.hpp @@ -0,0 +1,28 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_COMMON_HPP) +#define BOOST_SPIRIT_X3_CALC8_COMMON_HPP + +#include <boost/spirit/home/x3.hpp> + +namespace client { namespace parser +{ + using x3::raw; + using x3::lexeme; + using x3::alpha; + using x3::alnum; + + struct identifier_class; + typedef x3::rule<identifier_class, std::string> identifier_type; + identifier_type const identifier = "identifier"; + + auto const identifier_def = raw[lexeme[(alpha | '_') >> *(alnum | '_')]]; + + BOOST_SPIRIT_DEFINE(identifier); +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/compiler.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/compiler.cpp new file mode 100644 index 00000000..6410a981 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/compiler.cpp @@ -0,0 +1,217 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "compiler.hpp" +#include "vm.hpp" +#include <boost/variant/apply_visitor.hpp> +#include <boost/assert.hpp> +#include <iostream> + +namespace client { namespace code_gen +{ + void program::op(int a) + { + code.push_back(a); + } + + void program::op(int a, int b) + { + code.push_back(a); + code.push_back(b); + } + + void program::op(int a, int b, int c) + { + code.push_back(a); + code.push_back(b); + code.push_back(c); + } + + int const* program::find_var(std::string const& name) const + { + auto i = variables.find(name); + if (i == variables.end()) + return 0; + return &i->second; + } + + void program::add_var(std::string const& name) + { + std::size_t n = variables.size(); + variables[name] = int(n); + } + + void program::print_variables(std::vector<int> const& stack) const + { + for (auto const& p : variables) + { + std::cout << " " << p.first << ": " << stack[p.second] << std::endl; + } + } + + void program::print_assembler() const + { + auto pc = code.begin(); + + std::vector<std::string> locals(variables.size()); + typedef std::pair<std::string, int> pair; + for (pair const& p : variables) + { + locals[p.second] = p.first; + std::cout << "local " + << p.first << ", @" << p.second << std::endl; + } + + while (pc != code.end()) + { + switch (*pc++) + { + case op_neg: + std::cout << "op_neg" << std::endl; + break; + + case op_add: + std::cout << "op_add" << std::endl; + break; + + case op_sub: + std::cout << "op_sub" << std::endl; + break; + + case op_mul: + std::cout << "op_mul" << std::endl; + break; + + case op_div: + std::cout << "op_div" << std::endl; + break; + + case op_load: + std::cout << "op_load " << locals[*pc++] << std::endl; + break; + + case op_store: + std::cout << "op_store " << locals[*pc++] << std::endl; + break; + + case op_int: + std::cout << "op_int " << *pc++ << std::endl; + break; + + case op_stk_adj: + std::cout << "op_stk_adj " << *pc++ << std::endl; + break; + } + } + } + + bool compiler::operator()(unsigned int x) const + { + program.op(op_int, x); + return true; + } + + bool compiler::operator()(ast::variable const& x) const + { + int const* p = program.find_var(x.name); + if (p == 0) + { + error_handler(x, "Undeclared variable: " + x.name); + return false; + } + program.op(op_load, *p); + return true; + } + + bool compiler::operator()(ast::operation const& x) const + { + if (!boost::apply_visitor(*this, x.operand_)) + return false; + switch (x.operator_) + { + case '+': program.op(op_add); break; + case '-': program.op(op_sub); break; + case '*': program.op(op_mul); break; + case '/': program.op(op_div); break; + default: BOOST_ASSERT(0); return false; + } + return true; + } + + bool compiler::operator()(ast::signed_ const& x) const + { + if (!boost::apply_visitor(*this, x.operand_)) + return false; + switch (x.sign) + { + case '-': program.op(op_neg); break; + case '+': break; + default: BOOST_ASSERT(0); return false; + } + return true; + } + + bool compiler::operator()(ast::expression const& x) const + { + if (!boost::apply_visitor(*this, x.first)) + return false; + for (ast::operation const& oper : x.rest) + { + if (!(*this)(oper)) + return false; + } + return true; + } + + bool compiler::operator()(ast::assignment const& x) const + { + if (!(*this)(x.rhs)) + return false; + int const* p = program.find_var(x.lhs.name); + if (p == 0) + { + error_handler(x.lhs, "Undeclared variable: " + x.lhs.name); + return false; + } + program.op(op_store, *p); + return true; + } + + bool compiler::operator()(ast::variable_declaration const& x) const + { + int const* p = program.find_var(x.assign.lhs.name); + if (p != 0) + { + error_handler(x.assign.lhs, "Duplicate variable: " + x.assign.lhs.name); + return false; + } + bool r = (*this)(x.assign.rhs); + if (r) // don't add the variable if the RHS fails + { + program.add_var(x.assign.lhs.name); + program.op(op_store, *program.find_var(x.assign.lhs.name)); + } + return r; + } + + bool compiler::operator()(ast::statement_list const& x) const + { + program.clear(); + + // op_stk_adj 0 for now. we'll know how many variables we'll have later + program.op(op_stk_adj, 0); + for (ast::statement const& s : x) + { + if (!boost::apply_visitor(*this, s)) + { + program.clear(); + return false; + } + } + program[1] = int(program.nvars()); // now store the actual number of variables + return true; + } +}} diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/compiler.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/compiler.hpp new file mode 100644 index 00000000..509298c9 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/compiler.hpp @@ -0,0 +1,80 @@ +/*============================================================================= + Copyright (c) 2001-2011 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_COMPILER_HPP) +#define BOOST_SPIRIT_X3_CALC8_COMPILER_HPP + +#include "ast.hpp" +#include "error_handler.hpp" +#include <vector> +#include <map> + +namespace client { namespace code_gen +{ + /////////////////////////////////////////////////////////////////////////// + // The Program + /////////////////////////////////////////////////////////////////////////// + struct program + { + void op(int a); + void op(int a, int b); + void op(int a, int b, int c); + + int& operator[](std::size_t i) { return code[i]; } + int operator[](std::size_t i) const { return code[i]; } + void clear() { code.clear(); variables.clear(); } + std::vector<int> const& operator()() const { return code; } + + std::size_t nvars() const { return variables.size(); } + int const* find_var(std::string const& name) const; + void add_var(std::string const& name); + + void print_variables(std::vector<int> const& stack) const; + void print_assembler() const; + + private: + + std::map<std::string, int> variables; + std::vector<int> code; + }; + + //////////////////////////////////////////////////////////////////////////// + // The Compiler + //////////////////////////////////////////////////////////////////////////// + struct compiler + { + typedef bool result_type; + typedef std::function< + void(x3::position_tagged, std::string const&)> + error_handler_type; + + template <typename ErrorHandler> + compiler( + client::code_gen::program& program + , ErrorHandler const& error_handler) + : program(program) + , error_handler( + [&](x3::position_tagged pos, std::string const& msg) + { error_handler(pos, msg); } + ) + {} + + bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; } + bool operator()(unsigned int x) const; + bool operator()(ast::variable const& x) const; + bool operator()(ast::operation const& x) const; + bool operator()(ast::signed_ const& x) const; + bool operator()(ast::expression const& x) const; + bool operator()(ast::assignment const& x) const; + bool operator()(ast::variable_declaration const& x) const; + bool operator()(ast::statement_list const& x) const; + + client::code_gen::program& program; + error_handler_type error_handler; + }; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/config.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/config.hpp new file mode 100644 index 00000000..d6664564 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/config.hpp @@ -0,0 +1,26 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_CONFIG_HPP) +#define BOOST_SPIRIT_X3_CALC8_CONFIG_HPP + +#include <boost/spirit/home/x3.hpp> +#include "error_handler.hpp" + +namespace client { namespace parser +{ + typedef std::string::const_iterator iterator_type; + typedef x3::phrase_parse_context<x3::ascii::space_type>::type phrase_context_type; + typedef error_handler<iterator_type> error_handler_type; + + typedef x3::context< + error_handler_tag + , std::reference_wrapper<error_handler_type> + , phrase_context_type> + context_type; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/error_handler.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/error_handler.hpp new file mode 100644 index 00000000..25cb268c --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/error_handler.hpp @@ -0,0 +1,44 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_ERROR_HANDLER_HPP) +#define BOOST_SPIRIT_X3_CALC8_ERROR_HANDLER_HPP + +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/utility/error_reporting.hpp> +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include "expression.hpp" +#include "statement.hpp" + +namespace client { namespace parser +{ + namespace x3 = boost::spirit::x3; + + //////////////////////////////////////////////////////////////////////////// + // Our error handler + //////////////////////////////////////////////////////////////////////////// + template <typename Iterator> + using error_handler = x3::error_handler<Iterator>; + + // tag used to get our error handler from the context + using error_handler_tag = x3::error_handler_tag; + + struct error_handler_base + { + template <typename Iterator, typename Exception, typename Context> + x3::error_handler_result on_error( + Iterator& first, Iterator const& last + , Exception const& x, Context const& context) + { + std::string message = "Error! Expecting: " + x.which() + " here:"; + auto& error_handler = x3::get<error_handler_tag>(context).get(); + error_handler(x.where(), message); + return x3::error_handler_result::fail; + } + }; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/expression.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/expression.cpp new file mode 100644 index 00000000..e7463cd3 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/expression.cpp @@ -0,0 +1,13 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "expression_def.hpp" +#include "config.hpp" + +namespace client { namespace parser +{ + BOOST_SPIRIT_INSTANTIATE(expression_type, iterator_type, context_type); +}} diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/expression.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/expression.hpp new file mode 100644 index 00000000..d619510c --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/expression.hpp @@ -0,0 +1,26 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_EXPRESSION_HPP) +#define BOOST_SPIRIT_X3_CALC8_EXPRESSION_HPP + +#include <boost/spirit/home/x3.hpp> +#include "ast.hpp" + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace parser + { + struct expression_class; + typedef x3::rule<expression_class, ast::expression> expression_type; + BOOST_SPIRIT_DECLARE(expression_type); + } + + parser::expression_type const& expression(); +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/expression_def.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/expression_def.hpp new file mode 100644 index 00000000..fe9eaee9 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/expression_def.hpp @@ -0,0 +1,91 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_EXPRESSION_DEF_HPP) +#define BOOST_SPIRIT_X3_CALC8_EXPRESSION_DEF_HPP + +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp> +#include "ast.hpp" +#include "ast_adapted.hpp" +#include "expression.hpp" +#include "common.hpp" +#include "error_handler.hpp" + +namespace client { namespace parser +{ + using x3::uint_; + using x3::char_; + using x3::raw; + using x3::lexeme; + using namespace x3::ascii; + + struct additive_expr_class; + struct multiplicative_expr_class; + struct unary_expr_class; + struct primary_expr_class; + + typedef x3::rule<additive_expr_class, ast::expression> additive_expr_type; + typedef x3::rule<multiplicative_expr_class, ast::expression> multiplicative_expr_type; + typedef x3::rule<unary_expr_class, ast::operand> unary_expr_type; + typedef x3::rule<primary_expr_class, ast::operand> primary_expr_type; + + expression_type const expression = "expression"; + additive_expr_type const additive_expr = "additive_expr"; + multiplicative_expr_type const multiplicative_expr = "multiplicative_expr"; + unary_expr_type unary_expr = "unary_expr"; + primary_expr_type primary_expr = "primary_expr"; + + auto const additive_expr_def = + multiplicative_expr + >> *( (char_('+') > multiplicative_expr) + | (char_('-') > multiplicative_expr) + ) + ; + + auto const multiplicative_expr_def = + unary_expr + >> *( (char_('*') > unary_expr) + | (char_('/') > unary_expr) + ) + ; + + auto const unary_expr_def = + primary_expr + | (char_('-') > primary_expr) + | (char_('+') > primary_expr) + ; + + auto const primary_expr_def = + uint_ + | identifier + | '(' > expression > ')' + ; + + auto const expression_def = additive_expr; + + BOOST_SPIRIT_DEFINE( + expression + , additive_expr + , multiplicative_expr + , unary_expr + , primary_expr + ); + + struct unary_expr_class : x3::annotate_on_success {}; + struct primary_expr_class : x3::annotate_on_success {}; + +}} + +namespace client +{ + parser::expression_type const& expression() + { + return parser::expression; + } +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/main.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/main.cpp new file mode 100644 index 00000000..e87ccbb7 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/main.cpp @@ -0,0 +1,114 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Now we'll introduce variables and assignment. This time, we'll also +// be renaming some of the rules -- a strategy for a grander scheme +// to come ;-) +// +// This version also shows off grammar modularization. Here you will +// see how expressions and statements are built as modular grammars. +// +// [ JDG April 9, 2007 ] spirit2 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG May 17, 2014 ] Ported from qi calc7 example. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "ast.hpp" +#include "vm.hpp" +#include "compiler.hpp" +#include "statement.hpp" +#include "error_handler.hpp" +#include "config.hpp" +#include <iostream> + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Statement parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type some statements... "; + std::cout << "An empty line ends input, compiles, runs and prints results\n\n"; + std::cout << "Example:\n\n"; + std::cout << " var a = 123;\n"; + std::cout << " var b = 456;\n"; + std::cout << " var c = a + b * 2;\n\n"; + std::cout << "-------------------------\n"; + + std::string str; + std::string source; + while (std::getline(std::cin, str)) + { + if (str.empty()) + break; + source += str + '\n'; + } + + using client::parser::iterator_type; + iterator_type iter(source.begin()); + iterator_type end(source.end()); + + client::vmachine vm; // Our virtual machine + client::code_gen::program program; // Our VM program + client::ast::statement_list ast; // Our AST + + using boost::spirit::x3::with; + using client::parser::error_handler_type; + using client::parser::error_handler_tag; + error_handler_type error_handler(iter, end, std::cerr); // Our error handler + + // Our compiler + client::code_gen::compiler compile(program, error_handler); + + // Our parser + auto const parser = + // we pass our error handler to the parser so we can access + // it later on in our on_error and on_sucess handlers + with<error_handler_tag>(std::ref(error_handler)) + [ + client::statement() + ]; + + using boost::spirit::x3::ascii::space; + bool success = phrase_parse(iter, end, parser, space, ast); + + std::cout << "-------------------------\n"; + + if (success && iter == end) + { + if (compile(ast)) + { + std::cout << "Success\n"; + std::cout << "-------------------------\n"; + vm.execute(program()); + + std::cout << "-------------------------\n"; + std::cout << "Assembler----------------\n\n"; + program.print_assembler(); + + std::cout << "-------------------------\n"; + std::cout << "Results------------------\n\n"; + program.print_variables(vm.get_stack()); + } + else + { + std::cout << "Compile failure\n"; + } + } + else + { + std::cout << "Parse failure\n"; + } + + std::cout << "-------------------------\n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/statement.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/statement.cpp new file mode 100644 index 00000000..ec4bca61 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/statement.cpp @@ -0,0 +1,13 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "statement_def.hpp" +#include "config.hpp" + +namespace client { namespace parser +{ + BOOST_SPIRIT_INSTANTIATE(statement_type, iterator_type, context_type); +}} diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/statement.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/statement.hpp new file mode 100644 index 00000000..7b8973d0 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/statement.hpp @@ -0,0 +1,26 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_STATEMENT_HPP) +#define BOOST_SPIRIT_X3_CALC8_STATEMENT_HPP + +#include <boost/spirit/home/x3.hpp> +#include "ast.hpp" + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace parser + { + struct statement_class; + typedef x3::rule<statement_class, ast::statement_list> statement_type; + BOOST_SPIRIT_DECLARE(statement_type); + } + + parser::statement_type const& statement(); +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/statement_def.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/statement_def.hpp new file mode 100644 index 00000000..8772c76f --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/statement_def.hpp @@ -0,0 +1,84 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_STATEMENT_DEF_HPP) +#define BOOST_SPIRIT_X3_CALC8_STATEMENT_DEF_HPP + +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp> +#include "ast.hpp" +#include "ast_adapted.hpp" +#include "statement.hpp" +#include "expression.hpp" +#include "common.hpp" +#include "error_handler.hpp" + +namespace client { namespace parser +{ + using x3::raw; + using x3::lexeme; + using namespace x3::ascii; + + struct statement_list_class; + struct variable_declaration_class; + struct assignment_class; + struct variable_class; + + typedef x3::rule<statement_list_class, ast::statement_list> statement_list_type; + typedef x3::rule<variable_declaration_class, ast::variable_declaration> variable_declaration_type; + typedef x3::rule<assignment_class, ast::assignment> assignment_type; + typedef x3::rule<variable_class, ast::variable> variable_type; + + statement_type const statement = "statement"; + statement_list_type const statement_list = "statement_list"; + variable_declaration_type const variable_declaration = "variable_declaration"; + assignment_type const assignment = "assignment"; + variable_type const variable = "variable"; + + // Import the expression rule + namespace { auto const& expression = client::expression(); } + + auto const statement_list_def = + +(variable_declaration | assignment) + ; + + auto const variable_declaration_def = + lexeme["var" >> !(alnum | '_')] // make sure we have whole words + > assignment + ; + + auto const assignment_def = + variable + > '=' + > expression + > ';' + ; + + auto const variable_def = identifier; + auto const statement_def = statement_list; + + BOOST_SPIRIT_DEFINE( + statement + , statement_list + , variable_declaration + , assignment + , variable + ); + + struct statement_class : error_handler_base, x3::annotate_on_success {}; + struct assignment_class : x3::annotate_on_success {}; + struct variable_class : x3::annotate_on_success {}; +}} + +namespace client +{ + parser::statement_type const& statement() + { + return parser::statement; + } +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/vm.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/vm.cpp new file mode 100644 index 00000000..ff6c8400 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/vm.cpp @@ -0,0 +1,64 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "vm.hpp" + +namespace client +{ + void vmachine::execute(std::vector<int> const& code) + { + auto pc = code.begin(); + auto locals = stack.begin(); + stack_ptr = stack.begin(); + + while (pc != code.end()) + { + switch (*pc++) + { + case op_neg: + stack_ptr[-1] = -stack_ptr[-1]; + break; + + case op_add: + --stack_ptr; + stack_ptr[-1] += stack_ptr[0]; + break; + + case op_sub: + --stack_ptr; + stack_ptr[-1] -= stack_ptr[0]; + break; + + case op_mul: + --stack_ptr; + stack_ptr[-1] *= stack_ptr[0]; + break; + + case op_div: + --stack_ptr; + stack_ptr[-1] /= stack_ptr[0]; + break; + + case op_load: + *stack_ptr++ = locals[*pc++]; + break; + + case op_store: + --stack_ptr; + locals[*pc++] = stack_ptr[0]; + break; + + case op_int: + *stack_ptr++ = *pc++; + break; + + case op_stk_adj: + stack_ptr = stack.begin() + *pc++; + break; + } + } + } +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/vm.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/vm.hpp new file mode 100644 index 00000000..29072977 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc8/vm.hpp @@ -0,0 +1,51 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC8_VM_HPP) +#define BOOST_SPIRIT_X3_CALC8_VM_HPP + +#include <vector> + +namespace client +{ + //////////////////////////////////////////////////////////////////////////// + // The Virtual Machine + //////////////////////////////////////////////////////////////////////////// + enum byte_code + { + op_neg, // negate the top stack entry + op_add, // add top two stack entries + op_sub, // subtract top two stack entries + op_mul, // multiply top two stack entries + op_div, // divide top two stack entries + + op_load, // load a variable + op_store, // store a variable + op_int, // push constant integer into the stack + op_stk_adj // adjust the stack for local variables + }; + + class vmachine + { + public: + + vmachine(unsigned stackSize = 4096) + : stack(stackSize) + , stack_ptr(stack.begin()) + { + } + + void execute(std::vector<int> const& code); + std::vector<int> const& get_stack() const { return stack; }; + + private: + + std::vector<int> stack; + std::vector<int>::iterator stack_ptr; + }; +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/ast.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/ast.hpp new file mode 100644 index 00000000..b3e82c49 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/ast.hpp @@ -0,0 +1,139 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_AST_HPP) +#define BOOST_SPIRIT_X3_CALC9_AST_HPP + +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include <boost/fusion/include/io.hpp> +#include <boost/optional.hpp> +#include <list> + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + namespace x3 = boost::spirit::x3; + + struct nil {}; + struct unary; + struct expression; + + struct variable : x3::position_tagged + { + variable(std::string const& name = "") : name(name) {} + std::string name; + }; + + struct operand : + x3::variant< + nil + , unsigned int + , variable + , x3::forward_ast<unary> + , x3::forward_ast<expression> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + enum optoken + { + op_plus, + op_minus, + op_times, + op_divide, + op_positive, + op_negative, + op_not, + op_equal, + op_not_equal, + op_less, + op_less_equal, + op_greater, + op_greater_equal, + op_and, + op_or + }; + + struct unary + { + optoken operator_; + operand operand_; + }; + + struct operation : x3::position_tagged + { + optoken operator_; + operand operand_; + }; + + struct expression : x3::position_tagged + { + operand first; + std::list<operation> rest; + }; + + struct assignment : x3::position_tagged + { + variable lhs; + expression rhs; + }; + + struct variable_declaration + { + assignment assign; + }; + + struct if_statement; + struct while_statement; + struct statement_list; + + struct statement : + x3::variant< + variable_declaration + , assignment + , boost::recursive_wrapper<if_statement> + , boost::recursive_wrapper<while_statement> + , boost::recursive_wrapper<statement_list> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + struct statement_list : std::list<statement> {}; + + struct if_statement + { + expression condition; + statement then; + boost::optional<statement> else_; + }; + + struct while_statement + { + expression condition; + statement body; + }; + + // print functions for debugging + inline std::ostream& operator<<(std::ostream& out, nil) + { + out << "nil"; + return out; + } + + inline std::ostream& operator<<(std::ostream& out, variable const& var) + { + out << var.name; return out; + } +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/ast_adapted.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/ast_adapted.hpp new file mode 100644 index 00000000..3f8d3cb1 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/ast_adapted.hpp @@ -0,0 +1,41 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_AST_ADAPTED_HPP) +#define BOOST_SPIRIT_X3_CALC9_AST_ADAPTED_HPP + +#include "ast.hpp" +#include <boost/fusion/include/adapt_struct.hpp> + +BOOST_FUSION_ADAPT_STRUCT(client::ast::unary, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::operation, + operator_, operand_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::expression, + first, rest +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::variable_declaration, + assign +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::assignment, + lhs, rhs +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::if_statement, + condition, then, else_ +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::while_statement, + condition, body +) + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/common.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/common.hpp new file mode 100644 index 00000000..1f1a7011 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/common.hpp @@ -0,0 +1,28 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_COMMON_HPP) +#define BOOST_SPIRIT_X3_CALC9_COMMON_HPP + +#include <boost/spirit/home/x3.hpp> + +namespace client { namespace parser +{ + using x3::raw; + using x3::lexeme; + using x3::alpha; + using x3::alnum; + + struct identifier_class; + typedef x3::rule<identifier_class, std::string> identifier_type; + identifier_type const identifier = "identifier"; + + auto const identifier_def = raw[lexeme[(alpha | '_') >> *(alnum | '_')]]; + + BOOST_SPIRIT_DEFINE(identifier); +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/compiler.cpp b/src/boost/libs/spirit/example/x3/calc/calc9/compiler.cpp new file mode 100644 index 00000000..620fa22d --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/compiler.cpp @@ -0,0 +1,376 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "compiler.hpp" +#include "vm.hpp" +#include <boost/variant/apply_visitor.hpp> +#include <boost/assert.hpp> +#include <iostream> +#include <set> +#include <iostream> + +namespace client { namespace code_gen +{ + void program::op(int a) + { + code.push_back(a); + } + + void program::op(int a, int b) + { + code.push_back(a); + code.push_back(b); + } + + void program::op(int a, int b, int c) + { + code.push_back(a); + code.push_back(b); + code.push_back(c); + } + + int const* program::find_var(std::string const& name) const + { + auto i = variables.find(name); + if (i == variables.end()) + return 0; + return &i->second; + } + + void program::add_var(std::string const& name) + { + std::size_t n = variables.size(); + variables[name] = int(n); + } + + void program::print_variables(std::vector<int> const& stack) const + { + for (auto const& p : variables) + { + std::cout << " " << p.first << ": " << stack[p.second] << std::endl; + } + } + + void program::print_assembler() const + { + auto pc = code.begin(); + + std::vector<std::string> locals(variables.size()); + typedef std::pair<std::string, int> pair; + for (pair const& p : variables) + { + locals[p.second] = p.first; + std::cout << "local " + << p.first << ", @" << p.second << std::endl; + } + + std::map<std::size_t, std::string> lines; + std::set<std::size_t> jumps; + + while (pc != code.end()) + { + std::string line; + std::size_t address = pc - code.begin(); + + switch (*pc++) + { + case op_neg: + line += " op_neg"; + break; + + case op_not: + line += " op_not"; + break; + + case op_add: + line += " op_add"; + break; + + case op_sub: + line += " op_sub"; + break; + + case op_mul: + line += " op_mul"; + break; + + case op_div: + line += " op_div"; + break; + + case op_eq: + line += " op_eq"; + break; + + case op_neq: + line += " op_neq"; + break; + + case op_lt: + line += " op_lt"; + break; + + case op_lte: + line += " op_lte"; + break; + + case op_gt: + line += " op_gt"; + break; + + case op_gte: + line += " op_gte"; + break; + + case op_and: + line += " op_and"; + break; + + case op_or: + line += " op_or"; + break; + + case op_load: + line += " op_load "; + line += locals[*pc++]; + break; + + case op_store: + line += " op_store "; + line += locals[*pc++]; + break; + + case op_int: + line += " op_int "; + line += std::to_string(*pc++); + break; + + case op_true: + line += " op_true"; + break; + + case op_false: + line += " op_false"; + break; + + case op_jump: + { + line += " op_jump "; + std::size_t pos = (pc - code.begin()) + *pc++; + if (pos == code.size()) + line += "end"; + else + line += std::to_string(pos); + jumps.insert(pos); + } + break; + + case op_jump_if: + { + line += " op_jump_if "; + std::size_t pos = (pc - code.begin()) + *pc++; + if (pos == code.size()) + line += "end"; + else + line += std::to_string(pos); + jumps.insert(pos); + } + break; + + case op_stk_adj: + line += " op_stk_adj "; + line += std::to_string(*pc++); + break; + } + lines[address] = line; + } + + std::cout << "start:" << std::endl; + for (auto const& l : lines) + { + std::size_t pos = l.first; + if (jumps.find(pos) != jumps.end()) + std::cout << pos << ':' << std::endl; + std::cout << l.second << std::endl; + } + + std::cout << "end:" << std::endl; + } + + bool compiler::operator()(unsigned int x) const + { + program.op(op_int, x); + return true; + } + + bool compiler::operator()(bool x) const + { + program.op(x ? op_true : op_false); + return true; + } + + bool compiler::operator()(ast::variable const& x) const + { + int const* p = program.find_var(x.name); + if (p == 0) + { + error_handler(x, "Undeclared variable: " + x.name); + return false; + } + program.op(op_load, *p); + return true; + } + + bool compiler::operator()(ast::operation const& x) const + { + if (!boost::apply_visitor(*this, x.operand_)) + return false; + switch (x.operator_) + { + case ast::op_plus: program.op(op_add); break; + case ast::op_minus: program.op(op_sub); break; + case ast::op_times: program.op(op_mul); break; + case ast::op_divide: program.op(op_div); break; + + case ast::op_equal: program.op(op_eq); break; + case ast::op_not_equal: program.op(op_neq); break; + case ast::op_less: program.op(op_lt); break; + case ast::op_less_equal: program.op(op_lte); break; + case ast::op_greater: program.op(op_gt); break; + case ast::op_greater_equal: program.op(op_gte); break; + + case ast::op_and: program.op(op_and); break; + case ast::op_or: program.op(op_or); break; + default: BOOST_ASSERT(0); return false; + } + return true; + } + + bool compiler::operator()(ast::unary const& x) const + { + if (!boost::apply_visitor(*this, x.operand_)) + return false; + switch (x.operator_) + { + case ast::op_negative: program.op(op_neg); break; + case ast::op_not: program.op(op_not); break; + case ast::op_positive: break; + default: BOOST_ASSERT(0); return false; + } + return true; + } + + bool compiler::operator()(ast::expression const& x) const + { + if (!boost::apply_visitor(*this, x.first)) + return false; + for (ast::operation const& oper : x.rest) + { + if (!(*this)(oper)) + return false; + } + return true; + } + + bool compiler::operator()(ast::assignment const& x) const + { + if (!(*this)(x.rhs)) + return false; + int const* p = program.find_var(x.lhs.name); + if (p == 0) + { + error_handler(x.lhs, "Undeclared variable: " + x.lhs.name); + return false; + } + program.op(op_store, *p); + return true; + } + + bool compiler::operator()(ast::variable_declaration const& x) const + { + int const* p = program.find_var(x.assign.lhs.name); + if (p != 0) + { + error_handler(x.assign.lhs, "Duplicate variable: " + x.assign.lhs.name); + return false; + } + bool r = (*this)(x.assign.rhs); + if (r) // don't add the variable if the RHS fails + { + program.add_var(x.assign.lhs.name); + program.op(op_store, *program.find_var(x.assign.lhs.name)); + } + return r; + } + + bool compiler::operator()(ast::statement const& x) const + { + return boost::apply_visitor(*this, x); + } + + bool compiler::operator()(ast::statement_list const& x) const + { + for (auto const& s : x) + { + if (!(*this)(s)) + return false; + } + return true; + } + + bool compiler::operator()(ast::if_statement const& x) const + { + if (!(*this)(x.condition)) + return false; + program.op(op_jump_if, 0); // we shall fill this (0) in later + std::size_t skip = program.size()-1; // mark its position + if (!(*this)(x.then)) + return false; + program[skip] = int(program.size()-skip); // now we know where to jump to (after the if branch) + + if (x.else_) // We got an alse + { + program[skip] += 2; // adjust for the "else" jump + program.op(op_jump, 0); // we shall fill this (0) in later + std::size_t exit = program.size()-1; // mark its position + if (!(*this)(*x.else_)) + return false; + program[exit] = int(program.size()-exit); // now we know where to jump to (after the else branch) + } + + return true; + } + + bool compiler::operator()(ast::while_statement const& x) const + { + std::size_t loop = program.size(); // mark our position + if (!(*this)(x.condition)) + return false; + program.op(op_jump_if, 0); // we shall fill this (0) in later + std::size_t exit = program.size()-1; // mark its position + if (!(*this)(x.body)) + return false; + program.op(op_jump, + int(loop-1) - int(program.size())); // loop back + program[exit] = int(program.size()-exit); // now we know where to jump to (to exit the loop) + return true; + } + + bool compiler::start(ast::statement_list const& x) const + { + program.clear(); + // op_stk_adj 0 for now. we'll know how many variables we'll have later + program.op(op_stk_adj, 0); + + if (!(*this)(x)) + { + program.clear(); + return false; + } + program[1] = int(program.nvars()); // now store the actual number of variables + return true; + } +}} diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/compiler.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/compiler.hpp new file mode 100644 index 00000000..96b32a70 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/compiler.hpp @@ -0,0 +1,87 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_COMPILER_HPP) +#define BOOST_SPIRIT_X3_CALC9_COMPILER_HPP + +#include "ast.hpp" +#include "error_handler.hpp" +#include <vector> +#include <map> + +namespace client { namespace code_gen +{ + /////////////////////////////////////////////////////////////////////////// + // The Program + /////////////////////////////////////////////////////////////////////////// + struct program + { + void op(int a); + void op(int a, int b); + void op(int a, int b, int c); + + int& operator[](std::size_t i) { return code[i]; } + int operator[](std::size_t i) const { return code[i]; } + void clear() { code.clear(); variables.clear(); } + std::size_t size() const { return code.size(); } + std::vector<int> const& operator()() const { return code; } + + std::size_t nvars() const { return variables.size(); } + int const* find_var(std::string const& name) const; + void add_var(std::string const& name); + + void print_variables(std::vector<int> const& stack) const; + void print_assembler() const; + + private: + + std::map<std::string, int> variables; + std::vector<int> code; + }; + + //////////////////////////////////////////////////////////////////////////// + // The Compiler + //////////////////////////////////////////////////////////////////////////// + struct compiler + { + typedef bool result_type; + typedef std::function< + void(x3::position_tagged, std::string const&)> + error_handler_type; + + template <typename ErrorHandler> + compiler( + client::code_gen::program& program + , ErrorHandler const& error_handler) + : program(program) + , error_handler( + [&](x3::position_tagged pos, std::string const& msg) + { error_handler(pos, msg); } + ) + {} + + bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; } + bool operator()(unsigned int x) const; + bool operator()(bool x) const; + bool operator()(ast::variable const& x) const; + bool operator()(ast::operation const& x) const; + bool operator()(ast::unary const& x) const; + bool operator()(ast::expression const& x) const; + bool operator()(ast::assignment const& x) const; + bool operator()(ast::variable_declaration const& x) const; + bool operator()(ast::statement_list const& x) const; + bool operator()(ast::statement const& x) const; + bool operator()(ast::if_statement const& x) const; + bool operator()(ast::while_statement const& x) const; + + bool start(ast::statement_list const& x) const; + + client::code_gen::program& program; + error_handler_type error_handler; + }; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/config.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/config.hpp new file mode 100644 index 00000000..a250bf72 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/config.hpp @@ -0,0 +1,26 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_CONFIG_HPP) +#define BOOST_SPIRIT_X3_CALC9_CONFIG_HPP + +#include <boost/spirit/home/x3.hpp> +#include "error_handler.hpp" + +namespace client { namespace parser +{ + typedef std::string::const_iterator iterator_type; + typedef x3::phrase_parse_context<x3::ascii::space_type>::type phrase_context_type; + typedef error_handler<iterator_type> error_handler_type; + + typedef x3::context< + error_handler_tag + , std::reference_wrapper<error_handler_type> + , phrase_context_type> + context_type; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/error_handler.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/error_handler.hpp new file mode 100644 index 00000000..8d4e8a65 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/error_handler.hpp @@ -0,0 +1,44 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_ERROR_HANDLER_HPP) +#define BOOST_SPIRIT_X3_CALC9_ERROR_HANDLER_HPP + +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/utility/error_reporting.hpp> +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include "expression.hpp" +#include "statement.hpp" + +namespace client { namespace parser +{ + namespace x3 = boost::spirit::x3; + + //////////////////////////////////////////////////////////////////////////// + // Our error handler + //////////////////////////////////////////////////////////////////////////// + template <typename Iterator> + using error_handler = x3::error_handler<Iterator>; + + // tag used to get our error handler from the context + using error_handler_tag = x3::error_handler_tag; + + struct error_handler_base + { + template <typename Iterator, typename Exception, typename Context> + x3::error_handler_result on_error( + Iterator& first, Iterator const& last + , Exception const& x, Context const& context) + { + std::string message = "Error! Expecting: " + x.which() + " here:"; + auto& error_handler = x3::get<error_handler_tag>(context).get(); + error_handler(x.where(), message); + return x3::error_handler_result::fail; + } + }; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/expression.cpp b/src/boost/libs/spirit/example/x3/calc/calc9/expression.cpp new file mode 100644 index 00000000..e7463cd3 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/expression.cpp @@ -0,0 +1,13 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "expression_def.hpp" +#include "config.hpp" + +namespace client { namespace parser +{ + BOOST_SPIRIT_INSTANTIATE(expression_type, iterator_type, context_type); +}} diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/expression.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/expression.hpp new file mode 100644 index 00000000..f9fa1cc3 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/expression.hpp @@ -0,0 +1,26 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_EXPRESSION_HPP) +#define BOOST_SPIRIT_X3_CALC9_EXPRESSION_HPP + +#include <boost/spirit/home/x3.hpp> +#include "ast.hpp" + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace parser + { + struct expression_class; + typedef x3::rule<expression_class, ast::expression> expression_type; + BOOST_SPIRIT_DECLARE(expression_type); + } + + parser::expression_type const& expression(); +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/expression_def.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/expression_def.hpp new file mode 100644 index 00000000..251fb840 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/expression_def.hpp @@ -0,0 +1,182 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_EXPRESSION_DEF_HPP) +#define BOOST_SPIRIT_X3_CALC9_EXPRESSION_DEF_HPP + +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp> +#include "ast.hpp" +#include "ast_adapted.hpp" +#include "expression.hpp" +#include "common.hpp" +#include "error_handler.hpp" + +namespace client { namespace parser +{ + using x3::uint_; + using x3::char_; + using x3::bool_; + using x3::raw; + using x3::lexeme; + using namespace x3::ascii; + + //////////////////////////////////////////////////////////////////////////// + // Tokens + //////////////////////////////////////////////////////////////////////////// + + x3::symbols<ast::optoken> equality_op; + x3::symbols<ast::optoken> relational_op; + x3::symbols<ast::optoken> logical_op; + x3::symbols<ast::optoken> additive_op; + x3::symbols<ast::optoken> multiplicative_op; + x3::symbols<ast::optoken> unary_op; + x3::symbols<> keywords; + + void add_keywords() + { + static bool once = false; + if (once) + return; + once = true; + + logical_op.add + ("&&", ast::op_and) + ("||", ast::op_or) + ; + + equality_op.add + ("==", ast::op_equal) + ("!=", ast::op_not_equal) + ; + + relational_op.add + ("<", ast::op_less) + ("<=", ast::op_less_equal) + (">", ast::op_greater) + (">=", ast::op_greater_equal) + ; + + additive_op.add + ("+", ast::op_plus) + ("-", ast::op_minus) + ; + + multiplicative_op.add + ("*", ast::op_times) + ("/", ast::op_divide) + ; + + unary_op.add + ("+", ast::op_positive) + ("-", ast::op_negative) + ("!", ast::op_not) + ; + + keywords.add + ("var") + ("true") + ("false") + ("if") + ("else") + ("while") + ; + } + + //////////////////////////////////////////////////////////////////////////// + // Main expression grammar + //////////////////////////////////////////////////////////////////////////// + + struct equality_expr_class; + struct relational_expr_class; + struct logical_expr_class; + struct additive_expr_class; + struct multiplicative_expr_class; + struct unary_expr_class; + struct primary_expr_class; + + typedef x3::rule<equality_expr_class, ast::expression> equality_expr_type; + typedef x3::rule<relational_expr_class, ast::expression> relational_expr_type; + typedef x3::rule<logical_expr_class, ast::expression> logical_expr_type; + typedef x3::rule<additive_expr_class, ast::expression> additive_expr_type; + typedef x3::rule<multiplicative_expr_class, ast::expression> multiplicative_expr_type; + typedef x3::rule<unary_expr_class, ast::operand> unary_expr_type; + typedef x3::rule<primary_expr_class, ast::operand> primary_expr_type; + + expression_type const expression = "expression"; + equality_expr_type const equality_expr = "equality_expr"; + relational_expr_type const relational_expr = "relational_expr"; + logical_expr_type const logical_expr = "logical_expr"; + additive_expr_type const additive_expr = "additive_expr"; + multiplicative_expr_type const multiplicative_expr = "multiplicative_expr"; + unary_expr_type const unary_expr = "unary_expr"; + primary_expr_type const primary_expr = "primary_expr"; + + auto const logical_expr_def = + equality_expr + >> *(logical_op > equality_expr) + ; + + auto const equality_expr_def = + relational_expr + >> *(equality_op > relational_expr) + ; + + auto const relational_expr_def = + additive_expr + >> *(relational_op > additive_expr) + ; + + auto const additive_expr_def = + multiplicative_expr + >> *(additive_op > multiplicative_expr) + ; + + auto const multiplicative_expr_def = + unary_expr + >> *(multiplicative_op > unary_expr) + ; + + auto const unary_expr_def = + primary_expr + | (unary_op > primary_expr) + ; + + auto const primary_expr_def = + uint_ + | bool_ + | (!keywords >> identifier) + | '(' > expression > ')' + ; + + auto const expression_def = logical_expr; + + BOOST_SPIRIT_DEFINE( + expression + , logical_expr + , equality_expr + , relational_expr + , additive_expr + , multiplicative_expr + , unary_expr + , primary_expr + ); + + struct unary_expr_class : x3::annotate_on_success {}; + struct primary_expr_class : x3::annotate_on_success {}; + +}} + +namespace client +{ + parser::expression_type const& expression() + { + parser::add_keywords(); + return parser::expression; + } +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/main.cpp b/src/boost/libs/spirit/example/x3/calc/calc9/main.cpp new file mode 100644 index 00000000..154860a3 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/main.cpp @@ -0,0 +1,110 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Now we'll introduce boolean expressions and control structures. +// Is it obvious now what we are up to? ;-) +// +// [ JDG April 9, 2007 ] spirit2 +// [ JDG February 18, 2011 ] Pure attributes. No semantic actions. +// [ JDG June 6, 2014 ] Ported from qi calc8 example. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "ast.hpp" +#include "vm.hpp" +#include "compiler.hpp" +#include "statement.hpp" +#include "error_handler.hpp" +#include "config.hpp" +#include <iostream> + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Statement parser...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type some statements... "; + std::cout << "An empty line ends input, compiles, runs and prints results\n\n"; + std::cout << "Example:\n\n"; + std::cout << " var a = 123;\n"; + std::cout << " var b = 456;\n"; + std::cout << " var c = a + b * 2;\n\n"; + std::cout << "-------------------------\n"; + + std::string str; + std::string source; + while (std::getline(std::cin, str)) + { + if (str.empty()) + break; + source += str + '\n'; + } + + using client::parser::iterator_type; + iterator_type iter(source.begin()); + iterator_type end(source.end()); + + + client::vmachine vm; // Our virtual machine + client::code_gen::program program; // Our VM program + client::ast::statement_list ast; // Our AST + + using boost::spirit::x3::with; + using client::parser::error_handler_type; + error_handler_type error_handler(iter, end, std::cerr); // Our error handler + + // Our compiler + client::code_gen::compiler compile(program, error_handler); + + // Our parser + auto const parser = + // we pass our error handler to the parser so we can access + // it later on in our on_error and on_sucess handlers + with<client::parser::error_handler_tag>(std::ref(error_handler)) + [ + client::statement() + ]; + + using boost::spirit::x3::ascii::space; + bool success = phrase_parse(iter, end, parser, space, ast); + + std::cout << "-------------------------\n"; + + if (success && iter == end) + { + if (compile.start(ast)) + { + std::cout << "Success\n"; + std::cout << "-------------------------\n"; + vm.execute(program()); + + std::cout << "-------------------------\n"; + std::cout << "Assembler----------------\n\n"; + program.print_assembler(); + + std::cout << "-------------------------\n"; + std::cout << "Results------------------\n\n"; + program.print_variables(vm.get_stack()); + } + else + { + std::cout << "Compile failure\n"; + } + } + else + { + std::cout << "Parse failure\n"; + } + + std::cout << "-------------------------\n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/statement.cpp b/src/boost/libs/spirit/example/x3/calc/calc9/statement.cpp new file mode 100644 index 00000000..ec4bca61 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/statement.cpp @@ -0,0 +1,13 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "statement_def.hpp" +#include "config.hpp" + +namespace client { namespace parser +{ + BOOST_SPIRIT_INSTANTIATE(statement_type, iterator_type, context_type); +}} diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/statement.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/statement.hpp new file mode 100644 index 00000000..d0c3f514 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/statement.hpp @@ -0,0 +1,27 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_STATEMENT_HPP) +#define BOOST_SPIRIT_X3_CALC9_STATEMENT_HPP + +#include <boost/spirit/home/x3.hpp> +#include "ast.hpp" + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace parser + { + struct statement_class; + typedef x3::rule<statement_class, ast::statement_list> statement_type; + typedef statement_type::id statement_id; + BOOST_SPIRIT_DECLARE(statement_type); + } + + parser::statement_type const& statement(); +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/statement_def.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/statement_def.hpp new file mode 100644 index 00000000..84355caa --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/statement_def.hpp @@ -0,0 +1,84 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_STATEMENT_DEF_HPP) +#define BOOST_SPIRIT_X3_CALC9_STATEMENT_DEF_HPP + +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp> +#include "ast.hpp" +#include "ast_adapted.hpp" +#include "statement.hpp" +#include "expression.hpp" +#include "common.hpp" +#include "error_handler.hpp" + +namespace client { namespace parser +{ + using x3::raw; + using x3::lexeme; + using namespace x3::ascii; + + struct statement_list_class; + struct variable_declaration_class; + struct assignment_class; + struct variable_class; + + typedef x3::rule<statement_list_class, ast::statement_list> statement_list_type; + typedef x3::rule<variable_declaration_class, ast::variable_declaration> variable_declaration_type; + typedef x3::rule<assignment_class, ast::assignment> assignment_type; + typedef x3::rule<variable_class, ast::variable> variable_type; + + statement_type const statement("statement"); + statement_list_type const statement_list("statement_list"); + variable_declaration_type const variable_declaration("variable_declaration"); + assignment_type const assignment("assignment"); + variable_type const variable("variable"); + + // Import the expression rule + namespace { auto const& expression = client::expression(); } + + auto const statement_list_def = + +(variable_declaration | assignment) + ; + + auto const variable_declaration_def = + lexeme["var" >> !(alnum | '_')] // make sure we have whole words + > assignment + ; + + auto const assignment_def = + variable + > '=' + > expression + > ';' + ; + + auto const variable_def = identifier; + auto const statement_def = statement_list; + + BOOST_SPIRIT_DEFINE( + statement + , statement_list + , variable_declaration + , assignment + , variable + ); + + struct statement_class : error_handler_base, x3::annotate_on_success {}; + struct assignment_class : x3::annotate_on_success {}; + struct variable_class : x3::annotate_on_success {}; +}} + +namespace client +{ + parser::statement_type const& statement() + { + return parser::statement; + } +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/vm.cpp b/src/boost/libs/spirit/example/x3/calc/calc9/vm.cpp new file mode 100644 index 00000000..d3c16713 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/vm.cpp @@ -0,0 +1,156 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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) +=============================================================================*/ +#include "vm.hpp" +#include <boost/assert.hpp> + +namespace client +{ + int vmachine::execute( + std::vector<int> const& code + , std::vector<int>::const_iterator pc + , std::vector<int>::iterator frame_ptr + ) + { + std::vector<int>::iterator stack_ptr = frame_ptr; + + while (pc != code.end()) + { + BOOST_ASSERT(pc != code.end()); + + switch (*pc++) + { + case op_neg: + stack_ptr[-1] = -stack_ptr[-1]; + break; + + case op_not: + stack_ptr[-1] = !bool(stack_ptr[-1]); + break; + + case op_add: + --stack_ptr; + stack_ptr[-1] += stack_ptr[0]; + break; + + case op_sub: + --stack_ptr; + stack_ptr[-1] -= stack_ptr[0]; + break; + + case op_mul: + --stack_ptr; + stack_ptr[-1] *= stack_ptr[0]; + break; + + case op_div: + --stack_ptr; + stack_ptr[-1] /= stack_ptr[0]; + break; + + case op_eq: + --stack_ptr; + stack_ptr[-1] = bool(stack_ptr[-1] == stack_ptr[0]); + break; + + case op_neq: + --stack_ptr; + stack_ptr[-1] = bool(stack_ptr[-1] != stack_ptr[0]); + break; + + case op_lt: + --stack_ptr; + stack_ptr[-1] = bool(stack_ptr[-1] < stack_ptr[0]); + break; + + case op_lte: + --stack_ptr; + stack_ptr[-1] = bool(stack_ptr[-1] <= stack_ptr[0]); + break; + + case op_gt: + --stack_ptr; + stack_ptr[-1] = bool(stack_ptr[-1] > stack_ptr[0]); + break; + + case op_gte: + --stack_ptr; + stack_ptr[-1] = bool(stack_ptr[-1] >= stack_ptr[0]); + break; + + case op_and: + --stack_ptr; + stack_ptr[-1] = bool(stack_ptr[-1]) && bool(stack_ptr[0]); + break; + + case op_or: + --stack_ptr; + stack_ptr[-1] = bool(stack_ptr[-1]) || bool(stack_ptr[0]); + break; + + case op_load: + *stack_ptr++ = frame_ptr[*pc++]; + break; + + case op_store: + --stack_ptr; + frame_ptr[*pc++] = stack_ptr[0]; + break; + + case op_int: + *stack_ptr++ = *pc++; + break; + + case op_true: + *stack_ptr++ = true; + break; + + case op_false: + *stack_ptr++ = false; + break; + + case op_jump: + pc += *pc; + break; + + case op_jump_if: + if (!bool(stack_ptr[-1])) + pc += *pc; + else + ++pc; + --stack_ptr; + break; + + case op_stk_adj: + stack_ptr = stack.begin() + *pc++; + break; + + case op_call: + { + int nargs = *pc++; + int jump = *pc++; + + // a function call is a recursive call to execute + int r = execute( + code + , code.begin() + jump + , stack_ptr - nargs + ); + + // cleanup after return from function + stack_ptr[-nargs] = r; // get return value + stack_ptr -= (nargs - 1); // the stack will now contain + // the return value + } + break; + + case op_return: + return stack_ptr[-1]; + } + } + return -1; + } +} diff --git a/src/boost/libs/spirit/example/x3/calc/calc9/vm.hpp b/src/boost/libs/spirit/example/x3/calc/calc9/vm.hpp new file mode 100644 index 00000000..345611cb --- /dev/null +++ b/src/boost/libs/spirit/example/x3/calc/calc9/vm.hpp @@ -0,0 +1,81 @@ +/*============================================================================= + Copyright (c) 2001-2014 Joel de Guzman + + 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(BOOST_SPIRIT_X3_CALC9_VM_HPP) +#define BOOST_SPIRIT_X3_CALC9_VM_HPP + +#include <vector> + +namespace client +{ + /////////////////////////////////////////////////////////////////////////// + // The Virtual Machine + /////////////////////////////////////////////////////////////////////////// + enum byte_code + { + op_neg, // negate the top stack entry + op_add, // add top two stack entries + op_sub, // subtract top two stack entries + op_mul, // multiply top two stack entries + op_div, // divide top two stack entries + + op_not, // boolean negate the top stack entry + op_eq, // compare the top two stack entries for == + op_neq, // compare the top two stack entries for != + op_lt, // compare the top two stack entries for < + op_lte, // compare the top two stack entries for <= + op_gt, // compare the top two stack entries for > + op_gte, // compare the top two stack entries for >= + + op_and, // logical and top two stack entries + op_or, // logical or top two stack entries + + op_load, // load a variable + op_store, // store a variable + + op_int, // push constant integer into the stack + op_true, // push constant 0 into the stack + op_false, // push constant 1 into the stack + + op_jump_if, // jump to a relative position in the code if top stack + // evaluates to false + op_jump, // jump to a relative position in the code + + op_stk_adj, // adjust the stack (for args and locals) + op_call, // function call + op_return // return from function + }; + + class vmachine + { + public: + + vmachine(unsigned stackSize = 4096) + : stack(stackSize) + { + } + + int execute( + std::vector<int> const& code // the program code + , std::vector<int>::const_iterator pc // program counter + , std::vector<int>::iterator frame_ptr // start of arguments and locals + ); + + int execute(std::vector<int> const& code) + { + return execute(code, code.begin(), stack.begin()); + }; + + std::vector<int> const& get_stack() const { return stack; }; + + private: + + std::vector<int> stack; + }; +} + +#endif + diff --git a/src/boost/libs/spirit/example/x3/complex_number.cpp b/src/boost/libs/spirit/example/x3/complex_number.cpp new file mode 100644 index 00000000..3d63ab61 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/complex_number.cpp @@ -0,0 +1,98 @@ +/*============================================================================= + Copyright (c) 2002-2015 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A complex number micro parser. +// +// [ JDG May 10, 2002 ] spirit1 +// [ JDG May 9, 2007 ] spirit2 +// [ JDG May 12, 2015 ] spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include <iostream> +#include <string> +#include <complex> + +/////////////////////////////////////////////////////////////////////////////// +// Our complex number parser/compiler +/////////////////////////////////////////////////////////////////////////////// +namespace client +{ + template <typename Iterator> + bool parse_complex(Iterator first, Iterator last, std::complex<double>& c) + { + using boost::spirit::x3::double_; + using boost::spirit::x3::_attr; + using boost::spirit::x3::phrase_parse; + using boost::spirit::x3::ascii::space; + + double rN = 0.0; + double iN = 0.0; + auto fr = [&](auto& ctx){ rN = _attr(ctx); }; + auto fi = [&](auto& ctx){ iN = _attr(ctx); }; + + bool r = phrase_parse(first, last, + + // Begin grammar + ( + '(' >> double_[fr] + >> -(',' >> double_[fi]) >> ')' + | double_[fr] + ), + // End grammar + + space); + + if (!r || first != last) // fail if we did not get a full match + return false; + c = std::complex<double>(rN, iN); + return r; + } +} + +//////////////////////////////////////////////////////////////////////////// +// Main program +//////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tA complex number micro parser for Spirit...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout << "Give me a complex number of the form r or (r) or (r,i) \n"; + std::cout << "Type [q or Q] to quit\n\n"; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + std::complex<double> c; + if (client::parse_complex(str.begin(), str.end(), c)) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << "got: " << c << std::endl; + std::cout << "\n-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/employee.cpp b/src/boost/libs/spirit/example/x3/employee.cpp new file mode 100644 index 00000000..62571027 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/employee.cpp @@ -0,0 +1,134 @@ +/*============================================================================= + Copyright (c) 2002-2015 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A parser for arbitrary tuples. This example presents a parser +// for an employee structure. +// +// [ JDG May 9, 2007 ] +// [ JDG May 13, 2015 ] spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/fusion/include/adapt_struct.hpp> +#include <boost/fusion/include/io.hpp> + +#include <iostream> +#include <string> + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // Our employee struct + /////////////////////////////////////////////////////////////////////////// + struct employee + { + int age; + std::string forename; + std::string surname; + double salary; + }; + + using boost::fusion::operator<<; +}} + +// We need to tell fusion about our employee struct +// to make it a first-class fusion citizen. This has to +// be in global scope. + +BOOST_FUSION_ADAPT_STRUCT(client::ast::employee, + age, forename, surname, salary +) + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // Our employee parser + /////////////////////////////////////////////////////////////////////////////// + namespace parser + { + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + using x3::int_; + using x3::lit; + using x3::double_; + using x3::lexeme; + using ascii::char_; + + x3::rule<class employee, ast::employee> const employee = "employee"; + + auto const quoted_string = lexeme['"' >> +(char_ - '"') >> '"']; + + auto const employee_def = + lit("employee") + >> '{' + >> int_ >> ',' + >> quoted_string >> ',' + >> quoted_string >> ',' + >> double_ + >> '}' + ; + + BOOST_SPIRIT_DEFINE(employee); + } +} + +//////////////////////////////////////////////////////////////////////////// +// Main program +//////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tAn employee parser for Spirit...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout + << "Give me an employee of the form :" + << "employee{age, \"forename\", \"surname\", salary } \n"; + std::cout << "Type [q or Q] to quit\n\n"; + + using boost::spirit::x3::ascii::space; + typedef std::string::const_iterator iterator_type; + using client::parser::employee; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + client::ast::employee emp; + iterator_type iter = str.begin(); + iterator_type const end = str.end(); + bool r = phrase_parse(iter, end, employee, space, emp); + + if (r && iter == end) + { + std::cout << boost::fusion::tuple_open('['); + std::cout << boost::fusion::tuple_close(']'); + std::cout << boost::fusion::tuple_delimiter(", "); + + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << "got: " << emp << std::endl; + std::cout << "\n-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/error_handling.cpp b/src/boost/libs/spirit/example/x3/error_handling.cpp new file mode 100644 index 00000000..3b890a5e --- /dev/null +++ b/src/boost/libs/spirit/example/x3/error_handling.cpp @@ -0,0 +1,277 @@ +/*============================================================================= + Copyright (c) 2002-2018 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// Based on the employee parser (see employee.cpp), this example shows how +// to implement error handling. This example also shows how to "inject" client +// data, using the "with" directive, that the handlers can access. +// +// [ JDG May 9, 2007 ] +// [ JDG May 13, 2015 ] spirit X3 +// [ JDG Feb 19, 2018 ] Error handling for spirit X3 +// +// I would like to thank Rainbowverse, llc (https://primeorbial.com/) +// for sponsoring this work and donating it to the community. +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include <boost/spirit/home/x3/support/utility/error_reporting.hpp> +#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp> +#include <boost/fusion/include/adapt_struct.hpp> +#include <boost/fusion/include/io.hpp> + +#include <iostream> +#include <string> + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // Our AST (employee and person structs) + /////////////////////////////////////////////////////////////////////////// + namespace x3 = boost::spirit::x3; + + struct person : x3::position_tagged + { + person( + std::string const& first_name = "" + , std::string const& last_name = "" + ) + : first_name(first_name) + , last_name(last_name) + {} + + std::string first_name, last_name; + }; + + struct employee : x3::position_tagged + { + int age; + person who; + double salary; + }; + + using boost::fusion::operator<<; +}} + +// We need to tell fusion about our employee struct +// to make it a first-class fusion citizen. This has to +// be in global scope. + +BOOST_FUSION_ADAPT_STRUCT(client::ast::person, + first_name, last_name +) + +BOOST_FUSION_ADAPT_STRUCT(client::ast::employee, + age, who, salary +) + +namespace client +{ + namespace parser + { + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + /////////////////////////////////////////////////////////////////////// + // Our error handler + /////////////////////////////////////////////////////////////////////// + struct error_handler + { + template <typename Iterator, typename Exception, typename Context> + x3::error_handler_result on_error( + Iterator& first, Iterator const& last + , Exception const& x, Context const& context) + { + auto& error_handler = x3::get<x3::error_handler_tag>(context).get(); + std::string message = "Error! Expecting: " + x.which() + " here:"; + error_handler(x.where(), message); + return x3::error_handler_result::fail; + } + }; + + /////////////////////////////////////////////////////////////////////// + // Our employee parser + /////////////////////////////////////////////////////////////////////// + + using x3::int_; + using x3::double_; + using x3::lexeme; + using ascii::char_; + + struct quoted_string_class; + struct person_class; + struct employee_class; + + x3::rule<quoted_string_class, std::string> const quoted_string = "quoted_string"; + x3::rule<person_class, ast::person> const person = "person"; + x3::rule<employee_class, ast::employee> const employee = "employee"; + + auto const quoted_string_def = lexeme['"' >> +(char_ - '"') >> '"']; + auto const person_def = quoted_string > ',' > quoted_string; + + auto const employee_def = + '{' + > int_ > ',' + > person > ',' + > double_ + > '}' + ; + + auto const employees = employee >> *(',' >> employee); + + BOOST_SPIRIT_DEFINE(quoted_string, person, employee); + + struct quoted_string_class {}; + struct person_class : x3::annotate_on_success {}; + struct employee_class : error_handler, x3::annotate_on_success {}; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Our main parse entry point +/////////////////////////////////////////////////////////////////////////////// + +void parse(std::string const& input) +{ + using boost::spirit::x3::ascii::space; + typedef std::string::const_iterator iterator_type; + + std::vector<client::ast::employee> ast; + iterator_type iter = input.begin(); + iterator_type const end = input.end(); + + using boost::spirit::x3::with; + using boost::spirit::x3::error_handler_tag; + using error_handler_type = boost::spirit::x3::error_handler<iterator_type>; + + // Our error handler + error_handler_type error_handler(iter, end, std::cerr); + + // Our parser + using client::parser::employees; + auto const parser = + // we pass our error handler to the parser so we can access + // it later in our on_error and on_sucess handlers + with<error_handler_tag>(std::ref(error_handler)) + [ + employees + ]; + + bool r = phrase_parse(iter, end, parser, space, ast); + + if (r && iter == end) + { + std::cout << boost::fusion::tuple_open('['); + std::cout << boost::fusion::tuple_close(']'); + std::cout << boost::fusion::tuple_delimiter(", "); + + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + + for (auto const& emp : ast) + { + std::cout << "got: " << emp << std::endl; + } + std::cout << "\n-------------------------\n"; + + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + ast.clear(); + } +} + +// Good sample: + +std::string good_input = R"( +{ + 23, + "Amanda", + "Stefanski", + 1000.99 +}, +{ + 35, + "Angie", + "Chilcote", + 2000.99 +}, +{ + 43, + "Dannie", + "Dillinger", + 3000.99 +}, +{ + 22, + "Dorene", + "Dole", + 2500.99 +}, +{ + 38, + "Rossana", + "Rafferty", + 5000.99 +} +)"; + +// Input sample with error: + +std::string bad_input = R"( +{ + 23, + "Amanda", + "Stefanski", + 1000.99 +}, +{ + 35, + "Angie", + "Chilcote", + 2000.99 +}, +{ + 43, + 'I am not a person!' <--- this should be a person + 3000.99 +}, +{ + 22, + "Dorene", + "Dole", + 2500.99 +}, +{ + 38, + "Rossana", + "Rafferty", + 5000.99 +} +)"; + +int +main() +{ + // Good input + parse(good_input); + + // Bad input + std::cout << "Now we have some errors" << std::endl; + parse(bad_input); + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/minimal/ast.hpp b/src/boost/libs/spirit/example/x3/minimal/ast.hpp new file mode 100644 index 00000000..b66bb072 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/minimal/ast.hpp @@ -0,0 +1,31 @@ +/*============================================================================= + Copyright (c) 2002-2018 Joel de Guzman + + 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(BOOST_SPIRIT_X3_MINIMAL_AST_HPP) +#define BOOST_SPIRIT_X3_MINIMAL_AST_HPP + +#include <boost/fusion/include/io.hpp> + +#include <iostream> +#include <string> + +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // Our employee AST struct + /////////////////////////////////////////////////////////////////////////// + struct employee + { + int age; + std::string forename; + std::string surname; + double salary; + }; + + using boost::fusion::operator<<; +}} + +#endif
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/minimal/ast_adapted.hpp b/src/boost/libs/spirit/example/x3/minimal/ast_adapted.hpp new file mode 100644 index 00000000..9d6c6fe9 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/minimal/ast_adapted.hpp @@ -0,0 +1,21 @@ +/*============================================================================= + Copyright (c) 2002-2018 Joel de Guzman + + 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(BOOST_SPIRIT_X3_MINIMAL_AST_ADAPTED_HPP) +#define BOOST_SPIRIT_X3_MINIMAL_AST_ADAPTED_HPP + +#include <boost/fusion/include/adapt_struct.hpp> +#include "ast.hpp" + +// We need to tell fusion about our employee struct +// to make it a first-class fusion citizen. This has to +// be in global scope. + +BOOST_FUSION_ADAPT_STRUCT(client::ast::employee, + age, forename, surname, salary +) + +#endif diff --git a/src/boost/libs/spirit/example/x3/minimal/config.hpp b/src/boost/libs/spirit/example/x3/minimal/config.hpp new file mode 100644 index 00000000..3787346a --- /dev/null +++ b/src/boost/libs/spirit/example/x3/minimal/config.hpp @@ -0,0 +1,20 @@ +/*============================================================================= + Copyright (c) 2001-2018 Joel de Guzman + + 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(BOOST_SPIRIT_X3_MINIMAL_CONFIG_HPP) +#define BOOST_SPIRIT_X3_MINIMAL_CONFIG_HPP + +#include <boost/spirit/home/x3.hpp> + +namespace client { namespace parser +{ + namespace x3 = boost::spirit::x3; + + using iterator_type = std::string::const_iterator; + using context_type = x3::phrase_parse_context<x3::ascii::space_type>::type; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/minimal/employee.cpp b/src/boost/libs/spirit/example/x3/minimal/employee.cpp new file mode 100644 index 00000000..8b2fa6c1 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/minimal/employee.cpp @@ -0,0 +1,13 @@ +/*============================================================================= + Copyright (c) 2001-2018 Joel de Guzman + + 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) +=============================================================================*/ +#include "employee_def.hpp" +#include "config.hpp" + +namespace client { namespace parser +{ + BOOST_SPIRIT_INSTANTIATE(employee_type, iterator_type, context_type); +}} diff --git a/src/boost/libs/spirit/example/x3/minimal/employee.hpp b/src/boost/libs/spirit/example/x3/minimal/employee.hpp new file mode 100644 index 00000000..a4475ec2 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/minimal/employee.hpp @@ -0,0 +1,30 @@ +/*============================================================================= + Copyright (c) 2002-2018 Joel de Guzman + + 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(BOOST_SPIRIT_X3_MINIMAL_EMPLOYEE_HPP) +#define BOOST_SPIRIT_X3_MINIMAL_EMPLOYEE_HPP + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include "ast.hpp" + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // Our employee parser declaration + /////////////////////////////////////////////////////////////////////////////// + namespace parser + { + namespace x3 = boost::spirit::x3; + using employee_type = x3::rule<class employee, ast::employee>; + BOOST_SPIRIT_DECLARE(employee_type); + } + + parser::employee_type employee(); +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/minimal/employee_def.hpp b/src/boost/libs/spirit/example/x3/minimal/employee_def.hpp new file mode 100644 index 00000000..227b2f7f --- /dev/null +++ b/src/boost/libs/spirit/example/x3/minimal/employee_def.hpp @@ -0,0 +1,56 @@ +/*============================================================================= + Copyright (c) 2002-2018 Joel de Guzman + + 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(BOOST_SPIRIT_X3_MINIMAL_EMPLOYEE_DEF_HPP) +#define BOOST_SPIRIT_X3_MINIMAL_EMPLOYEE_DEF_HPP + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include "ast.hpp" +#include "ast_adapted.hpp" +#include "employee.hpp" + +namespace client +{ + /////////////////////////////////////////////////////////////////////////////// + // Our employee parser definition + /////////////////////////////////////////////////////////////////////////////// + namespace parser + { + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + using x3::int_; + using x3::lit; + using x3::double_; + using x3::lexeme; + using ascii::char_; + + x3::rule<class employee, ast::employee> const employee = "employee"; + + auto const quoted_string = lexeme['"' >> +(char_ - '"') >> '"']; + + auto const employee_def = + lit("employee") + >> '{' + >> int_ >> ',' + >> quoted_string >> ',' + >> quoted_string >> ',' + >> double_ + >> '}' + ; + + BOOST_SPIRIT_DEFINE(employee); + } + + parser::employee_type employee() + { + return parser::employee; + } +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/minimal/main.cpp b/src/boost/libs/spirit/example/x3/minimal/main.cpp new file mode 100644 index 00000000..aeb3bc51 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/minimal/main.cpp @@ -0,0 +1,79 @@ +/*============================================================================= + Copyright (c) 2002-2018 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// This is the same employee parser (see employee.cpp) but structured to +// allow separate compilation of the actual parser in its own definition +// file (employee_def.hpp) and cpp file (employee.cpp). This main cpp file +// sees only the header file (employee.hpp). This is a good example on how +// parsers are structured in a C++ application. +// +// [ JDG May 9, 2007 ] +// [ JDG May 13, 2015 ] spirit X3 +// [ JDG Feb 20, 2018 ] Minimal "best practice" example +// +// I would like to thank Rainbowverse, llc (https://primeorbial.com/) +// for sponsoring this work and donating it to the community. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "ast.hpp" +#include "ast_adapted.hpp" +#include "employee.hpp" + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tAn employee parser for Spirit...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout + << "Give me an employee of the form :" + << "employee{age, \"forename\", \"surname\", salary } \n"; + std::cout << "Type [q or Q] to quit\n\n"; + + using boost::spirit::x3::ascii::space; + using iterator_type = std::string::const_iterator; + using client::employee; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + client::ast::employee emp; + iterator_type iter = str.begin(); + iterator_type const end = str.end(); + bool r = phrase_parse(iter, end, employee(), space, emp); + + if (r && iter == end) + { + std::cout << boost::fusion::tuple_open('['); + std::cout << boost::fusion::tuple_close(']'); + std::cout << boost::fusion::tuple_delimiter(", "); + + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << "got: " << emp << std::endl; + std::cout << "\n-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/num_list/num_list1.cpp b/src/boost/libs/spirit/example/x3/num_list/num_list1.cpp new file mode 100644 index 00000000..34371e67 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/num_list/num_list1.cpp @@ -0,0 +1,87 @@ +/*============================================================================= + Copyright (c) 2002-2015 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// This sample demontrates a parser for a comma separated list of numbers. +// No semantic actions. +// +// [ JDG May 10, 2002 ] spirit1 +// [ JDG March 24, 2007 ] spirit2 +// [ JDG May 12, 2015 ] spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include <iostream> +#include <string> +#include <vector> + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + /////////////////////////////////////////////////////////////////////////// + // Our number list parser + /////////////////////////////////////////////////////////////////////////// + template <typename Iterator> + bool parse_numbers(Iterator first, Iterator last) + { + using x3::double_; + using x3::phrase_parse; + using ascii::space; + + bool r = phrase_parse( + first, // Start Iterator + last, // End Iterator + double_ >> *(',' >> double_), // The Parser + space // The Skip-Parser + ); + if (first != last) // fail if we did not get a full match + return false; + return r; + } +} + +//////////////////////////////////////////////////////////////////////////// +// Main program +//////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tA comma separated list parser for Spirit...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout << "Give me a comma separated list of numbers.\n"; + std::cout << "Type [q or Q] to quit\n\n"; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + if (client::parse_numbers(str.begin(), str.end())) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << str << " Parses OK: " << std::endl; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/num_list/num_list2.cpp b/src/boost/libs/spirit/example/x3/num_list/num_list2.cpp new file mode 100644 index 00000000..67a75053 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/num_list/num_list2.cpp @@ -0,0 +1,101 @@ +/*============================================================================= + Copyright (c) 2002-2015 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// This sample demontrates a parser for a comma separated list of numbers. +// +// [ JDG May 10, 2002 ] spirit1 +// [ JDG March 24, 2007 ] spirit2 +// [ JDG May 12, 2015 ] spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include <iostream> +#include <string> +#include <vector> + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + /////////////////////////////////////////////////////////////////////////// + // Our number list compiler + /////////////////////////////////////////////////////////////////////////// + template <typename Iterator> + bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v) + { + using x3::double_; + using x3::phrase_parse; + using x3::_attr; + using ascii::space; + + auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); }; + + bool r = phrase_parse(first, last, + + // Begin grammar + ( + double_[push_back] >> *(',' >> double_[push_back]) + ) + , + // End grammar + + space); + + if (first != last) // fail if we did not get a full match + return false; + return r; + } +} + +//////////////////////////////////////////////////////////////////////////// +// Main program +//////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tA comma separated list parser for Spirit...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout << "Give me a comma separated list of numbers.\n"; + std::cout << "The numbers will be inserted in a vector of numbers\n"; + std::cout << "Type [q or Q] to quit\n\n"; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + std::vector<double> v; + if (client::parse_numbers(str.begin(), str.end(), v)) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << str << " Parses OK: " << std::endl; + + for (std::vector<double>::size_type i = 0; i < v.size(); ++i) + std::cout << i << ": " << v[i] << std::endl; + + std::cout << "\n-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/num_list/num_list3.cpp b/src/boost/libs/spirit/example/x3/num_list/num_list3.cpp new file mode 100644 index 00000000..53a275bb --- /dev/null +++ b/src/boost/libs/spirit/example/x3/num_list/num_list3.cpp @@ -0,0 +1,101 @@ +/*============================================================================= + Copyright (c) 2002-2015 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// This sample demontrates a parser for a comma separated list of numbers. +// +// [ JDG May 10, 2002 ] spirit1 +// [ JDG March 24, 2007 ] spirit2 +// [ JDG May 12, 2015 ] spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include <iostream> +#include <string> +#include <vector> + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + /////////////////////////////////////////////////////////////////////////// + // Our number list compiler + /////////////////////////////////////////////////////////////////////////// + template <typename Iterator> + bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v) + { + using x3::double_; + using x3::phrase_parse; + using x3::_attr; + using ascii::space; + + auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); }; + + bool r = phrase_parse(first, last, + + // Begin grammar + ( + double_[push_back] % ',' + ) + , + // End grammar + + space); + + if (first != last) // fail if we did not get a full match + return false; + return r; + } +} + +//////////////////////////////////////////////////////////////////////////// +// Main program +//////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tA comma separated list parser for Spirit...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout << "Give me a comma separated list of numbers.\n"; + std::cout << "The numbers will be inserted in a vector of numbers\n"; + std::cout << "Type [q or Q] to quit\n\n"; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + std::vector<double> v; + if (client::parse_numbers(str.begin(), str.end(), v)) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << str << " Parses OK: " << std::endl; + + for (std::vector<double>::size_type i = 0; i < v.size(); ++i) + std::cout << i << ": " << v[i] << std::endl; + + std::cout << "\n-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/num_list/num_list4.cpp b/src/boost/libs/spirit/example/x3/num_list/num_list4.cpp new file mode 100644 index 00000000..1c88b284 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/num_list/num_list4.cpp @@ -0,0 +1,103 @@ +/*============================================================================= + Copyright (c) 2002-2015 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// This sample demontrates a parser for a comma separated list of numbers. +// This time, the numbers are automatically collected into the attribute by +// the parser itself using the full power of attribute grammars. +// +// [ JDG May 10, 2002 ] spirit1 +// [ JDG March 24, 2007 ] spirit2 +// [ JDG May 12, 2015 ] spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include <iostream> +#include <string> +#include <vector> + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + /////////////////////////////////////////////////////////////////////////// + // Our number list compiler + /////////////////////////////////////////////////////////////////////////// + //[tutorial_numlist4 + template <typename Iterator> + bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v) + { + using x3::double_; + using x3::phrase_parse; + using x3::_attr; + using ascii::space; + + bool r = phrase_parse(first, last, + + // Begin grammar + ( + double_ % ',' + ) + , + // End grammar + + space, v); + + if (first != last) // fail if we did not get a full match + return false; + return r; + } + //] +} + +//////////////////////////////////////////////////////////////////////////// +// Main program +//////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tA comma separated list parser for Spirit...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout << "Give me a comma separated list of numbers.\n"; + std::cout << "The numbers will be inserted in a vector of numbers\n"; + std::cout << "Type [q or Q] to quit\n\n"; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + std::vector<double> v; + if (client::parse_numbers(str.begin(), str.end(), v)) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << str << " Parses OK: " << std::endl; + + for (std::vector<double>::size_type i = 0; i < v.size(); ++i) + std::cout << i << ": " << v[i] << std::endl; + + std::cout << "\n-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_examples/a.rexpr b/src/boost/libs/spirit/example/x3/rexpr/rexpr_examples/a.rexpr new file mode 100644 index 00000000..880f5bac --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_examples/a.rexpr @@ -0,0 +1,8 @@ +{ + "color" = "blue" + "size" = "29 cm." + "position" = { + "x" = "123" + "y" = "456" + } +}
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/Jamfile b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/Jamfile new file mode 100644 index 00000000..5723b1ed --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/Jamfile @@ -0,0 +1,20 @@ +# +# Copyright (C) 2015 Michael Caisse, ciere.com +# +# 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) +# + +project spirit-x3-example-rexpr + : requirements + <c++-template-depth>512 + <use>/boost//headers + <define>BOOST_SPIRIT_X3_NO_FILESYSTEM + <include>. + ; + +lib rexpr + : [ glob src/*.cpp ] + ; + +build-project test ; diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/ast.hpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/ast.hpp new file mode 100644 index 00000000..c7635b12 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/ast.hpp @@ -0,0 +1,42 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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(BOOST_SPIRIT_X3_REPR_AST_HPP) +#define BOOST_SPIRIT_X3_REPR_AST_HPP + +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> + +#include <map> + +namespace rexpr { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // The AST + /////////////////////////////////////////////////////////////////////////// + namespace x3 = boost::spirit::x3; + + struct rexpr; + + struct rexpr_value : x3::variant< + std::string + , x3::forward_ast<rexpr> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + typedef std::map<std::string, rexpr_value> rexpr_map; + typedef std::pair<std::string, rexpr_value> rexpr_key_value; + + struct rexpr : x3::position_tagged + { + rexpr_map entries; + }; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/ast_adapted.hpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/ast_adapted.hpp new file mode 100644 index 00000000..d325cbbd --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/ast_adapted.hpp @@ -0,0 +1,22 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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(BOOST_SPIRIT_X3_REPR_AST_ADAPTED_HPP) +#define BOOST_SPIRIT_X3_REPR_AST_ADAPTED_HPP + +#include "ast.hpp" + +#include <boost/fusion/include/adapt_struct.hpp> +#include <boost/fusion/include/std_pair.hpp> + +// We need to tell fusion about our rexpr and rexpr_key_value +// to make them a first-class fusion citizens + +BOOST_FUSION_ADAPT_STRUCT(rexpr::ast::rexpr, + entries +) + +#endif diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/config.hpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/config.hpp new file mode 100644 index 00000000..12a25def --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/config.hpp @@ -0,0 +1,35 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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(BOOST_SPIRIT_X3_REPR_CONFIG_HPP) +#define BOOST_SPIRIT_X3_REPR_CONFIG_HPP + +#include "error_handler.hpp" + +#include <boost/spirit/home/x3.hpp> + +namespace rexpr { namespace parser +{ + // Our Iterator Type + typedef std::string::const_iterator iterator_type; + + // The Phrase Parse Context + typedef + x3::phrase_parse_context<x3::ascii::space_type>::type + phrase_context_type; + + // Our Error Handler + typedef error_handler<iterator_type> error_handler_type; + + // Combined Error Handler and Phrase Parse Context + typedef x3::context< + error_handler_tag + , std::reference_wrapper<error_handler_type> + , phrase_context_type> + context_type; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/error_handler.hpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/error_handler.hpp new file mode 100644 index 00000000..a173d492 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/error_handler.hpp @@ -0,0 +1,72 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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(BOOST_SPIRIT_X3_REPR_ERROR_HANDLER_HPP) +#define BOOST_SPIRIT_X3_REPR_ERROR_HANDLER_HPP + +#include "rexpr.hpp" + +#include <boost/spirit/home/x3/support/ast/position_tagged.hpp> +#include <boost/spirit/home/x3/support/utility/error_reporting.hpp> + +#include <map> + +namespace rexpr { namespace parser +{ + namespace x3 = boost::spirit::x3; + + //////////////////////////////////////////////////////////////////////////// + // Our error handler + //////////////////////////////////////////////////////////////////////////// + // X3 Error Handler Utility + template <typename Iterator> + using error_handler = x3::error_handler<Iterator>; + + // tag used to get our error handler from the context + using error_handler_tag = x3::error_handler_tag; + + struct error_handler_base + { + error_handler_base(); + + template <typename Iterator, typename Exception, typename Context> + x3::error_handler_result on_error( + Iterator& first, Iterator const& last + , Exception const& x, Context const& context); + + std::map<std::string, std::string> id_map; + }; + + //////////////////////////////////////////////////////////////////////////// + // Implementation + //////////////////////////////////////////////////////////////////////////// + + inline error_handler_base::error_handler_base() + { + id_map["rexpr"] = "RExpression"; + id_map["rexpr_value"] = "Value"; + id_map["rexpr_key_value"] = "Key value pair"; + } + + template <typename Iterator, typename Exception, typename Context> + inline x3::error_handler_result + error_handler_base::on_error( + Iterator& first, Iterator const& last + , Exception const& x, Context const& context) + { + std::string which = x.which(); + auto iter = id_map.find(which); + if (iter != id_map.end()) + which = iter->second; + + std::string message = "Error! Expecting: " + which + " here:"; + auto& error_handler = x3::get<error_handler_tag>(context).get(); + error_handler(x.where(), message); + return x3::error_handler_result::fail; + } +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/printer.hpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/printer.hpp new file mode 100644 index 00000000..a1c307c6 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/printer.hpp @@ -0,0 +1,57 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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(BOOST_SPIRIT_X3_REPR_PRINTER_HPP) +#define BOOST_SPIRIT_X3_REPR_PRINTER_HPP + +#include "ast.hpp" + +#include <ostream> + +namespace rexpr { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // Print out the rexpr tree + /////////////////////////////////////////////////////////////////////////// + int const tabsize = 4; + + struct rexpr_printer + { + typedef void result_type; + + rexpr_printer(std::ostream& out, int indent = 0) + : out(out), indent(indent) {} + + void operator()(rexpr const& ast) const + { + out << '{' << std::endl; + for (auto const& entry : ast.entries) + { + tab(indent+tabsize); + out << '"' << entry.first << "\" = "; + boost::apply_visitor(rexpr_printer(out, indent+tabsize), entry.second); + } + tab(indent); + out << '}' << std::endl; + } + + void operator()(std::string const& text) const + { + out << '"' << text << '"' << std::endl; + } + + void tab(int spaces) const + { + for (int i = 0; i < spaces; ++i) + out << ' '; + } + + std::ostream& out; + int indent; + }; +}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/rexpr.hpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/rexpr.hpp new file mode 100644 index 00000000..f3979f9d --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/rexpr.hpp @@ -0,0 +1,33 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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(BOOST_SPIRIT_X3_REXPR_HPP) +#define BOOST_SPIRIT_X3_REXPR_HPP + +#include "ast.hpp" + +#include <boost/spirit/home/x3.hpp> + +namespace rexpr +{ + namespace x3 = boost::spirit::x3; + + /////////////////////////////////////////////////////////////////////////// + // rexpr public interface + /////////////////////////////////////////////////////////////////////////// + namespace parser + { + struct rexpr_class; + typedef + x3::rule<rexpr_class, ast::rexpr> + rexpr_type; + BOOST_SPIRIT_DECLARE(rexpr_type); + } + + parser::rexpr_type const& rexpr(); +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/rexpr_def.hpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/rexpr_def.hpp new file mode 100644 index 00000000..3875031a --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/rexpr/rexpr_def.hpp @@ -0,0 +1,94 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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(BOOST_SPIRIT_X3_REPR_REXPR_DEF_HPP) +#define BOOST_SPIRIT_X3_REPR_REXPR_DEF_HPP + +#include "ast.hpp" +#include "ast_adapted.hpp" +#include "error_handler.hpp" +#include "rexpr.hpp" + +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp> + +namespace rexpr { namespace parser +{ + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + using x3::lit; + using x3::lexeme; + + using ascii::char_; + using ascii::string; + + /////////////////////////////////////////////////////////////////////////// + // Rule IDs + /////////////////////////////////////////////////////////////////////////// + + struct rexpr_value_class; + struct rexpr_key_value_class; + struct rexpr_inner_class; + + /////////////////////////////////////////////////////////////////////////// + // Rules + /////////////////////////////////////////////////////////////////////////// + + x3::rule<rexpr_value_class, ast::rexpr_value> const + rexpr_value = "rexpr_value"; + + x3::rule<rexpr_key_value_class, ast::rexpr_key_value> const + rexpr_key_value = "rexpr_key_value"; + + x3::rule<rexpr_inner_class, ast::rexpr> const + rexpr_inner = "rexpr"; + + rexpr_type const rexpr = "rexpr"; + + /////////////////////////////////////////////////////////////////////////// + // Grammar + /////////////////////////////////////////////////////////////////////////// + + auto const quoted_string = + lexeme['"' >> *(char_ - '"') >> '"']; + + auto const rexpr_value_def = + quoted_string | rexpr_inner; + + auto const rexpr_key_value_def = + quoted_string > '=' > rexpr_value; + + auto const rexpr_inner_def = + '{' > *rexpr_key_value > '}'; + + auto const rexpr_def = rexpr_inner_def; + + BOOST_SPIRIT_DEFINE(rexpr_value, rexpr, rexpr_inner, rexpr_key_value); + + /////////////////////////////////////////////////////////////////////////// + // Annotation and Error handling + /////////////////////////////////////////////////////////////////////////// + + // We want these to be annotated with the iterator position. + struct rexpr_value_class : x3::annotate_on_success {}; + struct rexpr_key_value_class : x3::annotate_on_success {}; + struct rexpr_inner_class : x3::annotate_on_success {}; + + // We want error-handling only for the start (outermost) rexpr + // rexpr is the same as rexpr_inner but without error-handling (see error_handler.hpp) + struct rexpr_class : x3::annotate_on_success, error_handler_base {}; +}} + +namespace rexpr +{ + parser::rexpr_type const& rexpr() + { + return parser::rexpr; + } +} + +#endif diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/src/rexpr.cpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/src/rexpr.cpp new file mode 100644 index 00000000..14b84c60 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/src/rexpr.cpp @@ -0,0 +1,14 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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) +=============================================================================*/ +#include "../rexpr/rexpr_def.hpp" +#include "../rexpr/config.hpp" + +namespace rexpr { namespace parser +{ + BOOST_SPIRIT_INSTANTIATE( + rexpr_type, iterator_type, context_type); +}} diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/Jamfile b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/Jamfile new file mode 100644 index 00000000..4952e281 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/Jamfile @@ -0,0 +1,23 @@ +# +# Copyright (C) 2015 Michael Caisse, ciere.com +# +# 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) +# + +import testing ; + +project + : requirements + <source>..//rexpr + <source>/boost//regex + <source>/boost//filesystem + <c++-template-depth>300 + ; + +path-constant TEST_FILES : test_files ; + +unit-test parse_rexpr_test + : parse_rexpr_test.cpp + : <testing.arg>"$(TEST_FILES)" + ; diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/parse_rexpr_test.cpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/parse_rexpr_test.cpp new file mode 100644 index 00000000..bdbacf2d --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/parse_rexpr_test.cpp @@ -0,0 +1,86 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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) +=============================================================================*/ +#include <iostream> +#include <iterator> +#include <algorithm> +#include <sstream> + +#include "../rexpr/ast.hpp" +#include "../rexpr/rexpr.hpp" +#include "../rexpr/error_handler.hpp" +#include "../rexpr/config.hpp" +#include "../rexpr/printer.hpp" + +#include "testing.hpp" + +namespace fs = boost::filesystem; +namespace testing = boost::spirit::x3::testing; + +auto parse = [](std::string const& source, fs::path input_path)-> std::string +{ + std::stringstream out; + + using rexpr::parser::iterator_type; + iterator_type iter(source.begin()); + iterator_type const end(source.end()); + + // Our AST + rexpr::ast::rexpr ast; + + // Our error handler + using boost::spirit::x3::with; + using rexpr::parser::error_handler_type; + using rexpr::parser::error_handler_tag; + error_handler_type error_handler(iter, end, out, input_path.string()); // Our error handler + + // Our parser + auto const parser = + // we pass our error handler to the parser so we can access + // it later on in our on_error and on_sucess handlers + with<error_handler_tag>(std::ref(error_handler)) + [ + rexpr::rexpr() + ]; + + // Go forth and parse! + using boost::spirit::x3::ascii::space; + bool success = phrase_parse(iter, end, parser, space, ast); + + if (success) + { + if (iter != end) + error_handler(iter, "Error! Expecting end of input here: "); + else + rexpr::ast::rexpr_printer{out}(ast); + } + + return out.str(); +}; + +int num_files_tested = 0; +auto compare = [](fs::path input_path, fs::path expect_path) +{ + testing::compare(input_path, expect_path, parse); + ++num_files_tested; +}; + +int main(int argc, char* argv[]) +{ + if (argc < 2) + { + std::cout << "usage: " << fs::path(argv[0]).filename() << " path/to/test/files" << std::endl; + return -1; + } + + std::cout << "===================================================================================================" << std::endl; + std::cout << "Testing: " << fs::absolute(fs::path(argv[1])) << std::endl; + int r = testing::for_each_file(fs::path(argv[1]), compare); + if (r == 0) + std::cout << num_files_tested << " files tested." << std::endl; + std::cout << "===================================================================================================" << std::endl; + return r; +} diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/a.expect b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/a.expect new file mode 100644 index 00000000..635c145b --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/a.expect @@ -0,0 +1,8 @@ +{ + "color" = "blue" + "position" = { + "x" = "123" + "y" = "456" + } + "size" = "29 cm." +} diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/a.input b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/a.input new file mode 100644 index 00000000..880f5bac --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/a.input @@ -0,0 +1,8 @@ +{ + "color" = "blue" + "size" = "29 cm." + "position" = { + "x" = "123" + "y" = "456" + } +}
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/b.expect b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/b.expect new file mode 100644 index 00000000..3f485184 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/b.expect @@ -0,0 +1,4 @@ +In file <%.*?b.input%>, line 4: +Error! Expecting: Value here: + "position" = $ +_________________^_
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/b.input b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/b.input new file mode 100644 index 00000000..6f0743b1 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/b.input @@ -0,0 +1,8 @@ +{ + "color" = "blue" + "size" = "29 cm." + "position" = $ + "x" = "123" + "y" = "456" + } +}
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/c.expect b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/c.expect new file mode 100644 index 00000000..bc7efcd0 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/c.expect @@ -0,0 +1,4 @@ +In file <%.*?c.input%>, line 5: +Error! Expecting: '}' here: + banana +________^_
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/c.input b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/c.input new file mode 100644 index 00000000..6eaa1259 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/c.input @@ -0,0 +1,8 @@ +{ + "color" = "blue" + "size" = "29 cm." + "position" = { + banana + "y" = "456" + } +}
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/d.expect b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/d.expect new file mode 100644 index 00000000..5b376035 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/d.expect @@ -0,0 +1,4 @@ +In file <%.*?d.input%>, line 5: +Error! Expecting: '=' here: + "x" : "123" +____________^_ diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/d.input b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/d.input new file mode 100644 index 00000000..c8be6438 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/d.input @@ -0,0 +1,8 @@ +{ + "color" = "blue" + "size" = "29 cm." + "position" = { + "x" : "123" + "y" = "456" + } +}
\ No newline at end of file diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/e.expect b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/e.expect new file mode 100644 index 00000000..4fed2140 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/e.expect @@ -0,0 +1,5 @@ +In file <%.*?e.input%>, line 8: +Error! Expecting end of input here: +}; +_^_ + diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/e.input b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/e.input new file mode 100644 index 00000000..1bd447f1 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/test_files/e.input @@ -0,0 +1,8 @@ +{ + "color" = "blue" + "size" = "29 cm." + "position" = { + "x" = "123" + "y" = "456" + } +}; diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/testing.hpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/testing.hpp new file mode 100644 index 00000000..eb99f692 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_full/test/testing.hpp @@ -0,0 +1,269 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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(BOOST_SPIRIT_X3_TEST_UTILITIES) +#define BOOST_SPIRIT_X3_TEST_UTILITIES + +#include <boost/regex.hpp> +#include <boost/filesystem.hpp> +#include <boost/filesystem/fstream.hpp> + +namespace boost { namespace spirit { namespace x3 { namespace testing +{ + namespace fs = boost::filesystem; + + //////////////////////////////////////////////////////////////////////////// + // compare + // + // Compares the contents of in with the template tem. The template + // may include embedded regular expressions marked up within re_prefix + // and re_suffix tags. For example, given the default RE markup, this + // template <%[0-9]+%> will match any integer in in. The function + // will return the first non-matching position. The flag full_match + // indicates a full match. It is possible for returned pos to be + // at the end of in (in.end()) while still returning full_match == + // false. In that case, we have a partial match. + //////////////////////////////////////////////////////////////////////////// + + template <typename Iterator> + struct compare_result + { + compare_result( + Iterator pos + , bool full_match + ) : pos(pos), full_match(full_match) {} + + Iterator pos; + bool full_match; + }; + + template <typename Range> + compare_result<typename Range::const_iterator> + compare( + Range const& in + , Range const& tem + , char const* re_prefix = "<%" + , char const* re_suffix = "%>" + ); + + //////////////////////////////////////////////////////////////////////////// + // compare + // + // 1) Call f, given the contents of input_path loaded in a string. + // The result of calling f is the output string. + // 2) Compare the result of calling f with expected template + // file (expect_path) using the low-level compare utility + // abive + //////////////////////////////////////////////////////////////////////////// + template <typename F> + bool compare( + fs::path input_path, fs::path expect_path + , F f + , char const* re_prefix = "<%" + , char const* re_suffix = "%>" + ); + + //////////////////////////////////////////////////////////////////////////// + // for_each_file + // + // For each *.input and *.expect file in a given directory, + // call the function f, passing in the *.input and *.expect paths. + //////////////////////////////////////////////////////////////////////////// + template <typename F> + int for_each_file(fs::path p, F f); + + //////////////////////////////////////////////////////////////////////////// + // load_file + // + // Load file into a string. + //////////////////////////////////////////////////////////////////////////// + std::string load(fs::path p); + + //////////////////////////////////////////////////////////////////////////// + // Implementation + //////////////////////////////////////////////////////////////////////////// + + template <typename Iterator> + inline bool is_regex( + Iterator& first + , Iterator last + , std::string& re + , char const* re_prefix + , char const* re_suffix + ) + { + boost::regex e(re_prefix + std::string("(.*?)") + re_suffix); + boost::match_results<Iterator> what; + if (boost::regex_search( + first, last, what, e + , boost::match_default | boost::match_continuous)) + { + re = what[1].str(); + first = what[0].second; + return true; + } + return false; + } + + template <typename Range> + inline compare_result<typename Range::const_iterator> + compare( + Range const& in + , Range const& tem + , char const* re_prefix + , char const* re_suffix + ) + { + typedef typename Range::const_iterator iter_t; + typedef compare_result<iter_t> compare_result_t; + + iter_t in_first = in.begin(); + iter_t in_last = in.end(); + iter_t tem_first = tem.begin(); + iter_t tem_last = tem.end(); + std::string re; + + while (in_first != in_last && tem_first != tem_last) + { + if (is_regex(tem_first, tem_last, re, re_prefix, re_suffix)) + { + boost::match_results<iter_t> what; + boost::regex e(re); + if (!boost::regex_search( + in_first, in_last, what, e + , boost::match_default | boost::match_continuous)) + { + // RE mismatch: exit now. + return compare_result_t(in_first, false); + } + else + { + // RE match: gobble the matching string. + in_first = what[0].second; + } + } + else + { + // Char by char comparison. Exit if we have a mismatch. + if (*in_first++ != *tem_first++) + return compare_result_t(in_first, false); + } + } + + // Ignore trailing spaces in template + bool has_trailing_nonspaces = false; + while (tem_first != tem_last) + { + if (!std::isspace(*tem_first++)) + { + has_trailing_nonspaces = true; + break; + } + } + while (in_first != in_last) + { + if (!std::isspace(*in_first++)) + { + has_trailing_nonspaces = true; + break; + } + } + // return a full match only if the template is fully matched and if there + // are no more characters to match in the source + return compare_result_t(in_first, !has_trailing_nonspaces); + } + + template <typename F> + inline int for_each_file(fs::path p, F f) + { + try + { + if (fs::exists(p) && fs::is_directory(p)) + { + for (auto i = fs::directory_iterator(p); i != fs::directory_iterator(); ++i) + { + auto ext = fs::extension(i->path()); + if (ext == ".input") + { + auto input_path = i->path(); + auto expect_path = input_path; + expect_path.replace_extension(".expect"); + f(input_path, expect_path); + } + } + } + else + { + std::cerr << "Directory: " << fs::absolute(p) << " does not exist." << std::endl; + return 1; + } + } + + catch (const fs::filesystem_error& ex) + { + std::cerr << ex.what() << '\n'; + return 1; + } + return 0; + } + + inline std::string load(fs::path p) + { + boost::filesystem::ifstream file(p); + if (!file) + return ""; + std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); + return contents; + } + + template <typename F> + inline bool compare( + fs::path input_path, fs::path expect_path + , F f + , char const* re_prefix + , char const* re_suffix + ) + { + std::string output = f(load(input_path), input_path); + std::string expected = load(expect_path); + + auto result = compare(output, expected, re_prefix, re_suffix); + if (!result.full_match) + { + std::cout << "=============================================" << std::endl; + std::cout << "==== Mismatch Found:" << std::endl; + int line = 1; + int col = 1; + for (auto i = output.begin(); i != result.pos; ++i) + { + if (*i == '\n') + { + line++; + col = 0; + } + ++col; + } + + std::cerr + << "==== File: " << expect_path + << ", Line: " << line + << ", Column: " << col + << std::endl; + std::cerr << "=============================================" << std::endl; + + // Print output + std::cerr << output; + std::cerr << "=============================================" << std::endl; + std::cerr << "==== End" << std::endl; + std::cerr << "=============================================" << std::endl; + return false; + } + return true; + } + +}}}} + +#endif diff --git a/src/boost/libs/spirit/example/x3/rexpr/rexpr_min/rexpr.cpp b/src/boost/libs/spirit/example/x3/rexpr/rexpr_min/rexpr.cpp new file mode 100644 index 00000000..f3a3556d --- /dev/null +++ b/src/boost/libs/spirit/example/x3/rexpr/rexpr_min/rexpr.cpp @@ -0,0 +1,215 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A simple parser for X3 intended as a minimal starting point. +// 'rexpr' is a parser for a language resembling a minimal subset +// of json, but limited to a dictionary (composed of key=value pairs) +// where the value can itself be a string or a recursive dictionary. +// +// Example: +// +// { +// "color" = "blue" +// "size" = "29 cm." +// "position" = { +// "x" = "123" +// "y" = "456" +// } +// } +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <boost/spirit/home/x3/support/ast/variant.hpp> +#include <boost/fusion/include/adapt_struct.hpp> +#include <boost/fusion/include/std_pair.hpp> +#include <boost/fusion/include/io.hpp> + +#include <iostream> +#include <fstream> +#include <string> +#include <map> + +/////////////////////////////////////////////////////////////////////////////// +// Our AST +/////////////////////////////////////////////////////////////////////////////// +namespace client { namespace ast +{ + namespace fusion = boost::fusion; + namespace x3 = boost::spirit::x3; + + struct rexpr; + + struct rexpr_value : x3::variant< + std::string + , x3::forward_ast<rexpr> + > + { + using base_type::base_type; + using base_type::operator=; + }; + + typedef std::map<std::string, rexpr_value> rexpr_map; + typedef std::pair<std::string, rexpr_value> rexpr_key_value; + + struct rexpr + { + rexpr_map entries; + }; +}} + +// We need to tell fusion about our rexpr struct +// to make it a first-class fusion citizen +BOOST_FUSION_ADAPT_STRUCT(client::ast::rexpr, + entries +) + +/////////////////////////////////////////////////////////////////////////////// +// AST processing +/////////////////////////////////////////////////////////////////////////////// +namespace client { namespace ast +{ + /////////////////////////////////////////////////////////////////////////// + // Print out the rexpr tree + /////////////////////////////////////////////////////////////////////////// + int const tabsize = 4; + + struct rexpr_printer + { + typedef void result_type; + + rexpr_printer(int indent = 0) + : indent(indent) {} + + void operator()(rexpr const& ast) const + { + std::cout << '{' << std::endl; + for (auto const& entry : ast.entries) + { + tab(indent+tabsize); + std::cout << '"' << entry.first << "\" = "; + boost::apply_visitor(rexpr_printer(indent+tabsize), entry.second); + } + tab(indent); + std::cout << '}' << std::endl; + } + + void operator()(std::string const& text) const + { + std::cout << '"' << text << '"' << std::endl; + } + + void tab(int spaces) const + { + for (int i = 0; i < spaces; ++i) + std::cout << ' '; + } + + int indent; + }; +}} + +/////////////////////////////////////////////////////////////////////////////// +// Our rexpr grammar +/////////////////////////////////////////////////////////////////////////////// +namespace client { namespace parser +{ + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + using x3::lit; + using x3::lexeme; + + using ascii::char_; + using ascii::string; + + x3::rule<class rexpr_value, ast::rexpr_value> + rexpr_value = "rexpr_value"; + + x3::rule<class rexpr, ast::rexpr> + rexpr = "rexpr"; + + x3::rule<class rexpr_key_value, ast::rexpr_key_value> + rexpr_key_value = "rexpr_key_value"; + + auto const quoted_string = + lexeme['"' >> *(char_ - '"') >> '"']; + + auto const rexpr_value_def = + quoted_string | rexpr; + + auto const rexpr_key_value_def = + quoted_string >> '=' >> rexpr_value; + + auto const rexpr_def = + '{' >> *rexpr_key_value >> '}'; + + BOOST_SPIRIT_DEFINE(rexpr_value, rexpr, rexpr_key_value); +}} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int main(int argc, char **argv) +{ + char const* filename; + if (argc > 1) + { + filename = argv[1]; + } + else + { + std::cerr << "Error: No input file provided." << std::endl; + return 1; + } + + std::ifstream in(filename, std::ios_base::in); + + if (!in) + { + std::cerr << "Error: Could not open input file: " + << filename << std::endl; + return 1; + } + + std::string storage; // We will read the contents here. + in.unsetf(std::ios::skipws); // No white space skipping! + std::copy( + std::istream_iterator<char>(in), + std::istream_iterator<char>(), + std::back_inserter(storage)); + + using client::parser::rexpr; // Our grammar + client::ast::rexpr ast; // Our tree + + using boost::spirit::x3::ascii::space; + std::string::const_iterator iter = storage.begin(); + std::string::const_iterator end = storage.end(); + bool r = phrase_parse(iter, end, rexpr, space, ast); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << "-------------------------\n"; + client::ast::rexpr_printer printer; + printer(ast); + return 0; + } + else + { + std::string::const_iterator some = iter+30; + std::string context(iter, (some>end)?end:some); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "stopped at: \": " << context << "...\"\n"; + std::cout << "-------------------------\n"; + return 1; + } +} diff --git a/src/boost/libs/spirit/example/x3/roman.cpp b/src/boost/libs/spirit/example/x3/roman.cpp new file mode 100644 index 00000000..7423c19c --- /dev/null +++ b/src/boost/libs/spirit/example/x3/roman.cpp @@ -0,0 +1,179 @@ +/*============================================================================= + Copyright (c) 2001-2015 Joel de Guzman + Copyright (c) 2015 Ahmed Charles + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A Roman Numerals Parser (demonstrating the symbol table). This is +// discussed in the "Symbols" chapter in the Spirit User's Guide. +// +// [ JDG August 22, 2002 ] spirit1 +// [ JDG March 13, 2007 ] spirit2 +// [ JDG May 13, 2015 ] spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> + +#include <iostream> +#include <string> + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + /////////////////////////////////////////////////////////////////////////////// + // Parse roman hundreds (100..900) numerals using the symbol table. + // Notice that the data associated with each slot is the parser's attribute + // (which is passed to attached semantic actions). + /////////////////////////////////////////////////////////////////////////////// + struct hundreds_ : x3::symbols<unsigned> + { + hundreds_() + { + add + ("C" , 100) + ("CC" , 200) + ("CCC" , 300) + ("CD" , 400) + ("D" , 500) + ("DC" , 600) + ("DCC" , 700) + ("DCCC" , 800) + ("CM" , 900) + ; + } + + } hundreds; + + /////////////////////////////////////////////////////////////////////////////// + // Parse roman tens (10..90) numerals using the symbol table. + /////////////////////////////////////////////////////////////////////////////// + struct tens_ : x3::symbols<unsigned> + { + tens_() + { + add + ("X" , 10) + ("XX" , 20) + ("XXX" , 30) + ("XL" , 40) + ("L" , 50) + ("LX" , 60) + ("LXX" , 70) + ("LXXX" , 80) + ("XC" , 90) + ; + } + + } tens; + + /////////////////////////////////////////////////////////////////////////////// + // Parse roman ones (1..9) numerals using the symbol table. + /////////////////////////////////////////////////////////////////////////////// + struct ones_ : x3::symbols<unsigned> + { + ones_() + { + add + ("I" , 1) + ("II" , 2) + ("III" , 3) + ("IV" , 4) + ("V" , 5) + ("VI" , 6) + ("VII" , 7) + ("VIII" , 8) + ("IX" , 9) + ; + } + + } ones; + + /////////////////////////////////////////////////////////////////////////////// + // roman (numerals) grammar + // + // Note the use of the || operator. The expression + // a || b reads match a or b and in sequence. Try + // defining the roman numerals grammar in YACC or + // PCCTS. Spirit rules! :-) + /////////////////////////////////////////////////////////////////////////////// + namespace parser + { + using x3::eps; + using x3::lit; + using x3::_val; + using x3::_attr; + using ascii::char_; + + auto set_zero = [](auto& ctx){ _val(ctx) = 0; }; + auto add1000 = [](auto& ctx){ _val(ctx) += 1000; }; + auto add = [](auto& ctx){ _val(ctx) += _attr(ctx); }; + + x3::rule<class roman, unsigned> const roman = "roman"; + + auto const roman_def = + eps [set_zero] + >> + ( + -(+lit('M') [add1000]) + >> -hundreds [add] + >> -tens [add] + >> -ones [add] + ) + ; + + BOOST_SPIRIT_DEFINE(roman); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Main program +/////////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tRoman Numerals Parser\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "Type a Roman Numeral ...or [q or Q] to quit\n\n"; + + typedef std::string::const_iterator iterator_type; + using client::parser::roman; // Our parser + + std::string str; + unsigned result; + while (std::getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + iterator_type iter = str.begin(); + iterator_type const end = str.end(); + bool r = parse(iter, end, roman, result); + + if (r && iter == end) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << "result = " << result << std::endl; + std::cout << "-------------------------\n"; + } + else + { + std::string rest(iter, end); + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "stopped at: \": " << rest << "\"\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} diff --git a/src/boost/libs/spirit/example/x3/sum.cpp b/src/boost/libs/spirit/example/x3/sum.cpp new file mode 100644 index 00000000..81315fb2 --- /dev/null +++ b/src/boost/libs/spirit/example/x3/sum.cpp @@ -0,0 +1,98 @@ +/*============================================================================= + Copyright (c) 2002-2015 Joel de Guzman + + 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) +=============================================================================*/ +/////////////////////////////////////////////////////////////////////////////// +// +// A parser for summing a comma-separated list of numbers using phoenix. +// +// [ JDG June 28, 2002 ] spirit1 +// [ JDG March 24, 2007 ] spirit2 +// [ JDG May 12, 2015 ] spirit X3 +// +/////////////////////////////////////////////////////////////////////////////// + +#include <boost/config/warning_disable.hpp> +#include <boost/spirit/home/x3.hpp> +#include <iostream> +#include <string> + +namespace client +{ + namespace x3 = boost::spirit::x3; + namespace ascii = boost::spirit::x3::ascii; + + using x3::double_; + using ascii::space; + using x3::_attr; + + /////////////////////////////////////////////////////////////////////////// + // Our adder + /////////////////////////////////////////////////////////////////////////// + + template <typename Iterator> + bool adder(Iterator first, Iterator last, double& n) + { + auto assign = [&](auto& ctx){ n = _attr(ctx); }; + auto add = [&](auto& ctx){ n += _attr(ctx); }; + + bool r = x3::phrase_parse(first, last, + + // Begin grammar + ( + double_[assign] >> *(',' >> double_[add]) + ) + , + // End grammar + + space); + + if (first != last) // fail if we did not get a full match + return false; + return r; + } +} + +//////////////////////////////////////////////////////////////////////////// +// Main program +//////////////////////////////////////////////////////////////////////////// +int +main() +{ + std::cout << "/////////////////////////////////////////////////////////\n\n"; + std::cout << "\t\tA parser for summing a list of numbers...\n\n"; + std::cout << "/////////////////////////////////////////////////////////\n\n"; + + std::cout << "Give me a comma separated list of numbers.\n"; + std::cout << "The numbers are added using Phoenix.\n"; + std::cout << "Type [q or Q] to quit\n\n"; + + std::string str; + while (getline(std::cin, str)) + { + if (str.empty() || str[0] == 'q' || str[0] == 'Q') + break; + + double n; + if (client::adder(str.begin(), str.end(), n)) + { + std::cout << "-------------------------\n"; + std::cout << "Parsing succeeded\n"; + std::cout << str << " Parses OK: " << std::endl; + + std::cout << "sum = " << n; + std::cout << "\n-------------------------\n"; + } + else + { + std::cout << "-------------------------\n"; + std::cout << "Parsing failed\n"; + std::cout << "-------------------------\n"; + } + } + + std::cout << "Bye... :-) \n\n"; + return 0; +} |