From b0c3ba3f91926b0c95625bdbf4aab69269130b13 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Thu, 9 May 2024 10:06:02 -0400 Subject: Add runtime encoding detection and conversion Win-1251/1252 detection is a reduced C++ version of https://github.com/hsivonen/chardetng Add manually-specified encoding fallback Add default system encoding fallback Add error recovery to v2script Add unknown encoding detection warning Remove csv::Parser templating Fix lua files dropping data Update lexy to foonathan/lexy@1e5d99fa3826b1c3c8628d3a11117fb4fb4cc0d0 Remove exclusive reliance on lexy::default_encoding for v2script Move internal concepts to src/openvic-detail/InternalConcepts.hpp Move contents of DetectUtf8.hpp to src/detail/Detect.hpp Move openvic-dataloader/AbstractSyntaxTree.hpp to src Move DiagnosticLogger.hpp to src Move File.hpp to src Move openvic-dataloader/detail/utlity files to openvic-dataloader/detail Add ovdl::utility::type_concat Add ovdl::utility::type_prepend Add ovdl::utility::is_instance_of Overhaul parse error messages --- src/openvic-dataloader/csv/CsvGrammar.hpp | 244 ++++++++++++++++-------------- 1 file changed, 132 insertions(+), 112 deletions(-) (limited to 'src/openvic-dataloader/csv/CsvGrammar.hpp') diff --git a/src/openvic-dataloader/csv/CsvGrammar.hpp b/src/openvic-dataloader/csv/CsvGrammar.hpp index 5451f26..19aee54 100644 --- a/src/openvic-dataloader/csv/CsvGrammar.hpp +++ b/src/openvic-dataloader/csv/CsvGrammar.hpp @@ -9,22 +9,20 @@ #include #include +#include #include +#include #include +#include +#include +#include +#include "detail/Convert.hpp" +#include "detail/InternalConcepts.hpp" #include "detail/dsl.hpp" // Grammar Definitions // namespace ovdl::csv::grammar { - using EncodingType = ovdl::csv::EncodingType; - - template - concept ParseChars = requires() { - { T::character }; - { T::control }; - }; - - template struct ParseOptions { /// @brief Seperator character char SepChar; @@ -33,12 +31,34 @@ namespace ovdl::csv::grammar { /// @brief Paradox-style localization escape characters /// @note Is ignored if SupportStrings is true char EscapeChar; + }; - static constexpr auto parse_chars = T {}; - static constexpr auto character = parse_chars.character; - static constexpr auto control = parse_chars.control; + struct ConvertErrorHandler { + static constexpr void on_invalid_character(detail::IsStateType auto& state, auto reader) { + state.logger().warning("invalid character value '{}' found", static_cast(reader.peek())) // + .primary(BasicNodeLocation { reader.position() }, "here") + .finish(); + } }; + constexpr bool IsUtf8(auto encoding) { + return std::same_as, lexy::utf8_char_encoding>; + } + + template + constexpr auto convert_as_string = convert::convert_as_string< + String, + ConvertErrorHandler>; + + constexpr auto ansi_character = lexy::dsl::ascii::character / dsl::lit_b_range<0x80, 0xFF>; + constexpr auto ansi_control = + lexy::dsl::ascii::control / + lexy::dsl::lit_b<0x81> / lexy::dsl::lit_b<0x8D> / lexy::dsl::lit_b<0x8F> / + lexy::dsl::lit_b<0x90> / lexy::dsl::lit_b<0x9D>; + + constexpr auto utf_character = lexy::dsl::unicode::character; + constexpr auto utf_control = lexy::dsl::unicode::control; + constexpr auto escaped_symbols = lexy::symbol_table // .map<'"'>('"') .map<'\''>('\'') @@ -55,38 +75,95 @@ namespace ovdl::csv::grammar { template struct CsvGrammar { - struct StringValue { - static constexpr auto rule = [] { - // Arbitrary code points - auto c = Options.character - Options.control; + struct StringValue : lexy::scan_production, + lexy::token_production { + + template + static constexpr scan_result scan(lexy::rule_scanner& scanner, detail::IsFileParseState auto& state) { + using encoding = typename Reader::encoding; + + constexpr auto rule = [] { + // Arbitrary code points + auto c = [] { + if constexpr (std::same_as || std::same_as) { + return ansi_character - ansi_control; + } else { + return utf_character - utf_control; + } + }(); - auto back_escape = lexy::dsl::backslash_escape // - .symbol(); + auto back_escape = lexy::dsl::backslash_escape // + .symbol(); - auto quote_escape = lexy::dsl::escape(lexy::dsl::lit_c<'"'>) // - .template symbol(); + auto quote_escape = lexy::dsl::escape(lexy::dsl::lit_c<'"'>) // + .template symbol(); - return lexy::dsl::delimited(lexy::dsl::lit_c<'"'>, lexy::dsl::not_followed_by(lexy::dsl::lit_c<'"'>, lexy::dsl::lit_c<'"'>))(c, back_escape, quote_escape); - }(); + return lexy::dsl::delimited(lexy::dsl::lit_c<'"'>, lexy::dsl::not_followed_by(lexy::dsl::lit_c<'"'>, lexy::dsl::lit_c<'"'>))(c, back_escape, quote_escape); + }(); + + lexy::scan_result str_result = scanner.template parse(rule); + if (!scanner || !str_result) + return lexy::scan_failed; + return str_result.value(); + } - static constexpr auto value = lexy::as_string; + static constexpr auto rule = lexy::dsl::peek(lexy::dsl::lit_c<'"'>) >> lexy::dsl::scan; + + static constexpr auto value = convert_as_string >> lexy::forward; }; - struct PlainValue { - static constexpr auto rule = [] { + struct PlainValue : lexy::scan_production, + lexy::token_production { + + template + static constexpr auto _escape_check = character - (lexy::dsl::lit_b / lexy::dsl::ascii::newline); + + template + static constexpr scan_result scan(lexy::rule_scanner& scanner, detail::IsFileParseState auto& state) { + using encoding = typename Reader::encoding; + + constexpr auto rule = [] { + constexpr auto character = [] { + if constexpr (std::same_as || std::same_as) { + return ansi_character; + } else { + return utf_character; + } + }(); + + if constexpr (Options.SupportStrings) { + return lexy::dsl::identifier(character - (lexy::dsl::lit_b / lexy::dsl::ascii::newline)); + } else { + auto escape_check_char = _escape_check; + auto id_check_char = escape_check_char - lexy::dsl::lit_b<'\\'>; + auto id_segment = lexy::dsl::identifier(id_check_char); + auto escape_segement = lexy::dsl::token(escape_check_char); + auto escape_sym = lexy::dsl::symbol(escape_segement); + auto escape_rule = lexy::dsl::lit_b<'\\'> >> escape_sym; + return lexy::dsl::list(id_segment | escape_rule); + } + }(); + if constexpr (Options.SupportStrings) { - return lexy::dsl::identifier(Options.character - (lexy::dsl::lit_b / lexy::dsl::ascii::newline)); + auto lexeme_result = scanner.template parse>(rule); + if (!scanner || !lexeme_result) + return lexy::scan_failed; + return std::string { lexeme_result.value().begin(), lexeme_result.value().end() }; } else { - auto escape_check_char = Options.character - (lexy::dsl::lit_b / lexy::dsl::ascii::newline); - auto id_check_char = escape_check_char - lexy::dsl::lit_b<'\\'>; - auto id_segment = lexy::dsl::identifier(id_check_char); - auto escape_segement = lexy::dsl::token(escape_check_char); - auto escape_sym = lexy::dsl::symbol(escape_segement); - auto escape_rule = lexy::dsl::lit_b<'\\'> >> escape_sym; - return lexy::dsl::list(id_segment | escape_rule); + lexy::scan_result str_result = scanner.template parse(rule); + if (!scanner || !str_result) + return lexy::scan_failed; + return str_result.value(); } - }(); - static constexpr auto value = lexy::as_string; + } + + static constexpr auto rule = + dsl::peek( + _escape_check, + _escape_check) >> + lexy::dsl::scan; + + static constexpr auto value = convert_as_string >> lexy::forward; }; struct Value { @@ -114,17 +191,17 @@ namespace ovdl::csv::grammar { static constexpr auto rule = lexy::dsl::list(lexy::dsl::p, lexy::dsl::trailing_sep(lexy::dsl::p)); static constexpr auto value = lexy::fold_inplace( std::initializer_list {}, - [](ovdl::csv::LineObject& result, auto&& arg) { - if constexpr (std::is_same_v, std::size_t>) { - // Count seperators, adds to previous value, making it a position - using position_type = ovdl::csv::LineObject::position_type; - result.emplace_back(static_cast(arg + result.back().first), ""); + [](ovdl::csv::LineObject& result, std::size_t&& arg) { + // Count seperators, adds to previous value, making it a position + using position_type = ovdl::csv::LineObject::position_type; + result.emplace_back(static_cast(arg + result.back().first), ""); + }, + [](ovdl::csv::LineObject& result, std::string&& arg) { + if (result.empty()) { + result.emplace_back(0u, LEXY_MOV(arg)); } else { - if (result.empty()) result.emplace_back(0u, LEXY_MOV(arg)); - else { - auto& [pos, value] = result.back(); - value = arg; - } + auto& [pos, value] = result.back(); + value = LEXY_MOV(arg); } }); }; @@ -169,74 +246,17 @@ namespace ovdl::csv::grammar { static constexpr auto value = lexy::as_list>; }; - template - using CommaFile = File { ',', false, '$' }>; - template - using ColonFile = File { ':', false, '$' }>; - template - using SemiColonFile = File { ';', false, '$' }>; - template - using TabFile = File { '\t', false, '$' }>; - template - using BarFile = File { '|', false, '$' }>; - - namespace strings { - template - using CommaFile = File { ',', true, '$' }>; - template - using ColonFile = File { ':', true, '$' }>; - template - using SemiColonFile = File { ';', true, '$' }>; - template - using TabFile = File { '\t', true, '$' }>; - template - using BarFile = File { '|', true, '$' }>; - } -} - -namespace ovdl::csv::grammar::windows1252 { - struct windows1252_t { - static constexpr auto character = dsl::make_range<0x01, 0xFF>(); - static constexpr auto control = - lexy::dsl::ascii::control / - lexy::dsl::lit_b<0x81> / lexy::dsl::lit_b<0x8D> / lexy::dsl::lit_b<0x8F> / - lexy::dsl::lit_b<0x90> / lexy::dsl::lit_b<0x9D>; - }; - - using CommaFile = CommaFile; - using ColonFile = ColonFile; - using SemiColonFile = SemiColonFile; - using TabFile = TabFile; - using BarFile = BarFile; - - namespace strings { - using CommaFile = grammar::strings::CommaFile; - using ColonFile = grammar::strings::ColonFile; - using SemiColonFile = grammar::strings::SemiColonFile; - using TabFile = grammar::strings::TabFile; - using BarFile = grammar::strings::BarFile; - - } -} - -namespace ovdl::csv::grammar::utf8 { - struct unicode_t { - static constexpr auto character = lexy::dsl::unicode::character; - static constexpr auto control = lexy::dsl::unicode::control; - }; - - using CommaFile = CommaFile; - using ColonFile = ColonFile; - using SemiColonFile = SemiColonFile; - using TabFile = TabFile; - using BarFile = BarFile; + using CommaFile = File; + using ColonFile = File; + using SemiColonFile = File; + using TabFile = File; + using BarFile = File; namespace strings { - using CommaFile = grammar::strings::CommaFile; - using ColonFile = grammar::strings::ColonFile; - using SemiColonFile = grammar::strings::SemiColonFile; - using TabFile = grammar::strings::TabFile; - using BarFile = grammar::strings::BarFile; - + using CommaFile = File; + using ColonFile = File; + using SemiColonFile = File; + using TabFile = File; + using BarFile = File; } } \ No newline at end of file -- cgit v1.2.3-56-ga3b1