-
Notifications
You must be signed in to change notification settings - Fork 0
/
_l_state.h
454 lines (422 loc) · 16.6 KB
/
_l_state.h
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#pragma once
// Copyright David Lawrence Bien 1997 - 2021.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt).
// _l_state.h
// The state and transition objects for the implementation of the lexical analyzer.
// dbien
// 18DEC2020 - moved from _l_lxobj.h - original code from 1999.
#include "_l_ns.h"
#include "_l_types.h"
#include "_l_chrtr.h"
__LEXOBJ_BEGIN_NAMESPACE
template <class t_TyChar>
struct _l_transition
{
typedef typename _l_char_type_map<t_TyChar>::_TyUnsigned _TyUnsignedChar;
_TyUnsignedChar m_first;
_TyUnsignedChar m_last;
_l_state_proto<t_TyChar> *m_psp;
static std::string StaticGetJsonText(_TyUnsignedChar _uc)
{
std::string str;
if ((_uc > 32) && (_uc < 127))
(void)FPrintfStdStrNoThrow(str, "%c (%llu)", (char)_uc, (uint64_t)_uc);
else
(void)FPrintfStdStrNoThrow(str, "%llu", (uint64_t)_uc);
return str;
}
n_SysLog::vtyJsoValueSysLog GetJsonValue() const
{
n_SysLog::vtyJsoValueSysLog jv(ejvtObject);
jv("f").SetStringValue( StaticGetJsonText( m_first ) );
jv("l").SetStringValue( StaticGetJsonText( m_last ) );
#ifdef LXOBJ_STATENUMBERS
jv("n").SetValue( m_psp->m_nState );
#endif //LXOBJ_STATENUMBERS
return (jv);
}
};
// prototype struct - never gets instantiated - only cast to:
// don't ever use this as arg to sizeof().
template <class t_TyChar>
struct _l_state_proto
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
typedef typename _TyAnalyzer::_TyPMFnAccept _TyPMFnAccept;
typedef _l_transition<t_TyChar> _TyTransition;
// Common members, all instantiations of _l_state<> have these.
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState; // For debugging, not used by the lexang.
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_TyTransition m_rgt[7]; // You can access the transitions - i.e. it is ok to access them without using an accessor - 7 is just a random number - more or less.
private: // Variable length structure - use accessors.
_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead; // The associated lookahead action id.
vtyLookaheadVector m_rgValidLookahead[2]; // bit vector for valid associated lookahead actions.
_TyPMFnAccept m_rgpmfnTriggers[7]; // Array of pointers to trigger functions - there can be more or less than 7 - random number.
public:
_TyPMFnAccept PMFnGetAction() const
{
Assert(m_flAccept);
return *(_TyPMFnAccept *)((char *)this + m_usOffsetAccept);
}
vtyActionIdent AIGetLookahead() const
{
typedef _l_state<t_TyChar, 1, true, true, 3, 3> _TyStateAccept1Trans;
return *(vtyActionIdent *)((char *)this + m_usOffsetAccept +
(offsetof(_TyStateAccept1Trans, m_aiLookahead) - offsetof(_TyStateAccept1Trans, m_pmfnAccept)));
}
bool FIsAntiAcceptingState() const
{
return kucAntiAccepting == m_flAccept;
}
vtyLookaheadVector *PBeginValidLookahead() const
{
Assert(m_flAccept == kucLookaheadAccept ||
m_flAccept == kucLookaheadAcceptAndAccept ||
m_flAccept == kucLookaheadAcceptAndLookahead);
typedef _l_state<t_TyChar, 1, true, true, 3, 3> _TyStateAccept1Trans;
return (vtyLookaheadVector *)((char *)this + m_usOffsetAccept +
(offsetof(_TyStateAccept1Trans, m_rgValidLookahead) - offsetof(_TyStateAccept1Trans, m_pmfnAccept)));
}
_TyPMFnAccept *PPMFnGetTriggerBegin() const
{
return (_TyPMFnAccept *)((char *)this + m_usOffsetTriggers);
}
n_SysLog::vtyJsoValueSysLog GetJsonValue( bool _fLookahead ) const
{
n_SysLog::vtyJsoValueSysLog jv(ejvtObject);
#ifdef LXOBJ_STATENUMBERS
jv("State").SetValue(m_nState);
#endif //LXOBJ_STATENUMBERS
if ( m_nt )
{
jv("nTransitions").SetValue(m_nt);
n_SysLog::vtyJsoValueSysLog & jvTransitions = jv("Transitions");
const _TyTransition * ptCur = m_rgt;
const _TyTransition * const ptEnd = ptCur + m_nt;
for( ; ptEnd != ptCur; ++ptCur )
jvTransitions( ptCur - m_rgt ) = ptCur->GetJsonValue();
}
Assert( !m_nTriggers == !m_pspTrigger );
if ( m_nTriggers )
{
jv("nTriggers") = m_nTriggers;
#ifdef LXOBJ_STATENUMBERS
if ( !!m_pspTrigger )
jv("TriggerState") = m_pspTrigger->m_nState;
#endif //LXOBJ_STATENUMBERS
}
if ( _fLookahead )
{
// TODO - later.
}
return jv;
}
};
template <class t_TyChar, int t_iTransitions, int t_iTriggers>
struct _l_state<t_TyChar, t_iTransitions, false, false, 0, t_iTriggers>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_l_transition<t_TyChar> m_rgt[t_iTransitions];
typename _TyAnalyzer::_TyPMFnAccept m_rgpmfnTriggers[t_iTriggers];
};
#ifndef _STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTransitions>
struct _l_state<t_TyChar, t_iTransitions, false, false, 0, 0>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_l_transition<t_TyChar> m_rgt[t_iTransitions];
};
#endif //!_STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTransitions, int t_iTriggers>
struct _l_state<t_TyChar, t_iTransitions, true, false, 0, t_iTriggers>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_l_transition<t_TyChar> m_rgt[t_iTransitions];
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
typename _TyAnalyzer::_TyPMFnAccept m_rgpmfnTriggers[t_iTriggers];
};
#ifndef _STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTransitions>
struct _l_state<t_TyChar, t_iTransitions, true, false, 0, 0>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_l_transition<t_TyChar> m_rgt[t_iTransitions];
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
};
#endif //!_STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTransitions, int t_iTriggers>
struct _l_state<t_TyChar, t_iTransitions, true, true, 0, t_iTriggers>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_l_transition<t_TyChar> m_rgt[t_iTransitions];
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead;
typename _TyAnalyzer::_TyPMFnAccept m_rgpmfnTriggers[t_iTriggers];
};
#ifndef _STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTransitions>
struct _l_state<t_TyChar, t_iTransitions, true, true, 0, 0>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_l_transition<t_TyChar> m_rgt[t_iTransitions];
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead;
};
#endif //!_STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTransitions, int t_iLookaheadVectorEls, int t_iTriggers>
struct _l_state<t_TyChar, t_iTransitions, true, true, t_iLookaheadVectorEls, t_iTriggers>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_l_transition<t_TyChar> m_rgt[t_iTransitions];
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead;
vtyLookaheadVector m_rgValidLookahead[t_iLookaheadVectorEls];
typename _TyAnalyzer::_TyPMFnAccept m_rgpmfnTriggers[t_iTriggers];
};
#ifndef _STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTransitions, int t_iLookaheadVectorEls>
struct _l_state<t_TyChar, t_iTransitions, true, true, t_iLookaheadVectorEls, 0>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
_l_transition<t_TyChar> m_rgt[t_iTransitions];
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead;
vtyLookaheadVector m_rgValidLookahead[t_iLookaheadVectorEls];
};
#endif //!_STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTriggers>
struct _l_state<t_TyChar, 0, false, false, 0, t_iTriggers>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
typename _TyAnalyzer::_TyPMFnAccept m_rgpmfnTriggers[t_iTriggers];
};
#ifndef _STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar>
struct _l_state<t_TyChar, 0, false, false, 0, 0>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
};
#endif //!_STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTriggers>
struct _l_state<t_TyChar, 0, true, false, 0, t_iTriggers>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
typename _TyAnalyzer::_TyPMFnAccept m_rgpmfnTriggers[t_iTriggers];
};
#ifndef _STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar>
struct _l_state<t_TyChar, 0, true, false, 0, 0>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
};
#endif //!_STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iTriggers>
struct _l_state<t_TyChar, 0, true, true, 0, t_iTriggers>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead;
typename _TyAnalyzer::_TyPMFnAccept m_rgpmfnTriggers[t_iTriggers];
};
#ifndef _STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar>
struct _l_state<t_TyChar, 0, true, true, 0, 0>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead;
};
#endif //!_STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iLookaheadVectorEls, int t_iTriggers>
struct _l_state<t_TyChar, 0, true, true, t_iLookaheadVectorEls, t_iTriggers>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead;
vtyLookaheadVector m_rgValidLookahead[t_iLookaheadVectorEls];
typename _TyAnalyzer::_TyPMFnAccept m_rgpmfnTriggers[t_iTriggers];
};
#ifndef _STLP_ZERO_SIZE_ARRAYS
template <class t_TyChar, int t_iLookaheadVectorEls>
struct _l_state<t_TyChar, 0, true, true, t_iLookaheadVectorEls, 0>
{
typedef _l_an_mostbase<t_TyChar> _TyAnalyzer;
#ifdef LXOBJ_STATENUMBERS
vTyStateNumber m_nState;
#endif //LXOBJ_STATENUMBERS
vTyNTransitions m_nt;
vTyNTriggers m_nTriggers;
vTyStateFlags m_flAccept;
_l_state_proto<t_TyChar> *m_pspTrigger; // Transition on trigger.
unsigned short m_usOffsetAccept;
unsigned short m_usOffsetTriggers;
vtyTokenIdent m_tidAccept; // This is the token id of any accept action associated with this state or vktidInvalidIdToken.
typename _TyAnalyzer::_TyPMFnAccept m_pmfnAccept;
vtyActionIdent m_aiLookahead;
vtyLookaheadVector m_rgValidLookahead[t_iLookaheadVectorEls];
};
#endif //!_STLP_ZERO_SIZE_ARRAYS
__LEXOBJ_END_NAMESPACE