From 8c5de8279774f8acac0d0143927d3d8992aecdea Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Sun, 21 Jun 2015 18:00:42 -0400 Subject: testing some indentation-normalization code Want to be able to reformat indentation during code generation. This first bit of code will just flatten. Need to add indentation level tracking. --- src/input.h | 2 ++ src/tree.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/input.h b/src/input.h index 25a1825f..9811c21e 100644 --- a/src/input.h +++ b/src/input.h @@ -143,6 +143,8 @@ struct stream_impl struct ConsItem *cons_item; int consumed; + + int indent; }; struct stream_impl *colm_impl_new_pat( const char *name, struct Pattern *pattern ); diff --git a/src/tree.c b/src/tree.c index 40393599..3155428b 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1697,6 +1697,43 @@ void append_file( struct colm_print_args *args, const char *data, int length ) fwrite( data, 1, length, impl->file ); } +void append_file_indent( struct colm_print_args *args, const char *data, int length ) +{ + struct stream_impl *impl = (struct stream_impl*) args->arg; +restart: + if ( impl->indent ) { + /* Consume. */ + while ( length > 0 && ( *data == ' ' || *data == '\t' ) ) { + data += 1; + length -= 1; + } + + if ( length > 0 ) { + impl->indent = 0; + goto restart; + } + } + else { + char *nl = memchr( data, '\n', length ); + if ( nl ) { + int wl = nl - data + 1; + fwrite( data, 1, wl, impl->file ); + /* print indentation. */ + + + /* go into consume state. */ + data += wl; + length -= wl; + impl->indent = 1; + goto restart; + } + else { + fwrite( data, 1, length, impl->file ); + } + } +} + + tree_t *tree_trim( struct colm_program *prg, tree_t **sp, tree_t *tree ) { debug( prg, REALM_PARSE, "attaching left ignore\n" ); -- cgit v1.2.1