diff options
author | Adrian Thurston <thurston@complang.org> | 2011-12-18 05:27:35 +0000 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2011-12-18 05:27:35 +0000 |
commit | 565c3a3f47cf13bbd2c4a5062222bcc1a019bd1b (patch) | |
tree | 71b80e32741a9c02a0d9c43920758ce7e9bfc23d /colm/input.c | |
parent | 74f998987ea41022c6449543b633acc0ca5c189a (diff) | |
download | colm-565c3a3f47cf13bbd2c4a5062222bcc1a019bd1b.tar.gz |
Some progress towards making the more intricate parsing examples working with
split input structs working. Better, but still a ways to go.
Diffstat (limited to 'colm/input.c')
-rw-r--r-- | colm/input.c | 210 |
1 files changed, 124 insertions, 86 deletions
diff --git a/colm/input.c b/colm/input.c index 96376dbc..6d987408 100644 --- a/colm/input.c +++ b/colm/input.c @@ -819,16 +819,22 @@ int getDataImpl( InputStream *is, char *dest, int length ) //dynamicFuncs.getTree = &inputStreamDynamicGetTree; Tree *getTree( InputStream *is ) { - if ( is->queue != 0 && is->queue->type == RunBufTokenType ) { - RunBuf *runBuf = inputStreamPopHead2( is ); - - /* FIXME: using runbufs here for this is a poor use of memory. */ - Tree *tree = runBuf->tree; - free(runBuf); - return tree; + if ( isSourceStream( is ) ) { + Stream *stream = (Stream*)is->queue->tree; + return stream->in->funcs->getTree( stream->in ); } + else { + if ( is->queue != 0 && is->queue->type == RunBufTokenType ) { + RunBuf *runBuf = inputStreamPopHead2( is ); - return 0; + /* FIXME: using runbufs here for this is a poor use of memory. */ + Tree *tree = runBuf->tree; + free(runBuf); + return tree; + } + + return 0; + } } struct LangEl *getLangEl( InputStream *is, long *bindId, char **data, long *length ) @@ -853,101 +859,127 @@ void pushBackNamed( InputStream *is ) //dynamicFuncs.pushTree = &inputStreamDynamicPushTree; void pushTree( InputStream *is, Tree *tree, int ignore ) { -// #ifdef COLM_LOG_PARSE -// if ( colm_log_parse ) { -// cerr << "readying fake push" << endl; -// } -// #endif - -// takeBackBuffered( inputStream ); - - /* Create a new buffer for the data. This is the easy implementation. - * Something better is needed here. It puts a max on the amount of - * data that can be pushed back to the inputStream. */ - RunBuf *newBuf = newRunBuf(); - newBuf->type = ignore ? RunBufIgnoreType : RunBufTokenType; - newBuf->tree = tree; - - inputStreamPrepend2( is, newBuf ); + if ( isSourceStream( is ) ) { + Stream *stream = (Stream*)is->queue->tree; + return stream->in->funcs->pushTree( stream->in, tree, ignore ); + } + else { + // #ifdef COLM_LOG_PARSE + // if ( colm_log_parse ) { + // cerr << "readying fake push" << endl; + // } + // #endif + + // takeBackBuffered( inputStream ); + + /* Create a new buffer for the data. This is the easy implementation. + * Something better is needed here. It puts a max on the amount of + * data that can be pushed back to the inputStream. */ + RunBuf *newBuf = newRunBuf(); + newBuf->type = ignore ? RunBufIgnoreType : RunBufTokenType; + newBuf->tree = tree; + + inputStreamPrepend2( is, newBuf ); + } } //dynamicFuncs.pushText = &inputStreamDynamicPushText; void pushText( InputStream *is, const char *data, long length ) { -// #ifdef COLM_LOG_PARSE -// if ( colm_log_parse ) { -// cerr << "readying fake push" << endl; -// } -// #endif + if ( isSourceStream( is ) ) { + Stream *stream = (Stream*)is->queue->tree; + return stream->in->funcs->pushText( stream->in, data, length ); + } + else { + // #ifdef COLM_LOG_PARSE + // if ( colm_log_parse ) { + // cerr << "readying fake push" << endl; + // } + // #endif -// takeBackBuffered( inputStream ); + // takeBackBuffered( inputStream ); - /* Create a new buffer for the data. This is the easy implementation. - * Something better is needed here. It puts a max on the amount of - * data that can be pushed back to the inputStream. */ - assert( length < FSM_BUFSIZE ); + /* Create a new buffer for the data. This is the easy implementation. + * Something better is needed here. It puts a max on the amount of + * data that can be pushed back to the inputStream. */ + assert( length < FSM_BUFSIZE ); - RunBuf *newBuf = newRunBuf(); - newBuf->length = length; - memcpy( newBuf->data, data, length ); + RunBuf *newBuf = newRunBuf(); + newBuf->length = length; + memcpy( newBuf->data, data, length ); - pushBackBuf( is, newBuf ); + pushBackBuf( is, newBuf ); + } } //dynamicFuncs.undoPush = &inputStreamDynamicUndoPush; Tree *undoPush( InputStream *is, int length ) { - if ( is->queue->type == RunBufDataType ) { - char tmp[length]; - int have = 0; - while ( have < length ) { - int res = getData( is, tmp, length-have ); - have += res; - } - return 0; + if ( isSourceStream( is ) ) { + Stream *stream = (Stream*)is->queue->tree; + return stream->in->funcs->undoPush( stream->in, length ); } else { - /* FIXME: leak here. */ - RunBuf *rb = inputStreamPopHead2( is ); - Tree *tree = rb->tree; - free(rb); - return tree; + if ( is->queue->type == RunBufDataType ) { + char tmp[length]; + int have = 0; + while ( have < length ) { + int res = getData( is, tmp, length-have ); + have += res; + } + return 0; + } + else { + /* FIXME: leak here. */ + RunBuf *rb = inputStreamPopHead2( is ); + Tree *tree = rb->tree; + free(rb); + return tree; + } } } //accumFuncs.appendData = &inputStreamAccumAppendData; -void appendData( InputStream *_is, const char *data, long len ) +void appendData( InputStream *is, const char *data, long len ) { - InputStream *is = (InputStream*)_is; - - while ( len > 0 ) { - RunBuf *ad = newRunBuf(); - inputStreamAppend2( is, ad ); + if ( isSourceStream( is ) ) { + Stream *stream = (Stream*)is->queue->tree; + return stream->in->funcs->appendData( stream->in, data, len ); + } + else { + while ( len > 0 ) { + RunBuf *ad = newRunBuf(); + inputStreamAppend2( is, ad ); - long consume = - len <= (long)sizeof(ad->data) ? - len : (long)sizeof(ad->data); + long consume = + len <= (long)sizeof(ad->data) ? + len : (long)sizeof(ad->data); - memcpy( ad->data, data, consume ); - ad->length = consume; + memcpy( ad->data, data, consume ); + ad->length = consume; - len -= consume; - data += consume; + len -= consume; + data += consume; + } } } //accumFuncs.appendTree = &inputStreamAccumAppendTree; -void appendTree( InputStream *_is, Tree *tree ) +void appendTree( InputStream *is, Tree *tree ) { - InputStream *is = (InputStream*)_is; - - RunBuf *ad = newRunBuf(); + if ( isSourceStream( is ) ) { + Stream *stream = (Stream*)is->queue->tree; + return stream->in->funcs->appendTree( stream->in, tree ); + } + else { + RunBuf *ad = newRunBuf(); - inputStreamAppend2( is, ad ); + inputStreamAppend2( is, ad ); - ad->type = RunBufTokenType; - ad->tree = tree; - ad->length = 0; + ad->type = RunBufTokenType; + ad->tree = tree; + ad->length = 0; + } } void appendStream( InputStream *in, struct ColmTree *tree ) @@ -964,21 +996,27 @@ void appendStream( InputStream *in, struct ColmTree *tree ) //dynamicFuncs.undoAppend = &inputStreamDynamicUndoAppend; Tree *undoAppend( InputStream *is, int length ) { - if ( is->queueTail->type == RunBufDataType ) { - char tmp[length]; - int have = 0; - while ( have < length ) { - int res = inputStreamDynamicGetDataRev2( is, tmp, length-have ); - have += res; - } - return 0; + if ( isSourceStream( is ) ) { + Stream *stream = (Stream*)is->queue->tree; + return stream->in->funcs->undoAppend( stream->in, length ); } else { - /* FIXME: leak here. */ - RunBuf *rb = inputStreamPopTail2( is ); - Tree *tree = rb->tree; - free(rb); - return tree; + if ( is->queueTail->type == RunBufDataType ) { + char tmp[length]; + int have = 0; + while ( have < length ) { + int res = inputStreamDynamicGetDataRev2( is, tmp, length-have ); + have += res; + } + return 0; + } + else { + /* FIXME: leak here. */ + RunBuf *rb = inputStreamPopTail2( is ); + Tree *tree = rb->tree; + free(rb); + return tree; + } } } @@ -987,7 +1025,7 @@ void pushBackBuf( InputStream *is, RunBuf *runBuf ) { if ( isSourceStream( is ) ) { Stream *stream = (Stream*)is->queue->tree; - inputStreamPrepend( stream->in, runBuf ); + stream->in->funcs->pushBackBuf( stream->in, runBuf ); } else { inputStreamPrepend2( is, runBuf ); |