diff options
Diffstat (limited to 'colm/fsmexec.cpp')
-rw-r--r-- | colm/fsmexec.cpp | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/colm/fsmexec.cpp b/colm/fsmexec.cpp new file mode 100644 index 00000000..8f5d7600 --- /dev/null +++ b/colm/fsmexec.cpp @@ -0,0 +1,204 @@ +/* + * Copyright 2007 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 FsmRun::execAction( GenAction *genAction ) +{ + for ( InlineList::Iter item = *genAction->inlineList; item.lte(); item++ ) { + switch ( item->type ) { + case InlineItem::Text: + assert(false); + break; + case InlineItem::LmSetActId: + act = item->longestMatchPart->longestMatchId; + break; + case InlineItem::LmSetTokEnd: + tokend = p + 1; + break; + case InlineItem::LmInitTokStart: + assert(false); + break; + case InlineItem::LmInitAct: + act = 0; + break; + case InlineItem::LmSetTokStart: + tokstart = p; + break; + case InlineItem::LmSwitch: + /* If the switch handles error then we also forced the error state. It + * will exist. */ + p = tokend; + if ( item->tokenRegion->lmSwitchHandlesError && act == 0 ) { + p = tokstart; + cs = tables->errorState; + } + else { + for ( TokenDefList::Iter lmi = item->tokenRegion->tokenDefList; + lmi.lte(); lmi++ ) + { + if ( lmi->inLmSelect && act == lmi->longestMatchId ) + emitToken( lmi->token ); + } + } + gotoResume = true; + break; + case InlineItem::LmOnLast: + p += 1; + emitToken( item->longestMatchPart->token ); + gotoResume = true; + break; + case InlineItem::LmOnNext: + emitToken( item->longestMatchPart->token ); + gotoResume = true; + break; + case InlineItem::LmOnLagBehind: + p = tokend; + emitToken( item->longestMatchPart->token ); + gotoResume = true; + break; + } + } + + if ( genAction->markType == MarkMark ) + mark[genAction->markId] = p; +} + +void FsmRun::execute() +{ + int _klen; + unsigned int _trans; + const long *_acts; + unsigned int _nacts; + const char *_keys; + +_resume: + if ( cs == tables->errorState ) + goto out; + + if ( p == pe ) + goto out; + +_loop_head: + _acts = tables->actions + tables->fromStateActions[cs]; + _nacts = (unsigned int) *_acts++; + while ( _nacts-- > 0 ) + execAction( tables->actionSwitch[*_acts++] ); + + _keys = tables->transKeys + tables->keyOffsets[cs]; + _trans = tables->indexOffsets[cs]; + + _klen = tables->singleLengths[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 ( (*p) < *_mid ) + _upper = _mid - 1; + else if ( (*p) > *_mid ) + _lower = _mid + 1; + else { + _trans += (_mid - _keys); + goto _match; + } + } + _keys += _klen; + _trans += _klen; + } + + _klen = tables->rangeLengths[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 ( (*p) < _mid[0] ) + _upper = _mid - 2; + else if ( (*p) > _mid[1] ) + _lower = _mid + 2; + else { + _trans += ((_mid - _keys)>>1); + goto _match; + } + } + _trans += _klen; + } + +_match: + cs = tables->transTargsWI[_trans]; + + if ( tables->transActionsWI[_trans] == 0 ) + goto _again; + + gotoResume = false; + _acts = tables->actions + tables->transActionsWI[_trans]; + _nacts = (unsigned int) *_acts++; + while ( _nacts-- > 0 ) + execAction( tables->actionSwitch[*_acts++] ); + if ( gotoResume ) + goto _resume; + +_again: + _acts = tables->actions + tables->toStateActions[cs]; + _nacts = (unsigned int) *_acts++; + while ( _nacts-- > 0 ) + execAction( tables->actionSwitch[*_acts++] ); + + if ( cs == tables->errorState ) + goto out; + + if ( ++p != pe ) + goto _loop_head; +out: + if ( p == peof ) { + gotoResume = false; + _acts = tables->actions + tables->eofActions[cs]; + _nacts = (unsigned int) *_acts++; + + if ( tables->eofTargs[cs] >= 0 ) + cs = tables->eofTargs[cs]; + + while ( _nacts-- > 0 ) + execAction( tables->actionSwitch[*_acts++] ); + if ( gotoResume ) + goto _resume; + } +} + + |