Autumn!: 2 scanner: rename format to formaterr scanner: generate doc comments 2 files changed, 82 insertions(+), 38 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~sircmpwn/hare-dev/patches/44634/mbox | git am -3Learn more about email & git
Signed-off-by: Autumn! <autumnull@posteo.net> --- wayland/scanner/scan.ha | 74 ++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/wayland/scanner/scan.ha b/wayland/scanner/scan.ha index e51f760..1bc827d 100644 --- a/wayland/scanner/scan.ha +++ b/wayland/scanner/scan.ha @@ -6,14 +6,14 @@ use strconv; use strings; // Returned when scanning an XML file which does not meet the expected schema. -export type format = !void; +export type formaterr = !void; // Errors which can occur while parsing protocols. -export type error = !(format | xml::error); +export type error = !(formaterr | xml::error); export fn strerror(err: error) const str = { match (err) { - case format => + case formaterr => return "XML does not fulfill required schema"; case let err: xml::error => return xml::strerror(err); @@ -34,10 +34,10 @@ fn scan_proto(par: *xml::parser) (protocol | error) = { case "protocol" => yield; case => - return format; + return formaterr; }; case => - return format; + return formaterr; }; let name = ""; @@ -51,7 +51,7 @@ fn scan_proto(par: *xml::parser) (protocol | error) = { case "name" => name = strings::dup(at.1); case => - return format; + return formaterr; }; case let el: xml::elementstart => switch (el) { @@ -64,12 +64,12 @@ fn scan_proto(par: *xml::parser) (protocol | error) = { case "interface" => append(ifaces, scan_iface(par)?); case => - return format; + return formaterr; }; case xml::elementend => break; case => - return format; + return formaterr; }; }; @@ -85,7 +85,7 @@ fn scan_copyright(par: *xml::parser) (void | error) = { for (true) { match (xml::scan(par)?) { case void => - return format; + return formaterr; case let tok: xml::token => match (tok) { case xml::elementend => @@ -110,10 +110,10 @@ fn scan_iface(par: *xml::parser) (interface | error) = { case let u: uint => iface.version = u; case => - return format; + return formaterr; }; case => - return format; + return formaterr; }; case let el: xml::elementstart => switch (el) { @@ -128,12 +128,12 @@ fn scan_iface(par: *xml::parser) (interface | error) = { case "enum" => append(iface.enums, scan_enum(par)?); case => - return format; + return formaterr; }; case xml::elementend => break; case => - return format; + return formaterr; }; }; return iface; @@ -144,7 +144,7 @@ fn scan_description(par: *xml::parser) ((str, str) | error) = { for (true) { match (xml::scan(par)?) { case void => - return format; + return formaterr; case let t: xml::token => match (t) { case let tx: xml::text => @@ -154,12 +154,12 @@ fn scan_description(par: *xml::parser) ((str, str) | error) = { case "summary" => summary = strings::dup(at.1); case => - return format; + return formaterr; }; case xml::elementend => break; case => - return format; + return formaterr; }; }; }; @@ -182,10 +182,10 @@ fn scan_request(par: *xml::parser) (request | error) = { case let u: uint => req.since = u; case => - return format; + return formaterr; }; case => - return format; + return formaterr; }; case let el: xml::elementstart => switch (el) { @@ -196,12 +196,12 @@ fn scan_request(par: *xml::parser) (request | error) = { req.summary = desc.0; req.description = desc.1; case => - return format; + return formaterr; }; case xml::elementend => break; case => - return format; + return formaterr; }; }; return req; @@ -220,12 +220,12 @@ fn scan_event(par: *xml::parser) (event | error) = { case let u: uint => ev.since = u; case => - return format; + return formaterr; }; case "type" => ev.type_ = strings::dup(at.1); case => - return format; + return formaterr; }; case let el: xml::elementstart => switch (el) { @@ -236,12 +236,12 @@ fn scan_event(par: *xml::parser) (event | error) = { ev.summary = desc.0; ev.description = desc.1; case => - return format; + return formaterr; }; case let e: xml::elementend => break; case => - return format; + return formaterr; }; }; return ev; @@ -266,12 +266,12 @@ fn scan_arg(par: *xml::parser) (argument | error) = { case "summary" => arg.summary = strings::dup(at.1); case => - return format; + return formaterr; }; case xml::elementend => break; case => - return format; + return formaterr; }; }; return arg; @@ -290,12 +290,12 @@ fn scan_enum(par: *xml::parser) (enum_ | error) = { case let u: uint => en.since = u; case => - return format; + return formaterr; }; case "bitfield" => en.bitfield = at.1 == "true"; case => - return format; + return formaterr; }; case let el: xml::elementstart => switch (el) { @@ -306,12 +306,12 @@ fn scan_enum(par: *xml::parser) (enum_ | error) = { en.summary = desc.0; en.description = desc.1; case => - return format; + return formaterr; }; case xml::elementend => break; case => - return format; + return formaterr; }; }; return en; @@ -332,22 +332,22 @@ fn scan_entry(par: *xml::parser) (enum_entry | error) = { case let u: uint => ent.since = u; case => - return format; + return formaterr; }; case "summary" => ent.summary = strings::dup(at.1); case => - return format; + return formaterr; }; case let em: xml::elementstart => if (em != "description") { - return format; + return formaterr; }; until(par, "description")?; case xml::elementend => break; case => - return format; + return formaterr; }; }; return ent; @@ -358,7 +358,7 @@ fn want(par: *xml::parser) (xml::token | error) = { case let tok: xml::token => yield tok; case void => - return format; + return formaterr; }; for (tok is xml::text) { @@ -366,7 +366,7 @@ fn want(par: *xml::parser) (xml::token | error) = { case let tok: xml::token => yield tok; case void => - return format; + return formaterr; }; }; @@ -406,7 +406,7 @@ fn strtype(s: str) (argtype | error) = { return map[i].1; }; }; - return format; + return formaterr; }; // Frees resources associated with a [[protocol]]. -- 2.42.0
Thanks! To git@git.sr.ht:~sircmpwn/hare-wayland e59ffa5..6ec748c master -> master
Signed-off-by: Autumn! <autumnull@posteo.net> --- future work: adapt identifiers from wl_thing to [[thing]] cmd/scanner/main.ha | 46 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/cmd/scanner/main.ha b/cmd/scanner/main.ha index e744148..a5ec649 100644 --- a/cmd/scanner/main.ha +++ b/cmd/scanner/main.ha @@ -102,6 +102,7 @@ use wayland; // interface function def interface_src: str = ` +$comment export type $ifname = struct { object: wayland::object, listener: nullable *$ifname_listener, @@ -121,7 +122,7 @@ export fn $ifname_obj_finish(o: *wayland::object) void = { free(o); }; -export const $ifname_interface: wayland::interface = wayland::interface { +export const $ifname_interface = wayland::interface { name = "$ifacename", version = $ifaceversion, requests = [$requests @@ -165,6 +166,7 @@ def arg_src: str = ` // request_function function def request_function_src: str = ` +$comment export fn $ifname_$name(obj: *$ifname$fnargs) $rettype = { let buf = memio::dynamic();$inner match (wayland::queue_request(obj, $opcode, memio::buffer(&buf))) { @@ -202,6 +204,7 @@ export fn $ifname_$evname_unmarshal(id: wayland::object_id, conn: *wayland::clie // listener function def listener_src: str = ` +// a set of callback functions for [[$ifname]] export type $ifname_listener = struct { $events}; @@ -305,7 +308,11 @@ fn interface(state: *state, iface: *scanner::interface) void = { )!; }; + let comment = doc_comment(iface.description); + defer free(comment); + template::execute(&interface_template, os::stdout, + ("comment", comment), ("ifname", get_ifname(iface)), ("ifacename", iface.name), // TODO: escape me ("ifaceversion", iface.version), @@ -405,7 +412,11 @@ fn request_function( yield ""; }; + let comment = doc_comment(req.description); + defer free(comment); + template::execute(&request_function_template, os::stdout, + ("comment", comment), ("ifname", get_ifname(iface)), ("name", req.name), ("opcode", opcode), @@ -448,7 +459,11 @@ fn event_callback( fmt::fprintf(&evargs, ", ev_arg_{}", ev.args[i].name)!; }; + let comment = doc_comment(ev.description); + defer free(comment); + template::execute(&eventcallback_template, os::stdout, + ("comment", comment), ("ifname", get_ifname(iface)), ("evname", ev.name), ("argscb", memio::string(&argscb)!), @@ -465,6 +480,9 @@ fn listener( defer io::close(&events)!; for (let i = 0z; i < len(iface.events); i += 1) { + let comment = doc_comment(iface.events[i].description); + defer free(comment); + fmt::fprintln(&events, comment)!; fmt::fprintfln(&events, "\t{}: nullable *{}_{}_cb,", iface.events[i].name, get_ifname(iface), @@ -492,7 +510,11 @@ fn enum_( iface: *scanner::interface, em: *scanner::enum_, ) void = { + let comment = doc_comment(em.description); + defer free(comment); + fmt::println()!; + fmt::println(comment)!; fmt::printfln("export type {}_{} = enum u32 {{", iface.name, em.name)!; for (let i = 0z; i < len(em.entries); i += 1) { @@ -505,6 +527,28 @@ fn enum_( fmt::printfln("}};")!; }; +fn doc_comment(desc: str) str = { + const buf = memio::dynamic(); + const bs = strings::toutf8(strings::trim(desc, ' ', '\t', '\n')); + let start = 0z; + for (let i = 0z; i < len(bs)) switch (bs[i]) { + case '\n' => + let line = strings::fromutf8(bs[start..i])!; + fmt::fprintln(&buf, "//", line)!; + i += 1; + for (i < len(bs)) switch (bs[i]) { + case ' ', '\t' => i += 1; + case => break; + }; + start = i; + case => i += 1; + }; + let line = strings::fromutf8(bs[start..])!; + // NOTE: not fprintln. the comment should not end in a newline. + fmt::fprint(&buf, "//", line)!; + return memio::string(&buf)!; +}; + fn ememb_tohare(s: str) str = { const buf = memio::dynamic(); const iter = strings::iter(s); -- 2.42.0