MG: scanner will now give an error if a comment is not closed

git-svn-id: trunk@1803 -
This commit is contained in:
lazarus 2002-07-31 11:43:38 +00:00
parent eda6857479
commit c4895bad37
4 changed files with 51 additions and 15 deletions

View File

@ -46,6 +46,7 @@ ResourceString
ctsIncludeFileNotFound = 'include file not found "%s"'; ctsIncludeFileNotFound = 'include file not found "%s"';
ctsErrorInDirectiveExpression = 'error in directive expression'; ctsErrorInDirectiveExpression = 'error in directive expression';
ctsIncludeCircleDetected = 'Include circle detected'; ctsIncludeCircleDetected = 'Include circle detected';
ctsCommentEndNotFound = 'Comment end not found';
// customcodetool // customcodetool
ctsIdentExpectedButAtomFound = 'identifier expected, but %s found'; ctsIdentExpectedButAtomFound = 'identifier expected, but %s found';

View File

@ -551,6 +551,7 @@ begin
Add('NEAR' ,{$ifdef FPC}@{$endif}AllwaysTrue); Add('NEAR' ,{$ifdef FPC}@{$endif}AllwaysTrue);
Add('DEPRECATED' ,{$ifdef FPC}@{$endif}AllwaysTrue); Add('DEPRECATED' ,{$ifdef FPC}@{$endif}AllwaysTrue);
Add('PLATFORM' ,{$ifdef FPC}@{$endif}AllwaysTrue); Add('PLATFORM' ,{$ifdef FPC}@{$endif}AllwaysTrue);
Add('LOCAL' ,{$ifdef FPC}@{$endif}AllwaysTrue);
Add('[' ,{$ifdef FPC}@{$endif}AllwaysTrue); Add('[' ,{$ifdef FPC}@{$endif}AllwaysTrue);
end; end;
IsKeyWordProcedureTypeSpecifier:=TKeyWordFunctionList.Create; IsKeyWordProcedureTypeSpecifier:=TKeyWordFunctionList.Create;

View File

@ -171,6 +171,7 @@ type
procedure SkipComment; procedure SkipComment;
procedure SkipDelphiComment; procedure SkipDelphiComment;
procedure SkipOldTPComment; procedure SkipOldTPComment;
procedure CommentEndNotFound;
procedure EndComment; procedure EndComment;
procedure IncCommentLevel; procedure IncCommentLevel;
procedure DecCommentLevel; procedure DecCommentLevel;
@ -842,7 +843,7 @@ begin
FSkippingTillEndif:=false; FSkippingTillEndif:=false;
if Assigned(FOnGetInitValues) then if Assigned(FOnGetInitValues) then
FInitValues.Assign(FOnGetInitValues(FMainCode,FInitValuesChangeStep)); FInitValues.Assign(FOnGetInitValues(FMainCode,FInitValuesChangeStep));
//writeln('TLinkScanner.Scan C --------'); //writeln('TLinkScanner.Scan D --------');
Values.Assign(FInitValues); Values.Assign(FInitValues);
for cm:=Low(TCompilerMode) to High(TCompilerMode) do for cm:=Low(TCompilerMode) to High(TCompilerMode) do
if FInitValues.IsDefined(CompilerModeVars[cm]) then begin if FInitValues.IsDefined(CompilerModeVars[cm]) then begin
@ -854,7 +855,7 @@ begin
PascalCompiler:=pc; PascalCompiler:=pc;
end; end;
//writeln(Values.AsString); //writeln(Values.AsString);
//writeln('TLinkScanner.Scan D --------'); //writeln('TLinkScanner.Scan E --------');
FMacrosOn:=(Values.Variables['MACROS']<>'0'); FMacrosOn:=(Values.Variables['MACROS']<>'0');
if Src='' then exit; if Src='' then exit;
// beging scanning // beging scanning
@ -862,11 +863,11 @@ begin
AddLink(1,SrcPos,Code); AddLink(1,SrcPos,Code);
LastTokenType:=lsttNone; LastTokenType:=lsttNone;
{$IFDEF CTDEBUG} {$IFDEF CTDEBUG}
writeln('TLinkScanner.Scan D ',SrcLen); writeln('TLinkScanner.Scan F ',SrcLen);
{$ENDIF} {$ENDIF}
repeat repeat
ReadNextToken; ReadNextToken;
//writeln('TLinkScanner.Scan E "',copy(Src,TokenStart,SrcPos-TokenStart),'"'); //writeln('TLinkScanner.Scan G "',copy(Src,TokenStart,SrcPos-TokenStart),'"');
UpdateCleanedSource(SrcPos-1); UpdateCleanedSource(SrcPos-1);
if (SrcPos<=SrcLen+1) then begin if (SrcPos<=SrcLen+1) then begin
if (LastTokenType<>lsttEqual) if (LastTokenType<>lsttEqual)
@ -902,7 +903,6 @@ begin
IncCommentLevel; IncCommentLevel;
inc(SrcPos); inc(SrcPos);
CommentInnerStartPos:=SrcPos; CommentInnerStartPos:=SrcPos;
if SrcPos>SrcLen then exit;
{ HandleSwitches can dec CommentLevel } { HandleSwitches can dec CommentLevel }
while (SrcPos<=SrcLen) and (CommentLevel>0) do begin while (SrcPos<=SrcLen) and (CommentLevel>0) do begin
case Src[SrcPos] of case Src[SrcPos] of
@ -913,6 +913,7 @@ begin
end; end;
CommentEndPos:=SrcPos; CommentEndPos:=SrcPos;
CommentInnerEndPos:=SrcPos-1; CommentInnerEndPos:=SrcPos-1;
if (CommentLevel>0) then CommentEndNotFound;
{ handle compiler switches } { handle compiler switches }
if Src[CommentInnerStartPos]='$' then HandleDirectives; if Src[CommentInnerStartPos]='$' then HandleDirectives;
EndComment; EndComment;
@ -926,9 +927,8 @@ begin
IncCommentLevel; IncCommentLevel;
inc(SrcPos,2); inc(SrcPos,2);
CommentInnerStartPos:=SrcPos; CommentInnerStartPos:=SrcPos;
if SrcPos>SrcLen then exit;
if (Src[SrcPos]='$') then ;
while (SrcPos<=SrcLen) and (Src[SrcPos]<>#10) do inc(SrcPos); while (SrcPos<=SrcLen) and (Src[SrcPos]<>#10) do inc(SrcPos);
DecCommentLevel;
inc(SrcPos); inc(SrcPos);
CommentEndPos:=SrcPos; CommentEndPos:=SrcPos;
CommentInnerEndPos:=SrcPos-1; CommentInnerEndPos:=SrcPos-1;
@ -944,18 +944,30 @@ begin
IncCommentLevel; IncCommentLevel;
inc(SrcPos,2); inc(SrcPos,2);
CommentInnerStartPos:=SrcPos; CommentInnerStartPos:=SrcPos;
if SrcPos>SrcLen then exit;
// ToDo: nested comments // ToDo: nested comments
while (SrcPos<=SrcLen) while (SrcPos<SrcLen) do begin
and ((Src[SrcPos-1]<>'*') or (Src[SrcPos]<>')')) do inc(SrcPos); if ((Src[SrcPos]<>'*') or (Src[SrcPos+1]<>')')) then
inc(SrcPos); inc(SrcPos)
else begin
DecCommentLevel;
inc(SrcPos,2);
break;
end;
end;
CommentEndPos:=SrcPos; CommentEndPos:=SrcPos;
CommentInnerEndPos:=SrcPos-2; CommentInnerEndPos:=SrcPos-2;
if (CommentLevel>0) then CommentEndNotFound;
{ handle compiler switches } { handle compiler switches }
if Src[CommentInnerStartPos]='$' then HandleDirectives; if Src[CommentInnerStartPos]='$' then HandleDirectives;
EndComment; EndComment;
end; end;
procedure TLinkScanner.CommentEndNotFound;
begin
SrcPos:=CommentStartPos;
RaiseException(ctsCommentEndNotFound);
end;
procedure TLinkScanner.UpdateCleanedSource(SourcePos: integer); procedure TLinkScanner.UpdateCleanedSource(SourcePos: integer);
// add new parsed code to cleaned source string // add new parsed code to cleaned source string
var AddLen, i: integer; var AddLen, i: integer;
@ -2184,6 +2196,11 @@ begin
else else
inc(SrcPos); inc(SrcPos);
end; end;
end else if Src[SrcPos]='''' then begin
// skip string constant
inc(SrcPos);
while (SrcPos<=SrcLen) and (Src[SrcPos]<>'''') do inc(SrcPos);
inc(SrcPos);
end else begin end else begin
inc(SrcPos); inc(SrcPos);
if SrcPos>SrcLen then ReturnFromIncludeFile; if SrcPos>SrcLen then ReturnFromIncludeFile;

View File

@ -461,6 +461,23 @@ type
procedure Write(const Buf; Count: Longint); procedure Write(const Buf; Count: Longint);
end; end;
{function Utf8Decode(const S: UTF8String): WideString;
var
L: Integer;
Temp: WideString;
begin
Result := '';
if S = '' then Exit;
SetLength(Temp, Length(S));
L := Utf8ToUnicode(PWideChar(Temp), Length(Temp)+1, PChar(S), Length(S));
if L > 0 then
SetLength(Temp, L-1)
else
Temp := '';
Result := Temp;
end;}
{ TDelphiReader } { TDelphiReader }
procedure ReadError(Msg: string); procedure ReadError(Msg: string);
@ -959,14 +976,14 @@ var
WriteStr(''''); WriteStr('''');
while J < I do while J < I do
begin begin
//ToDo: WriteStr(Char(W[J])); WriteStr(Char(W[J]));
Inc(J); Inc(J);
end; end;
WriteStr(''''); WriteStr('''');
end else end else
begin begin
WriteStr('#'); WriteStr('#');
//ToDo: WriteStr(IntToStr(Ord(W[I]))); WriteStr(IntToStr(Ord(W[I])));
Inc(I); Inc(I);
if ((I - K) >= LineLength) then LineBreak := True; if ((I - K) >= LineLength) then LineBreak := True;
end; end;
@ -1004,8 +1021,8 @@ var
((I - K) >= LineLength); ((I - K) >= LineLength);
if ((I - K) >= LineLength) then if ((I - K) >= LineLength) then
begin begin
LIneBreak := True; LineBreak := True;
//ToDo: if ByteType(S, I) = mbTrailByte then Dec(I); if ByteType(S, I) = mbTrailByte then Dec(I);
end; end;
WriteStr(''''); WriteStr('''');
Writer.Write(S[J], I - J); Writer.Write(S[J], I - J);