commit 09d52064248c09473d5c2533764e12718cce60c5
parent 87b46ba774b65122d4b642dfa13feec3886f6ee0
Author: Alexey Yerin <yyp@disroot.org>
Date: Mon, 22 Mar 2021 21:21:22 +0300
lex: replace io::getrune by bufio::scanrune
Diffstat:
5 files changed, 4 insertions(+), 86 deletions(-)
diff --git a/hare/lex/lex.ha b/hare/lex/lex.ha
@@ -1,5 +1,6 @@
// hare::lex provides a lexer for Hare source code.
use ascii;
+use bufio;
use encoding::utf8;
use fmt;
use io;
@@ -469,7 +470,7 @@ fn next(lex: *lexer) (rune | io::EOF | io::error) = {
};
for (true) {
- return match (io::getrune(lex.in)) {
+ return match (bufio::scanrune(lex.in)) {
e: (io::EOF | io::error) => e,
r: rune => {
lexloc(lex, r);
diff --git a/io/+test/strings.ha b/io/+test/strings.ha
@@ -1,44 +0,0 @@
-use encoding::utf8;
-use rt;
-
-type bufstream = struct {
- stream: stream,
- buf: []u8,
-};
-
-@test fn getrune() void = {
- let bs = bufstream {
- stream = stream {
- name = "buffer",
- reader = &bs_read,
- ...
- },
- buf = [
- 0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81,
- 0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0x00,
- ],
- };
- let in = &bs.stream;
- const expected: [_](rune | utf8::invalid | EOF | error) = [
- 'こ', 'ん', 'に', 'ち', 'は', '\0', EOF,
- ];
- for (let i = 0z; i < len(expected); i += 1) {
- let want = expected[i];
- match (getrune(in)) {
- r: rune => assert(want is rune && want as rune == r),
- EOF => assert(want is EOF),
- * => abort(),
- };
- };
-};
-
-fn bs_read(s: *stream, buf: []u8) (size | error | EOF) = {
- let stream = s: *bufstream;
- if (len(stream.buf) == 0) {
- return EOF;
- };
- const n = if (len(buf) > len(stream.buf)) len(stream.buf) else len(buf);
- buf[..n] = stream.buf[..n];
- stream.buf = stream.buf[n..];
- return n;
-};
diff --git a/io/strings.ha b/io/strings.ha
@@ -1,34 +0,0 @@
-use encoding::utf8;
-use types;
-
-// TODO: Do we want some kind of io::text_stream?
-
-// Reads a rune from a UTF-8 stream.
-export fn getrune(in: *io::stream) (rune | utf8::invalid | EOF | error) = {
- let b: [4]u8 = [0...];
- match (read(in, b[..1])) {
- e: (error | EOF) => return e,
- n: size => assert(n == 1),
- };
-
- const sz = utf8::utf8sz(b[0]);
- if (sz == types::SIZE_MAX) {
- return utf8::invalid;
- };
-
- if (sz == 1) {
- return b[0]: u32: rune;
- };
-
- match (read(in, b[1..sz])) {
- e: (error | EOF) => return e,
- n: size => assert(n == sz - 1),
- };
-
- let dec = utf8::decode(b[..sz]);
- return match (utf8::next(&dec)) {
- r: rune => r,
- utf8::invalid => utf8::invalid,
- (void | utf8::more) => EOF,
- };
-};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -321,7 +321,6 @@ gensrcs_io() {
limit.ha \
println.ha \
stream.ha \
- strings.ha \
tee.ha \
types.ha \
$*
@@ -336,8 +335,7 @@ io() {
gensrcs_io \
+test/copy.ha \
+test/limit.ha \
- +test/stream.ha \
- +test/strings.ha
+ +test/stream.ha
fi
gen_ssa io strings
}
diff --git a/stdlib.mk b/stdlib.mk
@@ -421,7 +421,6 @@ stdlib_io_srcs= \
$(STDLIB)/io/limit.ha \
$(STDLIB)/io/println.ha \
$(STDLIB)/io/stream.ha \
- $(STDLIB)/io/strings.ha \
$(STDLIB)/io/tee.ha \
$(STDLIB)/io/types.ha
@@ -1047,13 +1046,11 @@ testlib_io_srcs= \
$(STDLIB)/io/limit.ha \
$(STDLIB)/io/println.ha \
$(STDLIB)/io/stream.ha \
- $(STDLIB)/io/strings.ha \
$(STDLIB)/io/tee.ha \
$(STDLIB)/io/types.ha \
$(STDLIB)/io/+test/copy.ha \
$(STDLIB)/io/+test/limit.ha \
- $(STDLIB)/io/+test/stream.ha \
- $(STDLIB)/io/+test/strings.ha
+ $(STDLIB)/io/+test/stream.ha
$(TESTCACHE)/io/io.ssa: $(testlib_io_srcs) $(testlib_rt) $(testlib_strings)
@printf 'HAREC \t$@\n'