mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-06-06 20:18:19 +02:00
codetools: config errors
git-svn-id: trunk@26902 -
This commit is contained in:
parent
6785d127dd
commit
a24f111a3e
@ -30,7 +30,7 @@ unit CodeToolsCfgScript;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, BasicCodeTools, AVL_Tree;
|
Classes, SysUtils, BasicCodeTools, AVL_Tree, KeywordFuncLists;
|
||||||
|
|
||||||
type
|
type
|
||||||
TCTCSValueType = (
|
TCTCSValueType = (
|
||||||
@ -66,7 +66,7 @@ type
|
|||||||
CreateIfNotExists: Boolean = false): PCTCfgScriptVariable;
|
CreateIfNotExists: Boolean = false): PCTCfgScriptVariable;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
type
|
||||||
TCTCfgScriptStackItemType = (
|
TCTCfgScriptStackItemType = (
|
||||||
ctcssNone,
|
ctcssNone,
|
||||||
ctcssStatement,
|
ctcssStatement,
|
||||||
@ -76,6 +76,9 @@ type
|
|||||||
ctcssRoundBracketOpen,
|
ctcssRoundBracketOpen,
|
||||||
ctcssBegin
|
ctcssBegin
|
||||||
);
|
);
|
||||||
|
const
|
||||||
|
ctcssAllStatementStarts = [ctcssNone,ctcssIfThen,ctcssIfElse,ctcssBegin];
|
||||||
|
type
|
||||||
TCTCfgScriptStackItem = record
|
TCTCfgScriptStackItem = record
|
||||||
Typ: TCTCfgScriptStackItemType;
|
Typ: TCTCfgScriptStackItemType;
|
||||||
StartPos: integer;
|
StartPos: integer;
|
||||||
@ -83,12 +86,14 @@ type
|
|||||||
end;
|
end;
|
||||||
PCTCfgScriptStackItem = ^TCTCfgScriptStackItem;
|
PCTCfgScriptStackItem = ^TCTCfgScriptStackItem;
|
||||||
|
|
||||||
|
type
|
||||||
{ TCTCfgScriptStack }
|
{ TCTCfgScriptStack }
|
||||||
|
|
||||||
TCTCfgScriptStack = class
|
TCTCfgScriptStack = class
|
||||||
public
|
public
|
||||||
Items: PCTCfgScriptStackItem;
|
Items: PCTCfgScriptStackItem;
|
||||||
Top: integer; // current item, -1 = empty
|
Top: integer; // current item, -1 = empty
|
||||||
|
TopTyp: TCTCfgScriptStackItemType;
|
||||||
Capacity: integer;
|
Capacity: integer;
|
||||||
constructor Create;
|
constructor Create;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
@ -97,17 +102,37 @@ type
|
|||||||
procedure Pop;
|
procedure Pop;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{ TCTCfgScriptError }
|
||||||
|
|
||||||
|
TCTCfgScriptError = class
|
||||||
|
public
|
||||||
|
Msg: string;
|
||||||
|
ErrorPos: integer;
|
||||||
|
constructor Create(const aMsg: string; anErrorPos: integer);
|
||||||
|
end;
|
||||||
|
|
||||||
{ TCTConfigScriptEngine }
|
{ TCTConfigScriptEngine }
|
||||||
|
|
||||||
TCTConfigScriptEngine = class
|
TCTConfigScriptEngine = class
|
||||||
private
|
private
|
||||||
FVariables: TCTCfgScriptVariables;
|
FVariables: TCTCfgScriptVariables;
|
||||||
FStack: TCTCfgScriptStack;
|
FStack: TCTCfgScriptStack;
|
||||||
|
FErrors: TFPList; // list of TCTCfgScriptError
|
||||||
|
function GetErrors(Index: integer): TCTCfgScriptError;
|
||||||
|
procedure AddError(const aMsg: string; ErrorPos: PChar); overload;
|
||||||
|
procedure AddError(const aMsg: string); overload;
|
||||||
public
|
public
|
||||||
|
Src: PChar;
|
||||||
|
AtomStart: PChar;
|
||||||
|
SrcStart: PChar;
|
||||||
constructor Create;
|
constructor Create;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
|
procedure ClearErrors;
|
||||||
property Variables: TCTCfgScriptVariables read FVariables;
|
property Variables: TCTCfgScriptVariables read FVariables;
|
||||||
procedure Execute(const Src: string; StopAfterErrors: integer = 1);
|
function Execute(const Source: string; StopAfterErrors: integer = 1): boolean;// true if no errors
|
||||||
|
function ErrorCount: integer;
|
||||||
|
property Errors[Index: integer]: TCTCfgScriptError read GetErrors;
|
||||||
|
function GetAtom: string;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function CompareCTCSVariables(Var1, Var2: Pointer): integer;
|
function CompareCTCSVariables(Var1, Var2: Pointer): integer;
|
||||||
@ -395,30 +420,96 @@ end;
|
|||||||
|
|
||||||
{ TCTConfigScriptEngine }
|
{ TCTConfigScriptEngine }
|
||||||
|
|
||||||
|
function TCTConfigScriptEngine.GetErrors(Index: integer): TCTCfgScriptError;
|
||||||
|
begin
|
||||||
|
Result:=TCTCfgScriptError(FErrors[Index]);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TCTConfigScriptEngine.AddError(const aMsg: string; ErrorPos: PChar);
|
||||||
|
var
|
||||||
|
Err: TCTCfgScriptError;
|
||||||
|
begin
|
||||||
|
Err:=TCTCfgScriptError.Create(aMsg,ErrorPos-SrcStart);
|
||||||
|
FErrors.Add(Err);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TCTConfigScriptEngine.AddError(const aMsg: string);
|
||||||
|
begin
|
||||||
|
AddError(aMsg,AtomStart);
|
||||||
|
end;
|
||||||
|
|
||||||
constructor TCTConfigScriptEngine.Create;
|
constructor TCTConfigScriptEngine.Create;
|
||||||
begin
|
begin
|
||||||
FVariables:=TCTCfgScriptVariables.Create;
|
FVariables:=TCTCfgScriptVariables.Create;
|
||||||
FStack:=TCTCfgScriptStack.Create;
|
FStack:=TCTCfgScriptStack.Create;
|
||||||
|
FErrors:=TFPList.Create;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TCTConfigScriptEngine.Destroy;
|
destructor TCTConfigScriptEngine.Destroy;
|
||||||
begin
|
begin
|
||||||
|
ClearErrors;
|
||||||
|
FreeAndNil(FErrors);
|
||||||
FreeAndNil(FVariables);
|
FreeAndNil(FVariables);
|
||||||
FreeAndNil(FStack);
|
FreeAndNil(FStack);
|
||||||
inherited Destroy;
|
inherited Destroy;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TCTConfigScriptEngine.Execute(const Src: string;
|
procedure TCTConfigScriptEngine.ClearErrors;
|
||||||
StopAfterErrors: integer);
|
|
||||||
var
|
var
|
||||||
p: PChar;
|
i: Integer;
|
||||||
|
begin
|
||||||
|
for i:=0 to FErrors.Count do
|
||||||
|
TObject(FErrors[i]).Free;
|
||||||
|
FErrors.Clear;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TCTConfigScriptEngine.Execute(const Source: string;
|
||||||
|
StopAfterErrors: integer): boolean;
|
||||||
|
|
||||||
|
procedure ErrorUnexpectedAtom;
|
||||||
|
begin
|
||||||
|
AddError('expected statement, but found '+GetAtom);
|
||||||
|
end;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
FStack.Clear;
|
FStack.Clear;
|
||||||
|
ClearErrors;
|
||||||
|
|
||||||
if Src='' then exit;
|
if Source='' then exit(true);
|
||||||
|
|
||||||
p:=PChar(Src);
|
SrcStart:=PChar(Source);
|
||||||
while p^<>#0 do inc(p);
|
Src:=SrcStart;
|
||||||
|
AtomStart:=Src;
|
||||||
|
// execute all statements
|
||||||
|
while true do begin
|
||||||
|
ReadRawNextPascalAtom(Src,AtomStart);
|
||||||
|
case UpChars[Src^] of
|
||||||
|
#0: break;
|
||||||
|
'I':
|
||||||
|
if CompareIdentifiers('IF',Src)=0 then begin
|
||||||
|
|
||||||
|
end;
|
||||||
|
else
|
||||||
|
ErrorUnexpectedAtom;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end;
|
||||||
|
|
||||||
|
Result:=ErrorCount=0;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TCTConfigScriptEngine.ErrorCount: integer;
|
||||||
|
begin
|
||||||
|
Result:=FErrors.Count;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TCTConfigScriptEngine.GetAtom: string;
|
||||||
|
begin
|
||||||
|
if (AtomStart=nil) or (AtomStart>Src) then
|
||||||
|
exit('');
|
||||||
|
SetLength(Result,Src-AtomStart);
|
||||||
|
if Result<>'' then
|
||||||
|
System.Move(AtomStart^,Result[1],length(Result));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TCTCfgScriptStack }
|
{ TCTCfgScriptStack }
|
||||||
@ -437,10 +528,16 @@ end;
|
|||||||
procedure TCTCfgScriptStack.Clear;
|
procedure TCTCfgScriptStack.Clear;
|
||||||
var
|
var
|
||||||
i: Integer;
|
i: Integer;
|
||||||
|
Item: PCTCfgScriptStackItem;
|
||||||
begin
|
begin
|
||||||
for i:=0 to Top do
|
for i:=0 to Top do begin
|
||||||
ClearCTCSVariable(@Items[i].Operand);
|
Item:=@Items[i];
|
||||||
|
ClearCTCSVariable(@Item^.Operand);
|
||||||
|
if Item^.Operand.Name<>nil then
|
||||||
|
ReAllocMem(Item^.Operand.Name,0);
|
||||||
|
end;
|
||||||
Top:=-1;
|
Top:=-1;
|
||||||
|
TopTyp:=ctcssNone;
|
||||||
Capacity:=0;
|
Capacity:=0;
|
||||||
ReAllocMem(Items,0);
|
ReAllocMem(Items,0);
|
||||||
end;
|
end;
|
||||||
@ -464,6 +561,7 @@ begin
|
|||||||
Item:=@Items[Top];
|
Item:=@Items[Top];
|
||||||
Item^.Typ:=Typ;
|
Item^.Typ:=Typ;
|
||||||
Item^.StartPos:=StartPos;
|
Item^.StartPos:=StartPos;
|
||||||
|
TopTyp:=Typ;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TCTCfgScriptStack.Pop;
|
procedure TCTCfgScriptStack.Pop;
|
||||||
@ -480,7 +578,21 @@ begin
|
|||||||
RaiseTooManyPop;
|
RaiseTooManyPop;
|
||||||
Item:=@Items[Top];
|
Item:=@Items[Top];
|
||||||
ClearCTCSVariable(@Item^.Operand);
|
ClearCTCSVariable(@Item^.Operand);
|
||||||
|
if Item^.Operand.Name<>nil then
|
||||||
|
ReAllocMem(Item^.Operand.Name,0);
|
||||||
dec(Top);
|
dec(Top);
|
||||||
|
if Top>=0 then
|
||||||
|
TopTyp:=Items[0].Typ
|
||||||
|
else
|
||||||
|
TopTyp:=ctcssNone;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TCTCfgScriptError }
|
||||||
|
|
||||||
|
constructor TCTCfgScriptError.Create(const aMsg: string; anErrorPos: integer);
|
||||||
|
begin
|
||||||
|
Msg:=aMsg;
|
||||||
|
ErrorPos:=anErrorPos;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
Loading…
Reference in New Issue
Block a user