diff options
author | Adrian Thurston <thurston@complang.org> | 2010-04-17 16:55:13 +0000 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2010-04-17 16:55:13 +0000 |
commit | bf1f5ff67d099e5f8b5899714d95a6a23f0cfc2b (patch) | |
tree | 2971ebb5b379ce9ab389f9110acf130fc539ae9c | |
parent | 693a120e20993161c56c9f91434ecf408909bd02 (diff) | |
download | colm-bf1f5ff67d099e5f8b5899714d95a6a23f0cfc2b.tar.gz |
More C porting.
-rw-r--r-- | colm/bytecode.cpp | 2 | ||||
-rw-r--r-- | colm/bytecode.h | 7 | ||||
-rw-r--r-- | colm/fsmrun.cpp | 8 | ||||
-rw-r--r-- | colm/parsedata.cpp | 12 | ||||
-rw-r--r-- | colm/pdarun.cpp | 49 | ||||
-rw-r--r-- | colm/pdarun.h | 57 | ||||
-rw-r--r-- | colm/pdarun2.h | 1 | ||||
-rw-r--r-- | colm/tree.cpp | 8 |
8 files changed, 65 insertions, 79 deletions
diff --git a/colm/bytecode.cpp b/colm/bytecode.cpp index 267937a0..da04fe68 100644 --- a/colm/bytecode.cpp +++ b/colm/bytecode.cpp @@ -309,7 +309,7 @@ void undoParseStream( Tree **&sp, Program *prg, Stream *input, Accum *accum, lon accum->pdaRun->targetConsumed = -1; accum->pdaRun->numRetry -= 1; - accum->fsmRun->region = accum->pdaRun->getNextRegion(); + accum->fsmRun->region = pdaRunGetNextRegion( accum->pdaRun, 0 ); accum->fsmRun->cs = accum->fsmRun->tables->entryByRegion[accum->fsmRun->region]; } } diff --git a/colm/bytecode.h b/colm/bytecode.h index 393e6bed..632a056d 100644 --- a/colm/bytecode.h +++ b/colm/bytecode.h @@ -411,13 +411,6 @@ void setAttr( Tree *tree, long pos, Tree *val ); Tree *getAttr( Tree *tree, long pos ); Kid *getAttrKid( Tree *tree, long pos ); -/* Return the size of a type in words. */ -template<class T> int sizeof_in_words() -{ - assert( (sizeof(T) % sizeof(Word)) == 0 ); - return sizeof(T) / sizeof(Word); -} - /* * Code Vector */ diff --git a/colm/fsmrun.cpp b/colm/fsmrun.cpp index e3d73ef7..0a2c789a 100644 --- a/colm/fsmrun.cpp +++ b/colm/fsmrun.cpp @@ -657,7 +657,7 @@ void sendHandleError( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *in parseError( inputStream, fsmRun, pdaRun, id, input->tree ) << "parse error" << endp; } else { - if ( pdaRun->isParserStopFinished() ) { + if ( isParserStopFinished( pdaRun ) ) { #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { cerr << "stopping the parse" << endl; @@ -774,7 +774,7 @@ void sendEof( Tree **sp, InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRu input->tree->id = pdaRun->tables->rtd->eofLelIds[pdaRun->parserId]; /* Set the state using the state of the parser. */ - fsmRun->region = pdaRun->getNextRegion(); + fsmRun->region = pdaRunGetNextRegion( pdaRun, 0 ); fsmRun->cs = fsmRun->tables->entryByRegion[fsmRun->region]; bool ctxDepParsing = fsmRun->prg->ctxDepParsing; @@ -822,7 +822,7 @@ void newToken( PdaRun *pdaRun, FsmRun *fsmRun ) fsmRun->tokstart = 0; /* Set the state using the state of the parser. */ - fsmRun->region = pdaRun->getNextRegion(); + fsmRun->region = pdaRunGetNextRegion( pdaRun, 0 ); fsmRun->cs = fsmRun->tables->entryByRegion[fsmRun->region]; #ifdef COLM_LOG_PARSE @@ -1009,7 +1009,7 @@ long scanToken( PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputStream ) void scannerError( Tree **sp, InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRun ) { - if ( pdaRun->getNextRegion( 1 ) != 0 ) { + if ( pdaRunGetNextRegion( pdaRun, 1 ) != 0 ) { #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { cerr << "scanner failed, trying next region" << endl; diff --git a/colm/parsedata.cpp b/colm/parsedata.cpp index fcce1c6d..805f6c9c 100644 --- a/colm/parsedata.cpp +++ b/colm/parsedata.cpp @@ -1487,10 +1487,8 @@ void ParseData::parsePatterns() initInputStream( in ); FsmRun *fsmRun = new FsmRun; - repl->pdaRun = new PdaRun( &prg, - pdaTables, fsmRun, repl->langEl->parserId, 0, false ); - - initPdaRun( repl->pdaRun, 0 ); + repl->pdaRun = new PdaRun; + initPdaRun( repl->pdaRun, &prg, pdaTables, fsmRun, repl->langEl->parserId, 0, false, 0 ); initFsmRun( fsmRun, &prg ); newToken( repl->pdaRun, fsmRun ); parseLoop( root, repl->pdaRun, fsmRun, in ); @@ -1507,10 +1505,8 @@ void ParseData::parsePatterns() initInputStream( in ); FsmRun *fsmRun = new FsmRun; - pat->pdaRun = new PdaRun( &prg, - pdaTables, fsmRun, pat->langEl->parserId, 0, false ); - - initPdaRun( pat->pdaRun, 0 ); + pat->pdaRun = new PdaRun; + initPdaRun( pat->pdaRun, &prg, pdaTables, fsmRun, pat->langEl->parserId, 0, false, 0 ); initFsmRun( fsmRun, &prg ); newToken( pat->pdaRun, fsmRun ); parseLoop( root, pat->pdaRun, fsmRun, in ); diff --git a/colm/pdarun.cpp b/colm/pdarun.cpp index 7acc6f77..b042d02e 100644 --- a/colm/pdarun.cpp +++ b/colm/pdarun.cpp @@ -57,6 +57,11 @@ using std::endl; i = (Tree*)w; \ } while(0) +/* Offset can be used to look at the next nextRegionInd. */ +int pdaRunGetNextRegion( PdaRun *pdaRun, int offset = 0 ) +{ + return pdaRun->tables->tokenRegions[pdaRun->nextRegionInd+offset]; +} Tree *getParsedRoot( PdaRun *pdaRun, bool stop ) { @@ -77,17 +82,27 @@ void cleanParser( Tree **sp, PdaRun *pdaRun ) // pdaRun->clearContext( sp ); } -bool PdaRun::isParserStopFinished() +bool isParserStopFinished( PdaRun *pdaRun ) { bool done = - stackTop->next != 0 && - stackTop->next->next == 0 && - stackTop->tree->id == stopTarget; + pdaRun->stackTop->next != 0 && + pdaRun->stackTop->next->next == 0 && + pdaRun->stackTop->tree->id == pdaRun->stopTarget; return done; } -void initPdaRun( PdaRun *pdaRun, Tree *context ) +void initPdaRun( PdaRun *pdaRun, Program *prg, PdaTables *tables, + FsmRun *fsmRun, int parserId, long stopTarget, bool revertOn, Tree *context ) { + memset( pdaRun, 0, sizeof(PdaRun) ); + pdaRun->prg = prg; + pdaRun->tables = tables; + pdaRun->fsmRun = fsmRun; + pdaRun->parserId = parserId; + pdaRun->stopTarget = stopTarget; + pdaRun->revertOn = revertOn; + pdaRun->targetConsumed = -1; + #ifdef COLM_LOG_PARSE if ( colm_log_parse ) { cerr << "initializing PdaRun" << endl; @@ -118,22 +133,22 @@ void initPdaRun( PdaRun *pdaRun, Tree *context ) pdaRun->context = splitTree( pdaRun->prg, context ); } -void PdaRun::clearContext( Tree **sp ) +void clearContext( PdaRun *pdaRun, Tree **sp ) { - if ( context != 0 ) - treeDownref( prg, sp, context ); + if ( pdaRun->context != 0 ) + treeDownref( pdaRun->prg, sp, pdaRun->context ); } -long PdaRun::stackTopTarget() +long stackTopTarget( PdaRun *pdaRun ) { long state; - if ( pt(stackTop->tree)->state < 0 ) - state = prg->rtd->startStates[parserId]; + if ( pt(pdaRun->stackTop->tree)->state < 0 ) + state = pdaRun->prg->rtd->startStates[pdaRun->parserId]; else { - state = tables->targs[(int)tables->indicies[tables->offsets[ - pt(stackTop->tree)->state] + - (stackTop->tree->id - tables->keys[pt(stackTop->tree)->state<<1])]]; + state = pdaRun->tables->targs[(int)pdaRun->tables->indicies[pdaRun->tables->offsets[ + pt(pdaRun->stackTop->tree)->state] + + (pdaRun->stackTop->tree->id - pdaRun->tables->keys[pt(pdaRun->stackTop->tree)->state<<1])]]; } return state; } @@ -593,7 +608,7 @@ parseError: } #endif pdaRun->numRetry -= 1; - pdaRun->cs = pdaRun->stackTopTarget(); + pdaRun->cs = stackTopTarget( pdaRun ); pdaRun->nextRegionInd = next; return; } @@ -605,7 +620,7 @@ parseError: } #endif - pdaRun->cs = pdaRun->stackTopTarget(); + pdaRun->cs = stackTopTarget( pdaRun ); goto _out; } } @@ -713,7 +728,7 @@ parseError: /* Transfer the retry from undoLel to input. */ pt(input->tree)->retry_lower = pt(undoLel->tree)->retry_upper; pt(input->tree)->retry_upper = 0; - pt(input->tree)->state = pdaRun->stackTopTarget(); + pt(input->tree)->state = stackTopTarget( pdaRun ); } /* Free the reduced item. */ diff --git a/colm/pdarun.h b/colm/pdarun.h index 4eeb9b36..e1e2df14 100644 --- a/colm/pdarun.h +++ b/colm/pdarun.h @@ -113,58 +113,22 @@ inline void initRevTreeIter( RevTreeIter *revTriter, const Ref *rootRef, revTriter->ref.next = 0; } -void initUserIter( UserIter *userIter, Tree **stackRoot, long argSize, long searchId ); - -bool makeReverseCode( CodeVect *all, CodeVect &reverseCode ); - typedef RtVector<Tree*> Bindings; struct PdaRun { - PdaRun( Program *prg, PdaTables *tables, FsmRun *fsmRun, int parserId, - long stopTarget, bool revertOn ) - : - prg(prg), - tables(tables), - fsmRun(fsmRun), - parserId(parserId), - stopParsing(false), - stopTarget(stopTarget), - queue(0), - queueLast(0), - revertOn(revertOn), - context(0), - consumed(0), - targetConsumed(-1) - { - } - int numRetry; Kid *stackTop; int errCount; int cs; int nextRegionInd; - /* Offset can be used to look at the next nextRegionInd. */ - int getNextRegion( int offset = 0 ) - { return tables->tokenRegions[nextRegionInd+offset]; } Program *prg; PdaTables *tables; FsmRun *fsmRun; int parserId; - long stackTopTarget(); - void commitKid( Tree **root, Kid *lel ); - void commit(); - bool isParserStopFinished(); - void match( Kid *tree, Kid *pattern ); - - Kid *extractIgnore(); - - /* Report an error encountered by the parser. */ - ostream &parseError( int tokId, Tree *tree ); - /* Reused. */ CodeVect reverseCode; CodeVect *allReverseCode; @@ -180,7 +144,6 @@ struct PdaRun bool revertOn; Tree *context; - void clearContext( Tree **sp ); //bool fragStop; bool stop; @@ -189,7 +152,25 @@ struct PdaRun long targetConsumed; }; -void initPdaRun( PdaRun *pdaRun, Tree *tree ); +void initUserIter( UserIter *userIter, Tree **stackRoot, long argSize, long searchId ); + +bool makeReverseCode( CodeVect *all, CodeVect &reverseCode ); + + +void initPdaRun( PdaRun *pdaRun, Program *prg, PdaTables *tables, + FsmRun *fsmRun, int parserId, long stopTarget, bool revertOn, Tree *context ); + +void clearContext( PdaRun *pdaRun, Tree **sp ); +Kid *extractIgnore( PdaRun *pdaRun ); +long stackTopTarget( PdaRun *pdaRun ); +void commitKid( PdaRun *pdaRun, Tree **root, Kid *lel ); +void runCommit( PdaRun *pdaRun ); +bool isParserStopFinished( PdaRun *pdaRun ); +void pdaRunMatch( PdaRun *pdaRun, Kid *tree, Kid *pattern ); + + +/* Offset can be used to look at the next nextRegionInd. */ +int pdaRunGetNextRegion( PdaRun *pdaRun, int offset ); void cleanParser( Tree **root, PdaRun *pdaRun ); void ignore( PdaRun *pdaRun, Tree *tree ); diff --git a/colm/pdarun2.h b/colm/pdarun2.h index 98fe0cf6..78e2e704 100644 --- a/colm/pdarun2.h +++ b/colm/pdarun2.h @@ -407,6 +407,7 @@ typedef struct _Program } Program; + #ifdef __cplusplus } #endif diff --git a/colm/tree.cpp b/colm/tree.cpp index 12bba7ae..ff561aad 100644 --- a/colm/tree.cpp +++ b/colm/tree.cpp @@ -1016,11 +1016,11 @@ Tree *createGeneric( Program *prg, long genericId ) accum->id = genericInfo->langElId; accum->genericInfo = genericInfo; accum->fsmRun = new FsmRun; - accum->pdaRun = new PdaRun( prg, prg->rtd->pdaTables, - accum->fsmRun, genericInfo->parserId, false, false ); + accum->pdaRun = new PdaRun; /* Start off the parsing process. */ - initPdaRun( accum->pdaRun, 0 ); + initPdaRun( accum->pdaRun, prg, prg->rtd->pdaTables, + accum->fsmRun, genericInfo->parserId, false, false, 0 ); initFsmRun( accum->fsmRun, prg ); newToken( accum->pdaRun, accum->fsmRun ); @@ -1074,7 +1074,7 @@ free_tree: Accum *accum = (Accum*)tree; delete accum->fsmRun; cleanParser( sp, accum->pdaRun ); - accum->pdaRun->clearContext( sp ); + clearContext( accum->pdaRun, sp ); rcodeDownrefAll( prg, sp, accum->pdaRun->allReverseCode ); delete accum->pdaRun; treeDownref( prg, sp, (Tree*)accum->stream ); |