Commit 021b01a9 authored by PEGTL Upstream's avatar PEGTL Upstream Committed by Utkarsh Ayachit
Browse files

pegtl 2018-03-10 (078abc42)

Code extracted from:

    https://gitlab.kitware.com/third-party/pegtl.git

at commit 078abc42379905e3cd72cbc4c214c24cb7a7db2e (for/vtk).
parents
# XXX(kitware): Ignore whitespace issues.
* -whitespace
project (pegtl VERSION 2.4.0 LANGUAGES CXX)
# define a header-only library
add_library(vtkpegtl INTERFACE)
vtk_target_export(vtkpegtl)
target_include_directories(vtkpegtl INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${VTK_INSTALL_INCLUDE_DIR}>
)
# features used by the PEGTL
target_compile_features(vtkpegtl INTERFACE
cxx_alias_templates
cxx_auto_type
cxx_constexpr
cxx_decltype
cxx_default_function_template_args
cxx_defaulted_functions
cxx_delegating_constructors
cxx_deleted_functions
cxx_explicit_conversions
cxx_generalized_initializers
cxx_inheriting_constructors
cxx_inline_namespaces
cxx_noexcept
cxx_nonstatic_member_init
cxx_nullptr
cxx_range_for
cxx_rvalue_references
cxx_static_assert
cxx_strong_enums
cxx_template_template_parameters
cxx_trailing_return_types
cxx_uniform_initialization
cxx_variadic_macros
cxx_variadic_templates
)
if (NOT VTK_INSTALL_NO_DEVELOPMENT)
install(
DIRECTORY include/
DESTINATION "${VTK_INSTALL_INCLUDE_DIR}"
COMPONENT Development)
endif()
The MIT License (MIT)
Copyright (c) 2007-2018 Dr. Colin Hirsch and Daniel Frey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# tao/PEGTL fork for VTK
This branch contains changes required to embed PEGTL into VTK.
This includes changes to the build system and the following:
* add .gitattribues
* Integrate VTK's module system (added CMakeLists.vtk.txt)
No mangling is needed since this is header only and not intended
for use in public API in VTK.
## Welcome to the PEGTL
[![Release](https://img.shields.io/github/release/taocpp/PEGTL.svg)](https://github.com/taocpp/PEGTL/releases/latest)
[![TravisCI](https://travis-ci.org/taocpp/PEGTL.svg?branch=master)](https://travis-ci.org/taocpp/PEGTL)
[![AppVeyor](https://ci.appveyor.com/api/projects/status/pa5sbnw68tu650aq/branch/master?svg=true)](https://ci.appveyor.com/project/taocpp/PEGTL)
[![Doozer.io](https://doozer.io/badge/taocpp/PEGTL/buildstatus/master)](https://doozer.io/user/taocpp/PEGTL)
[![Coverage](https://coveralls.io/repos/github/taocpp/PEGTL/badge.svg?branch=master)](https://coveralls.io/github/taocpp/PEGTL)
The Parsing Expression Grammar Template Library (PEGTL) is a zero-dependency C++11 header-only parser combinator library for creating parsers according to a [Parsing Expression Grammar](http://en.wikipedia.org/wiki/Parsing_expression_grammar) (PEG).
### Documentation
* [Version 2.x Documentation](doc/README.md)
* [Version 1.3 Documentation](https://github.com/taocpp/PEGTL/blob/1.3.x/doc/README.md)
### Introduction
Grammars are written as regular C++ code, created with template programming (not template meta programming), i.e. nested template instantiations that naturally correspond to the inductive definition of PEGs (and other parser-combinator approaches).
A comprehensive set of [parser rules](doc/Rule-Reference.md) that can be combined and extended by the user is included, as are mechanisms for debugging grammars, and for attaching user-defined [actions](doc/Actions-and-States.md) to grammar rules.
Here is an example of how a PEG grammar rule is implemented as C++ class with the PEGTL.
```c++
// PEG rule for integers consisting of a non-empty
// sequence of digits with an optional sign:
// integer ::= ( '+' / '-' )? digit+
// The same parsing rule implemented with the PEGTL:
using namespace tao::pegtl;
struct integer : seq< opt< one< '+', '-' > >, plus< digit > > {};
```
PEGs are superficially similar to Context-Free Grammars (CFGs), however the more deterministic nature of PEGs gives rise to some very important differences.
The included [grammar analysis](doc/Grammar-Analysis.md) finds several typical errors in PEGs, including left recursion.
### Design
The PEGTL is designed to be "lean and mean", the core library consists of approximately 5000 lines of code.
Emphasis is on simplicity and efficiency, preferring a well-tuned simple approach over complicated optimisations.
The PEGTL is mostly concerned with parsing combinators and grammar rules, and with giving the user of the library (the possibility of) full control over all other aspects of a parsing run. Whether/which actions are taken, and whether/which data structures are created during a parsing run, is entirely up to the user.
Included are some [examples](doc/Contrib-and-Examples.md#examples) for typical situation like unescaping escape sequences in strings, building a generic [JSON](http://www.json.org/) data structure, and on-the-fly evaluation of arithmetic expressions.
Through the use of template programming and template specialisations it is possible to write a grammar once, and use it in multiple ways with different (semantic) actions in different (or the same) parsing runs.
Unlike [Antlr](http://www.antlr.org/) and Yacc/[Bison](http://www.gnu.org/software/bison/), the grammar is expressed in C++ and is part of the C++ source code.
Also, with the PEG formalism the separation into lexer and parser stages is usually dropped -- everything is done in a single grammar.
Unlike [Spirit](http://boost-spirit.com/), the grammar is implemented with compile-time template instantiations rather than run-time operator calls.
This leads to slightly increased compile times as the C++ compiler is given the task of optimising PEGTL grammars.
### Status
The master branch of the PEGTL is stable in the sense that all known bugs are fixed and all unit tests run without errors.
Each commit is automatically tested with multiple architectures, operating systems, compilers, and versions thereof.
* Windows
* Visual Studio 2015 (x86, x64)
* Visual Studio 2017 (x86, x64)
* MinGW (i686), GCC 5
* MinGW-w64 (i686), GCC 5, 6
* MinGW-w64 (x86_64), GCC 6
* Mac OS X / macOS (using libc++)
* Mac OS X 10.10, Xcode 6.4
* Mac OS X 10.11, Xcode 7.3
* macOS 10.12, Xcode 8.3
* macOS 10.12, Xcode 9.2
* Linux (using libstdc++)
* Debian 8 (i386), GCC 4.9
* Ubuntu 12.04 LTS (amd64), Clang 3.4, 3.7
* Ubuntu 14.04 LTS (amd64), GCC 4.8, 4.9, 5, 6, 7
* Ubuntu 14.04 LTS (amd64), Clang 3.5, 3.6, 3.8, 3.9, 4.0, 5.0
* Ubuntu 14.04 LTS (i386, amd64), GCC 4.8
* Ubuntu 16.04 LTS (i386, amd64, armhf, arm64), GCC 5
* Fedora 24 (x86_64), GCC 6
* Fedora 24 (x86_64), Clang 3.8
* Android
* Android 4.4
* Android 5.1
* Android 6.0
* Android 7.0
Additionally, each commit is checked with GCC's and Clang's sanitizers, as well as [`valgrind`](http://valgrind.org/)
and [`clang-tidy`](http://clang.llvm.org/extra/clang-tidy/). Code coverage is automatically measured and the unit tests
cover 100% of the core library code (for releases).
[Releases](https://github.com/taocpp/PEGTL/releases) are done in accordance with [Semantic Versioning](http://semver.org/).
Incompatible API changes are *only* allowed to occur between major versions.
For details see the [changelog](doc/Changelog.md).
### Thank You
* Christopher Diggins and the YARD parser for the general idea.
* George Makrydakis for the [inspiration](https://github.com/irrequietus/typestring) to `TAO_PEGTL_STRING`.
* Johannes Overmann for his invaluable [`streplace`](https://code.google.com/p/streplace/) command-line tool.
* Jörg-Christian Böhme for improving the Android CI build.
* Kai Wolf for help with CMake.
* Kenneth Geisshirt for Android compatibility and Android CI.
* Kuzma Shapran for EOL testing and fixes.
* Michael Becker for help with CMake.
* Paulo Custodio for Windows-related fixes.
* Sam Hocevar for contributing Visual Studio 2015 compatibility.
* Stephan Beal for the bug reports, suggestions and discussions.
* Sven Johannsen for help with CMake.
* Zhihao Yuan for fixing several warnings when compiling with Visual Studio 2015.
### Contact
For questions and suggestions regarding the PEGTL, success or failure stories, and any other kind of feedback, please feel free to contact the authors at `taocpp(at)icemx.net`.
### License
The PEGTL is certified [Open Source](http://www.opensource.org/docs/definition.html) software. It may be used for any purpose, including commercial purposes, at absolutely no cost. It is distributed under the terms of the [MIT license](http://www.opensource.org/licenses/mit-license.html) reproduced here.
> Copyright (c) 2007-2018 Dr. Colin Hirsch and Daniel Frey
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Copyright (c) 2014-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_HPP
#define TAO_PEGTL_HPP
#include "pegtl/config.hpp"
#include "pegtl/version.hpp"
#include "pegtl/ascii.hpp"
#include "pegtl/parse.hpp"
#include "pegtl/rules.hpp"
#include "pegtl/utf16.hpp"
#include "pegtl/utf32.hpp"
#include "pegtl/utf8.hpp"
#include "pegtl/argv_input.hpp"
#include "pegtl/buffer_input.hpp"
#include "pegtl/cstream_input.hpp"
#include "pegtl/file_input.hpp"
#include "pegtl/istream_input.hpp"
#include "pegtl/memory_input.hpp"
#include "pegtl/read_input.hpp"
#include "pegtl/string_input.hpp"
// The following are not included by
// default because they include <iostream>.
// #include "pegtl/analyze.hpp"
#endif
// Copyright (c) 2014-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_ANALYSIS_ANALYZE_CYCLES_HPP
#define TAO_PEGTL_ANALYSIS_ANALYZE_CYCLES_HPP
#include <cassert>
#include <map>
#include <set>
#include <iostream>
#include <utility>
#include "../config.hpp"
#include "grammar_info.hpp"
#include "insert_guard.hpp"
namespace tao
{
namespace TAO_PEGTL_NAMESPACE
{
namespace analysis
{
class analyze_cycles_impl
{
protected:
explicit analyze_cycles_impl( const bool verbose ) noexcept
: m_verbose( verbose ),
m_problems( 0 )
{
}
const bool m_verbose;
unsigned m_problems;
grammar_info m_info;
std::set< std::string > m_stack;
std::map< std::string, bool > m_cache;
std::map< std::string, bool > m_results;
const std::map< std::string, rule_info >::const_iterator find( const std::string& name ) const noexcept
{
const auto iter = m_info.map.find( name );
assert( iter != m_info.map.end() );
return iter;
}
bool work( const std::map< std::string, rule_info >::const_iterator& start, const bool accum )
{
const auto j = m_cache.find( start->first );
if( j != m_cache.end() ) {
return j->second;
}
if( const auto g = make_insert_guard( m_stack, start->first ) ) {
switch( start->second.type ) {
case rule_type::ANY: {
bool a = false;
for( const auto& r : start->second.rules ) {
a = a || work( find( r ), accum || a );
}
return m_cache[ start->first ] = true;
}
case rule_type::OPT: {
bool a = false;
for( const auto& r : start->second.rules ) {
a = a || work( find( r ), accum || a );
}
return m_cache[ start->first ] = false;
}
case rule_type::SEQ: {
bool a = false;
for( const auto& r : start->second.rules ) {
a = a || work( find( r ), accum || a );
}
return m_cache[ start->first ] = a;
}
case rule_type::SOR: {
bool a = true;
for( const auto& r : start->second.rules ) {
a = a && work( find( r ), accum );
}
return m_cache[ start->first ] = a;
}
}
throw std::runtime_error( "code should be unreachable" ); // NOLINT, LCOV_EXCL_LINE
}
if( !accum ) {
++m_problems;
if( m_verbose ) {
std::cout << "problem: cycle without progress detected at rule class " << start->first << std::endl; // LCOV_EXCL_LINE
}
}
return m_cache[ start->first ] = accum;
}
};
template< typename Grammar >
class analyze_cycles
: private analyze_cycles_impl
{
public:
explicit analyze_cycles( const bool verbose )
: analyze_cycles_impl( verbose )
{
Grammar::analyze_t::template insert< Grammar >( m_info );
}
std::size_t problems()
{
for( auto i = m_info.map.begin(); i != m_info.map.end(); ++i ) {
m_results[ i->first ] = work( i, false );
m_cache.clear();
}
return m_problems;
}
template< typename Rule >
bool consumes() const noexcept
{
const auto i = m_results.find( internal::demangle< Rule >() );
assert( i != m_results.end() );
return i->second;
}
};
} // namespace analysis
} // namespace TAO_PEGTL_NAMESPACE
} // namespace tao
#endif
// Copyright (c) 2014-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_ANALYSIS_COUNTED_HPP
#define TAO_PEGTL_ANALYSIS_COUNTED_HPP
#include "../config.hpp"
#include "generic.hpp"
namespace tao
{
namespace TAO_PEGTL_NAMESPACE
{
namespace analysis
{
template< rule_type Type, unsigned Count, typename... Rules >
struct counted
: generic< ( Count != 0 ) ? Type : rule_type::OPT, Rules... >
{
};
} // namespace analysis
} // namespace TAO_PEGTL_NAMESPACE
} // namespace tao
#endif
// Copyright (c) 2014-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_ANALYSIS_GENERIC_HPP
#define TAO_PEGTL_ANALYSIS_GENERIC_HPP
#include "../config.hpp"
#include "grammar_info.hpp"
#include "insert_rules.hpp"
#include "rule_type.hpp"
namespace tao
{
namespace TAO_PEGTL_NAMESPACE
{
namespace analysis
{
template< rule_type Type, typename... Rules >
struct generic
{
template< typename Name >
static std::string insert( grammar_info& g )
{
const auto r = g.insert< Name >( Type );
if( r.second ) {
insert_rules< Rules... >::insert( g, r.first->second );
}
return r.first->first;
}
};
} // namespace analysis
} // namespace TAO_PEGTL_NAMESPACE
} // namespace tao
#endif
// Copyright (c) 2014-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_ANALYSIS_GRAMMAR_INFO_HPP
#define TAO_PEGTL_ANALYSIS_GRAMMAR_INFO_HPP
#include <map>
#include <string>
#include <utility>
#include "../config.hpp"
#include "../internal/demangle.hpp"
#include "rule_info.hpp"
namespace tao
{
namespace TAO_PEGTL_NAMESPACE
{
namespace analysis
{
struct grammar_info
{
using map_t = std::map< std::string, rule_info >;
map_t map;
template< typename Name >
std::pair< map_t::iterator, bool > insert( const rule_type type )
{
return map.insert( map_t::value_type( internal::demangle< Name >(), rule_info( type ) ) );
}
};
} // namespace analysis
} // namespace TAO_PEGTL_NAMESPACE
} // namespace tao
#endif
// Copyright (c) 2014-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_ANALYSIS_INSERT_GUARD_HPP
#define TAO_PEGTL_ANALYSIS_INSERT_GUARD_HPP
#include <utility>
#include "../config.hpp"
namespace tao
{
namespace TAO_PEGTL_NAMESPACE
{
namespace analysis
{
template< typename C >
class insert_guard
{
public:
insert_guard( C& container, const typename C::value_type& value )
: m_i( container.insert( value ) ),
m_c( &container )
{
}
insert_guard( const insert_guard& ) = delete;
insert_guard( insert_guard&& other ) noexcept
: m_i( other.m_i ),
m_c( other.m_c )
{
other.m_c = nullptr;
}
~insert_guard()
{
if( m_c && m_i.second ) {
m_c->erase( m_i.first );
}
}
void operator=( const insert_guard& ) = delete;
void operator=( insert_guard&& ) = delete;
explicit operator bool() const noexcept
{
return m_i.second;
}
private:
const std::pair< typename C::iterator, bool > m_i;
C* m_c;
};
template< typename C >
insert_guard< C > make_insert_guard( C& container, const typename C::value_type& value )
{
return insert_guard< C >( container, value );
}
} // namespace analysis
} // namespace TAO_PEGTL_NAMESPACE
} // namespace tao
#endif
// Copyright (c) 2014-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_ANALYSIS_INSERT_RULES_HPP
#define TAO_PEGTL_ANALYSIS_INSERT_RULES_HPP
#include "../config.hpp"
#include "grammar_info.hpp"
#include "rule_info.hpp"
namespace tao
{
namespace TAO_PEGTL_NAMESPACE
{
namespace analysis
{
template< typename... >
struct insert_rules;
template<>
struct insert_rules<>
{
static void insert( grammar_info& /*unused*/, rule_info& /*unused*/ )
{
}
};
template< typename Rule, typename... Rules >
struct insert_rules< Rule, Rules... >
{
static void insert( grammar_info& g, rule_info& r )
{
r.rules.push_back( Rule::analyze_t::template insert< Rule >( g ) );
insert_rules< Rules... >::insert( g, r );
}
};
} // namespace analysis
} // namespace TAO_PEGTL_NAMESPACE
} // namespace tao
#endif
// Copyright (c) 2014-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
#ifndef TAO_PEGTL_ANALYSIS_RULE_INFO_HPP
#define TAO_PEGTL_ANALYSIS_RULE_INFO_HPP
#include <string>
#include <vector>
#include "../config.hpp"