!759

p {
  S text = loadSnippet("#1002764");
  snlCheckBrackets(text);
  print("Bracket check OK");
  Lisp tree = snlToTree(text);
  print("head: " + tree.head);
}

static void snlCheckBrackets(S snl) {
  L<S> tok = snlTok(snl);
  new L<S> stack;
  for (int i = 1; i < l(tok); i += 2) {
    S t = tok.get(i);
    if (eq(t, "[") || eq(t, "("))
      stack.add(t);
    else if (eq(t, "]")) {
      S op = popLast(stack);
      if (!eq(op, "["))
        fail("bracket failure at token " + i + "/" + l(tok) + ": " + op + " vs " + join(subList(tok, i, i+9)));
    } else if (eq(t, ")")) {
      S op = popLast(stack);
      if (!eq(op, "("))
        fail("bracket failure at token " + i + "/" + l(tok) + ": " + op + " vs " + join(subList(tok, i, i+9)));
    }
  }
  if (!stack.isEmpty())
    print("open brackets remain: " + structure(stack));
}