summaryrefslogtreecommitdiff
path: root/src/fsmexec.cc
blob: 8aa4a0723adf8a22851c7f82c202cb2b415767d3 (plain)
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
/*
 * Copyright 2007-2018 Adrian Thurston <thurston@colm.net>
 *
 * 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 <stdbool.h>

#include <assert.h>

#include "redfsm.h"
#include "compiler.h"

void execAction( struct pda_run *pdaRun, GenAction *genAction )
{
	for ( InlineList::Iter item = *genAction->inlineList; item.lte(); item++ ) {
		switch ( item->type ) {
		case InlineItem::Text:
			assert(false);
			break;
		case InlineItem::LmSetActId:
			pdaRun->act = item->longestMatchPart->longestMatchId;
			break;
		case InlineItem::LmSetTokEnd:
			pdaRun->tokend = pdaRun->tokpref + ( pdaRun->p - pdaRun->start ) + 1;
			break;
		case InlineItem::LmInitTokStart:
			assert(false);
			break;
		case InlineItem::LmInitAct:
			pdaRun->act = 0;
			break;
		case InlineItem::LmSetTokStart:
			pdaRun->tokstart = pdaRun->p;
			break;
		case InlineItem::LmSwitch:
			/* If the switch handles error then we also forced the error state. It
			 * will exist. */
			if ( item->tokenRegion->lmSwitchHandlesError && pdaRun->act == 0 ) {
				pdaRun->fsm_cs = pdaRun->fsm_tables->error_state;
			}
			else {
				for ( TokenInstanceListReg::Iter lmi = item->tokenRegion->tokenInstanceList; 
						lmi.lte(); lmi++ )
				{
					if ( lmi->inLmSelect && pdaRun->act == lmi->longestMatchId )
						pdaRun->matched_token = lmi->tokenDef->tdLangEl->id;
				}
			}
			pdaRun->return_result = true;
			pdaRun->skip_tokpref = true;
			break;
		case InlineItem::LmOnLast:
			pdaRun->p += 1;
			pdaRun->tokend = pdaRun->tokpref + ( pdaRun->p - pdaRun->start );
			pdaRun->matched_token = item->longestMatchPart->tokenDef->tdLangEl->id;
			pdaRun->return_result = true;
			break;
		case InlineItem::LmOnNext:
			pdaRun->tokend = pdaRun->tokpref + ( pdaRun->p - pdaRun->start );
			pdaRun->matched_token = item->longestMatchPart->tokenDef->tdLangEl->id;
			pdaRun->return_result = true;
			break;
		case InlineItem::LmOnLagBehind:
			pdaRun->matched_token = item->longestMatchPart->tokenDef->tdLangEl->id;
			pdaRun->return_result = true;
			pdaRun->skip_tokpref = true;
			break;
		}
	}

	if ( genAction->markType == MarkMark )
		pdaRun->mark[genAction->markId-1] = pdaRun->p;
}

extern "C" void internalFsmExecute( struct pda_run *pdaRun, struct input_impl *inputStream )
{
	int _klen;
	unsigned int _trans;
	const long *_acts;
	unsigned int _nacts;
	const char *_keys;
		
	pdaRun->start = pdaRun->p;

	/* Init the token match to nothing (the sentinal). */
	pdaRun->matched_token = 0;

/*_resume:*/
	if ( pdaRun->fsm_cs == pdaRun->fsm_tables->error_state )
		goto out;

	if ( pdaRun->p == pdaRun->pe )
		goto out;

_loop_head:
	_acts = pdaRun->fsm_tables->actions + pdaRun->fsm_tables->from_state_actions[pdaRun->fsm_cs];
	_nacts = (unsigned int) *_acts++;
	while ( _nacts-- > 0 )
		execAction( pdaRun, pdaRun->fsm_tables->action_switch[*_acts++] );

	_keys = pdaRun->fsm_tables->trans_keys + pdaRun->fsm_tables->key_offsets[pdaRun->fsm_cs];
	_trans = pdaRun->fsm_tables->index_offsets[pdaRun->fsm_cs];

	_klen = pdaRun->fsm_tables->single_lengths[pdaRun->fsm_cs];
	if ( _klen > 0 ) {
		const char *_lower = _keys;
		const char *_mid;
		const char *_upper = _keys + _klen - 1;
		while (1) {
			if ( _upper < _lower )
				break;

			_mid = _lower + ((_upper-_lower) >> 1);
			if ( (*pdaRun->p) < *_mid )
				_upper = _mid - 1;
			else if ( (*pdaRun->p) > *_mid )
				_lower = _mid + 1;
			else {
				_trans += (_mid - _keys);
				goto _match;
			}
		}
		_keys += _klen;
		_trans += _klen;
	}

	_klen = pdaRun->fsm_tables->range_lengths[pdaRun->fsm_cs];
	if ( _klen > 0 ) {
		const char *_lower = _keys;
		const char *_mid;
		const char *_upper = _keys + (_klen<<1) - 2;
		while (1) {
			if ( _upper < _lower )
				break;

			_mid = _lower + (((_upper-_lower) >> 1) & ~1);
			if ( (*pdaRun->p) < _mid[0] )
				_upper = _mid - 2;
			else if ( (*pdaRun->p) > _mid[1] )
				_lower = _mid + 2;
			else {
				_trans += ((_mid - _keys)>>1);
				goto _match;
			}
		}
		_trans += _klen;
	}

_match:
	pdaRun->fsm_cs = pdaRun->fsm_tables->transTargsWI[_trans];

	if ( pdaRun->fsm_tables->transActionsWI[_trans] == 0 )
		goto _again;

	pdaRun->return_result = false;
	pdaRun->skip_tokpref = false;
	_acts = pdaRun->fsm_tables->actions + pdaRun->fsm_tables->transActionsWI[_trans];
	_nacts = (unsigned int) *_acts++;
	while ( _nacts-- > 0 )
		execAction( pdaRun, pdaRun->fsm_tables->action_switch[*_acts++] );
	if ( pdaRun->return_result ) {
		if ( pdaRun->skip_tokpref )
			goto skip_tokpref;
		goto final;
	}

_again:
	_acts = pdaRun->fsm_tables->actions + pdaRun->fsm_tables->to_state_actions[pdaRun->fsm_cs];
	_nacts = (unsigned int) *_acts++;
	while ( _nacts-- > 0 )
		execAction( pdaRun, pdaRun->fsm_tables->action_switch[*_acts++] );

	if ( pdaRun->fsm_cs == pdaRun->fsm_tables->error_state )
		goto out;

	if ( ++pdaRun->p != pdaRun->pe )
		goto _loop_head;
out:
	if ( pdaRun->scan_eof ) {
		pdaRun->return_result = false;
		pdaRun->skip_tokpref = false;
		_acts = pdaRun->fsm_tables->actions + pdaRun->fsm_tables->eof_actions[pdaRun->fsm_cs];
		_nacts = (unsigned int) *_acts++;

		if ( pdaRun->fsm_tables->eof_targs[pdaRun->fsm_cs] >= 0 )
			pdaRun->fsm_cs = pdaRun->fsm_tables->eof_targs[pdaRun->fsm_cs];

		while ( _nacts-- > 0 )
			execAction( pdaRun, pdaRun->fsm_tables->action_switch[*_acts++] );
		if ( pdaRun->return_result ) {
			if ( pdaRun->skip_tokpref )
				goto skip_tokpref;
			goto final;
		}
	}

final:

	if ( pdaRun->p != 0 )
		pdaRun->tokpref += pdaRun->p - pdaRun->start;
skip_tokpref:
	{}
}