commit b64a5de6cc857e39f424578b756275559cd22f2f
parent 1803c26d00a8c55dbd14aeb87a17dfcd8a38cb6a
Author: Lou Woell <lou.woell@posteo.de>
Date: Tue, 16 Sep 2025 01:22:06 +0200
move identstr_trailing to util.ha
Diffstat:
3 files changed, 19 insertions(+), 27 deletions(-)
diff --git a/cmd/harehelper/+test/locate_test.ha b/cmd/harehelper/+test/locate_test.ha
@@ -1,11 +1,9 @@
-use bufio;
use dirs;
use fmt;
use hare::ast;
use hare::lex;
use hare::module;
use hare::parse;
-use memio;
use os;
use strings;
@@ -24,17 +22,7 @@ fn loc_test_fn (in: str, out: lex::location) void = {
tags = ["linux", "test"],
};
- let buf = memio::fixed(strings::toutf8(in));
- let sc = bufio::newscanner(&buf);
- defer bufio::finish(&sc);
- let lexer = lex::init(&sc, "<string>");
-
- let (id, trailing) = match (parse::ident_trailing(&lexer)) {
- case parse::error =>
- return void;
- case let r: (ast::ident, bool) =>
- yield r;
- };
+ let (id, trailing) = identstr_trailing(in)!;
defer ast::ident_free(id);
let loc: (lex::location | void) = void;
@@ -70,7 +58,7 @@ fn loc_test_fn (in: str, out: lex::location) void = {
@test fn locate_local_func() void = {
loc_test_fn("cmd::harehelper::loc_test_fn", lex::location {
path = "locate_test.ha",
- line = 13,
+ line = 11,
col = 20,
...
});
@@ -79,7 +67,7 @@ fn loc_test_fn (in: str, out: lex::location) void = {
@test fn locate_global_multiple1 () void = {
loc_test_fn("cmd::harehelper::s", lex::location {
path = "locate_test.ha",
- line = 12,
+ line = 10,
col = 18,
...
});
@@ -88,7 +76,7 @@ fn loc_test_fn (in: str, out: lex::location) void = {
@test fn locate_global_multiple2 () void = {
loc_test_fn("cmd::harehelper::d", lex::location {
path = "locate_test.ha",
- line = 12,
+ line = 10,
col = 18,
...
});
@@ -97,7 +85,7 @@ fn loc_test_fn (in: str, out: lex::location) void = {
@test fn locate_const_multiple1 () void = {
loc_test_fn("cmd::harehelper::q", lex::location {
path = "locate_test.ha",
- line = 10,
+ line = 8,
col = 13,
...
});
@@ -106,7 +94,7 @@ fn loc_test_fn (in: str, out: lex::location) void = {
@test fn locate_const_multiple2 () void = {
loc_test_fn("cmd::harehelper::w", lex::location {
path = "locate_test.ha",
- line = 10,
+ line = 8,
col = 13,
...
});
diff --git a/cmd/harehelper/locate.ha b/cmd/harehelper/locate.ha
@@ -12,14 +12,12 @@
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
-use bufio;
use fmt;
use getopt::{command};
use hare::ast;
use hare::lex;
use hare::module;
use hare::parse;
-use memio;
use os;
use path;
use strings;
@@ -28,12 +26,7 @@ fn locate (cmd: command, ctx: *module::context) void = {
if(len(cmd.args) == 0) fmt::fatal("Expected Argument");
- let in = memio::fixed(strings::toutf8(cmd.args[0]));
- let sc = bufio::newscanner(&in);
- defer bufio::finish(&sc);
- let lexer = lex::init(&sc, "<string>");
-
- let (id, trailing) = match (parse::ident_trailing(&lexer)) {
+ let (id, trailing) = match (identstr_trailing(cmd.args[0])) {
case parse::error =>
return void;
case let r: (ast::ident, bool) =>
@@ -50,7 +43,6 @@ fn locate (cmd: command, ctx: *module::context) void = {
loc = locate_module(ctx, id);
};
-
match (loc) {
case void=> void;
case let d: lex::location =>
diff --git a/cmd/harehelper/util.ha b/cmd/harehelper/util.ha
@@ -21,6 +21,7 @@ use hare::lex;
use hare::module;
use hare::parse;
use hare::unparse;
+use memio;
use io;
use os;
use strings;
@@ -75,6 +76,17 @@ fn decls_finish (decls: []ast::decl) void = {
};
+// See [[parse::identstr]] and [[parse::ident_trailing]].
+fn identstr_trailing (in: str) ((ast::ident, bool) | parse::error) = {
+ let in = memio::fixed(strings::toutf8(in));
+ let sc = bufio::newscanner(&in);
+ defer bufio::finish(&sc);
+ let lexer = lex::init(&sc, "<string>");
+ let ret = parse::ident_trailing(&lexer)?;
+ parse::want(&lexer, lex::ltok::EOF)?;
+ return ret;
+};
+
// The functions below are taken from haredoc (included with hare stdlib)
// GPL-3-only (C) the hare authors