Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aiya000
Created December 20, 2018 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aiya000/4e7601d03623a079e3c8632eb13c5b07 to your computer and use it in GitHub Desktop.
Save aiya000/4e7601d03623a079e3c8632eb13c5b07 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <boost/xpressive/xpressive.hpp>
// --regex-kotlin=/^[ \t]*(private|protected|public)?[ \t]*((abstract|final|sealed)[ \t]*)*(data[ \t]*)?class[ \t]+([a-zA-Z0-9_]+)/\8/c,class/
namespace xp = boost::xpressive;
int main() {
// 基本コンビネーター
xp::sregex empty = xp::sregex::compile("");
xp::sregex blank = *xp::space;
// Converts a string to xp::sregex
auto let = [empty](std::string x){
return x >> empty;
};
// 目的たち
xp::sregex visibility =
(let("private") | "protected" | "public")
>> blank
;
xp::sregex kind =
(let("abstract") | "final" | "sealed")
>> blank
;
xp::sregex class_ =
blank
>> !visibility
>> !kind
>> !let("data") >> blank
>> "class" >> blank
>> (xp::s1 = *(xp::alnum | '_'))
>> *xp::_
;
// Go!!
xp::smatch what;
std::string x = "public data class You(val me: String)";
std::string y = "sealed class Product {}";
if (xp::regex_match(x, what, class_)) {
std::cout << what[0] << '\n';
}
if (xp::regex_match(y, what, class_)) {
std::cout << what[0] << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment