diff options
author | Adrian Thurston <thurston@complang.org> | 2010-04-19 02:12:58 +0000 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2010-04-19 02:12:58 +0000 |
commit | 7201f124185e925670c36ac9340bf5fbd590cd5a (patch) | |
tree | 3938f2a90f36c2d2274b4ddf07943d51f02f0dc4 | |
parent | f3e8845d101f76875d5bcbc457928917f4ba02cc (diff) | |
download | colm-7201f124185e925670c36ac9340bf5fbd590cd5a.tar.gz |
More C porting.
-rw-r--r-- | colm/bytecode.cpp | 10 | ||||
-rw-r--r-- | colm/bytecode.h | 2 | ||||
-rw-r--r-- | colm/codevect.c | 1 | ||||
-rw-r--r-- | colm/compile.cpp | 1 | ||||
-rw-r--r-- | colm/debug.h | 2 | ||||
-rw-r--r-- | colm/fsmrun.cpp | 31 | ||||
-rw-r--r-- | colm/fsmrun.h | 1 | ||||
-rw-r--r-- | colm/fsmrun2.c | 18 | ||||
-rw-r--r-- | colm/fsmrun2.h | 1 | ||||
-rw-r--r-- | colm/input.c | 2 | ||||
-rw-r--r-- | colm/list.c | 2 | ||||
-rw-r--r-- | colm/map.c | 2 | ||||
-rw-r--r-- | colm/map.h | 22 | ||||
-rw-r--r-- | colm/parsedata.cpp | 1 | ||||
-rw-r--r-- | colm/pdacodegen.cpp | 1 | ||||
-rw-r--r-- | colm/pdarun.cpp | 13 | ||||
-rw-r--r-- | colm/pdarun.h | 561 | ||||
-rw-r--r-- | colm/pdarun2.h | 546 | ||||
-rw-r--r-- | colm/pool.c | 2 | ||||
-rw-r--r-- | colm/pool.h | 2 |
20 files changed, 598 insertions, 623 deletions
diff --git a/colm/bytecode.cpp b/colm/bytecode.cpp index 5d3f2b7b..583749bc 100644 --- a/colm/bytecode.cpp +++ b/colm/bytecode.cpp @@ -996,20 +996,20 @@ void execute( Execution *exec, Tree **sp ) exec->lhs = (Tree*) pop(); } -bool makeReverseCode( RtCodeVect *all, RtCodeVect &reverseCode ) +int makeReverseCode( RtCodeVect *all, RtCodeVect *reverseCode ) { /* Do we need to revert the left hand side? */ /* Check if there was anything generated. */ - if ( reverseCode.tabLen == 0 ) + if ( reverseCode->tabLen == 0 ) return false; long prevAllLength = all->tabLen; /* Go backwards, group by group, through the reverse code. Push each group * to the global reverse code stack. */ - Code *p = reverseCode.data + reverseCode.tabLen; - while ( p != reverseCode.data ) { + Code *p = reverseCode->data + reverseCode->tabLen; + while ( p != reverseCode->data ) { p--; long len = *p; p = p - len; @@ -1022,7 +1022,7 @@ bool makeReverseCode( RtCodeVect *all, RtCodeVect &reverseCode ) appendWord( all, length ); /* Clear the revere code buffer. */ - reverseCode.tabLen = 0; + reverseCode->tabLen = 0; return true; } diff --git a/colm/bytecode.h b/colm/bytecode.h index e768e98c..0c56bd6c 100644 --- a/colm/bytecode.h +++ b/colm/bytecode.h @@ -25,7 +25,7 @@ #include "rtvector.h" #include "config.h" #include "pool.h" -#include "pdarun2.h" +#include "pdarun.h" #include "map.h" #include <iostream> diff --git a/colm/codevect.c b/colm/codevect.c index 9fd57fc2..e8f6ddd0 100644 --- a/colm/codevect.c +++ b/colm/codevect.c @@ -20,6 +20,7 @@ */ #include "rtvector.h" +#include "pdarun.h" #include <string.h> #include <stdlib.h> diff --git a/colm/compile.cpp b/colm/compile.cpp index 06697681..2edbc2d0 100644 --- a/colm/compile.cpp +++ b/colm/compile.cpp @@ -22,6 +22,7 @@ #include "bytecode.h" #include "parsedata.h" #include "fsmrun.h" +#include "pdarun.h" #include "input.h" #include <iostream> #include <assert.h> diff --git a/colm/debug.h b/colm/debug.h index 01ee677a..67926db3 100644 --- a/colm/debug.h +++ b/colm/debug.h @@ -26,7 +26,7 @@ extern "C" { #ifdef COLM_LOG #define debug( realm, fmt, ...) _debug( realm, fmt, __VA_ARGS__ ) #else -# define debug( realm, fmt, ...) +#define debug( realm, fmt, ...) #endif int _debug( long realm, const char *fmt, ... ); diff --git a/colm/fsmrun.cpp b/colm/fsmrun.cpp index fd50a866..6cc806a9 100644 --- a/colm/fsmrun.cpp +++ b/colm/fsmrun.cpp @@ -26,11 +26,12 @@ #include "config.h" #include "fsmrun.h" #include "pdarun.h" -#include "pdarun2.h" #include "debug.h" +#include "bytecode.h" using std::cerr; using std::endl; +using std::ostream; exit_object endp; @@ -40,25 +41,6 @@ void operator<<( ostream &out, exit_object & ) exit(1); } -void connect( FsmRun *fsmRun, InputStream *inputStream ) -{ - if ( inputStream->hasData != 0 && inputStream->hasData != fsmRun ) { - takeBackBuffered( inputStream ); - } - - if ( inputStream->hasData != fsmRun ) { - #ifdef COLM_LOG_PARSE - if ( colm_log_parse ) { - cerr << "connecting fsmRun: " << fsmRun << " and input stream " << - inputStream << endl; - } - #endif - } - - inputStream->hasData = fsmRun; - fsmRun->haveDataOf = inputStream; -} - /* Load up a token, starting from tokstart if it is set. If not set then * start it at data. */ Head *streamPull( Program *prg, FsmRun *fsmRun, InputStream *inputStream, long length ) @@ -489,7 +471,7 @@ void executeGenerationAction( Tree **sp, Program *prg, FsmRun *fsmRun, PdaRun *p * queue is not empty. Pull the reverse code out and store in the * token. */ Tree *tree = pdaRun->queue->tree; - bool hasrcode = makeReverseCode( pdaRun->allReverseCode, pdaRun->reverseCode ); + bool hasrcode = makeReverseCode( pdaRun->allReverseCode, &pdaRun->reverseCode ); if ( hasrcode ) tree->flags |= AF_HAS_RCODE; } @@ -603,7 +585,8 @@ void sendHandleError( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *in /* Check the result. */ if ( pdaRun->errCount > 0 ) { /* Error occured in the top-level parser. */ - parseError( inputStream, fsmRun, pdaRun, id, input->tree ) << "parse error" << endp; + parseError( inputStream, fsmRun, pdaRun, id, input->tree ); + cerr << "parse error" << endp; } else { if ( isParserStopFinished( pdaRun ) ) { @@ -748,8 +731,8 @@ void sendEof( Tree **sp, InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRu sendWithIgnore( sp, pdaRun, fsmRun, inputStream, input ); if ( pdaRun->errCount > 0 ) { - parseError( inputStream, fsmRun, pdaRun, input->tree->id, input->tree ) << - "parse error" << endp; + parseError( inputStream, fsmRun, pdaRun, input->tree->id, input->tree ); + cerr<< "parse error" << endp; } } diff --git a/colm/fsmrun.h b/colm/fsmrun.h index c227c7f5..301a907f 100644 --- a/colm/fsmrun.h +++ b/colm/fsmrun.h @@ -68,7 +68,6 @@ Kid *makeToken( PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputStream, int id Head *tokdata, bool namedLangEl, int bindId ); void newToken( PdaRun *pdaRun, FsmRun *fsmRun ); -void ignore( PdaRun *pdaRun, Tree *tree ); void initBindings( PdaRun *pdaRun ); void makeTokenPushBinding( PdaRun *pdaRun, int bindId, Tree *tree ); diff --git a/colm/fsmrun2.c b/colm/fsmrun2.c index 37ed1f0b..bf5fc3ac 100644 --- a/colm/fsmrun2.c +++ b/colm/fsmrun2.c @@ -20,7 +20,7 @@ */ #include "fsmrun2.h" -#include "pdarun2.h" +#include "pdarun.h" #include "input.h" #include "debug.h" @@ -126,3 +126,19 @@ void takeBackBuffered( InputStream *inputStream ) fsmRun->haveDataOf = 0; } } + +void connect( FsmRun *fsmRun, InputStream *inputStream ) +{ + if ( inputStream->hasData != 0 && inputStream->hasData != fsmRun ) { + takeBackBuffered( inputStream ); + } + +#ifdef COLM_LOG + if ( inputStream->hasData != fsmRun ) + debug( REALM_PARSE, "connecting fsmRun: %p and input stream %p\n", fsmRun, inputStream ); +#endif + + inputStream->hasData = fsmRun; + fsmRun->haveDataOf = inputStream; +} + diff --git a/colm/fsmrun2.h b/colm/fsmrun2.h index 1765f8ca..35aca532 100644 --- a/colm/fsmrun2.h +++ b/colm/fsmrun2.h @@ -89,6 +89,7 @@ void initFsmRun( FsmRun *fsmRun, struct _Program *prg ); void updatePosition( InputStream *inputStream, const char *data, long length ); void undoPosition( InputStream *inputStream, const char *data, long length ); void takeBackBuffered( InputStream *inputStream ); +void connect( FsmRun *fsmRun, InputStream *inputStream ); #ifdef __cplusplus diff --git a/colm/input.c b/colm/input.c index c1dcf648..6433638b 100644 --- a/colm/input.c +++ b/colm/input.c @@ -21,7 +21,7 @@ #include "input.h" #include "fsmrun2.h" -#include "pdarun2.h" +#include "pdarun.h" #include <stdio.h> #include <stdlib.h> diff --git a/colm/list.c b/colm/list.c index f36f9bc3..b0abc26b 100644 --- a/colm/list.c +++ b/colm/list.c @@ -19,7 +19,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "pdarun2.h" +#include "pdarun.h" void listAddAfter( List *list, ListEl *prev_el, ListEl *new_el ) { @@ -20,7 +20,7 @@ */ #include <assert.h> -#include "pdarun2.h" +#include "pdarun.h" #include "map.h" #include "pool.h" @@ -22,6 +22,9 @@ #ifndef _MAP_H #define _MAP_H +#if defined(__cplusplus) +extern "C" { +#endif typedef struct _MapEl { @@ -49,10 +52,6 @@ typedef struct _Map GenericInfo *genericInfo; } Map; -#if defined(__cplusplus) -extern "C" { -#endif - void mapListAbandon( Map *map ); void mapListAddBefore( Map *map, MapEl *next_el, MapEl *new_el ); @@ -72,9 +71,24 @@ MapEl *mapInsertKey( Program *prg, Map *map, Tree *key, MapEl **lastFound ); MapEl *mapImplFind( Program *prg, Map *map, Tree *key ); MapEl *mapDetachByKey( Program *prg, Map *map, Tree *key ); MapEl *mapDetach( Program *prg, Map *map, MapEl *element ); +MapEl *mapCopyBranch( Program *prg, Map *map, MapEl *el, Kid *oldNextDown, Kid **newNextDown ); long cmpTree( Program *prg, const Tree *tree1, const Tree *tree2 ); +void mapImplRemoveEl( Program *prg, Map *map, MapEl *element ); +int mapImplRemoveKey( Program *prg, Map *map, Tree *key ); + +/* + * Iterators. + */ + +void initTreeIter( TreeIter *treeIter, const Ref *rootRef, int searchId, Tree **stackRoot ); +void initRevTreeIter( RevTreeIter *revTriter, const Ref *rootRef, + int searchId, Tree **stackRoot, int children ); + + +void initUserIter( UserIter *userIter, Tree **stackRoot, long argSize, long searchId ); + #if defined(__cplusplus) } diff --git a/colm/parsedata.cpp b/colm/parsedata.cpp index 805f6c9c..9fa94c47 100644 --- a/colm/parsedata.cpp +++ b/colm/parsedata.cpp @@ -35,6 +35,7 @@ #include "pdacodegen.h" #include "fsmcodegen.h" #include "fsmrun.h" +#include "pdarun.h" using namespace std; using std::ostringstream; diff --git a/colm/pdacodegen.cpp b/colm/pdacodegen.cpp index 2ad859c6..0d74b60d 100644 --- a/colm/pdacodegen.cpp +++ b/colm/pdacodegen.cpp @@ -86,6 +86,7 @@ void PdaCodeGen::writeFirst() "#include <colm/pdarun.h>\n" "#include <colm/fsmrun.h>\n" "#include <colm/debug.h>\n" + "#include <colm/bytecode.h>\n" "\n" "extern RuntimeData main_runtimeData;\n"; diff --git a/colm/pdarun.cpp b/colm/pdarun.cpp index 81fe65c4..cab50c5c 100644 --- a/colm/pdarun.cpp +++ b/colm/pdarun.cpp @@ -29,6 +29,7 @@ #include "config.h" #include "pdarun.h" #include "fsmrun.h" +#include "bytecode.h" using std::ostream; using std::cout; @@ -106,7 +107,7 @@ void cleanParser( Tree **sp, PdaRun *pdaRun ) // pdaRun->clearContext( sp ); } -bool isParserStopFinished( PdaRun *pdaRun ) +int isParserStopFinished( PdaRun *pdaRun ) { bool done = pdaRun->stackTop->next != 0 && @@ -115,8 +116,8 @@ bool isParserStopFinished( PdaRun *pdaRun ) return done; } -void initPdaRun( PdaRun *pdaRun, Program *prg, PdaTables *tables, - FsmRun *fsmRun, int parserId, long stopTarget, bool revertOn, Tree *context ) +extern "C" void initPdaRun( PdaRun *pdaRun, Program *prg, PdaTables *tables, + FsmRun *fsmRun, int parserId, long stopTarget, int revertOn, Tree *context ) { memset( pdaRun, 0, sizeof(PdaRun) ); pdaRun->prg = prg; @@ -552,7 +553,7 @@ again: } /* Pull out the reverse code, if any. */ - bool hasrcode = makeReverseCode( pdaRun->allReverseCode, pdaRun->reverseCode ); + bool hasrcode = makeReverseCode( pdaRun->allReverseCode, &pdaRun->reverseCode ); if ( hasrcode ) redLel->tree->flags |= AF_HAS_RCODE; @@ -770,7 +771,7 @@ _out: pdaRun->nextRegionInd = pdaRun->tables->tokenRegionInds[pdaRun->cs]; } -ostream &parseError( InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRun, int tokId, Tree *tree ) +void parseError( InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRun, int tokId, Tree *tree ) { cerr << "error:" << inputStream->line << ": at token "; if ( tokId < 128 ) @@ -784,6 +785,4 @@ ostream &parseError( InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRun, i cerr << "\""; } cerr << ": "; - - return cerr; } diff --git a/colm/pdarun.h b/colm/pdarun.h index 6f106c21..fec0e60c 100644 --- a/colm/pdarun.h +++ b/colm/pdarun.h @@ -1,5 +1,5 @@ /* - * Copyright 2007-2009 Adrian Thurston <thurston@complang.org> + * Copyright 2007-2010 Adrian Thurston <thurston@complang.org> */ /* This file is part of Colm. @@ -22,55 +22,557 @@ #ifndef _PDARUN_H #define _PDARUN_H -#include <iostream> -#include "bytecode.h" -#include "rtvector.h" -#include "fsmrun2.h" #include "input.h" -#include "pdarun2.h" - -using std::ostream; - -typedef struct _Tree Tree; - -#include "map.h" +#include "fsmrun2.h" #ifdef __cplusplus extern "C" { #endif -MapEl *mapCopyBranch( Program *prg, Map *map, MapEl *el, _Kid *oldNextDown, Kid **newNextDown ); +typedef unsigned char Code; +typedef unsigned long Word; +typedef unsigned long Half; +struct Bindings; -#ifdef __cplusplus -} -#endif +typedef struct _RtCodeVect +{ + Code *data; + long tabLen; + long allocLen; + + /* FIXME: leak when freed. */ +} RtCodeVect; + + +typedef struct _File +{ + struct _File *prev; + struct _File *next; +} File; + +typedef struct _Location +{ + File *file; + long line; + long column; + long byte; +} Location; + +/* Header located just before string data. */ +typedef struct _Head +{ + const char *data; long length; + Location *location; +} Head; + +typedef struct _Kid +{ + /* The tree needs to be first since pointers to kids are used to reference + * trees on the stack. A pointer to the word that is a Tree* is cast to + * a Kid*. */ + struct _Tree *tree; + struct _Kid *next; +} Kid; + +typedef struct _Ref +{ + Kid *kid; + struct _Ref *next; +} Ref; -void mapImplRemoveEl( Program *prg, Map *map, MapEl *element ); -bool mapImplRemoveKey( Program *prg, Map *map, Tree *key ); +typedef struct _Tree +{ + /* First four will be overlaid in other structures. */ + short id; + unsigned short flags; + long refs; + Kid *child; + + Head *tokdata; +} Tree; + +typedef struct _ParseTree +{ + /* Entire structure must overlay Tree. */ + short id; + unsigned short flags; + long refs; + Kid *child; + + Head *tokdata; + + /* Parsing algorithm. */ + long state; + long region; + char causeReduce; + char retry_lower; + char retry_upper; +} ParseTree; + +typedef struct _Int +{ + /* Must overlay Tree. */ + short id; + unsigned short flags; + long refs; + Kid *child; + + long value; +} Int; + +typedef struct _Pointer +{ + /* Must overlay Tree. */ + short id; + unsigned short flags; + long refs; + Kid *child; + + Kid *value; +} Pointer; + +typedef struct _Str +{ + /* Must overlay Tree. */ + short id; + unsigned short flags; + long refs; + Kid *child; + + Head *value; +} Str; + +typedef struct _ListEl +{ + /* Must overlay kid. */ + Tree *value; + struct _ListEl *next; + struct _ListEl *prev; +} ListEl; /* - * Iterators. + * Maps */ +typedef struct _GenericInfo +{ + long type; + long typeArg; + long keyOffset; + long keyType; + long langElId; + long parserId; +} GenericInfo; + +typedef struct _List +{ + /* Must overlay Tree. */ + short id; + unsigned short flags; + long refs; + ListEl *head; + + ListEl *tail; + long listLen; + GenericInfo *genericInfo; + +} List; + + +void listAddAfter( List *list, ListEl *prev_el, ListEl *new_el ); +void listAddBefore( List *list, ListEl *next_el, ListEl *new_el ); + +void listPrepend( List *list, ListEl *new_el ); +void listAppend( List *list, ListEl *new_el ); + +ListEl *listDetach( List *list, ListEl *el ); +ListEl *listDetachFirst(List *list ); +ListEl *listDetachLast(List *list ); + +long listLength(List *list); + +typedef struct _Stream +{ + /* Must overlay Tree. */ + short id; + unsigned short flags; + long refs; + Kid *child; + + FILE *file; + InputStream *in; +} Stream; + +typedef struct _FunctionInfo +{ + const char *name; + long frameId; + long argSize; + long ntrees; + long frameSize; +} FunctionInfo; + +typedef struct _UserIter +{ + /* The current item. */ + Ref ref; + Tree **stackRoot; + long argSize; + long stackSize; + Code *resume; + Tree **frame; + long searchId; +} UserIter; + +/* + * Program Data. + */ + +typedef struct _PatReplInfo +{ + long offset; + long numBindings; +} PatReplInfo; + +typedef struct _PatReplNode +{ + long id; + long next; + long child; + long bindId; + const char *data; + long length; + long ignore; + + /* Just match nonterminal, don't go inside. */ + unsigned char stop; +} PatReplNode; + +/* FIXME: should have a descriptor for object types to give the length. */ + +typedef struct _LangElInfo +{ + const char *name; + unsigned char repeat; + unsigned char list; + unsigned char literal; + unsigned char ignore; + + long frameId; + + long objectTypeId; + long ofiOffset; + long objectLength; + +// long contextTypeId; +// long contextLength; + + long termDupId; + long genericId; + long markId; + long captureAttr; + long numCaptureAttr; +} LangElInfo; + +typedef struct _ObjFieldInfo +{ + int typeId; +} ObjFieldInfo; + +typedef struct _ProdInfo +{ + long length; + unsigned long lhsId; + const char *name; + long frameId; + unsigned char lhsUpref; +} ProdInfo; + +typedef struct _FrameInfo +{ + Code *codeWV; + long codeLenWV; + Code *codeWC; + long codeLenWC; + char *trees; + long treesLen; +} FrameInfo; + +typedef struct _RegionInfo +{ + const char *name; + long defaultToken; + long eofFrameId; +} RegionInfo; + +typedef struct _CaptureAttr +{ + long mark_enter; + long mark_leave; + long offset; +} CaptureAttr; + +typedef struct _RuntimeData +{ + LangElInfo *lelInfo; + long numLangEls; + + ProdInfo *prodInfo; + long numProds; -void initTreeIter( TreeIter *treeIter, const Ref *rootRef, int searchId, Tree **stackRoot ); -void initRevTreeIter( RevTreeIter *revTriter, const Ref *rootRef, - int searchId, Tree **stackRoot, int children ); + RegionInfo *regionInfo; + long numRegions; + Code *rootCode; + long rootCodeLen; -void initUserIter( UserIter *userIter, Tree **stackRoot, long argSize, long searchId ); + FrameInfo *frameInfo; + long numFrames; -bool makeReverseCode( RtCodeVect *all, RtCodeVect &reverseCode ); + FunctionInfo *functionInfo; + long numFunctions; + + PatReplInfo *patReplInfo; + long numPatterns; + + PatReplNode *patReplNodes; + long numPatternNodes; + + GenericInfo *genericInfo; + long numGenerics; + + const char **litdata; + long *litlen; + Head **literals; + long numLiterals; + + CaptureAttr *captureAttr; + long numCapturedAttr; + + FsmTables *fsmTables; + struct _PdaTables *pdaTables; + int *startStates; + int *eofLelIds; + int *parserLelIds; + long numParsers; + + long globalSize; + + long firstNonTermId; + + long integerId; + long stringId; + long anyId; + long eofId; + long noTokenId; +} RuntimeData; + +typedef struct _PdaTables +{ + /* Parser table data. */ + int *indicies; + int *keys; + unsigned int *offsets; + unsigned int *targs; + unsigned int *actInds; + unsigned int *actions; + int *commitLen; + int *tokenRegionInds; + int *tokenRegions; + + int numIndicies; + int numKeys; + int numStates; + int numTargs; + int numActInds; + int numActions; + int numCommitLen; + int numRegionItems; + + RuntimeData *rtd; +} PdaTables; + + +typedef struct _PoolBlock +{ + void *data; + struct _PoolBlock *next; +} PoolBlock; + +typedef struct _PoolItem +{ + struct _PoolItem *next; +} PoolItem; + +typedef struct _PoolAlloc +{ + PoolBlock *head; + long nextel; + PoolItem *pool; + int sizeofT; +} PoolAlloc; + +typedef struct _Program +{ + int argc; + char **argv; + + unsigned char ctxDepParsing; + RuntimeData *rtd; + Tree *global; + + PoolAlloc kidPool; + PoolAlloc treePool; + PoolAlloc parseTreePool; + PoolAlloc listElPool; + PoolAlloc mapElPool; + PoolAlloc headPool; + PoolAlloc locationPool; + + Tree *trueVal; + Tree *falseVal; + + Kid *heap; + + Stream *stdinVal; + Stream *stdoutVal; + Stream *stderrVal; +} Program; + +typedef struct _TreeIter +{ + Ref rootRef; + Ref ref; + long searchId; + Tree **stackRoot; + long stackSize; +} TreeIter; + +/* This must overlay tree iter because some of the same bytecodes are used. */ +typedef struct _RevTreeIter +{ + Ref rootRef; + Ref ref; + long searchId; + Tree **stackRoot; + long stackSize; + + /* For detecting a split at the leaf. */ + Kid *kidAtYield; + long children; + Kid **cur; +} RevTreeIter; + +typedef struct _PdaRun +{ + int numRetry; + Kid *stackTop; + int errCount; + int cs; + int nextRegionInd; + + Program *prg; + PdaTables *tables; + FsmRun *fsmRun; + int parserId; + + /* Reused. */ + RtCodeVect reverseCode; + RtCodeVect *allReverseCode; + + int stopParsing; + long stopTarget; + + Kid *accumIgnore; + Kid *queue, *queueLast; + + struct Bindings *bindings; + + int revertOn; + + Tree *context; + + //bool fragStop; + int stop; + + long consumed; + long targetConsumed; +} PdaRun; + +typedef struct AccumStruct +{ + /* Must overlay Tree. */ + short id; + unsigned short flags; + long refs; + Kid *child; + + GenericInfo *genericInfo; + + PdaRun *pdaRun; + FsmRun *fsmRun; + Stream *stream; +} Accum; + +void rtCodeVectReplace( RtCodeVect *vect, long pos, const Code *val, long len ); +void rtCodeVectEmpty( RtCodeVect *vect ); +void rtCodeVectRemove( RtCodeVect *vect, long pos, long len ); + +void initRtCodeVect( RtCodeVect *codeVect ); + +//inline static void remove( RtCodeVect *vect, long pos ); +inline static void append( RtCodeVect *vect, const Code val ); +inline static void append2( RtCodeVect *vect, const Code *val, long len ); +inline static void appendHalf( RtCodeVect *vect, Half half ); +inline static void appendWord( RtCodeVect *vect, Word word ); + +inline static void append2( RtCodeVect *vect, const Code *val, long len ) +{ + rtCodeVectReplace( vect, vect->tabLen, val, len ); +} + +inline static void append( RtCodeVect *vect, const Code val ) +{ + rtCodeVectReplace( vect, vect->tabLen, &val, 1 ); +} + +inline static void appendHalf( RtCodeVect *vect, Half half ) +{ + /* not optimal. */ + append( vect, half & 0xff ); + append( vect, (half>>8) & 0xff ); +} + +inline static void appendWord( RtCodeVect *vect, Word word ) +{ + /* not optimal. */ + append( vect, word & 0xff ); + append( vect, (word>>8) & 0xff ); + append( vect, (word>>16) & 0xff ); + append( vect, (word>>24) & 0xff ); + #if SIZEOF_LONG == 8 + append( vect, (word>>32) & 0xff ); + append( vect, (word>>40) & 0xff ); + append( vect, (word>>48) & 0xff ); + append( vect, (word>>56) & 0xff ); + #endif +} + +void incrementConsumed( PdaRun *pdaRun ); +void decrementConsumed( PdaRun *pdaRun ); + +void parseError( InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRun, int tokId, Tree *tree ); +int makeReverseCode( RtCodeVect *all, RtCodeVect *reverseCode ); void initPdaRun( PdaRun *pdaRun, Program *prg, PdaTables *tables, - FsmRun *fsmRun, int parserId, long stopTarget, bool revertOn, Tree *context ); + FsmRun *fsmRun, int parserId, long stopTarget, int 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 ); +int isParserStopFinished( PdaRun *pdaRun ); void pdaRunMatch( PdaRun *pdaRun, Kid *tree, Kid *pattern ); @@ -82,6 +584,9 @@ void ignore( PdaRun *pdaRun, Tree *tree ); void parseToken( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputStream, Kid *input ); long undoParse( Tree **sp, PdaRun *pdaRun, FsmRun *fsmRun, InputStream *inputStream, Tree *tree ); void xml_print_list( RuntimeData *runtimeData, Kid *lel, int depth ); -ostream &parseError( InputStream *inputStream, FsmRun *fsmRun, PdaRun *pdaRun, int tokId, Tree *tree ); -#endif /* _PDARUN_H */ +#ifdef __cplusplus +} +#endif + +#endif diff --git a/colm/pdarun2.h b/colm/pdarun2.h deleted file mode 100644 index 4d343eb3..00000000 --- a/colm/pdarun2.h +++ /dev/null @@ -1,546 +0,0 @@ -#ifndef _PDARUN2_H -#define _PDARUN2_H - -#include "input.h" -#include "fsmrun2.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef unsigned char Code; -typedef unsigned long Word; -typedef unsigned long Half; -struct Bindings; - -typedef struct _RtCodeVect -{ - Code *data; - long tabLen; - long allocLen; - - /* FIXME: leak when freed. */ -} RtCodeVect; - - -typedef struct _File -{ - struct _File *prev; - struct _File *next; -} File; - -typedef struct _Location -{ - File *file; - long line; - long column; - long byte; -} Location; - -/* Header located just before string data. */ -typedef struct _Head -{ - const char *data; long length; - Location *location; -} Head; - -typedef struct _Kid -{ - /* The tree needs to be first since pointers to kids are used to reference - * trees on the stack. A pointer to the word that is a Tree* is cast to - * a Kid*. */ - struct _Tree *tree; - struct _Kid *next; -} Kid; - -typedef struct _Ref -{ - Kid *kid; - struct _Ref *next; -} Ref; - -typedef struct _Tree -{ - /* First four will be overlaid in other structures. */ - short id; - unsigned short flags; - long refs; - Kid *child; - - Head *tokdata; -} Tree; - -typedef struct _ParseTree -{ - /* Entire structure must overlay Tree. */ - short id; - unsigned short flags; - long refs; - Kid *child; - - Head *tokdata; - - /* Parsing algorithm. */ - long state; - long region; - char causeReduce; - char retry_lower; - char retry_upper; -} ParseTree; - -typedef struct _Int -{ - /* Must overlay Tree. */ - short id; - unsigned short flags; - long refs; - Kid *child; - - long value; -} Int; - -typedef struct _Pointer -{ - /* Must overlay Tree. */ - short id; - unsigned short flags; - long refs; - Kid *child; - - Kid *value; -} Pointer; - -typedef struct _Str -{ - /* Must overlay Tree. */ - short id; - unsigned short flags; - long refs; - Kid *child; - - Head *value; -} Str; - -typedef struct _ListEl -{ - /* Must overlay kid. */ - Tree *value; - struct _ListEl *next; - struct _ListEl *prev; -} ListEl; - -/* - * Maps - */ -typedef struct _GenericInfo -{ - long type; - long typeArg; - long keyOffset; - long keyType; - long langElId; - long parserId; -} GenericInfo; - -typedef struct _List -{ - /* Must overlay Tree. */ - short id; - unsigned short flags; - long refs; - ListEl *head; - - ListEl *tail; - long listLen; - GenericInfo *genericInfo; - -} List; - - -void listAddAfter( List *list, ListEl *prev_el, ListEl *new_el ); -void listAddBefore( List *list, ListEl *next_el, ListEl *new_el ); - -void listPrepend( List *list, ListEl *new_el ); -void listAppend( List *list, ListEl *new_el ); - -ListEl *listDetach( List *list, ListEl *el ); -ListEl *listDetachFirst(List *list ); -ListEl *listDetachLast(List *list ); - -long listLength(List *list); - -typedef struct _Stream -{ - /* Must overlay Tree. */ - short id; - unsigned short flags; - long refs; - Kid *child; - - FILE *file; - InputStream *in; -} Stream; - -typedef struct _FunctionInfo -{ - const char *name; - long frameId; - long argSize; - long ntrees; - long frameSize; -} FunctionInfo; - -typedef struct _UserIter -{ - /* The current item. */ - Ref ref; - Tree **stackRoot; - long argSize; - long stackSize; - Code *resume; - Tree **frame; - long searchId; -} UserIter; - -/* - * Program Data. - */ - -typedef struct _PatReplInfo -{ - long offset; - long numBindings; -} PatReplInfo; - -typedef struct _PatReplNode -{ - long id; - long next; - long child; - long bindId; - const char *data; - long length; - long ignore; - - /* Just match nonterminal, don't go inside. */ - unsigned char stop; -} PatReplNode; - -/* FIXME: should have a descriptor for object types to give the length. */ - -typedef struct _LangElInfo -{ - const char *name; - unsigned char repeat; - unsigned char list; - unsigned char literal; - unsigned char ignore; - - long frameId; - - long objectTypeId; - long ofiOffset; - long objectLength; - -// long contextTypeId; -// long contextLength; - - long termDupId; - long genericId; - long markId; - long captureAttr; - long numCaptureAttr; -} LangElInfo; - -typedef struct _ObjFieldInfo -{ - int typeId; -} ObjFieldInfo; - -typedef struct _ProdInfo -{ - long length; - unsigned long lhsId; - const char *name; - long frameId; - unsigned char lhsUpref; -} ProdInfo; - -typedef struct _FrameInfo -{ - Code *codeWV; - long codeLenWV; - Code *codeWC; - long codeLenWC; - char *trees; - long treesLen; -} FrameInfo; - -typedef struct _RegionInfo -{ - const char *name; - long defaultToken; - long eofFrameId; -} RegionInfo; - -typedef struct _CaptureAttr -{ - long mark_enter; - long mark_leave; - long offset; -} CaptureAttr; - -typedef struct _RuntimeData -{ - LangElInfo *lelInfo; - long numLangEls; - - ProdInfo *prodInfo; - long numProds; - - RegionInfo *regionInfo; - long numRegions; - - Code *rootCode; - long rootCodeLen; - - FrameInfo *frameInfo; - long numFrames; - - FunctionInfo *functionInfo; - long numFunctions; - - PatReplInfo *patReplInfo; - long numPatterns; - - PatReplNode *patReplNodes; - long numPatternNodes; - - GenericInfo *genericInfo; - long numGenerics; - - const char **litdata; - long *litlen; - Head **literals; - long numLiterals; - - CaptureAttr *captureAttr; - long numCapturedAttr; - - FsmTables *fsmTables; - struct _PdaTables *pdaTables; - int *startStates; - int *eofLelIds; - int *parserLelIds; - long numParsers; - - long globalSize; - - long firstNonTermId; - - long integerId; - long stringId; - long anyId; - long eofId; - long noTokenId; -} RuntimeData; - -typedef struct _PdaTables -{ - /* Parser table data. */ - int *indicies; - int *keys; - unsigned int *offsets; - unsigned int *targs; - unsigned int *actInds; - unsigned int *actions; - int *commitLen; - int *tokenRegionInds; - int *tokenRegions; - - int numIndicies; - int numKeys; - int numStates; - int numTargs; - int numActInds; - int numActions; - int numCommitLen; - int numRegionItems; - - RuntimeData *rtd; -} PdaTables; - - -typedef struct _PoolBlock -{ - void *data; - struct _PoolBlock *next; -} PoolBlock; - -typedef struct _PoolItem -{ - struct _PoolItem *next; -} PoolItem; - -typedef struct _PoolAlloc -{ - PoolBlock *head; - long nextel; - PoolItem *pool; - int sizeofT; -} PoolAlloc; - -typedef struct _Program -{ - int argc; - char **argv; - - unsigned char ctxDepParsing; - RuntimeData *rtd; - Tree *global; - - PoolAlloc kidPool; - PoolAlloc treePool; - PoolAlloc parseTreePool; - PoolAlloc listElPool; - PoolAlloc mapElPool; - PoolAlloc headPool; - PoolAlloc locationPool; - - Tree *trueVal; - Tree *falseVal; - - Kid *heap; - - Stream *stdinVal; - Stream *stdoutVal; - Stream *stderrVal; -} Program; - -typedef struct _TreeIter -{ - Ref rootRef; - Ref ref; - long searchId; - Tree **stackRoot; - long stackSize; -} TreeIter; - -/* This must overlay tree iter because some of the same bytecodes are used. */ -typedef struct _RevTreeIter -{ - Ref rootRef; - Ref ref; - long searchId; - Tree **stackRoot; - long stackSize; - - /* For detecting a split at the leaf. */ - Kid *kidAtYield; - long children; - Kid **cur; -} RevTreeIter; - -typedef struct _PdaRun -{ - int numRetry; - Kid *stackTop; - int errCount; - int cs; - int nextRegionInd; - - Program *prg; - PdaTables *tables; - FsmRun *fsmRun; - int parserId; - - /* Reused. */ - RtCodeVect reverseCode; - RtCodeVect *allReverseCode; - - int stopParsing; - long stopTarget; - - Kid *accumIgnore; - Kid *queue, *queueLast; - - struct Bindings *bindings; - - int revertOn; - - Tree *context; - - //bool fragStop; - int stop; - - long consumed; - long targetConsumed; -} PdaRun; - -typedef struct AccumStruct -{ - /* Must overlay Tree. */ - short id; - unsigned short flags; - long refs; - Kid *child; - - GenericInfo *genericInfo; - - PdaRun *pdaRun; - FsmRun *fsmRun; - Stream *stream; -} Accum; - -void rtCodeVectReplace( RtCodeVect *vect, long pos, const Code *val, long len ); -void rtCodeVectEmpty( RtCodeVect *vect ); -void rtCodeVectRemove( RtCodeVect *vect, long pos, long len ); - -void initRtCodeVect( RtCodeVect *codeVect ); - -//inline static void remove( RtCodeVect *vect, long pos ); -inline static void append( RtCodeVect *vect, const Code val ); -inline static void append2( RtCodeVect *vect, const Code *val, long len ); -inline static void appendHalf( RtCodeVect *vect, Half half ); -inline static void appendWord( RtCodeVect *vect, Word word ); - -inline static void append2( RtCodeVect *vect, const Code *val, long len ) -{ - rtCodeVectReplace( vect, vect->tabLen, val, len ); -} - -inline static void append( RtCodeVect *vect, const Code val ) -{ - rtCodeVectReplace( vect, vect->tabLen, &val, 1 ); -} - -inline static void appendHalf( RtCodeVect *vect, Half half ) -{ - /* not optimal. */ - append( vect, half & 0xff ); - append( vect, (half>>8) & 0xff ); -} - -inline static void appendWord( RtCodeVect *vect, Word word ) -{ - /* not optimal. */ - append( vect, word & 0xff ); - append( vect, (word>>8) & 0xff ); - append( vect, (word>>16) & 0xff ); - append( vect, (word>>24) & 0xff ); - #if SIZEOF_LONG == 8 - append( vect, (word>>32) & 0xff ); - append( vect, (word>>40) & 0xff ); - append( vect, (word>>48) & 0xff ); - append( vect, (word>>56) & 0xff ); - #endif -} - -void incrementConsumed( PdaRun *pdaRun ); -void decrementConsumed( PdaRun *pdaRun ); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/colm/pool.c b/colm/pool.c index dd0cfc7f..3a1cafbd 100644 --- a/colm/pool.c +++ b/colm/pool.c @@ -22,7 +22,7 @@ #include <string.h> #include <stdlib.h> -#include "pdarun2.h" +#include "pdarun.h" #include "pool.h" void initPoolAlloc( PoolAlloc *poolAlloc, int sizeofT ) diff --git a/colm/pool.h b/colm/pool.h index deb8a46c..8241a2d0 100644 --- a/colm/pool.h +++ b/colm/pool.h @@ -25,7 +25,7 @@ /* Allocation, number of items. */ #define FRESH_BLOCK 8128 -#include "pdarun2.h" +#include "pdarun.h" #include "map.h" #ifdef __cplusplus |