A format for expressing an ordered list of integers is to use a comma separated list of either

 individual integers
 Or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, '-'. (The range includes all integers in the interval including both endpoints)
 The range syntax is to be used only for, and for every range that expands to more than two values.
Example

The list of integers:

 -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20
Is accurately expressed by the range expression:

 -6,-3-1,3-5,7-11,14,15,17-20
(And vice-versa).



Task

Expand the range description:

  -6,-3--1,3-5,7-11,14,15,17-20 

Note that the second element above,
is the range from minus 3 to minus 1. 



Related task

   Range extraction


 
program Range_expansion;
 
{$APPTYPE CONSOLE}
 
uses
  System.SysUtils;
 
const
  input = '-6,-3--1,3-5,7-11,14,15,17-20';
 
var
  r: TArray<Integer>;
  last, i, n: Integer;
 
begin
  for var part in input.Split([',']) do
  begin
    i := part.Substring(1).IndexOf('-');
    if i = -1 then
    begin
      if not TryStrToInt(part, n) then
        raise Exception.Create('Error: value invalid ' + part);
      if Length(r) > 0 then
        if last = n then
        begin
          raise Exception.Create('Error: Duplicate value:' + n.ToString);
        end
        else
        begin
          if last > n then
            raise Exception.CreateFmt('Error: values not ordered: %s > %s', [last, n]);
        end;
      SetLength(r, Length(r) + 1);
      r[High(r)] := n;
      last := n;
    end
    else
    begin
      var n1 := 0;
      var n2 := 0;
      if not TryStrToInt(part.Substring(0, i + 1), n1) then
        raise Exception.Create('Error: value invalid ' + part);
      if not TryStrToInt(part.Substring(i + 2), n2) then
        raise Exception.Create('Error: value invalid ' + part);
      if n2 < n1 + 2 then
        raise Exception.Create('Error: Invalid range' + part);
      if Length(r) > 0 then
      begin
        if last = n1 then
          raise Exception.Create('Error: Duplicate value: ' + n1.ToString);
        if last > n1 then
          raise Exception.CreateFmt('Error: Values not ordened: %d > %d', [last, n1]);
      end;
 
      for i := n1 to n2 do
      begin
        SetLength(r, Length(r) + 1);
        r[High(r)] := i;
      end;
 
      last := n2;
    end;
  end;
  write('expanded: ');
  for var rr in r do
  begin
    write(rr, ',');
  end;
  readln;
end.