summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2015-06-21 18:00:42 -0400
committerAdrian Thurston <thurston@complang.org>2015-06-21 18:00:42 -0400
commit8c5de8279774f8acac0d0143927d3d8992aecdea (patch)
treedbcaf0a784ea5fe75996855a154b04091fc977dc
parent3d8f3c6a214ba93ef3e8f40e87a69636bbff5e5b (diff)
downloadcolm-8c5de8279774f8acac0d0143927d3d8992aecdea.tar.gz
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.
-rw-r--r--src/input.h2
-rw-r--r--src/tree.c37
2 files changed, 39 insertions, 0 deletions
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" );