Last active
December 2, 2016 22:18
-
-
Save sehe/7149067e6d5ef7edb36edb1867f110bc to your computer and use it in GitHub Desktop.
old fashioned grammar struct; using X3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "b.hpp" | |
#include <boost/spirit/home/x3.hpp> | |
#include <iostream> | |
namespace parsers { | |
namespace x3 = boost::spirit::x3; | |
namespace internal { | |
namespace /*file-static*/ { | |
auto the_usual = x3::rule<struct tag, std::string> { "the_usual" } | |
= x3::lexeme [ +x3::graph ]; | |
} | |
} | |
template <typename It, typename Attr> | |
bool subgrammar<It, Attr>::parse(It& f, It const& l, Attr& attr) const { | |
return x3::parse(f, l, internal::the_usual, attr); | |
} | |
} | |
// do the explicit instantiations that you wanted | |
namespace parsers { | |
template struct subgrammar<std::string::const_iterator, std::string>; | |
template struct subgrammar<char const*, std::string>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/spirit/home/x3/support/unused.hpp> | |
namespace parsers { | |
using boost::spirit::x3::unused_type; | |
namespace internal { } | |
template <typename It, typename Attr = unused_type> struct subgrammar { | |
bool parse(It& f, It const& l, Attr& attr) const; | |
}; | |
} | |
#include <vector> | |
namespace parsers { | |
// forward declare/extern declare some template instantiations | |
extern template struct subgrammar<std::string::const_iterator, std::string>; | |
extern template struct subgrammar<char const*, std::string>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ADD_EXECUTABLE(demo test.cpp b.cpp) | |
SET(CMAKE_CXX_COMPILER g++-5) | |
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \ | |
-std=c++1z \ | |
-g0 -O3 -pthread -march=native") | |
#-fsanitize=undefined,address | |
#-DBOOST_SPIRIT_X3_DEBUG=1 \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "b.hpp" | |
#include <iostream> | |
int main() { | |
std::string const input("hello world"); | |
parsers::subgrammar<std::string::const_iterator, std::string> g; | |
std::string word; | |
auto f = input.begin(), l = input.end(); | |
if (g.parse(f, l, word)) | |
std::cout << "Parsed: '" << word << "'\n"; | |
else | |
std::cout << "Failed\n"; | |
if (f != l) | |
std::cout << "remaining unparsed: '" << std::string(f,l) << "'\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment