#pragma once #include #include #include #include #include #include "detail/LexyLitRange.hpp" // Grammar Definitions // /* REQUIREMENTS: * DAT-626 * DAT-627 * DAT-628 * DAT-636 * DAT-641 * DAT-642 * DAT-643 */ namespace ovdl::v2script::grammar { struct ParseOptions { /// @brief Makes string parsing avoid string escapes bool NoStringEscape; }; static constexpr ParseOptions NoStringEscapeOption = ParseOptions { true }; static constexpr ParseOptions StringEscapeOption = ParseOptions { false }; template struct StatementListBlock; template struct AssignmentStatement; /* REQUIREMENTS: DAT-630 */ static constexpr auto whitespace_specifier = lexy::dsl::ascii::blank / lexy::dsl::ascii::newline; /* REQUIREMENTS: DAT-631 */ static constexpr auto comment_specifier = LEXY_LIT("#") >> lexy::dsl::until(lexy::dsl::newline).or_eof(); /* REQUIREMENTS: * DAT-632 * DAT-635 */ static constexpr auto data_specifier = lexy::dsl::ascii::alpha_digit_underscore / LEXY_ASCII_ONE_OF("+:@%&'-.") / lexy::dsl::lit_b<0x8A> / lexy::dsl::lit_b<0x8C> / lexy::dsl::lit_b<0x8E> / lexy::dsl::lit_b<0x92> / lexy::dsl::lit_b<0x97> / lexy::dsl::lit_b<0x9A> / lexy::dsl::lit_b<0x9C> / detail::lexydsl::make_range<0x9E, 0x9F>() / detail::lexydsl::make_range<0xC0, 0xD6>() / detail::lexydsl::make_range<0xD8, 0xF6>() / detail::lexydsl::make_range<0xF8, 0xFF>(); static constexpr auto data_char_class = LEXY_CHAR_CLASS("DataSpecifier", data_specifier); static constexpr auto escaped_symbols = lexy::symbol_table // .map<'"'>('"') .map<'\''>('\'') .map<'\\'>('\\') .map<'/'>('/') .map<'b'>('\b') .map<'f'>('\f') .map<'n'>('\n') .map<'r'>('\r') .map<'t'>('\t'); template struct Identifier { static constexpr auto rule = lexy::dsl::identifier(data_char_class); static constexpr auto value = lexy::callback( [](auto lexeme) { std::string str(lexeme.data(), lexeme.size()); return ast::make_node_ptr(ast::NodeLocation { lexeme.begin(), lexeme.end() }, LEXY_MOV(str)); }); }; /* REQUIREMENTS: * DAT-633 * DAT-634 */ template struct StringExpression { static constexpr auto rule = [] { if constexpr (Options.NoStringEscape) { auto c = ovdl::detail::lexydsl::make_range<0x20, 0xFF>() / lexy::dsl::lit_b<0x07> / lexy::dsl::lit_b<0x09> / lexy::dsl::lit_b<0x0A> / lexy::dsl::lit_b<0x0D>; return lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'"'>))(c); } else { // Arbitrary code points that aren't control characters. auto c = ovdl::detail::lexydsl::make_range<0x20, 0xFF>() - lexy::dsl::ascii::control; // Escape sequences start with a backlash. // They either map one of the symbols, // or a Unicode code point of the form uXXXX. auto escape = lexy::dsl::backslash_escape // .symbol(); return lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'"'>))(c, escape); } }(); static constexpr auto value = lexy::as_string >> lexy::callback( [](const char* begin, auto&& str, const char* end) { return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end), LEXY_MOV(str), Options.NoStringEscape); }); }; /* REQUIREMENTS: DAT-638 */ template struct ValueExpression { static constexpr auto rule = lexy::dsl::p> | lexy::dsl::p>; static constexpr auto value = lexy::forward; }; template struct SimpleAssignmentStatement { static constexpr auto rule = lexy::dsl::position(lexy::dsl::p>) >> (lexy::dsl::equal_sign + (lexy::dsl::p> | lexy::dsl::recurse_branch>)); static constexpr auto value = lexy::callback( [](const char* pos, auto name, auto&& initalizer) { return ast::make_node_ptr(pos, LEXY_MOV(name), LEXY_MOV(initalizer)); }); }; /* REQUIREMENTS: DAT-639 */ template struct AssignmentStatement { static constexpr auto rule = lexy::dsl::position(lexy::dsl::p>) >> (lexy::dsl::equal_sign >> (lexy::dsl::p> | lexy::dsl::recurse_branch>) | lexy::dsl::else_ >> lexy::dsl::return_) | lexy::dsl::p> | lexy::dsl::recurse_branch>; static constexpr auto value = lexy::callback( [](const char* pos, auto name, lexy::nullopt = {}) { return LEXY_MOV(name); }, [](auto name, lexy::nullopt = {}) { return LEXY_MOV(name); }, [](const char* pos, auto name, auto&& initalizer) { return ast::make_node_ptr(pos, LEXY_MOV(name), LEXY_MOV(initalizer)); }); }; /* REQUIREMENTS: DAT-640 */ template struct StatementListBlock { static constexpr auto rule = lexy::dsl::position(lexy::dsl::curly_bracketed.open()) >> (lexy::dsl::opt(lexy::dsl::list(lexy::dsl::recurse_branch>)) + lexy::dsl::opt(lexy::dsl::semicolon)) >> lexy::dsl::position(lexy::dsl::curly_bracketed.close()); static constexpr auto value = lexy::as_list> >> lexy::callback( [](const char* begin, lexy::nullopt, const char* end) { return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end)); }, [](const char* begin, auto&& list, const char* end) { return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end), LEXY_MOV(list)); }, [](const char* begin, lexy::nullopt, lexy::nullopt, const char* end) { return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end)); }, [](const char* begin, auto&& list, lexy::nullopt, const char* end) { return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end), LEXY_MOV(list)); }, [](const char* begin, auto& list, const char* end) { return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end), list); }); }; template struct File { // Allow arbitrary spaces between individual tokens. static constexpr auto whitespace = whitespace_specifier | comment_specifier; static constexpr auto rule = lexy::dsl::position + lexy::dsl::terminator(lexy::dsl::eof).opt_list(lexy::dsl::p>); static constexpr auto value = lexy::as_list> >> lexy::new_; }; }