///|
pub(all) struct Arguments {
  values : Array[String]
  diagnostics : Array[Diagnostic]
} derive(Eq, Debug)

///|
pub extend Arguments with Eq::{equal, not_equal}

///|
pub extend Arguments with @debug.Debug::{to_repr}

///|
/// Quotes group whitespace; roff escapes remain intact for inline decoding.
pub fn tokenize_arguments(raw : String, line~ : Int32 = 1) -> Arguments {
  let chars = raw.to_array()
  let values : Array[String] = []
  let diagnostics : Array[Diagnostic] = []
  let mut i = 0
  while i < chars.length() {
    while i < chars.length() && horizontal(chars[i]) {
      i = i + 1
    }
    if i >= chars.length() {
      break
    }
    let out = StringBuilder()
    let mut quoted = false
    let mut started = false
    let mut comment = false
    while i < chars.length() {
      let c = chars[i]
      if c == '\\' && i + 1 < chars.length() {
        let next = chars[i + 1]
        if next == '"' || next == '#' {
          comment = true
          i = chars.length()
          break
        }
        out.write_char(c)
        out.write_char(next)
        i = i + 2
        started = true
      } else if c == '"' {
        if quoted && i + 1 < chars.length() && chars[i + 1] == '"' {
          out.write_char('"')
          i = i + 2
        } else {
          quoted = !quoted
          i = i + 1
        }
        started = true
      } else if horizontal(c) && !quoted {
        break
      } else {
        out.write_char(c)
        i = i + 1
        started = true
      }
    }
    if started {
      values.push(out.to_string())
    }
    if quoted {
      diagnostics.push(
        diagnostic(
          "unclosed-quote", "Quoted request argument is not closed", line,
        ),
      )
    }
    if comment {
      break
    }
  }
  { values, diagnostics, }
}