LCOV - code coverage report
Current view: top level - jdx/util - TuplesParser.cpp (source / functions) Hit Total Coverage
Test: coverage_filtered.info Lines: 38 40 95.0 %
Date: 2025-06-15 16:18:27 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Robert Schiwon
       2             :  *
       3             :  * This file is part of libjdx.
       4             :  *
       5             :  * libjdx is free software: you can redistribute it and/or modify it under the
       6             :  * terms of the GNU Lesser General Public License as published by the Free
       7             :  * Software Foundation, either version 3 of the License, or (at your option) any
       8             :  * later version.
       9             :  *
      10             :  * libjdx is distributed in the hope that it will be useful, but WITHOUT ANY
      11             :  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      12             :  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
      13             :  * details.
      14             :  *
      15             :  * You should have received a copy of the GNU Lesser General Public License
      16             :  * along with libjdx. If not, see <https://www.gnu.org/licenses/>.
      17             :  */
      18             : #include "util/TuplesParser.hpp"
      19             : #include "jdx/ParseException.hpp"
      20             : #include "util/LdrUtils.hpp"
      21             : #include "util/StringUtils.hpp"
      22             : 
      23             : #include <limits>
      24             : 
      25          42 : libjdx::jdx::util::TuplesParser::TuplesParser(
      26          42 :     std::string variableList, std::string ldrName)
      27          42 :     : m_variableList{std::move(variableList)}
      28          42 :     , m_ldrName{std::move(ldrName)}
      29             : {
      30          42 : }
      31             : 
      32          82 : const std::string& libjdx::jdx::util::TuplesParser::getVariableList() const
      33             : {
      34          82 :     return m_variableList;
      35             : }
      36             : 
      37          66 : const std::string& libjdx::jdx::util::TuplesParser::getLdrName() const
      38             : {
      39          66 :     return m_ldrName;
      40             : }
      41             : 
      42             : std::vector<std::optional<std::string>>
      43          88 : libjdx::jdx::util::TuplesParser::extractTokens(
      44             :     const std::string& tuple, const std::regex& regex, size_t numTokens) const
      45             : {
      46         176 :     auto noCommentTuple = util::stripLineComment(tuple, true).first;
      47         170 :     std::smatch matches = match(noCommentTuple, regex);
      48             : 
      49          82 :     std::vector<std::optional<std::string>> tokens;
      50         526 :     for (size_t i = 0; i < numTokens; ++i)
      51             :     {
      52         888 :         auto token = extractToken(matches, i);
      53         444 :         tokens.push_back(token);
      54             :     }
      55         164 :     return tokens;
      56             : }
      57             : 
      58          82 : void libjdx::jdx::util::TuplesParser::checkForErrors(const std::string& varList,
      59             :     const std::multimap<std::string, std::tuple<bool, std::string>>& errorMap,
      60             :     const std::string& ldrName)
      61             : {
      62          82 :     if (errorMap.count(varList) == 0)
      63             :     {
      64           0 :         throw ParseException(std::string{"Unsupported variable list for "}
      65           0 :                              + ldrName + ": " + varList);
      66             :     }
      67          82 :     auto errorConditions = errorMap.equal_range(varList);
      68         174 :     for (auto it = errorConditions.first; it != errorConditions.second; ++it)
      69             :     {
      70          98 :         bool errorCondition = std::get<0>(it->second);
      71          98 :         if (errorCondition)
      72             :         {
      73           6 :             const auto& errorMessage = std::get<1>(it->second);
      74           6 :             throw ParseException(errorMessage);
      75             :         }
      76             :     }
      77          76 : }
      78             : 
      79         133 : double libjdx::jdx::util::TuplesParser::parseDoubleToken(
      80             :     const std::optional<std::string>& token)
      81             : {
      82         133 :     return token.value().empty() ? std::numeric_limits<double>::quiet_NaN()
      83         133 :                                  : strtod(token.value().data(), nullptr);
      84             : }
      85             : 
      86         444 : std::optional<std::string> libjdx::jdx::util::TuplesParser::extractToken(
      87             :     const std::smatch& matches, size_t index)
      88             : {
      89         444 :     return matches[index].matched
      90         810 :                ? std::optional<std::string>{matches[index].str()}
      91        1254 :                : std::nullopt;
      92             : }
      93             : 
      94          88 : std::smatch libjdx::jdx::util::TuplesParser::match(
      95             :     const std::string& tuple, const std::regex& regex) const
      96             : {
      97          88 :     std::smatch matches;
      98          88 :     if (!std::regex_match(tuple, matches, regex))
      99             :     {
     100           6 :         throw ParseException("Illegal " + m_ldrName + " tuple: " + tuple);
     101             :     }
     102          82 :     return matches;
     103             : }

Generated by: LCOV version 1.14