refactored parser

Mainly to simplify a the error handling code (it had gotten a bit verbose), but also to reduce compiled binary sizes.

also:
- moved windows terminal code page stuff in examples to a separate file
This commit is contained in:
Mark Gillard 2020-04-11 19:43:38 +03:00
parent f3990256ce
commit 42af364887
11 changed files with 1311 additions and 2396 deletions

View File

@ -1,17 +1,23 @@
// This file is a part of toml++ and is subject to the the terms of the MIT license.
// Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT
/*
This example demonstrates how to use the toml::json_formatter to
re-serialize TOML data as JSON.
*/
#include <iostream>
#include <fstream>
#include "utf8_console.h"
#define TOML_UNRELEASED_FEATURES 1
#include <toml++/toml.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
using namespace std::string_view_literals;
int main(int argc, char** argv)
{
#ifdef _WIN32
SetConsoleOutputCP(65001); //UTF-8 console output
#endif
init_utf8_console();
auto path = std::string{ argc > 1 ? argv[1] : "example.toml" };
auto file = std::ifstream{ path };
@ -27,7 +33,7 @@ int main(int argc, char** argv)
}
catch (const toml::parse_error& err)
{
std::cerr << "Error parsing file:\n"sv << err << std::endl;
std::cerr << err << std::endl;
return 1;
}
return 0;

View File

@ -1,19 +1,25 @@
// This file is a part of toml++ and is subject to the the terms of the MIT license.
// Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT
/*
This example demonstrates how to parse TOML from a file and
re-serialize it (print it out) to stdout.
*/
#include <iostream>
#include <fstream>
#include "utf8_console.h"
#define TOML_UNRELEASED_FEATURES 1
#include <toml++/toml.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
using namespace std::string_view_literals;
int main(int argc, char** argv)
{
#ifdef _WIN32
SetConsoleOutputCP(65001); //UTF-8 console output
#endif
init_utf8_console();
//read from a file
// read from a file if a path argument is given
if (argc > 1)
{
auto path = std::string{ argv[1] };
@ -31,23 +37,23 @@ int main(int argc, char** argv)
}
catch (const toml::parse_error& err)
{
std::cerr << "Error parsing file:\n"sv << err << std::endl;
std::cerr << err << std::endl;
return 1;
}
}
//read directly from stdin
// otherwise read directly from stdin
else
{
try
{
const auto table = toml::parse(std::cin);
const auto table = toml::parse(std::cin, "stdin"sv);
std::cout << toml::json_formatter{ table } << std::endl;
}
catch (const toml::parse_error& err)
{
std::cerr << "Error parsing stdin:\n"sv << err << std::endl;
std::cerr << err << std::endl;
return 1;
}

63
examples/utf8_console.h Normal file
View File

@ -0,0 +1,63 @@
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
//# Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#define NOATOM // - Atom Manager routines
#define NOBITMAP
#define NOCLIPBOARD // - Clipboard routines
#define NOCOLOR // - Screen colors
#define NOCOMM // - COMM driver routines
#define NOCTLMGR // - Control and Dialog routines
#define NODEFERWINDOWPOS // - DeferWindowPos routines
#define NODRAWTEXT // - DrawText() and DT_*
#define NOGDI // - All GDI defines and routines
#define NOGDICAPMASKS // - CC_*, LC_*, PC_*, CP_*, TC_*, RC_
#define NOHELP // - Help engine interface.
#define NOICONS // - IDI_*
#define NOKANJI // - Kanji support stuff.
#define NOKEYSTATES // - MK_*
#define NOKERNEL // - All KERNEL defines and routines
#define NOMB // - MB_* and MessageBox()
#define NOMCX // - Modem Configuration Extensions
#define NOMENUS // - MF_*
#define NOMEMMGR // - GMEM_*, LMEM_*, GHND, LHND, associated routines
#define NOMETAFILE // - typedef METAFILEPICT
#define NOMINMAX // - Macros min(a,b) and max(a,b)
#define NOMSG // - typedef MSG and associated routines
#define NONLS // - All NLS defines and routines
#define NOOPENFILE // - OpenFile(), OemToAnsi, AnsiToOem, and OF_*
#define NOPROFILER // - Profiler interface.
#define NORASTEROPS // - Binary and Tertiary raster ops
#define NOSCROLL // - SB_* and scrolling routines
#define NOSERVICE // - All Service Controller routines, SERVICE_ equates, etc.
#define NOSHOWWINDOW // - SW_*
#define NOSOUND // - Sound driver routines
#define NOSYSCOMMANDS // - SC_*
#define NOSYSMETRICS // - SM_*
#define NOTEXTMETRIC // - typedef TEXTMETRIC and associated routines
#define NOUSER // - All USER defines and routines
#define NOVIRTUALKEYCODES // - VK_*
#define NOWH // - SetWindowsHook and WH_*
#define NOWINOFFSETS // - GWL_*, GCL_*, associated routines
#define NOWINMESSAGES // - WM_*, EM_*, LB_*, CB_*
#define NOWINSTYLES // - WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
#include <Windows.h>
inline void init_utf8_console() noexcept
{
SetConsoleOutputCP(65001); //CP_UTF8
}
#else
inline void init_utf8_console() noexcept
{
// no-op
}
#endif

View File

@ -58,7 +58,6 @@
#undef TOML_DISABLE_ALL_WARNINGS
#undef TOML_POP_WARNINGS
#undef TOML_ALWAYS_INLINE
#undef TOML_NEVER_INLINE
#undef TOML_ASSUME
#undef TOML_UNREACHABLE
#undef TOML_INTERFACE

File diff suppressed because it is too large Load Diff

View File

@ -77,16 +77,12 @@
#define TOML_EMPTY_BASES __declspec(empty_bases)
#endif
#define TOML_ALWAYS_INLINE __forceinline
#define TOML_NEVER_INLINE __declspec(noinline)
#endif
#endif
#ifdef __has_attribute
#if !defined(TOML_ALWAYS_INLINE) && __has_attribute(always_inline)
#define TOML_ALWAYS_INLINE __attribute__((__always_inline__)) inline
#endif
#if !defined(TOML_NEVER_INLINE) && __has_attribute(noinline)
#define TOML_NEVER_INLINE __attribute__((__noinline__))
#endif
#if !defined(TOML_TRIVIAL_ABI) && __has_attribute(trivial_abi)
#define TOML_TRIVIAL_ABI __attribute__((__trivial_abi__))
#endif
@ -111,7 +107,6 @@
__pragma(warning(push, 0))
#define TOML_POP_WARNINGS __pragma(warning(pop))
#define TOML_ALWAYS_INLINE __forceinline
#define TOML_NEVER_INLINE __declspec(noinline)
#define TOML_ASSUME(cond) __assume(cond)
#define TOML_UNREACHABLE __assume(0)
#define TOML_INTERFACE __declspec(novtable)
@ -139,7 +134,6 @@
#define TOML_POP_WARNINGS _Pragma("GCC diagnostic pop")
#define TOML_GNU_ATTR(...) __attribute__((__VA_ARGS__))
#define TOML_ALWAYS_INLINE __attribute__((__always_inline__)) inline
#define TOML_NEVER_INLINE __attribute__((__noinline__))
#define TOML_UNREACHABLE __builtin_unreachable()
#if !defined(TOML_RELOPS_REORDERING) && defined(__cpp_impl_three_way_comparison)
#define TOML_RELOPS_REORDERING 1
@ -248,10 +242,6 @@
#define TOML_ALWAYS_INLINE inline
#endif
#ifndef TOML_NEVER_INLINE
#define TOML_NEVER_INLINE
#endif
#ifndef TOML_ASSUME
#define TOML_ASSUME(cond) (void)0
#endif

View File

@ -12,7 +12,6 @@ namespace toml::impl
template <typename... T>
[[nodiscard]]
TOML_GNU_ATTR(const)
TOML_ALWAYS_INLINE
constexpr bool is_match(char32_t codepoint, T... vals) noexcept
{
static_assert((std::is_same_v<char32_t, T> && ...));
@ -156,7 +155,7 @@ namespace toml::impl
[[nodiscard]]
TOML_GNU_ATTR(const)
constexpr bool is_bare_key_start_character(char32_t codepoint) noexcept
constexpr bool is_bare_key_character(char32_t codepoint) noexcept
{
return is_ascii_letter(codepoint)
|| is_decimal_digit(codepoint)
@ -166,16 +165,6 @@ namespace toml::impl
|| codepoint == U'+'
|| is_unicode_letter(codepoint)
|| is_unicode_number(codepoint)
#endif
;
}
[[nodiscard]]
TOML_GNU_ATTR(const)
constexpr bool is_bare_key_character(char32_t codepoint) noexcept
{
return is_bare_key_start_character(codepoint)
#if TOML_LANG_UNRELEASED // toml/issues/687 (unicode bare keys)
|| is_unicode_combining_mark(codepoint)
#endif
;

View File

@ -130,7 +130,8 @@ namespace toml::impl
source_position position;
template <typename Char = string_char>
[[nodiscard]] TOML_ALWAYS_INLINE
[[nodiscard]]
TOML_ALWAYS_INLINE
std::basic_string_view<Char> as_view() const noexcept
{
static_assert(
@ -143,13 +144,17 @@ namespace toml::impl
: std::basic_string_view<Char>{ reinterpret_cast<const Char*>(bytes) };
}
[[nodiscard]] TOML_ALWAYS_INLINE
[[nodiscard]]
TOML_GNU_ATTR(pure)
TOML_ALWAYS_INLINE
constexpr operator char32_t& () noexcept
{
return value;
}
[[nodiscard]] TOML_ALWAYS_INLINE
[[nodiscard]]
TOML_GNU_ATTR(pure)
TOML_ALWAYS_INLINE
constexpr operator const char32_t& () const noexcept
{
return value;

View File

@ -7,7 +7,7 @@
#define TOML_LIB_MAJOR 1
#define TOML_LIB_MINOR 2
#define TOML_LIB_PATCH 2
#define TOML_LIB_PATCH 3
#define TOML_LANG_MAJOR 1
#define TOML_LANG_MINOR 0

View File

@ -1,7 +1,7 @@
project(
'tomlplusplus',
'cpp',
version : '1.2.2',
version : '1.2.3',
license : 'MIT',
default_options : [
'cpp_std=c++17',
@ -32,9 +32,7 @@ if build_tests or build_examples
'-Wno-unused-command-line-argument',
'-march=native',
'-fno-rtti',
#'-fdata-sections',
#'-ffunction-sections',
#'-Wl,--gc-sections'
[ '-fdata-sections', '-ffunction-sections', '-Wl,--gc-sections' ]
],
language : 'cpp'
)
@ -44,7 +42,8 @@ if build_tests or build_examples
if compiler.get_id() == 'gcc'
add_project_arguments([
'-fmax-errors=5',
'-Wno-init-list-lifetime'
'-Wno-init-list-lifetime',
'-fmerge-constants'
],
language : 'cpp'
)
@ -54,6 +53,7 @@ if build_tests or build_examples
add_project_arguments([
'-ferror-limit=5',
'-fchar8_t',
'-fmerge-all-constants',
#'-Weverything',
'-Wno-c++98-compat',
'-Wno-c++98-compat-pedantic',
@ -62,7 +62,6 @@ if build_tests or build_examples
'-Wno-documentation',
'-Wno-documentation-unknown-command',
'-Wno-padded',
#'-Wno-weak-vtables',
'-Wno-double-promotion',
'-Wno-covered-switch-default',
#'-ftime-trace'

1789
toml.hpp

File diff suppressed because it is too large Load Diff