From 835c359b551831b8272dc71e5feaf702aa189576 Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Sun, 28 Jun 2015 18:01:52 -0400 Subject: increment and decrement absolute indentation on _IN_ and _EX_ types We can send these out when we are sending to a stream, or we can embed these types in parsers. --- src/global.h | 1 + src/input.c | 6 +++++- src/input.h | 2 ++ src/tree.c | 47 +++++++++++++++++++++++++++++++++++------------ src/tree.h | 2 ++ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/global.h b/src/global.h index 553d580e..1c74880f 100644 --- a/src/global.h +++ b/src/global.h @@ -62,6 +62,7 @@ extern bool printStatistics; extern int gblErrorCount; extern bool gblLibrary; +extern long gblActiveRealm; extern char machineMain[]; extern const char *exportHeaderFn; diff --git a/src/input.c b/src/input.c index 3fda62c7..5d6e3dcd 100644 --- a/src/input.c +++ b/src/input.c @@ -352,9 +352,13 @@ void init_stream_impl( struct stream_impl *is, const char *name ) is->line = 1; is->column = 1; is->byte = 0; + + /* Indentation turned off. */ + is->level = COLM_INDENT_OFF; } -void colm_clear_stream_impl( struct colm_program *prg, tree_t **sp, struct stream_impl *input_stream ) +void colm_clear_stream_impl( struct colm_program *prg, tree_t **sp, + struct stream_impl *input_stream ) { RunBuf *buf = input_stream->queue; while ( buf != 0 ) { diff --git a/src/input.h b/src/input.h index 9811c21e..2f26a2b8 100644 --- a/src/input.h +++ b/src/input.h @@ -144,6 +144,8 @@ struct stream_impl int consumed; + /* Indentation. */ + int level; int indent; }; diff --git a/src/tree.c b/src/tree.c index 3155428b..e40849c1 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1693,41 +1693,46 @@ void append_collect( struct colm_print_args *args, const char *data, int length void append_file( struct colm_print_args *args, const char *data, int length ) { + int level; struct stream_impl *impl = (struct stream_impl*) args->arg; - 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. */ + /* Consume mode. */ while ( length > 0 && ( *data == ' ' || *data == '\t' ) ) { data += 1; length -= 1; } if ( length > 0 ) { + /* Found some data, print the indentation and turn off indentation + * mode. */ + for ( level = 0; level < impl->level; level++ ) + fputc( '\t', impl->file ); + impl->indent = 0; + goto restart; } } else { - char *nl = memchr( data, '\n', length ); - if ( nl ) { + char *nl; + if ( impl->level != COLM_INDENT_OFF && + (nl = memchr( data, '\n', length )) ) + { + /* Print up to and including the newline. */ int wl = nl - data + 1; fwrite( data, 1, wl, impl->file ); - /* print indentation. */ - - /* go into consume state. */ + /* Go into consume state. If we see more non-indentation chars we + * will generate the appropriate indentation level. */ data += wl; length -= wl; impl->indent = 1; goto restart; } else { + /* Indentation off, or no indent trigger (newline). */ fwrite( data, 1, length, impl->file ); } } @@ -1871,7 +1876,9 @@ rec_call: /* Print the leading ignore list. Also implement the suppress right * in the process. */ - if ( print_args->comm && (!print_args->trim || (flags & TF_TERM_SEEN && kid->tree->id > 0)) ) { + if ( print_args->comm && (!print_args->trim || + (flags & TF_TERM_SEEN && kid->tree->id > 0)) ) + { ignore = leading_ignore; while ( ignore != 0 ) { if ( ignore->tree->flags & AF_SUPPRESS_RIGHT ) @@ -2044,6 +2051,22 @@ void colm_print_term_tree( program_t *prg, tree_t **sp, print_args->out( print_args, string_data( kid->tree->tokdata ), string_length( kid->tree->tokdata ) ); } + + struct lang_el_info *lel_info = prg->rtd->lel_info; + struct stream_impl *impl = (struct stream_impl*) print_args->arg; + + if ( strcmp( lel_info[kid->tree->id].name, "_IN_" ) == 0 ) { + if ( impl->level == COLM_INDENT_OFF ) { + impl->level = 1; + impl->indent = 1; + } + else { + impl->level += 1; + } + } + + if ( strcmp( lel_info[kid->tree->id].name, "_EX_" ) == 0 ) + impl->level -= 1; } diff --git a/src/tree.h b/src/tree.h index 67f3a199..1b1ee7a4 100644 --- a/src/tree.h +++ b/src/tree.h @@ -31,6 +31,8 @@ extern "C" { #include #include +#define COLM_INDENT_OFF -1 + typedef unsigned char code_t; typedef unsigned long word_t; typedef unsigned long half_t; -- cgit v1.2.1