roundtrip.ha (1154B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use bufio; 5 use fmt; 6 use hare::ast; 7 use hare::lex; 8 use hare::unparse; 9 use io; 10 use io::{mode}; 11 use memio; 12 use strings; 13 use types; 14 15 fn roundtrip(src: str) void = { 16 let unsrc = _roundtrip(src); 17 defer free(unsrc); 18 if (unsrc != src) { 19 fmt::errorfln("=== wanted\n{}", src)!; 20 fmt::errorfln("=== got\n{}", unsrc)!; 21 abort(); 22 }; 23 }; 24 25 fn roundtrip_reparse(src: str) void = { 26 let unsrc = _roundtrip(src); 27 defer free(unsrc); 28 roundtrip(unsrc); 29 }; 30 31 fn _roundtrip(src: str) str = { 32 let buf = memio::fixed(strings::toutf8(src)); 33 let sc = bufio::newscanner(&buf, types::SIZE_MAX); 34 defer bufio::finish(&sc); 35 let lexer = lex::init(&sc, "<test>", lex::flag::COMMENTS); 36 let u = ast::subunit { 37 imports = [], 38 decls: []ast::decl = match (decls(&lexer)) { 39 case let decls: []ast::decl => 40 yield decls; 41 case let err: error => 42 fmt::errorln(strerror(err))!; 43 abort(); 44 }, 45 }; 46 defer ast::subunit_finish(u); 47 let out = memio::dynamic(); 48 let z = unparse::subunit(&out, &unparse::syn_wrap, u)!; 49 let unsrc = memio::string(&out)!; 50 assert(z == len(unsrc)); 51 return unsrc; 52 };