hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 3b177d58d707f0c26546041ffcfcc718cd8fc33b
parent 97e437f4777d4eeb05489203ce0f25865bb64aae
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat, 19 Feb 2022 19:12:19 -0500

unparse: unparse documentation comments

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mhare/parse/+test/roundtrip.ha | 2+-
Mhare/parse/+test/unit.ha | 20++++++--------------
Mhare/unparse/decl.ha | 18++++++++++++++++++
3 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/hare/parse/+test/roundtrip.ha b/hare/parse/+test/roundtrip.ha @@ -10,7 +10,7 @@ use strio; fn roundtrip(src: str) void = { let buf = bufio::fixed(strings::toutf8(src), mode::READ); - let lexer = lex::init(&buf, "<test>"); + let lexer = lex::init(&buf, "<test>", lex::flags::COMMENTS); let u = ast::subunit { imports = [], decls: []ast::decl = match (decls(&lexer)) { diff --git a/hare/parse/+test/unit.ha b/hare/parse/+test/unit.ha @@ -132,18 +132,10 @@ use strings; }; @test fn docs() void = { - const src = "// Example text\nexport fn main() void = void;\n"; - let buf = bufio::fixed(strings::toutf8(src), mode::READ); - let lexer = lex::init(&buf, "<test>", lex::flags::COMMENTS); - let decls = match (decls(&lexer)) { - case let decls: []ast::decl => - yield decls; - case let err: error => - fmt::errorln(strerror(err))!; - abort(); - }; - defer for (let i = 0z; i < len(decls); i += 1) { - ast::decl_free(decls[i]); - }; - assert(decls[0].docs == " Example text\n"); + roundtrip("// According to all known laws of aviation, there is no\n" + "// way that a bee should be able to fly. Its wings are too\n" + "// small to get its fat little body off the ground. The bee,\n" + "// of course, flies anyway, because bees don't care what\n" + "// humans think is impossible.\n" + "export fn main() void = void;\n"); }; diff --git a/hare/unparse/decl.ha b/hare/unparse/decl.ha @@ -2,11 +2,15 @@ use io; use fmt; use hare::ast; use hare::lex; +use strings; use strio; // Unparses an [[ast::decl]]. export fn decl(out: io::handle, d: ast::decl) (size | io::error) = { let n = 0z; + if (len(d.docs) > 0) { + n += comment(out, d.docs, 0)?; + }; if (d.exported) { n += fmt::fprint(out, "export ")?; }; @@ -88,6 +92,20 @@ export fn decl(out: io::handle, d: ast::decl) (size | io::error) = { return n; }; +fn comment(out: io::handle, s: str, indent: size) (size | io::error) = { + let n = 0z; + const lines = strings::split(s, "\n"); + defer free(lines); + assert(len(lines) > 0); + for (let i = 0z; i < len(lines) - 1; i += 1) { + for (let j = 0z; j < indent; j += 1) { + n += fmt::fprint(out, "\t")?; + }; + n += fmt::fprintfln(out, "//{}", lines[i])?; + }; + return n; +}; + fn decl_test(d: ast::decl, expected: str) bool = { let buf = strio::dynamic(); decl(&buf, d) as size;