///|
/// Reads the longest decimal prefix accepted by JavaScript's parseFloat.
fn parse_array_stretch_number(text : String) -> Double? {
  let start = text
    .trim_start(
      chars=" \t\n\r\u{000b}\u{000c}\u{00a0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}",
    )
    .start_offset()
  let len = text.length()
  let mut index = start
  if index < len && (text[index] == '+' || text[index] == '-') {
    index = index + 1
  }
  if index + 8 <= len &&
    text.unsafe_substring(start=index, end=index + 8) == "Infinity" {
    return Some(if text[start] == '-' { -1.0 / 0.0 } else { 1.0 / 0.0 })
  }
  let integer_start = index
  while index < len && is_ascii_digit_code_unit(text[index]) {
    index = index + 1
  }
  let integer_digits = index - integer_start
  let mut fraction_digits = 0
  if index < len && text[index] == '.' {
    index = index + 1
    let fraction_start = index
    while index < len && is_ascii_digit_code_unit(text[index]) {
      index = index + 1
    }
    fraction_digits = index - fraction_start
  }
  guard integer_digits > 0 || fraction_digits > 0 else { None }
  if index < len && (text[index] == 'e' || text[index] == 'E') {
    let exponent_start = index
    index = index + 1
    if index < len && (text[index] == '+' || text[index] == '-') {
      index = index + 1
    }
    let digits_start = index
    while index < len && is_ascii_digit_code_unit(text[index]) {
      index = index + 1
    }
    if index == digits_start {
      index = exponent_start
    }
  }
  // The prefix has valid decimal syntax. MoonBit reports overflow as an error,
  // whereas JavaScript parseFloat returns the corresponding infinity.
  let value = @string.parse_double(text[start:index]) catch {
    _ => if text[start] == '-' { -1.0 / 0.0 } else { 1.0 / 0.0 }
  }
  Some(value)
}

///|
fn Parser::array_stretch(
  self : Parser,
  explicit : Double?,
) -> Double raise ParseFailure {
  if explicit is Some(stretch) {
    return stretch
  }
  guard self.gullet.expand_macro_as_text("\\arraystretch") is Some(text) else {
    1.0
  }
  guard parse_array_stretch_number(text) is Some(stretch) && stretch > 0.0 else {
    raise InvalidArgument(message="Invalid \\arraystretch: \{text}", loc=None)
  }
  stretch
}

///|
fn Parser::begin_array_row(self : Parser, auto_tag : Bool?) -> Unit {
  if auto_tag is Some(true) {
    self.gullet.macros.set("\\@eqnsw", Some(Text("1")), global=true)
  }
}

///|
fn Parser::consume_array_hlines(
  self : Parser,
) -> Array[Bool] raise ParseFailure {
  let lines : Array[Bool] = []
  self.consume_spaces()
  if self.fetch().text == "\\relax" {
    self.consume()
    self.consume_spaces()
  }
  while self.fetch().text == "\\hline" || self.fetch().text == "\\hdashline" {
    let dashed = self.fetch().text == "\\hdashline"
    self.consume()
    lines.push(dashed)
    self.consume_spaces()
  }
  lines
}

///|
fn Parser::take_array_tag(
  self : Parser,
  auto_tag : Bool?,
) -> (Array[ParseNode]?, Bool) raise ParseFailure {
  guard auto_tag is Some(automatic) else { (None, false) }
  guard self.gullet.macros.get("\\df@tag") is Some(_) else {
    (None, automatic && self.gullet.macros.get("\\@eqnsw") is Some(Text("1")))
  }
  let tag = self.subparse([Token::make("\\df@tag")])
  self.gullet.macros.set("\\df@tag", None, global=true)
  (Some(tag), false)
}

///|
/// Records the tag of the finished row when the environment collects tags:
/// pushes the tag (or `None` when the row has no `\tag`) and whether the tag
/// was automatic.
fn Parser::push_array_tag(
  self : Parser,
  tags : Array[Array[ParseNode]?],
  auto_tags : Array[Bool],
  auto_tag : Bool?,
) -> Unit raise ParseFailure {
  let (tag, automatic) = self.take_array_tag(auto_tag)
  if auto_tag is Some(_) {
    tags.push(tag)
    auto_tags.push(automatic)
  }
}

///|
/// Parses the optional `[height]` argument that may follow `\\` in an array
/// row. A space before the bracket means the gap is absent.
fn Parser::parse_array_row_gap(
  self : Parser,
) -> Measurement? raise ParseFailure {
  if self.gullet.future().text == " " {
    None
  } else {
    match self.parse_size_group(true) {
      Some(Size(value~, ..)) => Some(value)
      Some(_) => raise InternalInvariant(message="Expected array row gap")
      None => None
    }
  }
}

///|
/// True when the current row already holds `max_columns` cells, so the next
/// `&` would exceed the declared column limit.
fn array_row_at_max(
  body : Array[Array[ParseNode]],
  max_columns : Int32?,
) -> Bool {
  max_columns.map_or(false, maximum => {
    body.last() is Some(row) && row.length() >= maximum
  })
}

///|
/// Parses an array environment body: cells of the current row accumulate
/// until `&`, `\\`, or `\end`; `\\` takes an optional `[gap]` and the
/// `\hline`/`\hdashline` rules that open the next row; `\end` closes the
/// array, collecting the row's `\tag` when `auto_tag` is set. Row gaps, tags,
/// and the per-row hline list are collected alongside the body, and the
/// hline list is padded so it has one entry per row boundary.
fn Parser::parse_array_environment(
  self : Parser,
  options : ArrayEnvironmentOptions,
) -> ParseNode raise ParseFailure {
  self.gullet.begin_group()
  if !options.single_row {
    self.gullet.macros.set("\\cr", Some(MacroDefinition::text("\\\\\\relax")))
  }
  self.gullet.begin_group()
  let node = {
    errdefer {
      self.gullet.end_group() catch {
        _ => ()
      }
      self.gullet.end_group() catch {
        _ => ()
      }
    }
    let array_stretch = self.array_stretch(options.array_stretch)
    self.begin_array_row(options.auto_tag)
    let body : Array[Array[ParseNode]] = [[]]
    let row_gaps : Array[Measurement?] = []
    let hlines_before_row : Array[Array[Bool]] = [self.consume_array_hlines()]
    let tags : Array[Array[ParseNode]?] = []
    let auto_tags : Array[Bool] = []
    for ;; {
      let cell_body = self.parse_expression(false, Some("\\\\"))
      let empty_cell = cell_body.is_empty()
      let cell : ParseNode = Styling(
        mode=self.mode,
        body=[
          OrdGroup(mode=self.mode, loc=None, body=cell_body, semisimple=false),
        ],
        style=options.cell_style,
        reset_font=true,
      )
      self.gullet.end_group()
      self.gullet.begin_group()
      guard body.last() is Some(row) else {
        raise InternalInvariant(message="Missing array row")
      }
      row.push(cell)
      match self.fetch().text {
        "&" => {
          guard !array_row_at_max(body, options.max_columns) else {
            raise InvalidArgument(
              message="Too many tab characters: &",
              loc=None,
            )
          }
          self.consume()
        }
        "\\end" => {
          self.push_array_tag(tags, auto_tags, options.auto_tag)
          if row.length() == 1 &&
            empty_cell &&
            (body.length() > 1 || !options.empty_single_row) &&
            !(tags.last() is Some(Some(_))) &&
            !(auto_tags.last() is Some(true)) {
            let _ = body.pop()
          }
          break
        }
        "\\\\" => {
          guard !options.single_row else {
            raise InvalidArgument(message="Expected \\end", loc=None)
          }
          self.consume()
          row_gaps.push(self.parse_array_row_gap())
          self.push_array_tag(tags, auto_tags, options.auto_tag)
          hlines_before_row.push(self.consume_array_hlines())
          body.push([])
          self.begin_array_row(options.auto_tag)
        }
        token =>
          raise InvalidArgument(
            message="Expected & or \\\\ or \\end, got \{token}",
            loc=None,
          )
      }
    }
    if hlines_before_row.length() < body.length() + 1 {
      hlines_before_row.push([])
    }
    Array(
      mode=self.mode,
      body~,
      add_jot=options.add_jot,
      array_stretch~,
      columns=options.columns,
      row_gaps~,
      hskip_before_and_after=options.hskip_before_and_after,
      hlines_before_row~,
      column_separation_type=options.column_separation_type,
      tags=if options.auto_tag is Some(_) { Some(tags) } else { None },
      auto_tags=if options.auto_tag is Some(_) { Some(auto_tags) } else { None },
      leqno=options.leqno,
    )
  }
  {
    errdefer (self.gullet.end_group() catch { _ => () })
    self.gullet.end_group()
  }
  self.gullet.end_group()
  node
}