toml11/toml/parser.hpp

1081 lines
41 KiB
C++
Raw Normal View History

2017-05-06 09:44:19 +00:00
#ifndef TOML11_PARSER
#define TOML11_PARSER
#include "value.hpp"
#include "acceptor.hpp"
#include <algorithm>
2017-05-08 10:08:49 +00:00
#include <iostream>
2017-05-12 11:49:47 +00:00
#include <fstream>
2017-05-16 12:09:46 +00:00
#include <sstream>
2017-05-06 09:44:19 +00:00
namespace toml
{
2017-05-10 14:56:09 +00:00
namespace detail
{
// it is just an inferior vertion of boost/std::optional
template<typename T>
struct result
{
result() : ok_(false){}
~result() = default;
result(const result& rhs) = default;
result(result&& rhs) = default;
result& operator=(const result& rhs) = default;
result& operator=(result&& rhs) = default;
result(const T& v) : ok_(true), value_(v){}
result(T&& v) : ok_(true), value_(std::move(v)){}
result& operator=(const T& rhs){ok_ = true; value_ = rhs; return *this;}
result& operator=(T&& rhs) {ok_ = true; value_ = rhs; return *this;}
2017-05-11 06:08:38 +00:00
template<typename U>
result& operator=(const result<U>& u) {ok_ = u.ok(); if(ok_)value_ = u.move(); return *this;}
template<typename U>
result& operator=(result<U>&& u) {ok_ = u.ok(); if(ok_)value_ = u.move(); return *this;}
template<typename U>
result(const result<U>& u): ok_(u.ok()){if(ok_)value_ = u.get();}
template<typename U>
result(result<U>&& u): ok_(u.ok()){if(ok_)value_ = u.move();}
2017-05-10 14:56:09 +00:00
bool ok() const {return ok_;}
operator bool() const {return ok_;}
2017-05-11 06:08:38 +00:00
T& get() {if(!ok_) throw std::logic_error("result::get"); return value_;}
T const& get() const {if(!ok_) throw std::logic_error("result::get"); return value_;}
2017-05-10 14:56:09 +00:00
T&& move()
2017-05-11 06:08:38 +00:00
{if(!ok_) throw std::logic_error("result::move"); ok_ = false; return std::move(value_);}
2017-05-10 14:56:09 +00:00
private:
bool ok_;
T value_;
};
}//detail
2017-05-06 09:44:19 +00:00
struct parse_escape_sequence
{
2017-05-10 14:56:09 +00:00
typedef toml::charactor value_type;
typedef toml::String string_type;
typedef detail::result<string_type> result_type;
2017-05-06 09:44:19 +00:00
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator> invoke(Iterator iter, Iterator end)
2017-05-06 09:44:19 +00:00
{
if(iter == end || *iter != '\\')
return std::make_pair(result_type{}, iter);
2017-05-06 09:44:19 +00:00
++iter;
switch(*iter)
{
2017-05-10 14:56:09 +00:00
case '\\': return std::make_pair(string_type("\\"), std::next(iter));
case '"' : return std::make_pair(string_type("\""), std::next(iter));
case 'b' : return std::make_pair(string_type("\b"), std::next(iter));
case 't' : return std::make_pair(string_type("\t"), std::next(iter));
case 'n' : return std::make_pair(string_type("\n"), std::next(iter));
case 'f' : return std::make_pair(string_type("\f"), std::next(iter));
case 'r' : return std::make_pair(string_type("\r"), std::next(iter));
2017-05-06 09:44:19 +00:00
case 'u' :
{
if(std::distance(iter, end) < 5)
throw syntax_error("invalid escape sequence");
2017-05-10 14:56:09 +00:00
return std::make_pair(utf8_to_char(make_codepoint(
string_type(iter+1, iter+5))), iter+5);
}
2017-05-06 09:44:19 +00:00
case 'U':
{
if(std::distance(iter, end) < 8)
throw syntax_error("invalid escape sequence");
2017-05-10 14:56:09 +00:00
return std::make_pair(utf8_to_char(make_codepoint(
string_type(iter+1, iter+9))), iter+9);
}
2017-05-06 09:44:19 +00:00
default: throw syntax_error("unkwnon escape sequence");
}
}
static unsigned int make_codepoint(string_type str)
{
unsigned int codepoint;
2017-05-10 14:56:09 +00:00
std::basic_istringstream<value_type> iss(str);
2017-05-06 09:44:19 +00:00
iss >> std::hex >> codepoint;
return codepoint;
}
2017-05-10 14:56:09 +00:00
static string_type utf8_to_char(const unsigned int codepoint)
2017-05-06 09:44:19 +00:00
{
2017-05-10 14:56:09 +00:00
string_type charactor;
2017-05-06 09:44:19 +00:00
if(codepoint < 0x80)
{
charactor += static_cast<unsigned char>(codepoint);
}
else if(codepoint < 0x800)
{
charactor += static_cast<unsigned char>(0xC0| codepoint >> 6);
charactor += static_cast<unsigned char>(0x80|(codepoint & 0x3F));
}
else if(codepoint < 0x10000)
{
charactor += static_cast<unsigned char>(0xE0| codepoint >>12);
charactor += static_cast<unsigned char>(0x80|(codepoint >>6&0x3F));
charactor += static_cast<unsigned char>(0x80|(codepoint & 0x3F));
}
else
{
charactor += static_cast<unsigned char>(0xF0| codepoint >>18);
charactor += static_cast<unsigned char>(0x80|(codepoint >>12&0x3F));
charactor += static_cast<unsigned char>(0x80|(codepoint >>6 &0x3F));
charactor += static_cast<unsigned char>(0x80|(codepoint & 0x3F));
}
return charactor;
}
};
struct parse_basic_inline_string
{
2017-05-10 14:56:09 +00:00
typedef toml::charactor value_type;
typedef detail::result<toml::String> result_type;
2017-05-06 09:44:19 +00:00
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-06 09:44:19 +00:00
{
const Iterator end =
is_basic_inline_string<value_type>::invoke(iter, range_end);
2017-05-10 14:56:09 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
2017-05-06 09:44:19 +00:00
if(std::distance(iter, end) < 2)
2017-05-10 14:56:09 +00:00
throw internal_error("is_basic_inline_string");
toml::String result; result.reserve(std::distance(iter, end)-2);
++iter;
const Iterator last = std::prev(end); // ignore '"'
while(iter != last)
2017-05-06 09:44:19 +00:00
{
if(*iter == '\\')
{
auto r = parse_escape_sequence::invoke(iter, last);
if(!r.first.ok())
throw internal_error("parse_basic_inline_string");
2017-05-10 14:56:09 +00:00
result += r.first.move();
iter = r.second;
2017-05-06 09:44:19 +00:00
}
else
{
2017-05-10 14:56:09 +00:00
result += *iter;
2017-05-06 09:44:19 +00:00
++iter;
}
}
2017-05-10 14:56:09 +00:00
return std::make_pair(result, end);
2017-05-06 09:44:19 +00:00
}
};
struct parse_basic_multiline_string
{
2017-05-10 14:56:09 +00:00
typedef toml::charactor value_type;
typedef toml::String string_type;
typedef detail::result<string_type> result_type;
2017-05-06 09:44:19 +00:00
2017-05-10 14:56:09 +00:00
typedef is_chain_of<is_charactor<value_type, '\\'>, is_newline<value_type>>
2017-05-06 09:44:19 +00:00
is_line_ending_backslash;
2017-05-10 14:56:09 +00:00
typedef is_repeat_of<is_one_of<is_whitespace<value_type>, is_newline<value_type>>,
2017-05-06 09:44:19 +00:00
repeat_infinite()> ws_nl_after_backslash_remover;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-06 09:44:19 +00:00
{
const Iterator end =
is_basic_multiline_string<value_type>::invoke(iter, range_end);
2017-05-10 14:56:09 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
2017-05-06 09:44:19 +00:00
if(std::distance(iter, end) < 6)
2017-05-10 14:56:09 +00:00
throw internal_error("is_basic_inline_string");
toml::String result; result.reserve(std::distance(iter, end)-6);
2017-05-06 09:44:19 +00:00
std::advance(iter, 3);
2017-05-10 14:56:09 +00:00
const Iterator last = end - 3;
iter = is_newline<value_type>::invoke(iter, last);
2017-05-10 14:56:09 +00:00
while(iter != last)
2017-05-06 09:44:19 +00:00
{
if(*iter == '\\')
{
if(is_line_ending_backslash::invoke(iter, last) != iter)
2017-05-06 09:44:19 +00:00
{
iter = ws_nl_after_backslash_remover::invoke(std::next(iter), last);
2017-05-06 09:44:19 +00:00
}
else
{
auto r = parse_escape_sequence::invoke(iter, last);
if(!r.first.ok())
throw internal_error("parse_basic_inline_string");
2017-05-10 14:56:09 +00:00
result += r.first.move();
iter = r.second;
2017-05-06 09:44:19 +00:00
}
}
else
{
result.push_back(*iter);
++iter;
}
}
2017-05-10 14:56:09 +00:00
return std::make_pair(result, end);
2017-05-06 09:44:19 +00:00
}
};
struct parse_literal_inline_string
{
2017-05-10 14:56:09 +00:00
typedef toml::charactor value_type;
typedef detail::result<toml::String> result_type;
2017-05-06 09:44:19 +00:00
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-06 09:44:19 +00:00
{
const Iterator end =
is_literal_inline_string<value_type>::invoke(iter, range_end);
2017-05-10 14:56:09 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
2017-05-06 09:44:19 +00:00
if(std::distance(iter, end) < 2)
2017-05-10 14:56:09 +00:00
throw internal_error("is_literal_inline_string");
toml::String result; result.reserve(std::distance(iter, end)-2);
++iter;
const Iterator last = end - 1;
while(iter != last)
2017-05-06 09:44:19 +00:00
{
result.push_back(*iter);
++iter;
}
2017-05-10 14:56:09 +00:00
return std::make_pair(result, end);
2017-05-06 09:44:19 +00:00
}
};
struct parse_literal_multiline_string
{
2017-05-10 14:56:09 +00:00
typedef toml::charactor value_type;
typedef detail::result<toml::String> result_type;
2017-05-06 09:44:19 +00:00
2017-05-10 14:56:09 +00:00
typedef is_chain_of<is_charactor<value_type, '\\'>, is_newline<value_type>>
2017-05-06 09:44:19 +00:00
is_line_ending_backslash;
2017-05-10 14:56:09 +00:00
typedef is_repeat_of<is_one_of<is_whitespace<value_type>, is_newline<value_type>>,
2017-05-06 09:44:19 +00:00
repeat_infinite()> ws_nl_after_backslash_remover;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-06 09:44:19 +00:00
{
const Iterator end =
is_literal_multiline_string<value_type>::invoke(iter, range_end);
2017-05-10 14:56:09 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
2017-05-06 09:44:19 +00:00
if(std::distance(iter, end) < 6)
2017-05-10 14:56:09 +00:00
throw internal_error("is_literal_multiline_string");
toml::String result; result.reserve(std::distance(iter, end)-6);
2017-05-06 09:44:19 +00:00
std::advance(iter, 3);
2017-05-10 14:56:09 +00:00
const Iterator last = end - 3;
iter = is_newline<value_type>::invoke(iter, last); // trim first newline if exist
2017-05-10 14:56:09 +00:00
while(iter != last)
2017-05-06 09:44:19 +00:00
{
result.push_back(*iter);
++iter;
}
2017-05-10 14:56:09 +00:00
return std::make_pair(result, end);
2017-05-06 09:44:19 +00:00
}
};
2017-05-07 04:51:25 +00:00
struct parse_string
{
2017-05-10 14:56:09 +00:00
typedef toml::charactor value_type;
typedef detail::result<toml::String> result_type;
2017-05-07 04:51:25 +00:00
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-07 04:51:25 +00:00
{
2017-05-10 14:56:09 +00:00
std::pair<result_type, Iterator> result;
if((result = parse_basic_inline_string::invoke(iter, range_end)).first.ok())
2017-05-10 14:56:09 +00:00
return result;
else if((result = parse_basic_multiline_string::invoke(iter, range_end)).first.ok())
2017-05-10 14:56:09 +00:00
return result;
else if((result = parse_literal_inline_string::invoke(iter, range_end)).first.ok())
2017-05-10 14:56:09 +00:00
return result;
else if((result = parse_literal_multiline_string::invoke(iter, range_end)).first.ok())
2017-05-10 14:56:09 +00:00
return result;
2017-05-06 09:44:19 +00:00
else
2017-05-10 14:56:09 +00:00
return std::make_pair(result_type{}, iter);
2017-05-07 06:04:40 +00:00
}
};
struct parse_integer
{
typedef toml::charactor value_type;
typedef std::basic_string<value_type> string_type;
typedef detail::result<toml::Integer> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator> invoke(Iterator iter, Iterator range_end)
{
const Iterator end = is_integer<value_type>::invoke(iter, range_end);
if(iter == end) return std::make_pair(result_type{}, iter);
string_type result; result.resize(std::distance(iter, end));
std::copy_if(iter, end, result.begin(), [](value_type c){return c != '_';});
return std::make_pair(std::stoll(result), end);
}
};
struct parse_float
{
typedef toml::charactor value_type;
typedef std::basic_string<value_type> string_type;
typedef detail::result<toml::Float> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
{
const Iterator end = is_float<value_type>::invoke(iter, range_end);
if(iter == end) return std::make_pair(result_type{}, iter);
string_type result; result.resize(std::distance(iter, end));
std::copy_if(iter, end, result.begin(), [](value_type c){return c != '_';});
2017-05-12 11:49:47 +00:00
try{
return std::make_pair(std::stod(result), end);
}
catch(std::out_of_range& oor)
{
std::cout << "extremely large Float value appeared: "
<< result << "; it is negrected" << std::endl;
return std::make_pair(0, end);
}
}
};
struct parse_boolean
{
typedef toml::charactor value_type;
typedef detail::result<toml::Boolean> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
{
const Iterator end = is_boolean<value_type>::invoke(iter, range_end);
if(iter == end) return std::make_pair(result_type{}, iter);
return std::make_pair((std::distance(iter, end) == 4), end);
}
};
2017-05-11 06:09:11 +00:00
struct parse_local_time
{
typedef toml::charactor value_type;
typedef std::basic_string<value_type> string_type;
typedef detail::result<toml::Datetime> result_type;
typedef typename toml::Datetime::number_type number_type;
template<std::size_t N>
using nums = is_repeat_of<is_number<toml::charactor>, N>;
typedef is_charactor<toml::charactor, ':'> delim;
typedef is_charactor<toml::charactor, '.'> fract;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
const Iterator end = is_local_time<value_type>::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
toml::Datetime result;
result.hour = std::stoi(string_type(iter, nums<2>::invoke(iter, end)));
iter = delim::invoke(nums<2>::invoke(iter, end), end);
result.minute = std::stoi(string_type(iter, nums<2>::invoke(iter, end)));
iter = delim::invoke(nums<2>::invoke(iter, end), end);
result.second = std::stoi(string_type(iter, nums<2>::invoke(iter, end)));
iter = fract::invoke(nums<2>::invoke(iter, end), end);
2017-05-11 06:09:11 +00:00
if(iter == end)
{
result.millisecond = 0.0;
result.microsecond = 0.0;
}
else if(std::distance(iter, end) <= 3)
{
result.millisecond = parse_number(iter, end);
result.microsecond = 0.0;
}
else
{
result.millisecond = parse_number(iter, iter + 3);
result.microsecond = parse_number(iter + 3, end);
}
result.offset_hour = toml::Datetime::nooffset;
result.offset_minute = toml::Datetime::nooffset;
result.year = toml::Datetime::undef;
result.month = toml::Datetime::undef;
result.day = toml::Datetime::undef;
return std::make_pair(result, end);
}
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static number_type parse_number(Iterator iter, Iterator end)
{
if(std::distance(iter, end) > 3) end = iter + 3;
string_type str(iter, end);
while(str.size() < 3){str += '0';}
return std::stoi(str);
}
};
struct parse_local_date
{
typedef toml::charactor value_type;
typedef std::basic_string<value_type> string_type;
typedef detail::result<toml::Datetime> result_type;
template<std::size_t N>
using nums = is_repeat_of<is_number<value_type>, N>;
typedef is_charactor<value_type, '-'> delim;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
const Iterator end = is_local_date<value_type>::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
toml::Datetime result;
result.year = std::stoi(string_type(iter, nums<4>::invoke(iter, end)));
iter = delim::invoke(nums<4>::invoke(iter, end), end);
result.month = std::stoi(string_type(iter, nums<2>::invoke(iter, end)));
iter = delim::invoke(nums<2>::invoke(iter, end), end);
result.day = std::stoi(string_type(iter, nums<2>::invoke(iter, end)));
2017-05-11 06:09:11 +00:00
result.offset_hour = toml::Datetime::nooffset;
result.offset_minute = toml::Datetime::nooffset;
result.hour = toml::Datetime::undef;
result.minute = toml::Datetime::undef;
result.second = toml::Datetime::undef;
result.millisecond = toml::Datetime::undef;
result.microsecond = toml::Datetime::undef;
return std::make_pair(result, end);
}
};
struct parse_local_date_time
{
typedef toml::charactor value_type;
typedef std::basic_string<value_type> string_type;
typedef detail::result<toml::Datetime> result_type;
template<std::size_t N>
using nums = is_repeat_of<is_number<toml::charactor>, N>;
typedef is_charactor<toml::charactor, 'T'> delim;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
const Iterator end =
is_local_date_time<value_type>::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
auto ld = parse_local_date::invoke(iter, end);
2017-05-11 06:09:11 +00:00
if(!ld.first.ok()) throw syntax_error("invalid local datetime");
toml::Datetime result(ld.first.move());
iter = delim::invoke(ld.second, end);// 'T'
2017-05-11 06:09:11 +00:00
const auto time = parse_local_time::invoke(iter, end);
2017-05-11 06:09:11 +00:00
result.hour = time.first.get().hour;
result.minute = time.first.get().minute;
result.second = time.first.get().second;
result.millisecond = time.first.get().millisecond;
result.microsecond = time.first.get().microsecond;
result.offset_hour = toml::Datetime::nooffset;
result.offset_minute = toml::Datetime::nooffset;
return std::make_pair(result, end);
}
};
struct parse_offset_date_time
{
typedef toml::charactor value_type;
typedef std::basic_string<value_type> string_type;
typedef detail::result<toml::Datetime> result_type;
template<std::size_t N>
using nums = is_repeat_of<is_number<toml::charactor>, N>;
typedef is_charactor<toml::charactor, ':'> delim;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
const Iterator end =
is_offset_date_time<value_type>::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
auto ldt = parse_local_date_time::invoke(iter, end);
2017-05-11 06:09:11 +00:00
if(!ldt.first.ok()) throw syntax_error("invalid offset datetime");
toml::Datetime result(ldt.first.move());
iter = ldt.second;
if(*iter == 'Z')
{
result.offset_hour = 0;
result.offset_minute = 0;
}
else
{
if(*iter != '+' && *iter != '-')
throw syntax_error("invalid offset-datetime");
const int sign = (*iter == '-') ? -1 : 1;
++iter;
result.offset_hour = sign *
std::stoi(string_type(iter, nums<2>::invoke(iter, end)));
iter = delim::invoke(nums<2>::invoke(iter, end), end);
2017-05-11 06:09:11 +00:00
result.offset_minute = sign *
std::stoi(string_type(iter, nums<2>::invoke(iter, end)));
2017-05-11 06:09:11 +00:00
}
return std::make_pair(result, end);
}
};
struct parse_datetime
{
typedef toml::charactor value_type;
typedef detail::result<toml::Datetime> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
std::pair<result_type, Iterator> result;
if((result = parse_offset_date_time::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_local_date_time::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_local_date::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_local_time::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else
return std::make_pair(result_type{}, iter);
}
};
template<typename acceptorT, typename parserT>
struct parse_fixed_type_array
{
typedef toml::charactor value_type;
typedef detail::result<toml::Array> result_type;
typedef acceptorT acceptor_type;
typedef parserT parser_type;
typedef is_skippable_in_array<value_type> skippable;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator> invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
const Iterator end =
is_fixed_type_array<value_type, acceptorT>::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
toml::Array result;
const Iterator last = std::prev(end);
iter = skippable::invoke(std::next(iter), last);
2017-05-11 06:09:11 +00:00
while(iter != last)
{
const Iterator tmp = acceptor_type::invoke(iter, last);
2017-05-11 06:09:11 +00:00
if(tmp == iter) throw syntax_error("parse_array");
auto next = parser_type::invoke(iter, last);
2017-05-11 06:09:11 +00:00
if(!next.first.ok()) throw syntax_error("parse_array");
result.emplace_back(next.first.move());
iter = tmp;
iter = skippable::invoke(iter, last);
iter = is_charactor<value_type, ','>::invoke(iter, last);
iter = skippable::invoke(iter, last);
2017-05-11 06:09:11 +00:00
}
return std::make_pair(result, end);
}
};
template<typename charT>
struct parse_inline_table;
template<typename charT>
struct parse_array
{
typedef charT value_type;
static_assert(std::is_same<charT, toml::charactor>::value, "");
typedef detail::result<toml::Array> result_type;
typedef is_skippable_in_array<value_type> skippable;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
if(iter == is_array<value_type>::invoke(iter, range_end))
2017-05-11 06:09:11 +00:00
return std::make_pair(result_type{}, iter);
std::pair<result_type, Iterator> result;
if((result = parse_fixed_type_array<is_boolean<value_type>,
parse_boolean>::invoke(iter, range_end)).first.ok()) return result;
2017-05-11 06:09:11 +00:00
else if((result = parse_fixed_type_array<is_string<value_type>,
parse_string>::invoke(iter, range_end)).first.ok()) return result;
2017-05-11 06:09:11 +00:00
else if((result = parse_fixed_type_array<is_datetime<value_type>,
parse_datetime>::invoke(iter, range_end)).first.ok()) return result;
2017-05-11 06:09:11 +00:00
else if((result = parse_fixed_type_array<is_float<value_type>,
parse_float>::invoke(iter, range_end)).first.ok()) return result;
2017-05-11 06:09:11 +00:00
else if((result = parse_fixed_type_array<is_integer<value_type>,
parse_integer>::invoke(iter, range_end)).first.ok()) return result;
2017-05-11 06:09:11 +00:00
else if((result = parse_fixed_type_array<is_array<value_type>,
parse_array<value_type>>::invoke(iter, range_end)).first.ok()) return result;
2017-05-11 06:09:11 +00:00
else if((result = parse_fixed_type_array<is_inline_table<value_type>,
parse_inline_table<value_type>>::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if(skippable::invoke(std::next(iter), range_end) == // empty
std::prev(is_array<value_type>::invoke(iter, range_end))
2017-05-11 06:09:11 +00:00
) return std::make_pair(
toml::Array{}, is_array<value_type>::invoke(iter, range_end));
2017-05-11 06:09:11 +00:00
else throw syntax_error("no valid array here");
}
};
template<typename charT>
struct parse_value
{
typedef charT value_type;
static_assert(std::is_same<charT, toml::charactor>::value, "");
typedef detail::result<toml::value> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
std::pair<result_type, Iterator> result;
if((result = parse_boolean::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_string::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_datetime::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_float::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_integer::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_array<value_type>::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_inline_table<value_type>::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else
return std::make_pair(result_type{}, iter);
}
};
struct parse_barekey
{
typedef toml::charactor value_type;
typedef detail::result<toml::key> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
const Iterator end = is_barekey<value_type>::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
return std::make_pair(toml::key(iter, end), end);
}
};
struct parse_key
{
typedef toml::charactor value_type;
typedef detail::result<toml::key> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
std::pair<result_type, Iterator> result;
if((result = parse_barekey::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else if((result = parse_string::invoke(iter, range_end)).first.ok())
2017-05-11 06:09:11 +00:00
return result;
else return std::make_pair(result_type{}, iter);
}
};
template<typename charT>
struct parse_key_value_pair
{
typedef charT value_type;
static_assert(std::is_same<charT, toml::charactor>::value, "");
typedef detail::result<std::pair<toml::key, toml::value>> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
auto tmp_key = parse_key::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(!tmp_key.first.ok())
return std::make_pair(result_type{}, iter);
iter = is_any_num_of_ws<charT>::invoke(tmp_key.second, range_end);
2017-05-11 06:09:11 +00:00
if(*iter != '=') throw syntax_error("invalid key value pair");
iter = is_any_num_of_ws<charT>::invoke(std::next(iter), range_end);
2017-05-11 06:09:11 +00:00
auto tmp_value = parse_value<toml::charactor>::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(!tmp_value.first.ok())
throw syntax_error("invalid key value pair");
iter = tmp_value.second;
return std::make_pair(std::make_pair(
tmp_key.first.move(), tmp_value.first.move()),
is_any_num_of_ws<charT>::invoke(tmp_value.second, range_end));
2017-05-11 06:09:11 +00:00
}
};
template<typename charT>
struct parse_inline_table
{
typedef charT value_type;
static_assert(std::is_same<charT, toml::charactor>::value, "");
typedef detail::result<toml::Table> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 06:09:11 +00:00
{
const Iterator end = is_inline_table<value_type>::invoke(iter, range_end);
2017-05-11 06:09:11 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
iter = is_any_num_of_ws<value_type>::invoke(std::next(iter), range_end);
2017-05-11 06:09:11 +00:00
const Iterator last = std::prev(end);
toml::Table result;
while(iter != last)
{
auto tmp = parse_key_value_pair<value_type>::invoke(iter, last);
2017-05-11 06:09:11 +00:00
if(!tmp.first.ok()) throw syntax_error("parse_inline_table");
result.emplace(tmp.first.move());
iter = tmp.second;
iter = is_any_num_of_ws<value_type>::invoke(iter, last);
iter = is_charactor<value_type, ','>::invoke(iter, last);
iter = is_any_num_of_ws<value_type>::invoke(iter, last);
2017-05-11 06:09:11 +00:00
}
return std::make_pair(result, end);
}
};
2017-05-11 10:36:12 +00:00
struct parse_table_definition
{
typedef toml::charactor value_type;
typedef detail::result<std::vector<toml::key>> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 10:36:12 +00:00
{
const Iterator end =
is_table_definition<value_type>::invoke(iter, range_end);
2017-05-11 10:36:12 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
std::vector<toml::key> result;
result.reserve(std::count(iter, end, '.')+1);
const Iterator last = std::prev(end);
iter = is_any_num_of_ws<value_type>::invoke(iter, last);
iter = is_any_num_of_ws<value_type>::invoke(std::next(iter), last);
2017-05-11 10:36:12 +00:00
auto tmp = parse_key::invoke(iter, last);
2017-05-11 10:36:12 +00:00
if(!tmp.first.ok()) throw syntax_error("table definition");
result.emplace_back(tmp.first.move());
iter = is_any_num_of_ws<value_type>::invoke(tmp.second, last);
2017-05-11 10:36:12 +00:00
while(iter != last)
{
iter = is_charactor<value_type, '.'>::invoke(iter, last);
iter = is_any_num_of_ws<value_type>::invoke(iter, last);
2017-05-11 10:36:12 +00:00
tmp = parse_key::invoke(iter, last);
2017-05-11 10:36:12 +00:00
if(!tmp.first.ok()) throw syntax_error("table definition");
result.emplace_back(tmp.first.move());
iter = is_any_num_of_ws<value_type>::invoke(tmp.second, last);
2017-05-11 10:36:12 +00:00
}
return std::make_pair(result, end);
}
};
struct parse_array_of_table_definition
{
typedef toml::charactor value_type;
typedef detail::result<std::vector<toml::key>> result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<result_type, Iterator>
invoke(Iterator iter, Iterator range_end)
2017-05-11 10:36:12 +00:00
{
const Iterator end =
is_array_of_table_definition<value_type>::invoke(iter, range_end);
2017-05-11 10:36:12 +00:00
if(iter == end) return std::make_pair(result_type{}, iter);
if(std::distance(iter, end) < 5)
throw syntax_error("invalid array_of_table definition");
2017-05-11 10:36:12 +00:00
std::vector<toml::key> result;
result.reserve(std::count(iter, end, '.')+1);
const Iterator last = end - 2;
iter = is_any_num_of_ws<value_type>::invoke(iter, last) + 2;
iter = is_any_num_of_ws<value_type>::invoke(iter, last);
2017-05-11 10:36:12 +00:00
auto tmp = parse_key::invoke(iter, last);
2017-05-11 10:36:12 +00:00
if(!tmp.first.ok()) throw syntax_error("array of table definition");
result.emplace_back(tmp.first.move());
iter = is_any_num_of_ws<value_type>::invoke(tmp.second, last);
2017-05-11 10:36:12 +00:00
while(iter != last)
{
iter = is_charactor<value_type, '.'>::invoke(iter, last);
iter = is_any_num_of_ws<value_type>::invoke(iter, last);
2017-05-11 10:36:12 +00:00
tmp = parse_key::invoke(iter, last);
2017-05-11 10:36:12 +00:00
if(!tmp.first.ok()) throw syntax_error("array of table definition");
result.emplace_back(tmp.first.move());
iter = is_any_num_of_ws<value_type>::invoke(tmp.second, last);
2017-05-11 10:36:12 +00:00
}
return std::make_pair(result, end);
}
};
struct parse_data
{
typedef toml::charactor value_type;
typedef toml::Table result_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static result_type invoke(Iterator iter, const Iterator end)
{
toml::Table result;
auto noname = parse_table_contents(iter, end);
result = std::move(noname.first);
iter = skip_empty(noname.second, end);
while(iter != end)
{
iter = skip_empty(iter, end);
std::pair<detail::result<std::vector<toml::key>>, Iterator> tabname;
if((tabname = parse_table_definition::invoke(iter, end)).first.ok())
2017-05-11 10:36:12 +00:00
{
auto contents = parse_table_contents(tabname.second, end);
push_table(result, std::move(contents.first),
tabname.first.get().begin(), tabname.first.get().end());
iter = contents.second;
}
else if((tabname = parse_array_of_table_definition::invoke(iter, end)).first.ok())
2017-05-11 10:36:12 +00:00
{
auto contents = parse_table_contents(tabname.second, end);
push_array_of_table(result, std::move(contents.first),
tabname.first.get().begin(), tabname.first.get().end());
iter = contents.second;
}
else
throw syntax_error("parse_data: unknown line");
}
return result;
}
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator
skip_empty(Iterator iter, Iterator end)
{
while(iter != end)
{
if(*iter == '#')
{
while(iter != end &&
iter == is_newline<value_type>::invoke(iter, end)){++iter;}
2017-05-11 10:36:12 +00:00
}
else if(iter == is_newline<value_type>::invoke(iter, end) &&
iter == is_whitespace<value_type>::invoke(iter, end))
2017-05-11 10:36:12 +00:00
{
return iter;
}
else
{
++iter;
}
}
return iter;
}
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static std::pair<toml::Table, Iterator>
parse_table_contents(Iterator iter, Iterator end)
{
toml::Table table;
iter = skip_empty(iter, end);
while(iter != end)
{
auto kv = parse_key_value_pair<value_type>::invoke(iter, end);
2017-05-11 10:36:12 +00:00
if(!kv.first.ok()) return std::make_pair(table, iter);
table.emplace(kv.first.move());
iter = kv.second;
iter = skip_empty(iter, end);
}
return std::make_pair(table, iter);
}
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
toml::key>::value>::type>
static void
push_table(toml::Table& data, toml::Table&& v, Iterator iter, Iterator end)
{
if(iter == std::prev(end))
{
if(data.count(*iter) == 1)
2017-05-12 11:49:47 +00:00
throw syntax_error("duplicate key: " + *iter);
2017-05-11 10:36:12 +00:00
data.emplace(*iter, std::move(v));
return;
}
if(data.count(*iter) == 0)
2017-05-12 13:28:49 +00:00
{
2017-05-11 10:36:12 +00:00
data.emplace(*iter, toml::Table());
2017-05-12 13:28:49 +00:00
return push_table(data[*iter].template cast<value_t::Table>(),
std::move(v), std::next(iter), end);
}
else if(data[*iter].type() == value_t::Table)
{
return push_table(data[*iter].template cast<value_t::Table>(),
std::move(v), std::next(iter), end);
}
else if(data[*iter].type() == value_t::Array)
{
auto& ar = data[*iter].template cast<value_t::Array>();
if(ar.empty()) ar.emplace_back(toml::Table{});
if(ar.back().type() != value_t::Table)
throw syntax_error("assign table into array having non-table type: " + *iter);
return push_table(ar.back().template cast<value_t::Table>(),
std::move(v), std::next(iter), end);
}
else
throw syntax_error("assign table into not table: " + *iter);
2017-05-11 10:36:12 +00:00
}
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
toml::key>::value>::type>
static void
push_array_of_table(toml::Table& data, toml::Table&& v,
Iterator iter, Iterator end)
{
if(iter == std::prev(end))
{
if(data.count(*iter) == 0)
data.emplace(*iter, toml::Array());
else if(data.at(*iter).type() != value_t::Array)
2017-05-12 11:49:47 +00:00
throw syntax_error("duplicate key: " + *iter);
2017-05-11 10:36:12 +00:00
data[*iter].template cast<value_t::Array>().emplace_back(std::move(v));
return;
}
if(data.count(*iter) == 0)
2017-05-12 13:28:49 +00:00
{
2017-05-11 10:36:12 +00:00
data.emplace(*iter, toml::Table());
2017-05-12 13:28:49 +00:00
return push_array_of_table(data[*iter].template cast<value_t::Table>(),
std::move(v), std::next(iter), end);
}
else if(data[*iter].type() == value_t::Table)
{
return push_array_of_table(data[*iter].template cast<value_t::Table>(),
std::move(v), std::next(iter), end);
}
else if(data[*iter].type() == value_t::Array)
{
auto& ar = data[*iter].template cast<value_t::Array>();
if(ar.empty()) ar.emplace_back(toml::Table{});
if(ar.back().type() != value_t::Table)
throw syntax_error("assign table into array having non-table type: " + *iter);
return push_array_of_table(ar.back().template cast<value_t::Table>(),
std::move(v), std::next(iter), end);
}
else
throw syntax_error("assign array of table into not table: " + *iter);
2017-05-11 10:36:12 +00:00
}
};
2017-05-07 04:51:25 +00:00
2017-05-12 11:49:47 +00:00
template<typename traits = std::char_traits<toml::charactor>>
toml::Table parse(std::basic_istream<toml::charactor, traits>& is)
{
const auto initial = is.tellg();
is.seekg(0, std::ios::end);
const auto eofpos = is.tellg();
const std::size_t size = eofpos - initial;
is.seekg(initial);
std::vector<toml::charactor> contents(size);
is.read(contents.data(), size);
return parse_data::invoke(contents.cbegin(), contents.cend());
}
toml::Table parse(const std::string& filename)
{
std::ifstream ifs(filename);
if(!ifs.good()) throw std::runtime_error("file open error: " + filename);
return parse(ifs);
}
2017-05-06 09:44:19 +00:00
}// toml
#endif// TOML11_PARSER