diff options
author | Adrian Thurston <thurston@colm.net> | 2018-06-25 20:56:56 +0700 |
---|---|---|
committer | Adrian Thurston <thurston@colm.net> | 2018-06-25 20:56:56 +0700 |
commit | 970034563818bc6e976953465d74424c773f2a57 (patch) | |
tree | 945aba4029257ea076abad2e414c6e39e48a9887 /src | |
parent | d1829badfa217ab54a5a8f96eb4af1f1d382ea55 (diff) | |
download | colm-970034563818bc6e976953465d74424c773f2a57.tar.gz |
balancing pushes and pulls
Diffstat (limited to 'src')
-rw-r--r-- | src/bytecode.c | 7 | ||||
-rw-r--r-- | src/input.c | 31 | ||||
-rw-r--r-- | src/pdarun.c | 6 | ||||
-rw-r--r-- | src/pdarun.h | 2 |
4 files changed, 25 insertions, 21 deletions
diff --git a/src/bytecode.c b/src/bytecode.c index 220812bc..4e8d8e61 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -305,6 +305,13 @@ static tree_t *stream_pull_bc( program_t *prg, tree_t **sp, struct pda_run *pda_ return construct_string( prg, tokdata ); } + +static void undo_stream_pull( struct colm_program *prg, struct stream_impl *is, const char *data, long length ) +{ + //debug( REALM_PARSE, "undoing stream pull\n" ); + is->funcs->undo_consume_data( prg, is, data, length ); +} + static void undo_pull( program_t *prg, stream_t *stream, tree_t *str ) { struct stream_impl *impl = stream_to_impl( stream ); diff --git a/src/input.c b/src/input.c index e56a4b8c..9a185c67 100644 --- a/src/input.c +++ b/src/input.c @@ -513,7 +513,8 @@ static int text_get_data_source( struct colm_program *prg, struct stream_impl_da { long avail = si->dlen - si->offset; long take = avail < want ? avail : want; - memcpy( dest, si->data + si->offset, take ); + if ( take > 0 ) + memcpy( dest, si->data + si->offset, take ); si->offset += take; return take; } @@ -848,32 +849,33 @@ static int stream_undo_consume_data( struct colm_program *prg, struct stream_imp { //debug( REALM_INPUT, "stream %p, undoing consume of %ld bytes\n", is, length ); - if ( is->consumed == 0 && is_source_stream( is ) ) { - struct stream_impl *si = is->queue->si; - int pushed_back = si->funcs->undo_consume_data( prg, si, data, length ); + assert ( is->consumed == 0 && is_source_stream( is ) ); - if ( pushed_back < length ) { - struct seq_buf *b = input_stream_pop_stash( is ); - input_stream_seq_prepend( is, b ); - pushed_back += stream_undo_consume_data( prg, is, data + pushed_back, length - pushed_back ); - } + struct stream_impl *si = is->queue->si; + int pushed_back = si->funcs->undo_consume_data( prg, si, data, length ); - return pushed_back; + if ( pushed_back < length ) { + struct seq_buf *b = input_stream_pop_stash( is ); + input_stream_seq_prepend( is, b ); + pushed_back += stream_undo_consume_data( prg, is, data + pushed_back, length - pushed_back ); } + + return pushed_back; } static tree_t *stream_consume_tree( struct colm_program *prg, struct stream_impl_seq *is ) { + debug( prg, REALM_INPUT, "consume tree\n" ); + while ( is->queue != 0 && ( is->queue->type == SEQ_BUF_SOURCE_TYPE ) ) { + debug( prg, REALM_INPUT, " consume: clearing source type\n" ); struct seq_buf *seq_buf = input_stream_seq_pop_head( is ); - free( seq_buf ); + input_stream_stash_head( is, seq_buf ); } assert( is->queue != 0 && ( is->queue->type == SEQ_BUF_TOKEN_TYPE || is->queue->type == SEQ_BUF_IGNORE_TYPE ) ); - if ( is->queue != 0 && (is->queue->type == SEQ_BUF_TOKEN_TYPE || - is->queue->type == SEQ_BUF_IGNORE_TYPE) ) { struct seq_buf *seq_buf = input_stream_seq_pop_head( is ); input_stream_stash_head( is, seq_buf ); @@ -886,6 +888,7 @@ static tree_t *stream_consume_tree( struct colm_program *prg, struct stream_impl static void stream_undo_consume_tree( struct colm_program *prg, struct stream_impl_seq *is, tree_t *tree, int ignore ) { + debug( prg, REALM_INPUT, "undo consume tree\n" ); struct seq_buf *b = input_stream_pop_stash( is ); input_stream_seq_prepend( is, b ); assert( b->tree == tree ); @@ -934,6 +937,8 @@ static void stream_append_data( struct colm_program *prg, struct stream_impl_seq static void stream_prepend_tree( struct colm_program *prg, struct stream_impl_seq *is, tree_t *tree, int ignore ) { + debug( prg, REALM_INPUT, "prepend tree\n" ); + /* 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. */ diff --git a/src/pdarun.c b/src/pdarun.c index 04acc1cd..a1be290b 100644 --- a/src/pdarun.c +++ b/src/pdarun.c @@ -141,12 +141,6 @@ head_t *colm_stream_pull( program_t *prg, tree_t **sp, struct pda_run *pda_run, } } -void undo_stream_pull( struct colm_program *prg, struct stream_impl *is, const char *data, long length ) -{ - //debug( REALM_PARSE, "undoing stream pull\n" ); - - is->funcs->prepend_data( prg, is, data, length ); -} void colm_stream_push_text( struct colm_program *prg, struct stream_impl *is, const char *data, long length ) { diff --git a/src/pdarun.h b/src/pdarun.h index 1ac564ca..9190b430 100644 --- a/src/pdarun.h +++ b/src/pdarun.h @@ -70,8 +70,6 @@ struct fsm_tables long num_action_switch; }; -void undo_stream_pull( struct colm_program *prg, struct stream_impl *input_stream, const char *data, long length ); - #if SIZEOF_LONG != 4 && SIZEOF_LONG != 8 #error "SIZEOF_LONG contained an unexpected value" #endif |