Показать сообщение отдельно
  #4  
Старый 03.10.2010, 02:14
yanot yanot вне форума
Прохожий
 
Регистрация: 03.10.2010
Сообщения: 7
Репутация: 12
По умолчанию

Почему бы не использовать Pos.
Не так и громоздко

Особо не тестировал, но вроде работает.

Тут нет проверки на обязательный закрывающий комментарий */
и на правильность кавычек


PHP код:
function Valid(Lines: TStrings): boolean;

const
  
InnerArr: array[0..3] of char = (
  
'{',
  
'[',
  
'(',
  
'<'
  
);

  
OuterArr: array[0..3] of char = (
  
'}',
  
']',
  
')',
  
'>'
  
);

var
  
InnerCount: array[0..3] of integer;
  
OuterCount: array[0..3] of integer;
  
i, j, a: integer;
  
Comment: boolean;
  
CommentPos, Pos2: integer;
  
Kav: boolean; // Кавычки
  
Start: integer;

begin

  
for a := 0 to 3 do
  
begin
    InnerCount
[a] := 0;
    
OuterCount[a] := 0;
  
end;

  
Result := True;
  
Comment := False;

  for 
i := 0 to Lines.Count-1 do
  
begin
    
if Comment then
    begin
      CommentPos 
:= Pos('*/', Lines[i]);
      if 
CommentPos = 0 then
        
Continue
      else
      
begin
        Comment 
:= False;
        
Start := CommentPos+2;
      
end;
    
end
    
else
      
Start := 1;

    
CommentPos := Pos('//', Lines[i]);
    
Pos2 := Pos('/*', Lines[i]);
    if (
Pos2 > 0) and ((CommentPos = 0) or (Pos2 < CommentPos)) then
    begin
      CommentPos 
:= pos2;
      
Comment := True;
    
end;

    if 
CommentPos < Start then
      CommentPos 
:= Length( Lines[i] );

    
Kav := False;
    for 
j := Start to CommentPos do
    
begin

      
if Lines[i][j] = '"' then
        Kav 
:= not Kav;

      if 
not Kav then
        
for a := 0 to 3 do
        
begin
          
if Lines[i][j] = InnerArr[a] then
            Inc
( InnerCount[a] );

          if 
Lines[i][j] = OuterArr[a] then
            
if InnerCount[a] > OuterCount[a] then
              Inc
( OuterCount[a] )
            else
              
Result := False;   // Ошибка: закрывающая скобка раньше открывающей
        
end;
    
end;

  
end;

  if 
Result then
    
for a := 0 to 3 do
      if 
InnerCount[a] <> OuterCount[a] then
      begin
        Result 
:= False;
        Break;
      
end;

end; 
Ответить с цитированием