Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active October 31, 2022 07:18
Show Gist options
  • Save Superstar64/becbf527168403cc7e671f1149c4d5b8 to your computer and use it in GitHub Desktop.
Save Superstar64/becbf527168403cc7e671f1149c4d5b8 to your computer and use it in GitHub Desktop.
Proof of concept parsec style non backtracking parser combinators in C++ at compile time
/* Copyright (C) Freddy A Cubas "Superstar64"
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// based on https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/parsec-paper-letter.pdf
#include <cctype>
#include <iostream>
#include <string>
#include <string_view>
#include <variant>
template <class T> struct valid {
T item;
std::string_view remainder;
valid(T item, std::string_view remainder)
: item(item), remainder(remainder) {}
};
struct error {
std::string expected;
std::string_view remainder;
error(std::string expected, std::string_view remainder)
: expected(expected), remainder(remainder) {}
};
template <class T> struct reply {
std::variant<valid<T>, error> get;
reply(valid<T> valid) : get(valid) {}
reply(error error) : get(error) {}
};
template <class T>
reply<T> prependMessage(std::string expected0, reply<T> reply) {
if (reply.get.index() == 1) {
error &error = std::get<1>(reply.get);
error.expected = expected0 + " or " + error.expected;
return reply;
} else {
return reply;
}
}
template <class T>
std::ostream &operator<<(std::ostream &stream, reply<T> reply) {
if (reply.get.index() == 0) {
valid<T> valid = std::get<0>(reply.get);
stream << "Sucessful parse:";
stream << valid.item;
} else {
error error = std::get<1>(reply.get);
stream << "Parser error:";
stream << error.expected;
}
return stream;
}
inline auto pure = [](auto item) {
return [=](std::string_view input) {
return [=](auto consumed, auto empty) {
return empty(reply(valid(item, input)));
};
};
};
inline auto satify = [](auto validate, auto type) {
return [=](std::string_view input) {
return [=](auto empty, auto consumed) {
if (input.empty() || !validate(input.front())) {
return empty(reply<char>(error(type, input)));
} else {
return consumed(reply(valid(input.front(), input.substr(1))));
}
};
};
};
inline auto letter = satify(isalpha, "letter");
inline auto digit = satify(isdigit, "digit");
// haskell's >>=
inline auto bind = [](auto p, auto f) {
return [=](std::string_view input) {
return [=](auto empty, auto consumed) {
return p(input)(
[=](auto reply) {
if (reply.get.index() == 0) {
auto valid = std::get<0>(reply.get);
return f(valid.item)(valid.remainder)(empty, consumed);
} else {
return empty(reply);
}
},
[=](auto reply) {
if (reply.get.index() == 0) {
auto valid = std::get<0>(reply.get);
return f(valid.item)(valid.remainder)(consumed, consumed);
} else {
return consumed(reply);
}
});
};
};
};
template <class P, class Q> auto operator>>(P p, Q q) { return bind(p, q); }
// haskell's <|>
inline auto choice = [](auto p, auto q) {
return [=](std::string_view input) {
return [=](auto empty, auto consumed) {
return p(input)(
[=](auto reply) {
if (reply.get.index() == 0) {
return empty(reply);
} else {
std::string expected = std::get<1>(reply.get).expected;
return q(input)(
[=](auto reply2) {
return empty(prependMessage(expected, reply2));
},
[=](auto reply2) {
return consumed(prependMessage(expected, reply2));
});
}
},
consumed);
};
};
};
template <class P, class Q> auto operator|(P p, Q q) { return choice(p, q); }
inline auto parse = [](auto parser) {
return [=](std::string_view input) {
return parser(input)([](auto x) { return x; }, [](auto x) { return x; });
};
};
void test(std::string_view input) {
// parse letter followed by digit or digit
auto parser = (letter >> [](char l) { return digit; }) | digit;
std::cout << input << " :: " << parse(parser)(input) << std::endl;
}
int main(int argc, char **argv) {
test("a1");
test("1");
test("?");
test("ab");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment