-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlex.cpp
97 lines (86 loc) · 4.12 KB
/
lex.cpp
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
// MIT License
//
// Copyright (c) 2020 PG1003
//
// 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.
#include "lex.h"
#include <cctype>
template struct pg::lex::basic_match_result< char >;
template struct pg::lex::basic_match_result< wchar_t >;
#if defined( __cpp_lib_char8_t )
template struct pg::lex::basic_match_result< char8_t >;
#endif
template struct pg::lex::basic_match_result< char16_t >;
template struct pg::lex::basic_match_result< char32_t >;
template struct pg::lex::detail::string_context< char >;
template struct pg::lex::detail::string_context< wchar_t >;
#if defined( __cpp_lib_char8_t )
template struct pg::lex::detail::string_context< char8_t >;
#endif
template struct pg::lex::detail::string_context< char16_t >;
template struct pg::lex::detail::string_context< char32_t >;
template struct pg::lex::pattern< char >;
template struct pg::lex::pattern< wchar_t >;
#if defined( __cpp_lib_char8_t )
template struct pg::lex::pattern< char8_t >;
#endif
template struct pg::lex::pattern< char16_t >;
template struct pg::lex::pattern< char32_t >;
static const char * lex_error_text( pg::lex::error_type code ) noexcept
{
switch( code )
{
case pg::lex::pattern_too_complex: return "pattern too complex";
case pg::lex::pattern_ends_with_percent: return "malformed pattern (ends with '%%')";
case pg::lex::pattern_missing_closing_bracket: return "malformed pattern (missing ']')";
case pg::lex::balanced_no_arguments: return "malformed pattern (missing arguments to '%b')";
case pg::lex::frontier_no_open_bracket: return "missing '[' after '%%f' in pattern";
case pg::lex::capture_too_many: return "too many captures";
case pg::lex::capture_invalid_pattern: return "invalid pattern capture";
case pg::lex::capture_invalid_index: return "invalid capture index";
case pg::lex::capture_not_finished: return "unfinished capture";
case pg::lex::capture_out_of_range: return "capture out of range";
case pg::lex::percent_invalid_use_in_replacement: return "invalid use of '%%' in replacement string";
default: return "lex error";
}
}
pg::lex::lex_error::lex_error( pg::lex::error_type ec ) noexcept
: runtime_error( lex_error_text( ec ) )
, error_code( ec )
{}
bool pg::lex::detail::match_class( int c, int cl ) noexcept
{
bool res;
switch( std::tolower( cl ) )
{
case 'a' : res = std::isalpha( c ); break;
case 'c' : res = std::iscntrl( c ); break;
case 'd' : res = std::isdigit( c ); break;
case 'g' : res = std::isgraph( c ); break;
case 'l' : res = std::islower( c ); break;
case 'p' : res = std::ispunct( c ); break;
case 's' : res = std::isspace( c ); break;
case 'u' : res = std::isupper( c ); break;
case 'w' : res = std::isalnum( c ); break;
case 'x' : res = std::isxdigit( c ); break;
case 'z' : res = (c == 0); break; // Deprecated option
default: return cl == c;
}
return std::islower( cl ) ? res : !res;
}