#pragma once #include #include #include #include "ParseState.hpp" #include "SimpleGrammar.hpp" #include "detail/dsl.hpp" namespace ovdl::v2script::lua::grammar { template constexpr auto callback(Callback... cb) { return dsl::callback(cb...); } template constexpr auto construct = v2script::grammar::construct; template constexpr auto construct_list = v2script::grammar::construct_list; struct ParseOptions { }; template struct StatementListBlock; static constexpr auto comment_specifier = LEXY_LIT("--") >> lexy::dsl::until(lexy::dsl::newline).or_eof(); template struct Identifier { static constexpr auto rule = lexy::dsl::identifier(lexy::dsl::ascii::alpha_underscore, lexy::dsl::ascii::alpha_digit_underscore); static constexpr auto value = callback( [](ast::ParseState& state, auto lexeme) { auto value = state.ast().intern(lexeme.data(), lexeme.size()); return state.ast().create(lexeme.begin(), lexeme.end(), value); }); }; template struct Value { static constexpr auto rule = lexy::dsl::identifier(lexy::dsl::ascii::digit / lexy::dsl::lit_c<'.'> / lexy::dsl::lit_c<'-'>); static constexpr auto value = callback( [](ast::ParseState& state, auto lexeme) { auto value = state.ast().intern(lexeme.data(), lexeme.size()); return state.ast().create(lexeme.begin(), lexeme.end(), value); }); }; template struct String { static constexpr auto rule = [] { // Arbitrary code points that aren't control characters. auto c = dsl::make_range<0x20, 0xFF>() - lexy::dsl::ascii::control; return lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'"'>))(c) | lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'\''>))(c); }(); static constexpr auto value = lexy::as_string >> callback( [](ast::ParseState& state, const char* begin, const std::string& str, const char* end) { auto value = state.ast().intern(str.data(), str.length()); return state.ast().create(begin, end, value); }); }; template struct Expression { static constexpr auto rule = lexy::dsl::p> | lexy::dsl::p>; static constexpr auto value = lexy::forward; }; template struct AssignmentStatement { static constexpr auto rule = dsl::p> >> lexy::dsl::equal_sign >> (lexy::dsl::p> | lexy::dsl::recurse_branch>); static constexpr auto value = callback( [](ast::ParseState& state, const char* pos, ast::IdentifierValue* name, ast::Value* initializer) { return state.ast().create(pos, name, initializer); }); }; template struct StatementListBlock { static constexpr auto rule = dsl::curly_bracketed( lexy::dsl::opt( lexy::dsl::list( lexy::dsl::recurse_branch>, lexy::dsl::trailing_sep(lexy::dsl::lit_c<','>)))); static constexpr auto value = lexy::as_list >> construct_list; }; template struct File { // Allow arbitrary spaces between individual tokens. static constexpr auto whitespace = ovdl::v2script::grammar::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 >> construct; }; }