Commit 5217607d authored by Paul Asmuth's avatar Paul Asmuth
Browse files

move sexpr conversion code into sexpr_{conv,util}.{h,cc}

parent fb07bcfe
Loading
Loading
Loading
Loading
+0 −43
Original line number Diff line number Diff line
@@ -37,49 +37,6 @@ using namespace std::placeholders;

namespace plotfx {

ReturnCode parseAll(
    const Expr* expr,
    const ParserDefinitions& pdefs) {
  for (; expr; expr = expr_next(expr)) {
    if (!expr_is_value_literal(expr)) {
      return ReturnCode::error("EARG", "expected a literal");
    }

    auto param = expr_get_value(expr);
    const auto& pdef = pdefs.find(param);
    if (pdef == pdefs.end()) {
      return ReturnCode::errorf("EARG", "invalid paramter: '$0'", param);
    }

    if (expr = expr_next(expr); !expr) {
      return ReturnCode::errorf("EARG", "expected an argument for '$0'", param);
    }

    if (auto rc = pdef->second(expr); !rc.isSuccess()) {
      return ReturnCode::errorf(
          "EPARSE",
          "error while parsing property '$0': $1",
          param,
          rc.getMessage());

      return rc;
    }
  }

  return OK;
}

ReturnCode configure_string(
    const Expr* expr,
    std::string* value) {
  if (!expr_is_value(expr)) {
    return ReturnCode::error("EARG", "expected value");
  }

  *value = expr_get_value(expr);
  return OK;
}

/*
ReturnCode configure_measure(
    const Expr* prop,
+0 −5
Original line number Diff line number Diff line
@@ -49,11 +49,6 @@ using ParserDefinitions = std::unordered_map<std::string, ParserFn>;
ReturnCode parseAll(
    const Expr* expr,
    const ParserDefinitions& pdefs);

ReturnCode configure_string(
    const Expr* prop,
    std::string* value);

/*
template <typename T>
using EnumDefinitions = std::unordered_map<std::string, T>;

core/sexpr_conv.cc

0 → 100644
+52 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "config_helpers.h"
#include "utils/fileutil.h"
#include "utils/csv.h"
#include "utils/algo.h"
#include <iostream>

using namespace std::placeholders;

namespace plotfx {

ReturnCode expr_to_string(
    const Expr* expr,
    std::string* value) {
  if (!expr_is_value(expr)) {
    return ReturnCode::error("EARG", "expected value");
  }

  *value = expr_get_value(expr);
  return OK;
}

} // namespace plotfx

core/sexpr_conv.h

0 → 100644
+40 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#pragma once
#include <sexpr.h>

namespace plotfx {

ReturnCode expr_to_string(
    const Expr* prop,
    std::string* value);

} // namespace plotfx

core/sexpr_util.cc

0 → 100644
+70 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "sexpr_util.h"

namespace plotfx {

ReturnCode expr_walk_map(
    const Expr* expr,
    const std::unordered_map<
        std::string,
        std::function<ReturnCode (const Expr*)>>& pdefs) {
  for (; expr; expr = expr_next(expr)) {
    if (!expr_is_value_literal(expr)) {
      return ReturnCode::error("EARG", "expected a literal");
    }

    auto param = expr_get_value(expr);
    const auto& pdef = pdefs.find(param);
    if (pdef == pdefs.end()) {
      return ReturnCode::errorf("EARG", "invalid paramter: '$0'", param);
    }

    if (expr = expr_next(expr); !expr) {
      return ReturnCode::errorf("EARG", "expected an argument for '$0'", param);
    }

    if (auto rc = pdef->second(expr); !rc.isSuccess()) {
      return ReturnCode::errorf(
          "EPARSE",
          "error while parsing property '$0': $1",
          param,
          rc.getMessage());

      return rc;
    }
  }

  return OK;
}

} // namespace plotfx

Loading