2018-12-13 11:44:10 +00:00
|
|
|
// Copyright Toru Niina 2017.
|
|
|
|
// Distributed under the MIT License.
|
2018-12-04 11:29:39 +00:00
|
|
|
#ifndef TOML11_COMBINATOR_HPP
|
|
|
|
#define TOML11_COMBINATOR_HPP
|
2019-04-16 11:54:29 +00:00
|
|
|
#include <cassert>
|
2018-12-04 11:29:39 +00:00
|
|
|
#include <cctype>
|
2020-06-27 15:58:20 +00:00
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include "region.hpp"
|
|
|
|
#include "result.hpp"
|
|
|
|
#include "traits.hpp"
|
|
|
|
#include "utility.hpp"
|
2018-12-04 11:29:39 +00:00
|
|
|
|
|
|
|
// they scans characters and returns region if it matches to the condition.
|
|
|
|
// when they fail, it does not change the location.
|
|
|
|
// in lexer.hpp, these are used.
|
|
|
|
|
|
|
|
namespace toml
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
// to output character as an error message.
|
|
|
|
inline std::string show_char(const char c)
|
|
|
|
{
|
2021-08-27 23:52:45 +00:00
|
|
|
// It suppresses an error that occurs only in Debug mode of MSVC++ on Windows.
|
2018-12-04 13:17:20 +00:00
|
|
|
// I'm not completely sure but they check the value of char to be in the
|
|
|
|
// range [0, 256) and some of the COMPLETELY VALID utf-8 character sometimes
|
|
|
|
// has negative value (if char has sign). So here it re-interprets c as
|
|
|
|
// unsigned char through pointer. In general, converting pointer to a
|
|
|
|
// pointer that has different type cause UB, but `(signed|unsigned)?char`
|
|
|
|
// are one of the exceptions. Converting pointer only to char and std::byte
|
|
|
|
// (c++17) are valid.
|
2018-12-04 12:55:45 +00:00
|
|
|
if(std::isgraph(*reinterpret_cast<unsigned char const*>(std::addressof(c))))
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
|
|
|
return std::string(1, c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-16 11:54:29 +00:00
|
|
|
std::array<char, 5> buf;
|
|
|
|
buf.fill('\0');
|
|
|
|
const auto r = std::snprintf(
|
|
|
|
buf.data(), buf.size(), "0x%02x", static_cast<int>(c) & 0xFF);
|
2019-11-22 11:59:55 +00:00
|
|
|
(void) r; // Unused variable warning
|
2019-06-09 19:00:28 +00:00
|
|
|
assert(r == static_cast<int>(buf.size()) - 1);
|
2019-04-16 11:54:29 +00:00
|
|
|
return std::string(buf.data());
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<char C>
|
|
|
|
struct character
|
|
|
|
{
|
|
|
|
static constexpr char target = C;
|
|
|
|
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2019-05-31 08:07:52 +00:00
|
|
|
if(loc.iter() == loc.end()) {return none();}
|
2018-12-04 11:29:39 +00:00
|
|
|
const auto first = loc.iter();
|
|
|
|
|
|
|
|
const char c = *(loc.iter());
|
|
|
|
if(c != target)
|
|
|
|
{
|
2019-05-31 08:07:52 +00:00
|
|
|
return none();
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.advance(); // update location
|
2018-12-04 11:29:39 +00:00
|
|
|
|
2020-07-25 13:01:34 +00:00
|
|
|
return ok(region(loc, first, loc.iter()));
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template<char C>
|
|
|
|
constexpr char character<C>::target;
|
|
|
|
|
|
|
|
// closed interval [Low, Up]. both Low and Up are included.
|
|
|
|
template<char Low, char Up>
|
|
|
|
struct in_range
|
|
|
|
{
|
|
|
|
// assuming ascii part of UTF-8...
|
|
|
|
static_assert(Low <= Up, "lower bound should be less than upper bound.");
|
|
|
|
|
|
|
|
static constexpr char upper = Up;
|
|
|
|
static constexpr char lower = Low;
|
|
|
|
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2019-05-31 08:07:52 +00:00
|
|
|
if(loc.iter() == loc.end()) {return none();}
|
2018-12-04 11:29:39 +00:00
|
|
|
const auto first = loc.iter();
|
|
|
|
|
|
|
|
const char c = *(loc.iter());
|
|
|
|
if(c < lower || upper < c)
|
|
|
|
{
|
2019-05-31 08:07:52 +00:00
|
|
|
return none();
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.advance();
|
2020-07-25 13:01:34 +00:00
|
|
|
return ok(region(loc, first, loc.iter()));
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template<char L, char U> constexpr char in_range<L, U>::upper;
|
|
|
|
template<char L, char U> constexpr char in_range<L, U>::lower;
|
|
|
|
|
|
|
|
// keep iterator if `Combinator` matches. otherwise, increment `iter` by 1 char.
|
|
|
|
// for detecting invalid characters, like control sequences in toml string.
|
|
|
|
template<typename Combinator>
|
|
|
|
struct exclude
|
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2019-05-31 08:07:52 +00:00
|
|
|
if(loc.iter() == loc.end()) {return none();}
|
2018-12-04 11:29:39 +00:00
|
|
|
auto first = loc.iter();
|
|
|
|
|
2019-05-30 11:08:37 +00:00
|
|
|
auto rslt = Combinator::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_ok())
|
|
|
|
{
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.reset(first);
|
2019-05-31 08:07:52 +00:00
|
|
|
return none();
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.reset(std::next(first)); // XXX maybe loc.advance() is okay but...
|
2020-07-25 13:01:34 +00:00
|
|
|
return ok(region(loc, first, loc.iter()));
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// increment `iter`, if matches. otherwise, just return empty string.
|
|
|
|
template<typename Combinator>
|
|
|
|
struct maybe
|
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
const auto rslt = Combinator::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_ok())
|
|
|
|
{
|
|
|
|
return rslt;
|
|
|
|
}
|
2020-07-25 13:01:34 +00:00
|
|
|
return ok(region(loc));
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ... Ts>
|
|
|
|
struct sequence;
|
|
|
|
|
|
|
|
template<typename Head, typename ... Tail>
|
|
|
|
struct sequence<Head, Tail...>
|
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
|
|
|
const auto first = loc.iter();
|
2021-10-09 02:12:58 +00:00
|
|
|
auto rslt = Head::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_err())
|
|
|
|
{
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.reset(first);
|
2019-05-31 08:07:52 +00:00
|
|
|
return none();
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
2019-05-30 11:08:37 +00:00
|
|
|
return sequence<Tail...>::invoke(loc, std::move(rslt.unwrap()), first);
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called from the above function only, recursively.
|
2020-07-25 12:06:26 +00:00
|
|
|
template<typename Iterator>
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
|
|
|
invoke(location& loc, region reg, Iterator first)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
const auto rslt = Head::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_err())
|
|
|
|
{
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.reset(first);
|
2019-05-31 08:07:52 +00:00
|
|
|
return none();
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
reg += rslt.unwrap(); // concat regions
|
2019-05-30 11:08:37 +00:00
|
|
|
return sequence<Tail...>::invoke(loc, std::move(reg), first);
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Head>
|
|
|
|
struct sequence<Head>
|
|
|
|
{
|
|
|
|
// would be called from sequence<T ...>::invoke only.
|
2020-07-25 12:06:26 +00:00
|
|
|
template<typename Iterator>
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
|
|
|
invoke(location& loc, region reg, Iterator first)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
const auto rslt = Head::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_err())
|
|
|
|
{
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.reset(first);
|
2019-05-31 08:07:52 +00:00
|
|
|
return none();
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
reg += rslt.unwrap(); // concat regions
|
|
|
|
return ok(reg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ... Ts>
|
|
|
|
struct either;
|
|
|
|
|
|
|
|
template<typename Head, typename ... Tail>
|
|
|
|
struct either<Head, Tail...>
|
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
const auto rslt = Head::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_ok()) {return rslt;}
|
2019-05-30 11:08:37 +00:00
|
|
|
return either<Tail...>::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename Head>
|
|
|
|
struct either<Head>
|
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
return Head::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename N>
|
|
|
|
struct repeat;
|
|
|
|
|
|
|
|
template<std::size_t N> struct exactly{};
|
|
|
|
template<std::size_t N> struct at_least{};
|
|
|
|
struct unlimited{};
|
|
|
|
|
|
|
|
template<typename T, std::size_t N>
|
|
|
|
struct repeat<T, exactly<N>>
|
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
region retval(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
const auto first = loc.iter();
|
|
|
|
for(std::size_t i=0; i<N; ++i)
|
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
auto rslt = T::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_err())
|
|
|
|
{
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.reset(first);
|
2019-05-31 08:07:52 +00:00
|
|
|
return none();
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
retval += rslt.unwrap();
|
|
|
|
}
|
|
|
|
return ok(std::move(retval));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, std::size_t N>
|
|
|
|
struct repeat<T, at_least<N>>
|
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
region retval(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
|
|
|
|
const auto first = loc.iter();
|
|
|
|
for(std::size_t i=0; i<N; ++i)
|
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
auto rslt = T::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_err())
|
|
|
|
{
|
2019-04-12 09:32:46 +00:00
|
|
|
loc.reset(first);
|
2019-05-31 08:07:52 +00:00
|
|
|
return none();
|
2018-12-04 11:29:39 +00:00
|
|
|
}
|
|
|
|
retval += rslt.unwrap();
|
|
|
|
}
|
|
|
|
while(true)
|
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
auto rslt = T::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_err())
|
|
|
|
{
|
|
|
|
return ok(std::move(retval));
|
|
|
|
}
|
|
|
|
retval += rslt.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct repeat<T, unlimited>
|
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
static result<region, none_t>
|
2020-07-25 12:06:26 +00:00
|
|
|
invoke(location& loc)
|
2018-12-04 11:29:39 +00:00
|
|
|
{
|
2020-07-25 13:01:34 +00:00
|
|
|
region retval(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
while(true)
|
|
|
|
{
|
2019-05-30 11:08:37 +00:00
|
|
|
auto rslt = T::invoke(loc);
|
2018-12-04 11:29:39 +00:00
|
|
|
if(rslt.is_err())
|
|
|
|
{
|
|
|
|
return ok(std::move(retval));
|
|
|
|
}
|
|
|
|
retval += rslt.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
} // toml
|
|
|
|
#endif// TOML11_COMBINATOR_HPP
|