unit testX;

interface

implementation
  procedure _pre;
  var i1,i2: Integer;
      k1,k2: Integer;
      ptemp,pdata1,pdata2: PTokenData;
      phighlight1,phighlight2: PTokenHighlight;
      w: PToken;
      h,k: Integer;
      pline1,pline2: PLineRecord;
      i: Integer;
      token: PToken;
      bInserted: Boolean;
      bMatched: Boolean;
  begin
    while (index1 <= size1) or (index2 <= size2) do begin
      begin
        //ln1,ln2 <> nil because ia1,ia2 are without nil and index1,index2 <= size1,size2
        if (index1 > size1) or ((ln1 <> nil) and (PToken(ln1.i) = ia1[index1])) then begin
          //add to both the tokens of tree2 (in tree1 as insert / in tree2 as delete)
          if bMatched then begin
//            _managestacks(true,true,true,false,false,false,pdata1,pdata2,indent);
            _managestacks(true,true,true,true,false,false,pdata1,pdata2,indent,true);
            bMatched := false;
          end
          else begin
//            _managestacks(true,false,true,false,false,false,pdata1,pdata2,indent,true);
          end;

          _managestacks(false,true,false,true,false,true,pdata1,pdata2,indent,false);

          bInserted := true;

          if (ia2[index2].tokentype <= MAX_TERMINAL) then begin
              ci := ci + 1;
              if diffres <> ES_COMBINED then begin
                if diffres = ES_MATCH then
                  diffres := ES_INSERT
                else if diffres <> ES_INSERT then
                  diffres := ES_COMBINED;
              end;
              if bBkColor then
                c := COLOR_INSERT
              else
                c := COLOR_NOT_COMPARED;
              colorStrips1.add(Length(S1)+1,Length(ia2[index2].value),c,pdefhighlight.tc,Integer(pdefhighlight.style));
              colorStrips2.add(Length(S2)+1,Length(ia2[index2].value),c,phighlight2.tc,Integer(phighlight2.style));
//              colorStrips1.add(Length(S1)+1,Length(ia2[index2].value),COLOR_INSERT,TCOLOR_INSERT);
//              colorStrips2.add(Length(S2)+1,Length(ia2[index2].value),COLOR_INSERT,TCOLOR_INSERT);
              S1 := S1 + dupestring(' ',Length(ia2[index2].value));
              S2 := S2 + ia2[index2].value;
          end;

          index2 := index2 + 1;
        end
        else if true or (index2 > size2) or (PToken(ln2.i) = ia2[index2]) then begin
          //add to both the tokens of tree1 (in tree2 as insert / in tree1 as delete)
          //OR
          //none of current tokens is in mapping => add to both from tree1, until ia1[index1] = ln1.i
          //OR
          //anyway
          if bMatched then begin
//            _managestacks(true,true,false,true,false,false,pdata1,pdata2,indent);
            _managestacks(true,true,true,true,false,false,pdata1,pdata2,indent,true);
            bMatched := false;
          end
          else begin
//            _managestacks(false,true,false,true,false,false,pdata1,pdata2,indent,true);
          end;

          _managestacks(true,false,true,false,true,false,pdata1,pdata2,indent,false);

          if (ia1[index1].tokentype <= MAX_TERMINAL) then begin
              cd := cd + 1;
              if diffres <> ES_COMBINED then begin
                if diffres = ES_MATCH then
                  diffres := ES_DELETE
                else if diffres <> ES_DELETE then
                  diffres := ES_COMBINED;
              end;
              if bBkColor then
                c := COLOR_DELETE
              else
                c := COLOR_NOT_COMPARED;
              colorStrips1.add(Length(S1)+1,Length(ia1[index1].value),c,phighlight1.tc,Integer(phighlight1.style));
              colorStrips2.add(Length(S2)+1,Length(ia1[index1].value),c,pdefhighlight.tc,Integer(pdefhighlight.style));
//              colorStrips1.add(Length(S1)+1,Length(ia1[index1].value),COLOR_DELETE,TCOLOR_DELETE);
//              colorStrips2.add(Length(S2)+1,Length(ia1[index1].value),COLOR_DELETE,TCOLOR_DELETE);
              S1 := S1 + ia1[index1].value;
              S2 := S2 + dupestring(' ',Length(ia1[index1].value));
          end;

          index1 := index1 + 1;
        end;
      end;
    end;

    if not bInserted then begin
      _managestacks(true,true,true,true,false,false,pdef1,pdef2,indent,false);
    end
    else begin
      _managestacks(false,true,false,true,false,false,pdef1,pdef2,indent,true);
      _managestacks(true,true,true,true,false,false,pdef1,pdef2,indent,false);
    end;

    if (S1 <> '') or (S2 <> '') then begin
      _addlines(indent);
    end;

    colorstrips1.Free;
    colorstrips2.Free;
(*
      if not bInserted then begin
        if not QT.isEmpty then begin
          token := PToken(QT.top);
          while (not QT.isEmpty) do begin
            token := PToken(QT.pop);
            ptemp := PTokenData(QT.pop);
            //apply after section of PTokenData
            if ptemp.aindent < 0 then begin
              if S1 = dupestring(' ',indent) then
                S1 := dupestring(' ',indent+ptemp.aindent);
              if S2 = dupestring(' ',indent) then
                S2 := dupestring(' ',indent+ptemp.aindent);
            end;
            indent := indent + ptemp.aindent;
            if token.tokentype <= MAX_TERMINAL then begin  //terminal spaces
              S1 := S1 + dupestring(' ',ptemp.aspace);
              S2 := S2 + dupestring(' ',ptemp.aspace);
            end;
            for i := 1 to ptemp.aeoln do begin
              _addlines(indent);
            end;
            token := PToken(QT.top);
          end;
        end;
      end
      else begin
        if not QT2.isEmpty then begin
          token := PToken(QT2.top);
          while (not QT2.isEmpty) do begin
            token := PToken(QT2.pop);
            ptemp := PTokenData(QT2.pop);
            //apply after section of PTokenData
            if ptemp.aindent < 0 then begin
              if S1 = dupestring(' ',indent) then begin
                if S1 = dupestring(' ',indent) then
                  S1 := dupestring(' ',indent+ptemp.aindent);
                if S2 = dupestring(' ',indent) then
                  S2 := dupestring(' ',indent+ptemp.aindent);
              end;
            end;
            indent := indent + ptemp.aindent;
            if token.tokentype <= MAX_TERMINAL then begin  //terminal spaces
              S1 := S1 + dupestring(' ',ptemp.aspace);
              S2 := S2 + dupestring(' ',ptemp.aspace);
            end;
            for i := 1 to ptemp.aeoln do begin
              _addlines(indent);
            end;
            token := PToken(QT2.top);
          end;
        end;
      end;
*)
(*    if T <> nil then begin
      if not QT.isEmpty then begin
        token := PToken(QT.top);
        while (not QT.isEmpty) do begin
          token := PToken(QT.pop);
          ptemp := PTokenData(QT.pop);
          //apply after section of PTokenData
          ind := ind + ptemp.aindent;
          if token.tokentype <= MAX_TERMINAL then begin  //terminal spaces
            S1 := S1 + dupestring(' ',ptemp.aspace);
            S2 := S2 + dupestring(' ',ptemp.aspace);
          end;
          for i := 1 to ptemp.aeoln do begin
            _addlines(ind);
          end;
        end;
      end;
    end;
*)
(*
    if S1 <> '' then begin
      new(pline1);
      pline1.diffres := -1;
      pline1.colorStrips := colorStrips1;
      Lines1.AddObject(S1,TObject(pline1));
    end;
    if S2 <> '' then begin
      new(pline2);
      pline2.diffres := -1;
      pline2.colorStrips := colorStrips2;
      Lines2.AddObject(S2,TObject(pline2));
    end;
*)
(*
    while (PToken(ln1.i) = ia1[index1]) and (PToken(ln2.i) = ia2[index2]) do begin
      if (p.tokentype < MAX_TERMINAL) then begin
        if p.value = r.value then begin
          S1 := S1 + p.value;
          S2 := S2 + r.value;
        end;
        ln1 := ln1.next;
        ln2 := ln2.next;
        index1 := index1 + 1;
        index2 := index2 + 1;
      end;
      //pridat ind alebo riadok, podla ptokendataline pre p.tokentype

      ln1 := ln1.next;
      ln2 := ln2.next;
      pparent := p;
      p := p.child;
      rparent := r;
      r := r.child;
      while (p = ln1) and (r = ln2) do begin
        if p <> nil then begin

        end;
        _pre(p,r,pparent,rparent,ind);
        p := p.sibling;
      end;
    end;
    if (ln1 = p) or (ln2 <> r) then begin
      _pre(p,r.
    end
    else begin

    end;

    if p = nil then begin
      Exit;
    end;
//    p._parent := parent;

    if p.tokentype < MAX_TERMINAL then begin
      if bNewLine then begin
        S := dupestring(' ',ind);
        bNewLine := false;
      end;
      S := S + p.value + ' ';
      if p.value = ';' then begin
        if (parent = nil) or (not (parent.tokentype = 1000000032)) then begin
//          result.add(S);
          lineno := lineno + 1;
          S := '';
          bNewLine := true;
        end;
      end
      else begin
      end;
    end
    else begin
      parent := p;
      p := p.child;
      while p <> nil do begin
        if p.tokentype = 1000000012 then begin
          ind := ind + 2;
//          result.add(S);
          lineno := lineno + 1;
          S := '';
          bNewLine := true;
        end;
        _pre(p,parent,ind);
        if p.tokentype = 1000000012 then begin
          ind := ind - 2;
        end;
        p := p.sibling;
      end;
    end;
    *)
  end;


end.