2018-04-01 14:49:10 +10:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CRUFT_UTIL_CPP_HPP
|
|
|
|
#define CRUFT_UTIL_CPP_HPP
|
|
|
|
|
|
|
|
#include "string.hpp"
|
|
|
|
#include "view.hpp"
|
|
|
|
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
namespace util::cpp {
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
struct context {
|
|
|
|
std::stack<std::experimental::filesystem::path> source;
|
|
|
|
std::map<std::string,std::string> defines;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-05-07 13:28:31 +10:00
|
|
|
/// an abstract base that performs processing for a directive
|
2018-04-01 14:49:10 +10:00
|
|
|
class directive {
|
|
|
|
public:
|
|
|
|
virtual ~directive () = default;
|
|
|
|
|
2018-05-07 13:28:31 +10:00
|
|
|
/// handles a preprocessor directive by:
|
|
|
|
/// * perform some processing on any number of input lines,
|
|
|
|
/// * optionally writes to the supplied stream,
|
|
|
|
/// * and returns the line that should be consumed next
|
|
|
|
///
|
|
|
|
/// \param lines is a tokenised view over all the lines of the input
|
|
|
|
/// \return the next line that processing should continue from
|
2018-04-01 14:49:10 +10:00
|
|
|
virtual util::tokeniser<const char*>::iterator
|
|
|
|
process (std::ostream&,
|
|
|
|
context&,
|
2018-05-07 13:28:31 +10:00
|
|
|
util::view<util::tokeniser<const char*>::iterator> lines) const = 0;
|
2018-04-01 14:49:10 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
class processor {
|
|
|
|
public:
|
|
|
|
processor ();
|
|
|
|
void add (std::string token, std::unique_ptr<directive>);
|
|
|
|
|
|
|
|
void process (std::ostream&, const std::experimental::filesystem::path&) const;
|
|
|
|
|
|
|
|
std::experimental::filesystem::path
|
|
|
|
resolve (const std::experimental::filesystem::path&) const;
|
|
|
|
|
|
|
|
util::tokeniser<const char*>::iterator
|
|
|
|
process (std::ostream&,
|
|
|
|
context&,
|
|
|
|
util::view<util::tokeniser<const char*>::iterator>) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<std::string, std::unique_ptr<directive>> m_directives;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-05-07 13:28:31 +10:00
|
|
|
/// thrown when a processor encounters a directive it has not been
|
|
|
|
/// configured to handle
|
2018-04-01 14:49:10 +10:00
|
|
|
class unknown_directive : public std::runtime_error {
|
|
|
|
using runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-05-07 13:28:31 +10:00
|
|
|
/// silently ignores configured directive by advancing the input cursor
|
|
|
|
/// past the provided line without writing to the output stream.
|
|
|
|
class ignore : public directive {
|
|
|
|
util::tokeniser<const char*>::iterator
|
2018-04-01 14:49:10 +10:00
|
|
|
process (std::ostream&,
|
|
|
|
context&,
|
|
|
|
util::view<util::tokeniser<const char*>::iterator> lines) const override
|
|
|
|
{ return lines.begin ()++; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-07 13:28:31 +10:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// copies the supplied directive to the output stream without any
|
|
|
|
/// modification
|
|
|
|
class passthrough : public directive {
|
2018-04-01 14:49:10 +10:00
|
|
|
public:
|
|
|
|
passthrough (const std::string &name);
|
|
|
|
|
|
|
|
virtual util::tokeniser<const char*>::iterator
|
|
|
|
process (std::ostream&,
|
|
|
|
context&,
|
|
|
|
util::view<util::tokeniser<const char*>::iterator>) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string m_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-07 13:28:31 +10:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// handles include directives by copying the contents of the referenced
|
|
|
|
/// path into the input stream
|
2018-04-01 14:49:10 +10:00
|
|
|
class include : public directive {
|
|
|
|
public:
|
|
|
|
include (processor &_parent);
|
|
|
|
|
|
|
|
void add (const std::experimental::filesystem::path&);
|
|
|
|
|
|
|
|
virtual util::tokeniser<const char*>::iterator
|
|
|
|
process (std::ostream&,
|
|
|
|
context&,
|
|
|
|
util::view<util::tokeniser<const char*>::iterator>) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
processor &m_parent;
|
|
|
|
std::vector<std::experimental::filesystem::path> m_paths;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|