-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse_utils.hpp
337 lines (268 loc) · 10.9 KB
/
parse_utils.hpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ifndef BASIC_INT_PARSE_UTIL_H
#define BASIC_INT_PARSE_UTIL_H
#include "runtime.h"
#include "grammar.h"
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
namespace runtime
{
struct ParseArgs
{
const std::string_view str{};
std::string_view::iterator cur{};
const std::string_view::iterator end{};
ParseMode parseMode{};
boost::spirit::x3::ascii::space_type spaceParser{};
ParseArgs( std::string_view str, unsigned offset ):
str{ str }, cur{ str.begin() + offset }, end{ str.end()}
{}
template<class RuntimeT, class ParserT>
auto MakeFullParser( RuntimeT && runtime, ParserT && parser )
{
using boost::spirit::x3::with;
return
with<runtime_tag>( std::ref( runtime ) )[
with<line_begin_tag>( str.begin() )[
with<parse_mode_tag>( std::ref( parseMode ) )[
parser
]
]
];
}
};
template <class ParseFnc>
bool ParseSingle( std::string_view str, unsigned offset, std::string& err, ParseFnc && fnc )
{
using runtime::ParseMode;
bool r = false;
err.clear();
ParseArgs args{ str, offset };
try
{
r = fnc( args );
}
catch( const std::runtime_error& e )
{
err = e.what();
}
if( !r || args.cur != args.end )
{
err = (!r ? "ERROR[" : "UNEXPECTED[") + err + "]";
if( args.cur != args.end )
{
err += " \"";
err.append( args.str.begin(), args.cur );
err += "><";
err.append( args.cur, args.end );
err += "\"";
}
return false;
}
return true;
}
template< class ParserT, class RuntimeT, class ResultT>
bool ParseSequence( std::string_view str, unsigned offset, ParserT&& parser, RuntimeT& runtime, ResultT& result, std::string& err )
{
const auto parseFnc = [&runtime, &parser, &result]( auto &args )
{
SkipStatementRuntime skipRuntime;
auto sequence_separator{ main_pass::sequence_separator_rule() };
auto&& statementParser = args.MakeFullParser( runtime, parser );
auto&& statementSkipper = args.MakeFullParser( skipRuntime, main_pass::statement_rule() );
auto&& elseStatementParser = args.MakeFullParser( runtime, main_pass::else_statement_rule() );
auto&& elseStatementSkipper = args.MakeFullParser( skipRuntime, main_pass::else_statement_rule() );
enum class State
{
ParseStatement,
SkipStatement,
ParseElse,
SkipElse,
SkipHeadSeparator,
SkipTailSeparator
};
using TStateQueue = std::deque<State>;
static constexpr auto normalParseModeToStates = []( ParseMode mode, TStateQueue&stateQueue )
{
switch( mode )
{
case ParseMode::Normal:
//Nothing
break;
case ParseMode::ParseStatementSkipElse:
stateQueue.insert( stateQueue.begin(), {State::ParseStatement, State::SkipElse} );
break;
case ParseMode::SkipStatementParseElse:
stateQueue.insert( stateQueue.begin(), {State::SkipStatement, State::ParseElse} );
break;
case ParseMode::ParseElse:
stateQueue.insert( stateQueue.begin(), {State::ParseElse} );
break;
case ParseMode::SkipElse:
assert(false); //Cannot be reached in the normal mode
[[fallthrough]];
default:
throw std::runtime_error( "Unexpected mode" );
}
};
static constexpr auto skippingParseModeToStates = []( ParseMode mode, TStateQueue& stateQueue )
{
switch( mode )
{
case ParseMode::Normal:
//Nothing
break;
case ParseMode::SkipStatementParseElse:
[[fallthrough]];
case ParseMode::ParseStatementSkipElse:
stateQueue.insert( stateQueue.begin(), {State::SkipStatement, State::SkipElse} );
break;
case ParseMode::SkipElse:
[[fallthrough]];
case ParseMode::ParseElse:
stateQueue.insert( stateQueue.begin(), {State::SkipElse} );
break;
default:
throw std::runtime_error( "Unexpected mode" );
}
};
TStateQueue stateQueue{ State::SkipHeadSeparator, State::ParseStatement, State::SkipTailSeparator };
for(;;)
{
if( stateQueue.empty() )
throw std::runtime_error("State machine logic is broken");
State curState = stateQueue.front();
stateQueue.pop_front();
args.parseMode = ParseMode::Normal;
switch( curState )
{
case State::ParseStatement:
if( !phrase_parse( args.cur, args.end, statementParser, args.spaceParser, result ) )
return false;
if( !runtime.IsExpectedToContinueLineExecution() )
{
args.cur = args.end;
return true;
}
normalParseModeToStates( args.parseMode, stateQueue );
break;
case State::SkipStatement:
if( !phrase_parse(args.cur, args.end, statementSkipper, args.spaceParser) )
return false;
skippingParseModeToStates( args.parseMode, stateQueue );
break;
case State::ParseElse:
if( !phrase_parse( args.cur, args.end, elseStatementParser, args.spaceParser ) )
{
// We have a false condition of IF and no ELSE, it means we need to skip the rest
// of the line: "If several statements occur after the THEN, separated by colons,
// then they will be executed if and only if the expression is true."
// Overall, the line discarding rule looks like this: if we don't have the same
// number of IF's and ELSE's following the false condition in the line we need to
// discard the following statement after ':'. That also includes any nested
// IF THEN ELSE IF ELSE...
runtime.GotoNextLine();
args.cur = args.end;
return true;
}
//Else can have an embedded goto
if( !runtime.IsExpectedToContinueLineExecution() )
{
args.cur = args.end;
return true;
}
stateQueue.insert( stateQueue.begin(), {State::ParseStatement} );
break;
case State::SkipElse:
//ELSE could be optional
if( phrase_parse( args.cur, args.end, elseStatementSkipper, args.spaceParser ) )
stateQueue.insert( stateQueue.begin(), {State::SkipStatement} );
break;
case State::SkipHeadSeparator:
phrase_parse( args.cur, args.end, sequence_separator, args.spaceParser );
break;
case State::SkipTailSeparator:
if( phrase_parse(args.cur, args.end, sequence_separator, args.spaceParser) )
{
assert( stateQueue.empty() );
stateQueue = {State::ParseStatement, State::SkipTailSeparator };
}
else
{
//The separator is not found and it means the sequence is ended
return true;
}
break;
}
}
assert(false);
};
return runtime::ParseSingle( str, offset, err, parseFnc );
}
template<class RangeT, class OutputFncT>
void ListAllArrayElements( const RangeT& dimensions, OutputFncT&& out )
{
std::vector<RangeT::value_type> curIndex;
curIndex.reserve( curIndex.size() );
curIndex.push_back( 0 );
for(;;)
{
if( curIndex.back() > dimensions[curIndex.size() - 1] )
{
curIndex.pop_back();
if( curIndex.empty() )
break;
++curIndex.back();
}
else if( curIndex.size() < dimensions.size() )
{
curIndex.push_back( 0 );
}
else
{
out( curIndex );
++curIndex.back();
}
}
}
template<class GrammarT>
struct TestExecutor
{
TestExecutor( GrammarT&& grammar ) :
grammar{ grammar }
{}
auto operator()( std::string_view str )
{
using runtime::value_t;
value_t res{};
std::string err{};
runtime.ClearProgram();
runtime.AddLine( 100, str );
runtime.Start();
for( ;; )
{
const auto [pStr, lineNum, offset] = runtime.GetNextLine();
if( !pStr )
break;
if( !ParseSequence( *pStr, offset, grammar, runtime, res, err ) )
return value_t{ std::move( err ) };
}
auto&& strOut = runtime.GetOutput();
return strOut.empty() || res != value_t{} ? res : value_t{ strOut };
}
const GrammarT grammar;
TestRuntime runtime;
};
template<class GrammarT>
struct TestExecutorClear : TestExecutor<GrammarT>
{
using TBase = TestExecutor<GrammarT>;
using TBase::TestExecutor;
using TBase::runtime;
auto operator()( std::string_view str )
{
runtime.Clear();
return TBase::operator()( str );
}
};
}
#endif // BASIC_INT_PARSE_UTIL_H