summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@colm.net>2019-05-25 21:13:16 -0400
committerAdrian Thurston <thurston@colm.net>2019-05-25 21:13:16 -0400
commitf0926b394144cbf11bcdfe65892624b1f4e0cc72 (patch)
tree6f016104709671cec1d71206318fbdaf955bb461 /src
parente52169ed10b0bf0011c2471e1fa7644971d0c7ac (diff)
downloadcolm-f0926b394144cbf11bcdfe65892624b1f4e0cc72.tar.gz
support _IN_ _EX_ indentation in string accumulators
Diffstat (limited to 'src')
-rw-r--r--src/colm.h8
-rw-r--r--src/exports.cc3
-rw-r--r--src/input.h5
-rw-r--r--src/loadcolm.cc2
-rw-r--r--src/print.c87
-rw-r--r--src/stream.c5
-rw-r--r--src/tree.h5
7 files changed, 65 insertions, 50 deletions
diff --git a/src/colm.h b/src/colm.h
index aac0ee2c..55368840 100644
--- a/src/colm.h
+++ b/src/colm.h
@@ -35,6 +35,13 @@ struct colm_sections;
struct colm_tree;
struct colm_location;
+struct indent_impl
+{
+ /* Indentation. */
+ int level;
+ int indent;
+};
+
extern struct colm_sections colm_object;
typedef unsigned long colm_value_t;
@@ -59,6 +66,7 @@ struct colm_print_args
int comm;
int attr;
int trim;
+ struct indent_impl *indent;
void (*out)( struct colm_print_args *args, const char *data, int length );
void (*open_tree)( struct colm_program *prg, struct colm_tree **sp,
diff --git a/src/exports.cc b/src/exports.cc
index 6b9e2858..49228eff 100644
--- a/src/exports.cc
+++ b/src/exports.cc
@@ -74,7 +74,8 @@ void Compiler::generateExports()
"inline std::string printTreeStr( colm_program *prg, colm_tree *tree, bool trim )\n"
"{\n"
" std::string str;\n"
- " colm_print_args printArgs = { &str, 1, 0, trim, &appendString, \n"
+ " struct indent_impl indent = { -1, 0 };\n"
+ " colm_print_args printArgs = { &str, 1, 0, trim, &indent, &appendString, \n"
" &colm_print_null, &colm_print_term_tree, &colm_print_null };\n"
" colm_print_tree_args( prg, colm_vm_root(prg), &printArgs, tree );\n"
" return str;\n"
diff --git a/src/input.h b/src/input.h
index 285ba6a1..f116561f 100644
--- a/src/input.h
+++ b/src/input.h
@@ -24,6 +24,7 @@
#define _COLM_INPUT_H
#include <stdio.h>
+#include "colm.h"
#ifdef __cplusplus
extern "C" {
@@ -194,9 +195,7 @@ struct stream_impl_data
int consumed;
- /* Indentation. */
- int level;
- int indent;
+ struct indent_impl indent;
int *line_len;
int lines_alloc;
diff --git a/src/loadcolm.cc b/src/loadcolm.cc
index 21f0217a..289b65e1 100644
--- a/src/loadcolm.cc
+++ b/src/loadcolm.cc
@@ -1278,12 +1278,10 @@ struct LoadColm
if ( expr->op == '^' ) {
trim = ConsItem::TrimYes;
expr = expr->right;
- std::cerr << "trim" << std::endl;
}
else if ( expr->op == '@' ) {
trim = ConsItem::TrimNo;
expr = expr->right;
- std::cerr << "notrim" << std::endl;
}
}
return expr;
diff --git a/src/print.c b/src/print.c
index b28c2606..317b197d 100644
--- a/src/print.c
+++ b/src/print.c
@@ -62,6 +62,8 @@ void init_str_collect( str_collect_t *collect )
collect->data = (char*) malloc( BUFFER_INITIAL_SIZE );
collect->allocated = BUFFER_INITIAL_SIZE;
collect->length = 0;
+ collect->indent.indent = 0;
+ collect->indent.level = COLM_INDENT_OFF;
}
void str_collect_destroy( str_collect_t *collect )
@@ -99,11 +101,15 @@ 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_data *impl = (struct stream_impl_data*) args->arg;
+ fwrite( data, 1, length, impl->file );
+}
+static void out_indent( struct colm_print_args *args, const char *data, int length )
+{
+ int level;
restart:
- if ( impl->indent ) {
+ if ( args->indent->indent ) {
/* Consume mode. */
while ( length > 0 && ( *data == ' ' || *data == '\t' ) ) {
data += 1;
@@ -113,33 +119,33 @@ restart:
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 );
+ for ( level = 0; level < args->indent->level; level++ )
+ args->out( args, "\t", 1 );
- impl->indent = 0;
+ args->indent->indent = 0;
goto restart;
}
}
else {
char *nl;
- if ( impl->level != COLM_INDENT_OFF &&
+ if ( args->indent->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 );
+ args->out( args, data, wl );
/* 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;
+ args->indent->indent = 1;
goto restart;
}
else {
/* Indentation off, or no indent trigger (newline). */
- fwrite( data, 1, length, impl->file );
+ args->out( args, data, length );
}
}
}
@@ -415,7 +421,7 @@ void colm_print_tree_args( program_t *prg, tree_t **sp,
struct colm_print_args *print_args, tree_t *tree )
{
if ( tree == 0 )
- print_args->out( print_args, "NIL", 3 );
+ out_indent( print_args, "NIL", 3 );
else {
/* This term tree allows us to print trailing ignores. */
tree_t term_tree;
@@ -440,19 +446,19 @@ void colm_print_null( program_t *prg, tree_t **sp,
}
void colm_print_term_tree( program_t *prg, tree_t **sp,
- struct colm_print_args *print_args, kid_t *kid )
+ struct colm_print_args *args, kid_t *kid )
{
debug( prg, REALM_PRINT, "printing term %p\n", kid->tree );
if ( kid->tree->id == LEL_ID_PTR ) {
char buf[INT_SZ];
- print_args->out( print_args, "#<", 2 );
+ out_indent( args, "#<", 2 );
sprintf( buf, "%lx", ((pointer_t*)kid->tree)->value );
- print_args->out( print_args, buf, strlen(buf) );
- print_args->out( print_args, ">", 1 );
+ out_indent( args, buf, strlen(buf) );
+ out_indent( args, ">", 1 );
}
else if ( kid->tree->id == LEL_ID_STR ) {
- print_str( print_args, ((str_t*)kid->tree)->value );
+ print_str( args, ((str_t*)kid->tree)->value );
}
// else if ( kid->tree->id == LEL_ID_STREAM ) {
// char buf[INT_SZ];
@@ -463,33 +469,32 @@ void colm_print_term_tree( program_t *prg, tree_t **sp,
else if ( kid->tree->tokdata != 0 &&
string_length( kid->tree->tokdata ) > 0 )
{
- print_args->out( print_args, string_data( kid->tree->tokdata ),
+ out_indent( args, string_data( kid->tree->tokdata ),
string_length( kid->tree->tokdata ) );
}
struct lang_el_info *lel_info = prg->rtd->lel_info;
- struct stream_impl_data *impl = (struct stream_impl_data*) 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;
+ if ( args->indent->level == COLM_INDENT_OFF ) {
+ args->indent->level = 1;
+ args->indent->indent = 1;
}
else {
- impl->level += 1;
+ args->indent->level += 1;
}
}
if ( strcmp( lel_info[kid->tree->id].name, "_EX_" ) == 0 )
- impl->level -= 1;
+ args->indent->level -= 1;
}
void colm_print_tree_collect( program_t *prg, tree_t **sp,
str_collect_t *collect, tree_t *tree, int trim )
{
struct colm_print_args print_args = {
- collect, true, false, trim, &append_collect,
- &colm_print_null, &colm_print_term_tree, &colm_print_null
+ collect, true, false, trim, &collect->indent,
+ &append_collect, &colm_print_null,
+ &colm_print_term_tree, &colm_print_null
};
colm_print_tree_args( prg, sp, &print_args, tree );
@@ -499,19 +504,21 @@ void colm_print_tree_collect_a( program_t *prg, tree_t **sp,
str_collect_t *collect, tree_t *tree, int trim )
{
struct colm_print_args print_args = {
- collect, true, true, trim, &append_collect,
- &colm_print_null, &colm_print_term_tree, &colm_print_null
+ collect, true, true, trim, &collect->indent,
+ &append_collect, &colm_print_null,
+ &colm_print_term_tree, &colm_print_null
};
colm_print_tree_args( prg, sp, &print_args, tree );
}
-void colm_print_tree_file( program_t *prg, tree_t **sp, struct stream_impl *impl,
- tree_t *tree, int trim )
+void colm_print_tree_file( program_t *prg, tree_t **sp,
+ struct stream_impl_data *impl, tree_t *tree, int trim )
{
struct colm_print_args print_args = {
- impl, true, false, trim, &append_file,
- &colm_print_null, &colm_print_term_tree, &colm_print_null
+ impl, true, false, trim, &impl->indent,
+ &append_file, &colm_print_null,
+ &colm_print_term_tree, &colm_print_null
};
colm_print_tree_args( prg, sp, &print_args, tree );
@@ -590,12 +597,12 @@ static void xml_close( program_t *prg, tree_t **sp,
}
void colm_print_xml_stdout( program_t *prg, tree_t **sp,
- struct stream_impl *impl, tree_t *tree,
+ struct stream_impl_data *impl, tree_t *tree,
int comm_attr, int trim )
{
struct colm_print_args print_args = {
- impl, comm_attr, comm_attr, trim, &append_file,
- &xml_open, &xml_term, &xml_close };
+ impl, comm_attr, comm_attr, trim, &impl->indent,
+ &append_file, &xml_open, &xml_term, &xml_close };
colm_print_tree_args( prg, sp, &print_args, tree );
}
@@ -724,8 +731,8 @@ void colm_postfix_tree_collect( program_t *prg, tree_t **sp,
str_collect_t *collect, tree_t *tree, int trim )
{
struct colm_print_args print_args = {
- collect, false, false, false, &append_collect,
- &postfix_open, &postfix_term, &postfix_close
+ collect, false, false, false, &collect->indent,
+ &append_collect, &postfix_open, &postfix_term, &postfix_close
};
colm_print_tree_args( prg, sp, &print_args, tree );
@@ -751,8 +758,8 @@ void colm_print_tree_collect_xml( program_t *prg, tree_t **sp,
str_collect_t *collect, tree_t *tree, int trim )
{
struct colm_print_args print_args = {
- collect, false, false, trim, &append_collect,
- &xml_open, &xml_term, &xml_close
+ collect, false, false, trim, &collect->indent,
+ &append_collect, &xml_open, &xml_term, &xml_close
};
colm_print_tree_args( prg, sp, &print_args, tree );
@@ -762,8 +769,8 @@ void colm_print_tree_collect_xml_ac( program_t *prg, tree_t **sp,
str_collect_t *collect, tree_t *tree, int trim )
{
struct colm_print_args print_args = {
- collect, true, true, trim, &append_collect,
- &xml_open, &xml_term, &xml_close
+ collect, true, true, trim, &collect->indent,
+ &append_collect, &xml_open, &xml_term, &xml_close
};
colm_print_tree_args( prg, sp, &print_args, tree );
diff --git a/src/stream.c b/src/stream.c
index 0af80fcf..341197e5 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -404,7 +404,7 @@ static void data_print_tree( struct colm_program *prg, tree_t **sp,
struct stream_impl_data *si, tree_t *tree, int trim )
{
if ( si->file != 0 )
- colm_print_tree_file( prg, sp, (struct stream_impl*)si, tree, trim );
+ colm_print_tree_file( prg, sp, si, tree, trim );
else if ( si->collect != 0 )
colm_print_tree_collect( prg, sp, si->collect, tree, trim );
}
@@ -649,7 +649,8 @@ static void si_data_init( struct stream_impl_data *is, char *name )
is->byte = 0;
/* Indentation turned off. */
- is->level = COLM_INDENT_OFF;
+ is->indent.level = COLM_INDENT_OFF;
+ is->indent.indent = 0;
}
struct stream_impl *colm_impl_new_accum( char *name )
diff --git a/src/tree.h b/src/tree.h
index ee05cdc3..fefb6b20 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -300,6 +300,7 @@ typedef struct colm_str_collect
char *data;
int allocated;
int length;
+ struct indent_impl indent;
} str_collect_t;
void init_str_collect( str_collect_t *collect );
@@ -315,9 +316,9 @@ void colm_print_tree_collect_a( struct colm_program *prg, tree_t **sp,
str_collect_t *collect, tree_t *tree, int trim );
void colm_print_tree_file( struct colm_program *prg, tree_t **sp,
- struct stream_impl *impl, tree_t *tree, int trim );
+ struct stream_impl_data *impl, tree_t *tree, int trim );
void colm_print_xml_stdout( struct colm_program *prg, tree_t **sp,
- struct stream_impl *impl, tree_t *tree, int comm_attr, int trim );
+ struct stream_impl_data *impl, tree_t *tree, int comm_attr, int trim );
void colm_postfix_tree_collect( struct colm_program *prg, tree_t **sp,
str_collect_t *collect, tree_t *tree, int trim );