unit JASSerException;

//==============================================================================
//  INTERFACE
//==============================================================================
interface
uses SysUtils;

//==============================================================================
type TJASSerException = class(Exception)
private
public
  linen:integer;
  msg:string;
  two:boolean;
  linen2:integer;
  msg2:string;
  macro1:integer;
  macro2:integer;
  block:string;
end;

// Double exception types are related for double declarations of some identifier name

//==============================================================================
function JASSerLineException(i:integer;msg:string):TJASSerException;
function JASSerLineDoubleException(i:integer;msg:string; j:integer; msg2:string):TJASSerException;

function JASSerLineExceptionNoTextMacro(i:integer;msg:string):TJASSerException;overload;
function JASSerLineExceptionNoTextMacro(msg:string):TJASSerException;overload;
function JASSerLineDoubleExceptionNoTextMacro(i:integer;msg:string; j:integer; msg2:string):TJASSerException;

procedure VerifyEndOfLine(const s:string; const st:integer; LineNumber:integer);

procedure setCurrentBlock(const block:string);
function getCurrentBlock():string;

//==============================================================================
//  IMPLEMENTATION
//==============================================================================
implementation
uses JHInput, TextOnly, Constants;

var
    // trigger, library or scope
    currentBlock : string;

//==============================================================================
procedure setCurrentBlock(const block:string);
begin
    currentBlock := block;
end;

//==============================================================================
function getCurrentBlock():string;
begin
    if currentBlock = '' then begin
        Result := 'N\A';
    end else begin
        Result := currentBlock;
    end;
end;

//==============================================================================
function JASSerLineException(i:integer;msg:string):TJASSerException;
begin
    Result:=TJASSerException.Create(msg);
    Result.msg:=msg;
    Result.block:=getCurrentBlock();
    Result.linen:=i+offset[i];
    Result.two:= false;
    Result.macro1:=textmacrotrace[i]-1;
    Result.macro2:=-1;
end;

//==============================================================================
function JASSerLineDoubleException(i:integer;msg:string; j:integer; msg2:string):TJASSerException;
begin
   Result:=TJASSerException.Create(msg);
   Result.msg:=msg;
   Result.block:=getCurrentBlock();
   Result.linen:=i+offset[i];
   Result.two:= true;
   Result.msg2:=msg2;
   Result.linen2:=j+offset[j];

   Result.macro1:=textmacrotrace[i]-1;
   Result.macro2:=textmacrotrace[j]-1;

end;

//==============================================================================
function JASSerLineExceptionNoTextMacro(i:integer;msg:string):TJASSerException;
begin
   Result:=TJASSerException.Create(msg);
   Result.msg:=msg;
   Result.block:=getCurrentBlock();
   Result.linen:=i;
   Result.two:= false;
   Result.macro1:=-1;
   Result.macro2:=-1;
end;

//==============================================================================
function JASSerLineExceptionNoTextMacro(msg:string):TJASSerException;
var i:integer;
begin
   i := inputN - 1;
   Result:=TJASSerException.Create(msg);
   Result.msg:=msg;
   Result.block:=getCurrentBlock();
   Result.linen:=i;
   Result.two:= false;
   Result.macro1:=-1;
   Result.macro2:=-1;
end;




//==============================================================================
function JASSerLineDoubleExceptionNoTextMacro(i:integer;msg:string; j:integer; msg2:string):TJASSerException;
begin
   Result:=TJASSerException.Create(msg);
   Result.msg:=msg;
   Result.block:=getCurrentBlock();
   Result.linen:=i;
   Result.two:= true;
   Result.msg2:=msg2;
   Result.linen2:=j;

   Result.macro1:=-1;
   Result.macro2:=-1;

end;

//==============================================================================
procedure VerifyEndOfLine(const s:string; const st:integer; LineNumber:integer);
var i,L:integer;
begin
   if(not IsWhitespace(s,st)) then begin
       i:=st;
       L:=Length(s);
       while (i<=L) do begin
           if(s[i]='/') and (i<L) and (s[i+1]='/') then begin
                break;
           end;
           i:=i+1;
       end;
       raise JasserLineException(LineNumber,'Unexpected: "'+Copy(s,st,i-st)+'"');
   end;
end;


end.
