Repository navigation
Expand file tree
/
Copy pathparseConstPactTest.cpp
More file actions
156 lines (117 loc) · 4.83 KB
/
Copy pathparseConstPactTest.cpp
File metadata and controls
156 lines (117 loc) · 4.83 KB
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
#include "parseConstPact.hpp"
/// Only an odd run of backslashes escapes a grammar quote
constexpr bool validateGrammarQuotes()
{
bool success=true;
for(const char delimiter : {'\'', '"'})
for(size_t count=0;count<=6;count++)
{
std::string contents(count,'\\');
if(count%2!=0)
{
contents+=delimiter;
contents+="inside";
}
const std::string input=
std::string(1,delimiter)+contents+delimiter+"tail";
pp::internal::StringMatcher matcher(input);
const auto matched=
matcher.matchLiteralOrRegex(delimiter,true);
if(matched!=contents or matcher.ref!="tail")
success=false;
}
return success;
}
static_assert(validateGrammarQuotes());
/// Block-comment delimiters also scan safely during constant evaluation
constexpr bool validateBlockComments()
{
bool success=true;
for(const char* input : {"/**/tail","/***/tail","/****/tail",
"/* *x **/tail","/* first\nsecond */tail"})
{
pp::internal::StringMatcher matcher(input);
if(not matcher.matchBlockComment() or matcher.ref!="tail")
success=false;
}
for(const char* input : {"","/","*","/x","ordinary"})
{
pp::internal::StringMatcher matcher(input);
if(matcher.matchBlockComment() or matcher.ref!=input)
success=false;
}
return success;
}
static_assert(validateBlockComments());
/// Compile-time regex matcher recognizing one or more 'a' characters
constexpr auto matcher=
pp::createRegexMatcher<"a+">();
static_assert(matcher.match<"aaa">()->matchedString.size()==3);
/// Compile-time matching and tokenization use the same longest-prefix rule
constexpr auto prefixMatcher=
pp::createRegexMatcher<"a","abc","b","x">();
static_assert(prefixMatcher.match<"abx">()->matchedString=="a");
static_assert(prefixMatcher.match<"ab">()->matchedString=="a");
static_assert(prefixMatcher.match<"abcx">()->matchedString=="abc");
static_assert(prefixMatcher.match<"abcx">()->iToken==1);
static_assert(not prefixMatcher.match<"z">());
constexpr auto prefixTokenizer=
pp::createTokenizer<"a","abc","b","x">();
constexpr auto prefixTokens=
prefixTokenizer.tokenize<"abx">();
static_assert(prefixTokens.size()==3);
static_assert(prefixTokens[0].matchedString=="a" and prefixTokens[0].iToken==0);
static_assert(prefixTokens[1].matchedString=="b" and prefixTokens[1].iToken==2);
static_assert(prefixTokens[2].matchedString=="x" and prefixTokens[2].iToken==3);
static_assert(prefixTokenizer.tokenize<"">().empty());
/// Nullable regexes remain usable when every actual token consumes input
constexpr auto nullableTokenizer=
pp::createTokenizer<"a*">();
static_assert(nullableTokenizer.tokenize<"">().empty());
static_assert(nullableTokenizer.tokenize<"aaa">().size()==1);
static_assert(nullableTokenizer.tokenize<"aaa">()[0].matchedString=="aaa");
/// Compile-time tokenizer recognizing runs of 'a' characters and spaces
constexpr auto tokenizer=
pp::createTokenizer<"a+"," +">();
/// Spaces are retained as tokens, so this input produces three entries
constexpr auto tokens=
tokenizer.tokenize<"aa a">();
static_assert(tokens.size()==3);
/// Minimal fixed-size grammar with a named reduction action
/// Namespace scope allows the parse-tree factory to reference it as a template argument
constexpr auto grammar=
pp::createGrammar<"smoke { start: 'a' [value]; }">();
/// Flattened tree: shifted 'a', the 'value' reduction, and the augmented start reduction
constexpr auto tree=
pp::internal::createParseTree<grammar,"a">();
static_assert(tree.size()==3);
static_assert(std::get<pp::internal::ShiftNode>(tree[0]).txt=="a");
static_assert(std::get<pp::internal::ReduceNode>(tree[1]).iAction==0);
/////////////////////////////////////////////////////////////////
/// Checks agreement between runtime and compile-time parse trees
/// Returns 1 for a size mismatch, 2 for shifted text, or 3 for reduction metadata
int main()
{
/// Runtime version of the same grammar and input
const auto runtimeGrammar=
pp::createGrammar("smoke { start: 'a' [value]; }");
const auto runtimeTree=
pp::internal::createParseTree(runtimeGrammar,"a");
if(runtimeTree.size()!=tree.size())
return 1;
if(std::get<pp::internal::ShiftNode>(runtimeTree[0]).txt!=
std::get<pp::internal::ShiftNode>(tree[0]).txt)
return 2;
// Compare both reductions, including the augmented start production.
for(size_t i=1;i<tree.size();i++)
{
const auto& runtimeReduction=
std::get<pp::internal::ReduceNode>(runtimeTree[i]);
const auto& constantReduction=
std::get<pp::internal::ReduceNode>(tree[i]);
if(runtimeReduction.iAction!=constantReduction.iAction or
runtimeReduction.nSubNodes!=constantReduction.nSubNodes)
return 3;
}
return 0;
}