#! @file walk.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the walk `args::parse` reads the arguments by, through what the #! program marks of the command it is given, the checks of those marks, and the #! forms of the types std gives: numbers, text, paths, `maybe`, and vectors. #! #! A field's type decides how it is read, which `if const` tells by the traits #! the type implements, and by the kind of its declaration, which only a type #! the program or std declares has: a switch, a `value`, a vector, an enum of #! choices, or a union of subcommands. Each step on a field is a function #! deciding so itself, since `if const` decides by types and kinds alone, not by #! what a function gives. #! #! The options of a command are counted in the order they are declared, those #! of a group where the group's field is, so each step on an option given by its #! index goes into the group holding it with the index within the group. import std::semver; import std::io; module std::args; # A type each argument for it is added to, as a vector is, rather than the last # being kept. trait repeated { def add_argument(&mut this, text: strview): result; } # A type that may be left out, as `maybe` is, so a positional argument of it # takes one only where one is left over. trait vacant { def vacant_argument(): self; } # How many positional arguments a field takes: one, one where one is left over, # or as many as are left over. enum shape: u8 { one, optional, many } # The index of no option. const no_option: u64 = 0xFFFFFFFFFFFFFFFF; # Reads the arguments into a command, after checking its marks. def read_command(args: span, reached: &mut reach): result where T: zeroable { check_command::(); return read_part::(args, 0, reached); } # Reads the arguments from an index into a command: a struct of options and # arguments, or a union of subcommands. def read_part(args: span, from: u64, reached: &mut reach): result where T: zeroable { if const T is switch || T is value || T is repeated { compile_error("The type is not `@command`, so its arguments are not read. Mark a struct or union `@command`, and each field to read `@option` or `@argument`."); return error { kind = error_kind::missing_command, at = from }; } else if const attached::(decl_of::()).length == 0 { compile_error("The type is not `@command`, so its arguments are not read. Mark it `@command`, and each field to read `@option` or `@argument`."); return error { kind = error_kind::missing_command, at = from }; } else if const decl_of::().kind == target::unions { return read_union::(args, from, reached); } else { return read_struct::(args, from, reached); } } # Reads a union of subcommands: the options it has, which are only those asking # for the help text and the version, then the word naming the subcommand, then # the subcommand's own arguments. def read_union(args: span, from: u64, reached: &mut reach): result where U: zeroable { var at := from; while at < args.length { val arg := args[at]; if arg == "--" { at++; break; } break if arg.length < 2 || arg[0] != '-'; if arg == "-h" || arg == "--help" { return asked(error_kind::help_asked, at, arg); } val version := attached::(decl_of::())[0].version; if arg == "--version" && version.length != 0 { reached.version = version; return asked(error_kind::version_asked, at, arg); } return error { kind = error_kind::unknown_option, at = at, text = arg, name = arg }; } if at >= args.length { return error { kind = error_kind::missing_command, at = args.length }; } val word := args[at]; for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::(field).length > 0 { if named_as(word, field.name, attached::(field)[0].name) { reached.words.append(word); var sub := read_part::(args, at + 1, reached) else (wrong) { return wrong; }; var made: U; made.(field) = move(sub); return move(made); } } } return error { kind = error_kind::unknown_command, at = at, text = word, name = word }; } # Reads a struct of options and positional arguments, the last of which may be # a union of subcommands, which every argument after its word belongs to. def read_struct(args: span, from: u64, reached: &mut reach): result where T: zeroable { var made: T; var seen := vector::::new(); var positional := vector::::new(); var after := vector::::new(); val before := arguments_before_command::(); val commanded := before != no_option; var named := false; var ended := false; var at := from; while at < args.length { val arg := args[at]; var plain := ended; if !ended { if arg == "--" { ended = true; } else if arg.length > 2 && arg[0] == '-' && arg[1] == '-' { _ = read_long::(&mut made, args, &mut at, &mut seen, reached) else (wrong) { return wrong; }; } else if arg.length > 1 && arg[0] == '-' && !(arg[1].is_digit && !digit_named::()) { _ = read_short::(&mut made, args, &mut at, &mut seen, reached) else (wrong) { return wrong; }; } else { plain = true; } } if plain { # The positional argument after those the fields before it take names a # subcommand, which reads the rest. if commanded && positional.length + after.length == before { _ = read_subcommand::(&mut made, args, at, reached) else (wrong) { return wrong; }; named = true; break; } if ended { after.append(at); } else { positional.append(at); } } at++; } if commanded && !named { return error { kind = error_kind::missing_command, at = args.length }; } val left := required_left::(&seen, 0); if left.length != 0 { return error { kind = error_kind::missing_option, at = args.length, name = keep($"--{left}") }; } _ = read_positional::(&mut made, args, &mut positional, &after) else (wrong) { return wrong; }; return move(made); } # Reads an option written by its long name, as `--name value` or `--name=value`, # taking its value from the next argument where none is attached, or the help # text or the version where it asks for them. def read_long(made: &mut T, args: span, at: &mut u64, seen: &mut vector, reached: &mut reach): result where T: zeroable { val arg := args[at]; var name := arg.substr(2, arg.length - 2); var value: strview = ""; var joined := false; for i in 0..name.length { if name[i] == '=' { value = name.substr(i + 1, name.length - i - 1); name = name.substr(0, i); joined = true; break; } } val written := arg.substr(0, name.length + 2); val which := option_named::(name); if which == no_option { if name == "help" && !joined && helps::(false) { return asked(error_kind::help_asked, at, arg); } val version := attached::(decl_of::())[0].version; if name == "version" && !joined && version.length != 0 { reached.version = version; return asked(error_kind::version_asked, at, arg); } return error { kind = error_kind::unknown_option, at = at, text = arg, name = written }; } seen.append(which); if !takes_value::(which) { if joined { return error { kind = error_kind::switch_given_value, at = at, text = value, name = written }; } return take_option::(made, which, "", at, written); } if !joined { if at + 1 >= args.length { return error { kind = error_kind::missing_value, at = args.length, text = arg, name = written }; } at++; value = args[at]; } return take_option::(made, which, value, at, written); } # Reads options written by their short names, as `-v`, `-vq`, `-O 2`, or `-vO2`: # switches may be written together, and the last written together may take a # value, which is what follows it, or the next argument. def read_short(made: &mut T, args: span, at: &mut u64, seen: &mut vector, reached: &mut reach): result where T: zeroable { val arg := args[at]; var i := 1u; while i < arg.length { val letter := arg[i]; val written := short_written(letter); val which := option_lettered::(letter); if which == no_option { if letter == 'h' && helps::(true) { return asked(error_kind::help_asked, at, arg); } return error { kind = error_kind::unknown_option, at = at, text = arg, name = written.length == 2 ? written : arg }; } seen.append(which); if !takes_value::(which) { _ = take_option::(made, which, "", at, written) else (wrong) { return wrong; }; i++; continue; } if i + 1 < arg.length { return take_option::(made, which, arg.substr(i + 1, arg.length - i - 1), at, written); } if at + 1 >= args.length { return error { kind = error_kind::missing_value, at = args.length, text = arg, name = written }; } at++; return take_option::(made, which, args[at], at, written); } return; } # Reads the subcommand whose word is at an index into the field holding the # union of them. def read_subcommand(made: &mut T, args: span, at: u64, reached: &mut reach): result where T: zeroable { for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::(field).length > 0 { _ = read_into::(&mut made.(field), args, at, reached) else (wrong) { return wrong; }; } } return; } # Reads a subcommand into a field, where the field holds a union of them, and # does nothing to any other. def read_into(place: &mut V, args: span, at: u64, reached: &mut reach): result { if const V is switch || V is value || V is repeated { return; } else if const decl_of::().kind == target::unions && attached::(decl_of::()).length > 0 { if const V is zeroable { var sub := read_part::(args, at, reached) else (wrong) { return wrong; }; place = move(sub); } return; } else { return; } } # Reads the positional arguments into the fields marked `@argument`, in the # order they are declared: one takes one, a `maybe` one where one is left over # for those after it, and a vector as many as are left over. The arguments after # `--` go to the one taking the rest, where there is one, and are otherwise read # as the others are. def read_positional(made: &mut T, args: span, positional: &mut vector, after: &vector): result where T: zeroable { var shapes := vector::::new(); var rests := vector::::new(); for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::(field).length > 0 { if !is_command::() { shapes.append(shape_of::()); rests.append(attached::(field)[0].rest); } } } var resting := false; for &each in rests { resting = resting || each; } if !resting { for &each in after { positional.append(each); } } # How many each takes, leaving those after it the one each needs. var counts := vector::::new(); var left := positional.length; for k in 0..shapes.length { var needed := 0u; for j in k + 1..shapes.length { if !rests[j] && shapes[j] == shape::one { needed++; } } var taken := 0u; if !rests[k] { if shapes[k] == shape::one { taken = left > 0 ? 1 : 0; } else if shapes[k] == shape::optional { taken = left > needed ? 1 : 0; } else { taken = left > needed ? left - needed : 0; } } counts.append(taken); left -= taken; } if left > 0 { val extra := positional[positional.length - left]; return error { kind = error_kind::unexpected_argument, at = extra, text = args[extra] }; } var k := 0u; var next := 0u; for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::(field).length > 0 { if !is_command::() { val name := attached::(field)[0].value; if rests[k] { for &each in after { _ = store::(&mut made.(field), args[each]) else (why) { return refused(each, args[each], argument_name(field.name, name), why); }; } } else { if shapes[k] == shape::one && counts[k] == 0 { return error { kind = error_kind::missing_argument, at = args.length, name = argument_name(field.name, name) }; } for _ in 0..counts[k] { val each := positional[next]; next++; _ = store::(&mut made.(field), args[each]) else (why) { return refused(each, args[each], argument_name(field.name, name), why); }; } } k++; } } } return; } # Reads an option into its field, by its index among the options: a count adds # one, a switch is turned on, and anything else reads the value written. def take_option(made: &mut T, which: u64, value: strview, at: u64, written: strview): result where T: zeroable { var index := 0u; for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::