From baf56d05fd7c14bcd91e8d4a4563847a68a5451f Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Thu, 9 Jan 2020 15:27:39 +0200 Subject: colm: use a string's location (if present) when pushing to an input refs #103 --- colm/bytecode.c | 41 ++++++++++++++++++++++++++++++++++------- colm/input.c | 5 +++-- colm/input.h | 4 ++-- colm/pdarun.c | 26 -------------------------- colm/pdarun.h | 6 ------ colm/stream.c | 8 +++++++- 6 files changed, 46 insertions(+), 44 deletions(-) diff --git a/colm/bytecode.c b/colm/bytecode.c index af50f679..39aee070 100644 --- a/colm/bytecode.c +++ b/colm/bytecode.c @@ -200,6 +200,34 @@ static head_t *tree_to_str_postfix( program_t *prg, tree_t **sp, tree_t *tree, i return ret; } +static void input_push_text( struct colm_program *prg, struct input_impl *is, + struct colm_location *loc, const char *data, long length ) +{ + is->funcs->prepend_data( prg, is, loc, colm_alph_from_cstr( data ), length ); +} + +static void colm_stream_push_tree( struct colm_program *prg, struct input_impl *is, + tree_t *tree, int ignore ) +{ + is->funcs->prepend_tree( prg, is, tree, ignore ); +} + +static void colm_stream_push_stream( struct colm_program *prg, struct input_impl *is, stream_t *stream ) +{ + is->funcs->prepend_stream( prg, is, stream ); +} + +static void colm_undo_stream_push( program_t *prg, tree_t **sp, struct input_impl *is, long length ) +{ + if ( length < 0 ) { + /* tree_t *tree = */ is->funcs->undo_prepend_tree( prg, is ); + // colm_tree_downref( prg, sp, tree ); + } + else { + is->funcs->undo_prepend_data( prg, is, length ); + } +} + static word_t stream_append_text( program_t *prg, tree_t **sp, input_t *dest, tree_t *input, int trim ) { @@ -304,7 +332,7 @@ static void undo_pull( program_t *prg, input_t *input, tree_t *str ) undo_stream_pull( prg, impl, data, length ); } -static long stream_push( program_t *prg, tree_t **sp, struct input_impl *in, tree_t *tree, int ignore ) +static long input_push( program_t *prg, tree_t **sp, struct input_impl *in, tree_t *tree, int ignore ) { long length = -1; if ( tree->id == LEL_ID_PTR ) { @@ -320,10 +348,9 @@ static long stream_push( program_t *prg, tree_t **sp, struct input_impl *in, tre init_str_collect( &collect ); colm_print_tree_collect( prg, sp, &collect, tree, false ); - colm_stream_push_text( prg, in, collect.data, collect.length ); + input_push_text( prg, in, tree->tokdata->location, collect.data, collect.length ); length = collect.length; str_collect_destroy( &collect ); - } else { colm_tree_upref( prg, tree ); @@ -333,7 +360,7 @@ static long stream_push( program_t *prg, tree_t **sp, struct input_impl *in, tre return length; } -static long stream_push_stream( program_t *prg, tree_t **sp, +static long input_push_stream( program_t *prg, tree_t **sp, struct input_impl *in, stream_t *stream ) { colm_stream_push_stream( prg, in, stream ); @@ -2816,7 +2843,7 @@ again: input_t *input = vm_pop_input(); tree_t *tree = vm_pop_tree(); - long len = stream_push( prg, sp, input_to_impl( input ), tree, false ); + long len = input_push( prg, sp, input_to_impl( input ), tree, false ); vm_push_tree( 0 ); /* Single unit. */ @@ -2832,7 +2859,7 @@ again: input_t *input = vm_pop_input(); tree_t *tree = vm_pop_tree(); - long len = stream_push( prg, sp, input_to_impl( input ), tree, true ); + long len = input_push( prg, sp, input_to_impl( input ), tree, true ); vm_push_tree( 0 ); /* Single unit. */ @@ -2858,7 +2885,7 @@ again: input_t *input = vm_pop_input(); stream_t *to_push = vm_pop_stream(); - long len = stream_push_stream( prg, sp, input_to_impl( input ), to_push ); + long len = input_push_stream( prg, sp, input_to_impl( input ), to_push ); vm_push_tree( 0 ); /* Single unit. */ diff --git a/colm/input.c b/colm/input.c index 4342249a..043791f2 100644 --- a/colm/input.c +++ b/colm/input.c @@ -492,13 +492,14 @@ static void input_undo_consume_tree( struct colm_program *prg, struct input_impl * Prepend */ static void input_prepend_data( struct colm_program *prg, struct input_impl_seq *si, - const alph_t *data, long length ) + struct colm_location *loc, const alph_t *data, long length ) { debug( prg, REALM_INPUT, "input_prepend_data: stream %p prepend data length %d\n", si, length ); maybe_split( prg, si ); - struct stream_impl *sub_si = colm_impl_new_text( "", data, length ); + char *name = loc != 0 ? (char*)loc->name : ""; + struct stream_impl *sub_si = colm_impl_new_text( name, loc, data, length ); struct seq_buf *new_buf = new_seq_buf(); new_buf->type = SB_ACCUM; diff --git a/colm/input.h b/colm/input.h index c0a896d5..8cb20088 100644 --- a/colm/input.h +++ b/colm/input.h @@ -67,7 +67,7 @@ struct input_funcs \ 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, alph_t **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 alph_t *data, long len ); \ + void (*prepend_data)( struct colm_program *prg, struct _input_impl *si, struct colm_location *loc, const alph_t *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 ); \ @@ -222,7 +222,7 @@ struct colm_stream *colm_stream_open_collect( struct colm_program *prg ); char *colm_filename_add( struct colm_program *prg, const char *fn ); struct stream_impl *colm_impl_new_accum( char *name ); struct stream_impl *colm_impl_consumed( char *name, int len ); -struct stream_impl *colm_impl_new_text( char *name, const alph_t *data, int len ); +struct stream_impl *colm_impl_new_text( char *name, struct colm_location *loc, const alph_t *data, int len ); #ifdef __cplusplus } diff --git a/colm/pdarun.c b/colm/pdarun.c index b2c2ca66..f1885ec6 100644 --- a/colm/pdarun.c +++ b/colm/pdarun.c @@ -141,32 +141,6 @@ head_t *colm_stream_pull( program_t *prg, tree_t **sp, struct pda_run *pda_run, } } -void colm_stream_push_text( struct colm_program *prg, struct input_impl *is, const char *data, long length ) -{ - is->funcs->prepend_data( prg, is, colm_alph_from_cstr( data ), length ); -} - -void colm_stream_push_tree( struct colm_program *prg, struct input_impl *is, tree_t *tree, int ignore ) -{ - is->funcs->prepend_tree( prg, is, tree, ignore ); -} - -void colm_stream_push_stream( struct colm_program *prg, struct input_impl *is, stream_t *stream ) -{ - is->funcs->prepend_stream( prg, is, stream ); -} - -void colm_undo_stream_push( program_t *prg, tree_t **sp, struct input_impl *is, long length ) -{ - if ( length < 0 ) { - /* tree_t *tree = */ is->funcs->undo_prepend_tree( prg, is ); - // colm_tree_downref( prg, sp, tree ); - } - else { - is->funcs->undo_prepend_data( prg, is, length ); - } -} - /* Should only be sending back whole tokens/ignores, therefore the send back * should never cross a buffer boundary. Either we slide back data, or we move to * a previous buffer and slide back data. */ diff --git a/colm/pdarun.h b/colm/pdarun.h index 27e075c9..4003b9be 100644 --- a/colm/pdarun.h +++ b/colm/pdarun.h @@ -439,12 +439,6 @@ head_t *colm_stream_pull( struct colm_program *prg, struct colm_tree **sp, struct pda_run *pda_run, struct input_impl *is, long length ); head_t *colm_string_alloc_pointer( struct colm_program *prg, const char *data, long length ); -void colm_stream_push_text( struct colm_program *prg, struct input_impl *input_stream, const char *data, long length ); -void colm_stream_push_tree( struct colm_program *prg, struct input_impl *input_stream, tree_t *tree, int ignore ); -void colm_stream_push_stream( struct colm_program *prg, struct input_impl *input_stream, stream_t *stream ); -void colm_undo_stream_push( struct colm_program *prg, tree_t **sp, - struct input_impl *input_stream, long length ); - kid_t *make_token_with_data( struct colm_program *prg, struct pda_run *pda_run, struct input_impl *input_stream, int id, head_t *tokdata ); diff --git a/colm/stream.c b/colm/stream.c index abe1b636..77779aae 100644 --- a/colm/stream.c +++ b/colm/stream.c @@ -710,7 +710,7 @@ struct stream_impl *colm_impl_consumed( char *name, int len ) return (struct stream_impl*)si; } -struct stream_impl *colm_impl_new_text( char *name, const alph_t *data, int len ) +struct stream_impl *colm_impl_new_text( char *name, struct colm_location *loc, const alph_t *data, int len ) { struct stream_impl_data *si = (struct stream_impl_data*) malloc(sizeof(struct stream_impl_data)); @@ -723,6 +723,12 @@ struct stream_impl *colm_impl_new_text( char *name, const alph_t *data, int len si->data = buf; si->dlen = len; + if ( loc != 0 ) { + si->line = loc->line; + si->column = loc->column; + si->byte = loc->byte; + } + return (struct stream_impl*)si; } -- cgit v1.2.1