/usr/include/pegtl/contrib/raw_string.hh is in pegtl-dev 1.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | // Copyright (c) 2014-2015 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/ColinH/PEGTL/
#ifndef PEGTL_CONTRIB_RAW_STRING_HH
#define PEGTL_CONTRIB_RAW_STRING_HH
#include "../apply_mode.hh"
#include "../nothing.hh"
#include "../internal/must.hh"
#include "../internal/until.hh"
#include "../internal/state.hh"
#include "../internal/skip_control.hh"
#include "../analysis/generic.hh"
namespace pegtl
{
namespace internal
{
template< char Open, char Intermediate, char Close >
struct raw_string_tag
{ };
template< typename Tag >
struct raw_string_state
{
template< typename Input, typename ... States >
raw_string_state( const Input & in, States && ... )
: line( in.line() ),
column( in.column() ),
size( in.size() )
{ }
template< apply_mode A, template< typename ... > class Action, template< typename ... > class Control, typename Input, typename ... States >
typename std::enable_if< ( ( A == apply_mode::ACTION ) && ( ! is_nothing< Action, Tag >::value ) ) >::type
success( const Input & in, States && ... st ) const
{
Input content( line, column, in.begin() - ( size - in.size() ), in.begin() - count, in.source() );
const bool skip = ( content.peek_char( count ) == '\n' );
content.bump( count + skip );
Action< Tag >::apply( const_cast< const Input & >( content ), st ... );
}
template< apply_mode A, template< typename ... > class Action, template< typename ... > class Control, typename Input, typename ... States >
typename std::enable_if< ! ( ( A == apply_mode::ACTION ) && ( ! is_nothing< Action, Tag >::value ) ) >::type
success( const Input &, States && ... ) const
{ }
raw_string_state( const raw_string_state & ) = delete;
void operator= ( const raw_string_state & ) = delete;
std::size_t line;
std::size_t column;
std::size_t size;
std::size_t count = 0;
};
template< typename Tag, char Open, char Intermediate >
struct raw_string_open
{
using analyze_t = analysis::generic< analysis::rule_type::ANY >;
template< apply_mode A, template< typename ... > class Action, template< typename ... > class Control, typename Input >
static bool match( Input & in, raw_string_state< Tag > & ls )
{
if ( in.empty() || ( in.peek_char( 0 ) != Open ) ) {
return false;
}
for ( std::size_t i = 1; i < in.size(); ++i ) {
switch ( const auto c = in.peek_char( i ) ) {
case Open:
ls.count = i + 1;
in.bump( ls.count );
return true;
case Intermediate:
break;
default:
return false;
}
}
return false;
}
};
template< typename Tag, char Intermediate, char Close >
struct raw_string_close
{
using analyze_t = analysis::generic< analysis::rule_type::ANY >;
template< apply_mode A, template< typename ... > class Action, template< typename ... > class Control, typename Input >
static bool match( Input & in, const raw_string_state< Tag > & ls )
{
if ( in.size() < ls.count ) {
return false;
}
if ( in.peek_char( 0 ) != Close ) {
return false;
}
if ( in.peek_char( ls.count - 1 ) != Close ) {
return false;
}
for ( std::size_t i = 0; i < ls.count - 2; ++i ) {
if ( in.peek_char( i + 1 ) != Intermediate ) {
return false;
}
}
in.bump( ls.count );
return true;
}
};
template< typename Tag, char Open, char Intermediate >
struct skip_control< raw_string_open< Tag, Open, Intermediate > > : std::true_type {};
template< typename Tag, char Intermediate, char Close >
struct skip_control< raw_string_close< Tag, Intermediate, Close > > : std::true_type {};
} // internal
// raw_string matches Lua-style long literals.
//
// The following description was taken from the Lua documentation
// (see http://www.lua.org/docs.html):
//
// - An "opening long bracket of level n" is defined as an opening square
// bracket followed by n equal signs followed by another opening square
// bracket. So, an opening long bracket of level 0 is written as `[[`,
// an opening long bracket of level 1 is written as `[=[`, and so on.
// - A "closing long bracket" is defined similarly; for instance, a closing
// long bracket of level 4 is written as `]====]`.
// - A "long literal" starts with an opening long bracket of any level and
// ends at the first closing long bracket of the same level. It can
// contain any text except a closing bracket of the same level.
// - Literals in this bracketed form can run for several lines, do not
// interpret any escape sequences, and ignore long brackets of any other
// level.
// - For convenience, when the opening long bracket is immediately followed
// by a newline, the newline is not included in the string.
//
// Note that unlike Lua's long literal, a raw_string is customizable to use
// other characters than `[`, `=` and `]` for matching. Also note that Lua
// introduced newline-specific replacements in Lua 5.2, which we do not
// support on the grammar level.
template< char Open, char Intermediate, char Close, typename Tag = internal::raw_string_tag< Open, Intermediate, Close > >
struct raw_string : state< internal::raw_string_state< Tag >,
internal::raw_string_open< Tag, Open, Intermediate >,
internal::must< internal::until< internal::raw_string_close< Tag, Intermediate, Close > > > >
{
// This is used to bind an action to the content
using content = Tag;
// This is used for error-reporting when a raw string is not closed properly
using close = internal::until< internal::raw_string_close< Tag, Intermediate, Close > >;
};
} // pegtl
#endif
|