commit e7abf41b23140c1ea3c6e00b32f8818b93f9070f
parent e1df2adc62afad7ecc1818d932de6e53b7f15969
Author: Andres Navarro <canavarro82@gmail.com>
Date: Fri, 22 Apr 2011 00:30:11 -0300
Added a workaround to the problem of the dangling '\n' from previous line in the repl altering the source code info. Now the source code info for the repl is from the first significant char of that prompt.
Diffstat:
4 files changed, 30 insertions(+), 9 deletions(-)
diff --git a/src/kread.c b/src/kread.c
@@ -534,13 +534,26 @@ TValue kread_peek_char_from_port(klisp_State *K, TValue port, bool peek)
{
K->curr_port = port;
K->curr_in = kport_file(port);
- /* only needed if not peek, but do it anyways */
- ktok_set_source_info(K, kport_filename(port),
- kport_line(port), kport_col(port));
- int ch = peek? ktok_peekc(K) : ktok_getc(K);
- TValue res = ch == EOF? KEOF : ch2tv((char)ch);
- /* same as above */
- kport_update_source_info(port, K->ktok_source_info.line,
- K->ktok_source_info.col);
- return res;
+ int ch;
+ if (peek) {
+ ch = ktok_peekc(K);
+ } else {
+ ktok_set_source_info(K, kport_filename(port),
+ kport_line(port), kport_col(port));
+ ch = ktok_getc(K);
+ kport_update_source_info(port, K->ktok_source_info.line,
+ K->ktok_source_info.col);
+ }
+ return ch == EOF? KEOF : ch2tv((char)ch);
+}
+
+/* This is needed by the repl to ignore trailing spaces (especially newlines)
+ that could affect the source info */
+void kread_ignore_whitespace_and_comments_from_port(klisp_State *K,
+ TValue port)
+{
+ K->curr_port = port;
+ K->curr_in = kport_file(port);
+ /* source code info isn't important because it will be reset later */
+ ktok_ignore_whitespace_and_comments(K);
}
diff --git a/src/kread.h b/src/kread.h
@@ -16,5 +16,8 @@
TValue kread_from_port(klisp_State *K, TValue port, bool mut);
TValue kread_peek_char_from_port(klisp_State *K, TValue port, bool peek);
+void kread_ignore_whitespace_and_comments_from_port(klisp_State *K,
+ TValue port);
+
#endif
diff --git a/src/krepl.c b/src/krepl.c
@@ -43,6 +43,9 @@ void read_fn(klisp_State *K, TValue *xparams, TValue obj)
TValue port = kcdr(K->kd_in_port_key);
klisp_assert(kport_file(port) == stdin);
+ /* workaround to the problem of the dangling '\n' in repl
+ (from previous line) */
+ kread_ignore_whitespace_and_comments_from_port(K, port);
kport_reset_source_info(port);
obj = kread_from_port(K, port, true); /* read mutable pairs */
kapply_cc(K, obj);
diff --git a/src/ktoken.h b/src/ktoken.h
@@ -29,6 +29,8 @@ void clear_shared_dict(klisp_State *K);
/* This is used in for peek-char & read-char */
int ktok_getc(klisp_State *K);
int ktok_peekc(klisp_State *K);
+/* needed by the repl */
+void ktok_ignore_whitespace_and_comments(klisp_State *K);
/* This is needed for string->symbol to check if a symbol has external
representation as an identifier */