summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2015-06-28 18:01:52 -0400
committerAdrian Thurston <thurston@complang.org>2015-06-28 18:01:52 -0400
commit835c359b551831b8272dc71e5feaf702aa189576 (patch)
tree58e15647d51659c5816eb1dbadbd406da7f7641b
parent16e9e149c06e7407afe57fac0072bb4c35e8cb80 (diff)
downloadcolm-835c359b551831b8272dc71e5feaf702aa189576.tar.gz
increment and decrement absolute indentation on _IN_ and _EX_ typescolm-barracuda-v5
We can send these out when we are sending to a stream, or we can embed these types in parsers.
-rw-r--r--src/global.h1
-rw-r--r--src/input.c6
-rw-r--r--src/input.h2
-rw-r--r--src/tree.c47
-rw-r--r--src/tree.h2
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 <colm/input.h>
#include <colm/internal.h>
+#define COLM_INDENT_OFF -1
+
typedef unsigned char code_t;
typedef unsigned long word_t;
typedef unsigned long half_t;