summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@colm.net>2019-05-22 18:51:58 -0400
committerAdrian Thurston <thurston@colm.net>2019-05-22 18:51:58 -0400
commit11bcbbea88466f75990314f58dcc66cbf76ab75b (patch)
treedd74b0a9a1b8e2ab59a178983564f5032655fd4b /src
parenteebed98e759b328f0f0e8ef2fd0afe8f2adf2720 (diff)
downloadcolm-11bcbbea88466f75990314f58dcc66cbf76ab75b.tar.gz
added and auto_trim flag to stream anad input
When set to true, all tree prints to the stream or sends to the input will result in a trim. Achieved by passing trim to the generic print.
Diffstat (limited to 'src')
-rw-r--r--src/bytecode.c26
-rw-r--r--src/bytecode.h2
-rw-r--r--src/ctinput.cc4
-rw-r--r--src/declare.cc9
-rw-r--r--src/input.c10
-rw-r--r--src/input.h5
-rw-r--r--src/stream.c12
7 files changed, 64 insertions, 4 deletions
diff --git a/src/bytecode.c b/src/bytecode.c
index ee238b31..fbb94646 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -212,7 +212,7 @@ static word_t stream_append_text( program_t *prg, tree_t **sp, input_t *dest, tr
/* Collect the tree data. */
str_collect_t collect;
init_str_collect( &collect );
- colm_print_tree_collect( prg, sp, &collect, input, false );
+ colm_print_tree_collect( prg, sp, &collect, input, ((struct input_impl_seq*)impl)->auto_trim );
/* Load it into the input. */
impl->funcs->append_data( prg, impl, collect.data, collect.length );
@@ -2510,6 +2510,30 @@ again:
vm_push_stream( stream );
break;
}
+ case IN_INPUT_AUTO_TRIM_WC: {
+ debug( prg, REALM_BYTECODE, "IN_INPUT_AUTO_TRIM_WC\n" );
+
+ stream_t *stream = vm_pop_stream();
+ value_t auto_trim = vm_pop_value();
+ struct stream_impl *si = stream->impl;
+
+ si->funcs->auto_trim( prg, si, (long) auto_trim );
+
+ vm_push_stream( stream );
+ break;
+ }
+ case IN_IINPUT_AUTO_TRIM_WC: {
+ debug( prg, REALM_BYTECODE, "IN_INPUT_AUTO_TRIM_WC\n" );
+
+ input_t *input = vm_pop_input();
+ value_t auto_trim = vm_pop_value();
+ struct input_impl *ii = input->impl;
+
+ ii->funcs->auto_trim( prg, ii, (long) auto_trim );
+
+ vm_push_input( input );
+ break;
+ }
case IN_SET_ERROR: {
debug( prg, REALM_BYTECODE, "IN_SET_ERROR\n" );
diff --git a/src/bytecode.h b/src/bytecode.h
index 8793c909..7197cc36 100644
--- a/src/bytecode.h
+++ b/src/bytecode.h
@@ -251,6 +251,8 @@ typedef unsigned char uchar;
#define IN_INPUT_PULL_BKT 0x9f
#define IN_INPUT_CLOSE_WC 0xef
+#define IN_INPUT_AUTO_TRIM_WC 0x82
+#define IN_IINPUT_AUTO_TRIM_WC 0x83
#define IN_PARSE_FRAG_W 0xa2
#define IN_PARSE_INIT_BKT 0xa1
diff --git a/src/ctinput.cc b/src/ctinput.cc
index 0625249d..a18a7ffb 100644
--- a/src/ctinput.cc
+++ b/src/ctinput.cc
@@ -270,6 +270,8 @@ input_funcs_ct pat_funcs =
0, 0, 0, 0, 0, 0, /* prepend funcs. */
0, 0, 0, 0, 0, 0, /* append funcs */
+ 0,
+
&ct_set_eof_mark,
&ct_transfer_loc_seq,
@@ -487,6 +489,8 @@ input_funcs_ct repl_funcs =
0, 0, 0, 0, 0, 0, /* prepend. */
0, 0, 0, 0, 0, 0, /* append. */
+ 0,
+
&ct_set_eof_mark,
&ct_transfer_loc_seq,
diff --git a/src/declare.cc b/src/declare.cc
index d521e7c6..884c446a 100644
--- a/src/declare.cc
+++ b/src/declare.cc
@@ -50,6 +50,7 @@ void Compiler::initUniqueTypes( )
uniqeTypeMap.insert( uniqueTypeIgnore );
uniqeTypeMap.insert( uniqueTypeAny );
+ uniqeTypeMap.insert( uniqueTypeInput );
uniqeTypeMap.insert( uniqueTypeStream );
}
@@ -888,6 +889,9 @@ void Compiler::declareInputFields( )
initFunction( uniqueTypeVoid, inputObj, ObjectMethod::Call, "close",
IN_INPUT_CLOSE_WC, IN_INPUT_CLOSE_WC, false );
+ initFunction( uniqueTypeVoid, inputObj, ObjectMethod::Call, "auto_trim",
+ IN_IINPUT_AUTO_TRIM_WC, IN_IINPUT_AUTO_TRIM_WC, uniqueTypeBool, false );
+
declareInputField( inputObj, 0 );
}
@@ -910,6 +914,9 @@ void Compiler::declareStreamFields( )
initFunction( uniqueTypeVoid, streamObj, ObjectMethod::Call, "close",
IN_INPUT_CLOSE_WC, IN_INPUT_CLOSE_WC, false );
+ initFunction( uniqueTypeVoid, streamObj, ObjectMethod::Call, "auto_trim",
+ IN_INPUT_AUTO_TRIM_WC, IN_INPUT_AUTO_TRIM_WC, uniqueTypeBool, false );
+
declareStreamField( streamObj, 0 );
}
@@ -1485,7 +1492,7 @@ void Compiler::initParserFunctions( GenericType *gen )
initFunction( gen->elUt, gen->objDef, ObjectMethod::ParseFinish, "eof",
IN_PARSE_FRAG_W, IN_PARSE_FRAG_W, true );
- initFunction( uniqueTypeStream, gen->objDef, ObjectMethod::Call, "gets",
+ initFunction( uniqueTypeInput, gen->objDef, ObjectMethod::Call, "gets",
IN_GET_PARSER_STREAM, IN_GET_PARSER_STREAM, true );
}
diff --git a/src/input.c b/src/input.c
index a0b7a337..4a7d9124 100644
--- a/src/input.c
+++ b/src/input.c
@@ -196,11 +196,18 @@ static void input_stream_seq_prepend( struct input_impl_seq *is, struct seq_buf
}
}
+static void input_auto_trim( struct colm_program *prg, struct input_impl_seq *ii, int auto_trim )
+{
+ ii->auto_trim = auto_trim ? 1 : 0;
+}
+
+
void input_set_eof_mark( struct colm_program *prg, struct input_impl_seq *si, char eof_mark )
{
si->eof_mark = eof_mark;
}
+
static void input_destructor( program_t *prg, tree_t **sp, struct input_impl_seq *si )
{
struct seq_buf *buf = si->queue.head;
@@ -686,6 +693,9 @@ struct input_funcs_seq input_funcs =
&input_append_stream,
&input_undo_append_stream,
+ /* Trimming */
+ &input_auto_trim,
+
/* EOF */
&input_set_eof_mark,
diff --git a/src/input.h b/src/input.h
index 6e3b16b6..58f0e2ae 100644
--- a/src/input.h
+++ b/src/input.h
@@ -76,6 +76,7 @@ struct input_funcs \
struct colm_tree *(*undo_append_tree)( struct colm_program *prg, struct _input_impl *si ); \
void (*append_stream)( struct colm_program *prg, struct _input_impl *si, struct colm_stream *stream ); \
struct colm_tree *(*undo_append_stream)( struct colm_program *prg, struct _input_impl *si ); \
+ void (*auto_trim)( struct colm_program *prg, struct _input_impl *si, int auto_trim ); \
void (*set_eof_mark)( struct colm_program *prg, struct _input_impl *si, char eof_mark ); \
void (*transfer_loc)( struct colm_program *prg, struct colm_location *loc, struct _input_impl *si ); \
void (*destructor)( struct colm_program *prg, struct colm_tree **sp, struct _input_impl *si ); \
@@ -93,6 +94,7 @@ struct stream_funcs \
struct colm_str_collect *(*get_collect)( struct colm_program *prg, struct _stream_impl *si ); \
void (*flush_stream)( struct colm_program *prg, struct _stream_impl *si ); \
void (*close_stream)( struct colm_program *prg, struct _stream_impl *si ); \
+ void (*auto_trim)( struct colm_program *prg, struct _stream_impl *si, int auto_trim ); \
void (*print_tree)( struct colm_program *prg, struct colm_tree **sp, \
struct _stream_impl *impl, struct colm_tree *tree, int trim ); \
struct stream_impl *(*split_consumed)( struct colm_program *prg, struct _stream_impl *si ); \
@@ -149,6 +151,7 @@ struct input_impl_seq
struct seq_buf *stash;
int consumed;
+ int auto_trim;
};
struct run_buf
@@ -196,6 +199,8 @@ struct stream_impl_data
int *line_len;
int lines_alloc;
int lines_cur;
+
+ int auto_trim;
};
void stream_impl_push_line( struct stream_impl_data *ss, int ll );
diff --git a/src/stream.c b/src/stream.c
index ca6e3641..56ea3612 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -390,13 +390,18 @@ static void data_close_stream( struct colm_program *prg, struct stream_impl_data
}
}
+static void data_auto_trim( struct colm_program *prg, struct stream_impl_data *si, int auto_trim )
+{
+ si->auto_trim = auto_trim ? 1 : 0;
+}
+
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, false );
+ colm_print_tree_file( prg, sp, (struct stream_impl*)si, tree, si->auto_trim );
else if ( si->collect != 0 )
- colm_print_tree_collect( prg, sp, si->collect, tree, false );
+ colm_print_tree_collect( prg, sp, si->collect, tree, si->auto_trim );
}
static int data_get_parse_block( struct colm_program *prg, struct stream_impl_data *ss, int *pskip, char **pdp, int *copied )
@@ -593,7 +598,9 @@ struct stream_funcs_data file_funcs =
&data_get_collect,
&data_flush_stream,
&data_close_stream,
+ &data_auto_trim,
&data_print_tree,
+
&data_split_consumed,
&data_append_data,
&data_undo_append_data,
@@ -613,6 +620,7 @@ struct stream_funcs_data accum_funcs =
&data_get_collect,
&data_flush_stream,
&data_close_stream,
+ &data_auto_trim,
&data_print_tree,
&data_split_consumed,