summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@colm.net>2018-07-13 09:59:05 +0800
committerAdrian Thurston <thurston@colm.net>2018-07-13 09:59:05 +0800
commit62007044d100c310a5f293e96e9376957cb38588 (patch)
tree6eda1eb6a1087d6225bdba3e579512690da2ba05 /src
parent6cd5ad2ab54bfef99a8372c8de34a58ea5184754 (diff)
downloadcolm-62007044d100c310a5f293e96e9376957cb38588.tar.gz
delegate splitting to the stream_impl class
Diffstat (limited to 'src')
-rw-r--r--src/input.c20
-rw-r--r--src/input.h73
-rw-r--r--src/stream.c41
3 files changed, 72 insertions, 62 deletions
diff --git a/src/input.c b/src/input.c
index a404b788..c6026328 100644
--- a/src/input.c
+++ b/src/input.c
@@ -110,24 +110,22 @@ static struct seq_buf *input_stream_pop_stash( struct colm_program *prg, struct
return seq_buf;
}
-
-static void maybe_split( struct colm_program *prg, struct input_impl_seq *si )
+static void maybe_split( struct colm_program *prg, struct input_impl_seq *iis )
{
- if ( si->queue.head != 0 && is_stream( si->queue.head ) ) {
-
- /* NOT A GOOD IDEA. Use func instead */
- struct stream_impl_data *sid = (struct stream_impl_data*)si->queue.head->si;
- if ( sid->consumed > 0 ) {
+ struct seq_buf *head = iis->queue.head;
+ if ( head != 0 && is_stream( head ) ) {
+ /* Maybe the stream will split itself off. */
+ struct stream_impl *split_off = head->si->funcs->split_consumed( prg, head->si );
+
+ if ( split_off != 0 ) {
debug( prg, REALM_INPUT, "maybe split: consumed is > 0, splitting\n" );
- struct stream_impl *sub_si = colm_impl_consumed( "<text>", sid->consumed );
- sid->consumed = 0;
struct seq_buf *new_buf = new_seq_buf();
new_buf->type = SB_ACCUM;
- new_buf->si = sub_si;
+ new_buf->si = split_off;
new_buf->own_si = 1;
- input_stream_stash_head( prg, si, new_buf );
+ input_stream_stash_head( prg, iis, new_buf );
}
}
}
diff --git a/src/input.h b/src/input.h
index e4cfbde4..13435b4e 100644
--- a/src/input.h
+++ b/src/input.h
@@ -82,49 +82,50 @@ struct run_buf
struct run_buf *new_run_buf( int sz );
-#define DEF_INPUT_FUNCS( input_funcs, input_impl ) \
+#define DEF_INPUT_FUNCS( input_funcs, _input_impl ) \
struct input_funcs \
{ \
- int (*get_parse_block)( struct colm_program *prg, struct input_impl *si, int *pskip, char **pdp, int *copied ); \
- int (*get_data)( struct colm_program *prg, struct input_impl *si, char *dest, int length ); \
- int (*consume_data)( struct colm_program *prg, struct input_impl *si, int length, struct colm_location *loc ); \
- int (*undo_consume_data)( struct colm_program *prg, struct input_impl *si, const char *data, int length ); \
- struct colm_tree *(*consume_tree)( struct colm_program *prg, struct input_impl *si ); \
- void (*undo_consume_tree)( struct colm_program *prg, struct input_impl *si, struct colm_tree *tree, int ignore ); \
- struct LangEl *(*consume_lang_el)( struct colm_program *prg, struct input_impl *si, long *bind_id, char **data, long *length ); \
- void (*undo_consume_lang_el)( struct colm_program *prg, struct input_impl *si ); \
- void (*prepend_data)( struct colm_program *prg, struct input_impl *si, const char *data, long len ); \
- int (*undo_prepend_data)( struct colm_program *prg, struct input_impl *si, int length ); \
- void (*prepend_tree)( struct colm_program *prg, struct input_impl *si, struct colm_tree *tree, int ignore ); \
- struct colm_tree *(*undo_prepend_tree)( struct colm_program *prg, struct input_impl *si ); \
- void (*prepend_stream)( struct colm_program *prg, struct input_impl *si, struct colm_stream *stream ); \
- struct colm_tree *(*undo_prepend_stream)( struct colm_program *prg, struct input_impl *si ); \
- void (*append_data)( struct colm_program *prg, struct input_impl *si, const char *data, long len ); \
- struct colm_tree *(*undo_append_data)( struct colm_program *prg, struct input_impl *si, int length ); \
- void (*append_tree)( struct colm_program *prg, struct input_impl *si, struct colm_tree *tree ); \
- struct colm_tree *(*undo_append_tree)( struct colm_program *prg, struct input_impl *si ); \
- void (*append_stream)( struct colm_program *prg, struct input_impl *si, struct colm_stream *stream ); \
- struct colm_tree *(*undo_append_stream)( struct colm_program *prg, struct input_impl *si ); \
- void (*set_eof_mark)( struct colm_program *prg, struct input_impl *si, char eof_mark ); \
- void (*transfer_loc)( struct colm_program *prg, struct colm_location *loc, struct input_impl *si ); \
- void (*destructor)( struct colm_program *prg, struct colm_tree **sp, struct input_impl *si ); \
+ int (*get_parse_block)( struct colm_program *prg, struct _input_impl *si, int *pskip, char **pdp, int *copied ); \
+ int (*get_data)( struct colm_program *prg, struct _input_impl *si, char *dest, int length ); \
+ int (*consume_data)( struct colm_program *prg, struct _input_impl *si, int length, struct colm_location *loc ); \
+ int (*undo_consume_data)( struct colm_program *prg, struct _input_impl *si, const char *data, int length ); \
+ struct colm_tree *(*consume_tree)( struct colm_program *prg, struct _input_impl *si ); \
+ void (*undo_consume_tree)( struct colm_program *prg, struct _input_impl *si, struct colm_tree *tree, int ignore ); \
+ struct LangEl *(*consume_lang_el)( struct colm_program *prg, struct _input_impl *si, long *bind_id, char **data, long *length ); \
+ void (*undo_consume_lang_el)( struct colm_program *prg, struct _input_impl *si ); \
+ void (*prepend_data)( struct colm_program *prg, struct _input_impl *si, const char *data, long len ); \
+ int (*undo_prepend_data)( struct colm_program *prg, struct _input_impl *si, int length ); \
+ void (*prepend_tree)( struct colm_program *prg, struct _input_impl *si, struct colm_tree *tree, int ignore ); \
+ struct colm_tree *(*undo_prepend_tree)( struct colm_program *prg, struct _input_impl *si ); \
+ void (*prepend_stream)( struct colm_program *prg, struct _input_impl *si, struct colm_stream *stream ); \
+ struct colm_tree *(*undo_prepend_stream)( struct colm_program *prg, struct _input_impl *si ); \
+ void (*append_data)( struct colm_program *prg, struct _input_impl *si, const char *data, long len ); \
+ struct colm_tree *(*undo_append_data)( struct colm_program *prg, struct _input_impl *si, int length ); \
+ void (*append_tree)( struct colm_program *prg, struct _input_impl *si, struct colm_tree *tree ); \
+ struct colm_tree *(*undo_append_tree)( struct colm_program *prg, struct _input_impl *si ); \
+ void (*append_stream)( struct colm_program *prg, struct _input_impl *si, struct colm_stream *stream ); \
+ struct colm_tree *(*undo_append_stream)( struct colm_program *prg, struct _input_impl *si ); \
+ void (*set_eof_mark)( struct colm_program *prg, struct _input_impl *si, char eof_mark ); \
+ void (*transfer_loc)( struct colm_program *prg, struct colm_location *loc, struct _input_impl *si ); \
+ void (*destructor)( struct colm_program *prg, struct colm_tree **sp, struct _input_impl *si ); \
}
-#define DEF_STREAM_FUNCS( stream_funcs, stream_impl ) \
+#define DEF_STREAM_FUNCS( stream_funcs, _stream_impl ) \
struct stream_funcs \
{ \
- int (*get_parse_block)( struct colm_program *prg, struct stream_impl *si, int *pskip, char **pdp, int *copied ); \
- int (*get_data)( struct colm_program *prg, struct stream_impl *si, char *dest, int length ); \
- int (*get_data_source)( struct colm_program *prg, struct stream_impl *si, char *dest, int length ); \
- int (*consume_data)( struct colm_program *prg, struct stream_impl *si, int length, struct colm_location *loc ); \
- int (*undo_consume_data)( struct colm_program *prg, struct stream_impl *si, const char *data, int length ); \
- void (*transfer_loc)( struct colm_program *prg, struct colm_location *loc, struct stream_impl *si ); \
- struct colm_str_collect *(*get_collect)( struct colm_program *prg, struct stream_impl *si ); \
- void (*flush_stream)( struct colm_program *prg, struct stream_impl *si ); \
- void (*close_stream)( struct colm_program *prg, struct stream_impl *si ); \
+ int (*get_parse_block)( struct colm_program *prg, struct _stream_impl *si, int *pskip, char **pdp, int *copied ); \
+ int (*get_data)( struct colm_program *prg, struct _stream_impl *si, char *dest, int length ); \
+ int (*get_data_source)( struct colm_program *prg, struct _stream_impl *si, char *dest, int length ); \
+ int (*consume_data)( struct colm_program *prg, struct _stream_impl *si, int length, struct colm_location *loc ); \
+ int (*undo_consume_data)( struct colm_program *prg, struct _stream_impl *si, const char *data, int length ); \
+ void (*transfer_loc)( struct colm_program *prg, struct colm_location *loc, struct _stream_impl *si ); \
+ struct colm_str_collect *(*get_collect)( struct colm_program *prg, struct _stream_impl *si ); \
+ void (*flush_stream)( struct colm_program *prg, struct _stream_impl *si ); \
+ void (*close_stream)( struct colm_program *prg, struct _stream_impl *si ); \
void (*print_tree)( struct colm_program *prg, struct colm_tree **sp, \
- struct stream_impl *impl, struct colm_tree *tree, int trim ); \
- void (*destructor)( struct colm_program *prg, struct colm_tree **sp, struct stream_impl *si ); \
+ struct _stream_impl *impl, struct colm_tree *tree, int trim ); \
+ struct stream_impl *(*split_consumed)( struct colm_program *prg, struct _stream_impl *si ); \
+ void (*destructor)( struct colm_program *prg, struct colm_tree **sp, struct _stream_impl *si ); \
}
DEF_INPUT_FUNCS( input_funcs, input_impl );
diff --git a/src/stream.c b/src/stream.c
index 8dc68c32..2a1ca491 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -56,20 +56,6 @@ static void close_stream_file( FILE *file )
}
}
-static void si_data_init( struct stream_impl_data *is, char *name )
-{
- memset( is, 0, sizeof(struct stream_impl_data) );
-
- is->type = 'D';
- is->name = name;
- is->line = 1;
- is->column = 1;
- is->byte = 0;
-
- /* Indentation turned off. */
- is->level = COLM_INDENT_OFF;
-}
-
static void si_data_push_tail( struct stream_impl_data *ss, struct run_buf *run_buf )
{
if ( ss->queue.head == 0 ) {
@@ -227,6 +213,16 @@ static int data_get_data( struct colm_program *prg, struct stream_impl_data *ss,
return copied;
}
+static struct stream_impl *data_split_consumed( program_t *prg, struct stream_impl_data *sid )
+{
+ struct stream_impl *split_off = 0;
+ if ( sid->consumed > 0 ) {
+ debug( prg, REALM_INPUT, "maybe split: consumed is > 0, splitting\n" );
+ split_off = colm_impl_consumed( "<text>", sid->consumed );
+ sid->consumed = 0;
+ }
+ return split_off;
+}
static void data_destructor( program_t *prg, tree_t **sp, struct stream_impl_data *si )
{
@@ -454,6 +450,7 @@ struct stream_funcs_data file_funcs =
&data_flush_stream,
&data_close_stream,
&data_print_tree,
+ &data_split_consumed,
&data_destructor,
};
@@ -472,9 +469,24 @@ struct stream_funcs_data accum_funcs =
&data_close_stream,
&data_print_tree,
+ &data_split_consumed,
&data_destructor,
};
+static void si_data_init( struct stream_impl_data *is, char *name )
+{
+ memset( is, 0, sizeof(struct stream_impl_data) );
+
+ is->type = 'D';
+ is->name = name;
+ is->line = 1;
+ is->column = 1;
+ is->byte = 0;
+
+ /* Indentation turned off. */
+ is->level = COLM_INDENT_OFF;
+}
+
struct stream_impl *colm_impl_new_accum( char *name )
{
struct stream_impl_data *si = (struct stream_impl_data*)malloc(sizeof(struct stream_impl_data));
@@ -484,7 +496,6 @@ struct stream_impl *colm_impl_new_accum( char *name )
return (struct stream_impl*)si;
}
-
static struct stream_impl *colm_impl_new_file( char *name, FILE *file )
{
struct stream_impl_data *ss = (struct stream_impl_data*)malloc(sizeof(struct stream_impl_data));