diff options
Diffstat (limited to 'colm/fsmrun.cpp')
-rw-r--r-- | colm/fsmrun.cpp | 203 |
1 files changed, 96 insertions, 107 deletions
diff --git a/colm/fsmrun.cpp b/colm/fsmrun.cpp index 133d0048..811899f1 100644 --- a/colm/fsmrun.cpp +++ b/colm/fsmrun.cpp @@ -415,14 +415,58 @@ void send_queued_tokens( FsmRun *fsmRun, PdaRun *parser ) } } -void FsmRun::sendNamedLangEl( PdaRun *parser ) +Kid *make_token( FsmRun *fsmRun, PdaRun *parser, int id, Head *tokdata, bool + namedLangEl, int bindId ) +{ + /* Make the token object. */ + long objectLength = parser->tables->rtd->lelInfo[id].objectLength; + Kid *attrs = alloc_attrs( fsmRun->prg, objectLength ); + + Kid *input = 0; + input = fsmRun->prg->kidPool.allocate(); + input->tree = (Tree*)fsmRun->prg->parseTreePool.allocate(); + input->tree->flags |= AF_PARSE_TREE; + + if ( namedLangEl ) + input->tree->flags |= AF_NAMED; + + input->tree->refs = 1; + input->tree->id = id; + input->tree->tokdata = tokdata; + + /* No children and ignores get added later. */ + input->tree->child = attrs; + + LangElInfo *lelInfo = parser->tables->rtd->lelInfo; + if ( lelInfo[id].numCaptureAttr > 0 ) { + for ( int i = 0; i < lelInfo[id].numCaptureAttr; i++ ) { + CaptureAttr *ca = &parser->tables->rtd->captureAttr[lelInfo[id].captureAttr + i]; + Head *data = string_alloc_new( fsmRun->prg, + fsmRun->mark[ca->mark_enter], fsmRun->mark[ca->mark_leave] + - fsmRun->mark[ca->mark_enter] ); + Tree *string = construct_string( fsmRun->prg, data ); + tree_upref( string ); + set_attr( input->tree, ca->offset, string ); + } + } + + /* If the item is bound then store it in the bindings array. */ + if ( bindId > 0 ) { + parser->bindings.push( input->tree ); + tree_upref( input->tree ); + } + + return input; +} + +void send_named_lang_el( FsmRun *fsmRun, PdaRun *parser ) { /* All three set by getLangEl. */ long bindId; char *data; long length; - KlangEl *klangEl = inputStream->getLangEl( bindId, data, length ); + KlangEl *klangEl = fsmRun->inputStream->getLangEl( bindId, data, length ); if ( klangEl->termDup != 0 ) klangEl = klangEl->termDup; @@ -435,10 +479,10 @@ void FsmRun::sendNamedLangEl( PdaRun *parser ) /* Copy the token data. */ Head *tokdata = 0; if ( data != 0 ) - tokdata = string_alloc_new( prg, data, length ); + tokdata = string_alloc_new( fsmRun->prg, data, length ); - Kid *input = makeToken( parser, klangEl->id, tokdata, true, bindId ); - send_handle_error( this, parser, input ); + Kid *input = make_token( fsmRun, parser, klangEl->id, tokdata, true, bindId ); + send_handle_error( fsmRun, parser, input ); } void execute_generation_action( Program *prg, PdaRun *parser, Code *code, long id, Head *tokdata ) @@ -467,7 +511,8 @@ void execute_generation_action( Program *prg, PdaRun *parser, Code *code, long i * -invoke failure (the backtracker) */ -void FsmRun::generationAction( PdaRun *parser, int id, Head *tokdata, bool namedLangEl, int bindId ) +void generation_action( FsmRun *fsmRun, PdaRun *parser, int id, Head *tokdata, + bool namedLangEl, int bindId ) { #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { @@ -481,55 +526,13 @@ void FsmRun::generationAction( PdaRun *parser, int id, Head *tokdata, bool named parser->tables->rtd->lelInfo[id].frameId].codeWV; /* Execute the action and process the queue. */ - execute_generation_action( prg, parser, code, id, tokdata ); + execute_generation_action( fsmRun->prg, parser, code, id, tokdata ); /* Finished with the match text. */ - string_free( prg, tokdata ); + string_free( fsmRun->prg, tokdata ); /* Send the queued tokens. */ - send_queued_tokens( this, parser ); -} - -Kid *FsmRun::makeToken( PdaRun *parser, int id, Head *tokdata, bool namedLangEl, int bindId ) -{ - /* Make the token object. */ - long objectLength = parser->tables->rtd->lelInfo[id].objectLength; - Kid *attrs = alloc_attrs( prg, objectLength ); - - Kid *input = 0; - input = prg->kidPool.allocate(); - input->tree = (Tree*)prg->parseTreePool.allocate(); - input->tree->flags |= AF_PARSE_TREE; - - if ( namedLangEl ) - input->tree->flags |= AF_NAMED; - - input->tree->refs = 1; - input->tree->id = id; - input->tree->tokdata = tokdata; - - /* No children and ignores get added later. */ - input->tree->child = attrs; - - LangElInfo *lelInfo = parser->tables->rtd->lelInfo; - if ( lelInfo[id].numCaptureAttr > 0 ) { - for ( int i = 0; i < lelInfo[id].numCaptureAttr; i++ ) { - CaptureAttr *ca = &parser->tables->rtd->captureAttr[lelInfo[id].captureAttr + i]; - Head *data = string_alloc_new( prg, - mark[ca->mark_enter], mark[ca->mark_leave] - mark[ca->mark_enter] ); - Tree *string = construct_string( prg, data ); - tree_upref( string ); - set_attr( input->tree, ca->offset, string ); - } - } - - /* If the item is bound then store it in the bindings array. */ - if ( bindId > 0 ) { - parser->bindings.push( input->tree ); - tree_upref( input->tree ); - } - - return input; + send_queued_tokens( fsmRun, parser ); } /* Send back the accumulated ignore tokens. */ @@ -613,7 +616,7 @@ void PdaRun::ignore( Tree *tree ) accumIgnore = ignore; } -void FsmRun::execGen( PdaRun *parser, long id ) +void exec_gen( FsmRun *fsmRun, PdaRun *parser, long id ) { #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { @@ -623,24 +626,24 @@ void FsmRun::execGen( PdaRun *parser, long id ) LangElInfo *lelInfo = parser->tables->rtd->lelInfo; if ( lelInfo[id].markId >= 0 ) - p = mark[lelInfo[id].markId]; + fsmRun->p = fsmRun->mark[lelInfo[id].markId]; /* Make the token data. */ - long length = p - tokstart; - Head *tokdata = string_alloc_const( prg, tokstart, length ); + long length = fsmRun->p - fsmRun->tokstart; + Head *tokdata = string_alloc_const( fsmRun->prg, fsmRun->tokstart, length ); /* Note that we don't update the position now. It is done when the token * data is pulled from the stream. */ - p = tokstart; - tokstart = 0; + fsmRun->p = fsmRun->tokstart; + fsmRun->tokstart = 0; - generationAction( parser, id, tokdata, false, 0 ); + generation_action( fsmRun, parser, id, tokdata, false, 0 ); - memset( mark, 0, sizeof(mark) ); + memset( fsmRun->mark, 0, sizeof(fsmRun->mark) ); } -void FsmRun::sendIgnore( PdaRun *parser, long id ) +void send_ignore( FsmRun *fsmRun, PdaRun *parser, long id ) { #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { @@ -650,15 +653,15 @@ void FsmRun::sendIgnore( PdaRun *parser, long id ) LangElInfo *lelInfo = parser->tables->rtd->lelInfo; if ( lelInfo[id].markId >= 0 ) - p = mark[lelInfo[id].markId]; + fsmRun->p = fsmRun->mark[lelInfo[id].markId]; /* Make the ignore string. */ - int length = p - tokstart; - Head *ignoreStr = string_alloc_const( prg, tokstart, length ); - update_position( this, tokstart, length ); - tokstart = 0; + int length = fsmRun->p - fsmRun->tokstart; + Head *ignoreStr = string_alloc_const( fsmRun->prg, fsmRun->tokstart, length ); + update_position( fsmRun, fsmRun->tokstart, length ); + fsmRun->tokstart = 0; - Tree *tree = prg->treePool.allocate(); + Tree *tree = fsmRun->prg->treePool.allocate(); tree->refs = 1; tree->id = id; tree->tokdata = ignoreStr; @@ -667,13 +670,13 @@ void FsmRun::sendIgnore( PdaRun *parser, long id ) parser->ignore( tree ); /* Prepare for more scanning. */ - region = parser->getNextRegion(); - cs = tables->entryByRegion[region]; + fsmRun->region = parser->getNextRegion(); + fsmRun->cs = fsmRun->tables->entryByRegion[fsmRun->region]; - memset( mark, 0, sizeof(mark) ); + memset( fsmRun->mark, 0, sizeof(fsmRun->mark) ); } -void FsmRun::sendToken( PdaRun *parser, long id ) +void send_token( FsmRun *fsmRun, PdaRun *parser, long id ) { #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { @@ -683,35 +686,21 @@ void FsmRun::sendToken( PdaRun *parser, long id ) LangElInfo *lelInfo = parser->tables->rtd->lelInfo; if ( lelInfo[id].markId >= 0 ) - p = mark[lelInfo[id].markId]; + fsmRun->p = fsmRun->mark[lelInfo[id].markId]; /* Make the token data. */ - long length = p - tokstart; - Head *tokdata = string_alloc_const( prg, tokstart, length ); - update_position( this, tokstart, length ); + long length = fsmRun->p - fsmRun->tokstart; + Head *tokdata = string_alloc_const( fsmRun->prg, fsmRun->tokstart, length ); + update_position( fsmRun, fsmRun->tokstart, length ); /* By default the match is consumed and this is what we need. Just * need to reset tokstart. */ - tokstart = 0; - - Kid *input = makeToken( parser, id, tokdata, false, 0 ); - send_handle_error( this, parser, input ); + fsmRun->tokstart = 0; - memset( mark, 0, sizeof(mark) ); -} + Kid *input = make_token( fsmRun, parser, id, tokdata, false, 0 ); + send_handle_error( fsmRun, parser, input ); -void FsmRun::emitToken( KlangEl *token ) -{ -// bool ctxDepParsing = prg->ctxDepParsing; -// LangElInfo *lelInfo = parser->tables->rtd->lelInfo; -// if ( ctxDepParsing && lelInfo[token->id].frameId >= 0 ) -// execGen( token->id ); -// else if ( token->ignore ) -// sendIgnore( token->id ); -// else -// sendToken( token->id ); - - matchedToken = token->id; + memset( fsmRun->mark, 0, sizeof(fsmRun->mark) ); } /* Load up a token, starting from tokstart if it is set. If not set then @@ -745,7 +734,7 @@ Head *FsmRun::extractToken( PdaRun *parser, long length ) return tokdata; } -void FsmRun::sendEOF( PdaRun *parser ) +void send_eof( FsmRun *fsmRun, PdaRun *parser ) { #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { @@ -753,15 +742,15 @@ void FsmRun::sendEOF( PdaRun *parser ) } #endif - Kid *input = prg->kidPool.allocate(); - input->tree = (Tree*)prg->parseTreePool.allocate(); + Kid *input = fsmRun->prg->kidPool.allocate(); + input->tree = (Tree*)fsmRun->prg->parseTreePool.allocate(); input->tree->flags |= AF_PARSE_TREE; input->tree->refs = 1; input->tree->id = parser->tables->rtd->eofLelIds[parser->parserId]; - bool ctxDepParsing = prg->ctxDepParsing; - long frameId = parser->tables->rtd->regionInfo[region].eofFrameId; + bool ctxDepParsing = fsmRun->prg->ctxDepParsing; + long frameId = parser->tables->rtd->regionInfo[fsmRun->region].eofFrameId; if ( ctxDepParsing && frameId >= 0 ) { #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { @@ -773,10 +762,10 @@ void FsmRun::sendEOF( PdaRun *parser ) Code *code = parser->tables->rtd->frameInfo[frameId].codeWV; /* Execute the action and process the queue. */ - execute_generation_action( prg, parser, code, input->tree->id, 0 ); + execute_generation_action( fsmRun->prg, parser, code, input->tree->id, 0 ); /* Send the generated tokens. */ - send_queued_tokens( this, parser ); + send_queued_tokens( fsmRun, parser ); } parser->send( input ); @@ -786,9 +775,9 @@ void FsmRun::sendEOF( PdaRun *parser ) "parse error" << endp; } - tokstart = 0; - region = parser->getNextRegion(); - cs = tables->entryByRegion[region]; + fsmRun->tokstart = 0; + fsmRun->region = parser->getNextRegion(); + fsmRun->cs = fsmRun->tables->entryByRegion[fsmRun->region]; } @@ -903,7 +892,7 @@ void parse( FsmRun *fsmRun, PdaRun *parser ) /* Check for EOF. */ if ( tokenId == SCAN_EOF ) { fsmRun->eofSent = true; - fsmRun->sendEOF( parser ); + send_eof( fsmRun, parser ); if ( fsmRun->eofSent ) break; continue; @@ -911,7 +900,7 @@ void parse( FsmRun *fsmRun, PdaRun *parser ) /* Check for a named language element. */ if ( tokenId == SCAN_LANG_EL ) { - fsmRun->sendNamedLangEl( parser ); + send_named_lang_el( fsmRun, parser ); continue; } @@ -924,11 +913,11 @@ void parse( FsmRun *fsmRun, PdaRun *parser ) bool ctxDepParsing = fsmRun->prg->ctxDepParsing; LangElInfo *lelInfo = parser->tables->rtd->lelInfo; if ( ctxDepParsing && lelInfo[tokenId].frameId >= 0 ) - fsmRun->execGen( parser, tokenId ); + exec_gen( fsmRun, parser, tokenId ); else if ( lelInfo[tokenId].ignore ) - fsmRun->sendIgnore( parser, tokenId ); + send_ignore( fsmRun, parser, tokenId ); else - fsmRun->sendToken( parser, tokenId ); + send_token( fsmRun, parser, tokenId ); /* Fall through here either when the input buffer has been exhausted * or the scanner is in an error state. Otherwise we must continue. */ |