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
|
/*
* Copyright 2007-2011 Adrian Thurston <thurston@complang.org>
*/
/* This file is part of Colm.
*
* Colm is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Colm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Colm; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <iostream>
#include "config.h"
#include "fsmrun.h"
#include "redfsm.h"
#include "parsedata.h"
#include "parsetree.h"
#include "pdarun.h"
#include "colm.h"
void execAction( FsmRun *fsmRun, GenAction *genAction )
{
for ( InlineList::Iter item = *genAction->inlineList; item.lte(); item++ ) {
switch ( item->type ) {
case InlineItem::Text:
assert(false);
break;
case InlineItem::LmSetActId:
fsmRun->act = item->longestMatchPart->longestMatchId;
break;
case InlineItem::LmSetTokEnd:
fsmRun->tokend = fsmRun->p + 1;
break;
case InlineItem::LmInitTokStart:
assert(false);
break;
case InlineItem::LmInitAct:
fsmRun->act = 0;
break;
case InlineItem::LmSetTokStart:
fsmRun->tokstart = fsmRun->p;
break;
case InlineItem::LmSwitch:
/* If the switch handles error then we also forced the error state. It
* will exist. */
fsmRun->p = fsmRun->tokend;
if ( item->tokenRegion->lmSwitchHandlesError && fsmRun->act == 0 ) {
fsmRun->p = fsmRun->tokstart;
fsmRun->cs = fsmRun->tables->errorState;
}
else {
for ( TokenDefList::Iter lmi = item->tokenRegion->tokenDefList;
lmi.lte(); lmi++ )
{
if ( lmi->inLmSelect && fsmRun->act == lmi->longestMatchId )
fsmRun->matchedToken = lmi->token->id;
}
}
fsmRun->returnResult = true;
break;
case InlineItem::LmOnLast:
fsmRun->p += 1;
fsmRun->matchedToken = item->longestMatchPart->token->id;
fsmRun->returnResult = true;
break;
case InlineItem::LmOnNext:
fsmRun->matchedToken = item->longestMatchPart->token->id;
fsmRun->returnResult = true;
break;
case InlineItem::LmOnLagBehind:
fsmRun->p = fsmRun->tokend;
fsmRun->matchedToken = item->longestMatchPart->token->id;
fsmRun->returnResult = true;
break;
}
}
if ( genAction->markType == MarkMark )
fsmRun->mark[genAction->markId-1] = fsmRun->p;
}
void fsmExecute( FsmRun *fsmRun, InputStream *inputStream )
{
int _klen;
unsigned int _trans;
const long *_acts;
unsigned int _nacts;
const char *_keys;
/* Init the token match to nothing (the sentinal). */
fsmRun->matchedToken = 0;
/*_resume:*/
if ( fsmRun->cs == fsmRun->tables->errorState )
goto out;
if ( fsmRun->p == fsmRun->pe )
goto out;
_loop_head:
_acts = fsmRun->tables->actions + fsmRun->tables->fromStateActions[fsmRun->cs];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 )
execAction( fsmRun, fsmRun->tables->actionSwitch[*_acts++] );
_keys = fsmRun->tables->transKeys + fsmRun->tables->keyOffsets[fsmRun->cs];
_trans = fsmRun->tables->indexOffsets[fsmRun->cs];
_klen = fsmRun->tables->singleLengths[fsmRun->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 ( (*fsmRun->p) < *_mid )
_upper = _mid - 1;
else if ( (*fsmRun->p) > *_mid )
_lower = _mid + 1;
else {
_trans += (_mid - _keys);
goto _match;
}
}
_keys += _klen;
_trans += _klen;
}
_klen = fsmRun->tables->rangeLengths[fsmRun->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 ( (*fsmRun->p) < _mid[0] )
_upper = _mid - 2;
else if ( (*fsmRun->p) > _mid[1] )
_lower = _mid + 2;
else {
_trans += ((_mid - _keys)>>1);
goto _match;
}
}
_trans += _klen;
}
_match:
fsmRun->cs = fsmRun->tables->transTargsWI[_trans];
if ( fsmRun->tables->transActionsWI[_trans] == 0 )
goto _again;
fsmRun->returnResult = false;
_acts = fsmRun->tables->actions + fsmRun->tables->transActionsWI[_trans];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 )
execAction( fsmRun, fsmRun->tables->actionSwitch[*_acts++] );
if ( fsmRun->returnResult )
return;
_again:
_acts = fsmRun->tables->actions + fsmRun->tables->toStateActions[fsmRun->cs];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 )
execAction( fsmRun, fsmRun->tables->actionSwitch[*_acts++] );
if ( fsmRun->cs == fsmRun->tables->errorState )
goto out;
if ( ++fsmRun->p != fsmRun->pe )
goto _loop_head;
out:
if ( fsmRun->p == fsmRun->peof ) {
fsmRun->returnResult = false;
_acts = fsmRun->tables->actions + fsmRun->tables->eofActions[fsmRun->cs];
_nacts = (unsigned int) *_acts++;
if ( fsmRun->tables->eofTargs[fsmRun->cs] >= 0 )
fsmRun->cs = fsmRun->tables->eofTargs[fsmRun->cs];
while ( _nacts-- > 0 )
execAction( fsmRun, fsmRun->tables->actionSwitch[*_acts++] );
if ( fsmRun->returnResult )
return;
}
}
|