Changeset 32 for branches/Analyzátor gramatiky/UProgram.pas
- Timestamp:
- Nov 11, 2009, 2:14:44 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Analyzátor gramatiky/UProgram.pas
r31 r32 37 37 constructor Create; 38 38 procedure Insert(Path: TGrammerPath; Character: Char); 39 procedure Parse(Grammer: TGrammer; T ext: string);39 procedure Parse(Grammer: TGrammer; Tokens: TStringList); 40 40 destructor Destroy; override; 41 41 end; … … 88 88 end; 89 89 90 procedure TProgram.Parse(Grammer: TGrammer; T ext: string);90 procedure TProgram.Parse(Grammer: TGrammer; Tokens: TStringList); 91 91 var 92 92 Path: TGrammerPath; 93 93 I, II: Integer; 94 C: Integer;95 Scope: TPossibleCharacters;96 94 UseIndex: Integer; 97 95 UseCharIndex: Integer; 98 96 UseRule: TGrammerRule; 99 ExpectedCharacters: string;100 97 Level: array of Integer; 101 98 begin 102 99 Path := TGrammerPath.Create; 103 Scope := TPossibleCharacters.Create;104 105 100 with Path.Items[Path.Items.Add(TPathItem.Create)] do begin 106 101 Rule := Grammer.TopRule; … … 109 104 end; 110 105 111 for I := 1 to Length(Text) do begin 112 if (Text[I] <> #13) and (Text[I] <> #10) then begin 106 for I := 0 to Tokens.Count - 1 do begin 113 107 UseIndex := Path.Items[Path.Items.Count - 1].ItemIndex; 114 108 UseCharIndex := Path.Items[Path.Items.Count - 1].CharIndex; 115 109 UseRule := Path.Items[Path.Items.Count - 1].Rule; 116 110 Path.Items.Delete(Path.Items.Count - 1); 117 Scope.Items.Clear;118 111 Grammer.ClearProcessed; 119 UseRule.GetPossibleCharacters(Path, Scope, UseIndex, UseCharIndex); 120 C := Scope.Items.Count; 121 122 // Possible character selection 123 SetLength(Level, 0); 124 for II := 0 to Scope.Items.Count - 1 do 125 with Scope.Items[II] do 112 if UseRule.Check(Path, Tokens[I], UseIndex, UseCharIndex) then 126 113 begin 127 if (Character = Text[I]) then 128 begin 129 C := II; 130 Break; 131 { 132 MaxLevel := Max(Length(RulePath.Items), Length(Path.Items)); 133 Different := False; 134 for Q := 0 to MaxLevel - 1 do 135 begin 136 if (Q >= Length(RulePath.Items)) or (Q >= Length(Path.Items)) or 137 (RulePath.Items[Q].ItemIndex <> Path.Items[Q].ItemIndex) then 138 begin 139 Different := True; 140 Break; 141 end; 142 end; 143 if Different and (Q >= LevelLength) then 144 begin 145 LevelLength := Q; 146 C := II; 147 end; 148 149 } 150 { 151 // Append missing level items 152 if Length(RulePath.Items) > Length(Level) then begin 153 LevelLength := Length(Level); 154 SetLength(Level, Length(RulePath.Items)); 155 for Q := LevelLength to High(Level) do 156 Level[Q] := High(Integer); 157 end; 158 // Compare leftness for all rulepath items 159 LevelIsLeft := True; 160 for Q := 0 to High(Level) do begin 161 if Level[Q] > RulePath.Items[Q].ItemIndex then begin 162 LevelIsLeft := False; 163 Break; 164 end; 165 if Level[Q] < RulePath.Items[Q].ItemIndex then Break; 166 end; 167 168 if (not LevelIsLeft) or (Length(RulePath.Items) > Length(Level)) then begin 169 SetLength(Level, Length(RulePath.Items)); 170 for Q := 0 to High(Level) do 171 Level[Q] := RulePath.Items[Q].ItemIndex; 172 C := II; 173 end; 174 } 175 end; 176 end; 177 178 if C < Scope.Items.Count then begin 179 Path.Assign(Scope.Items[C].RulePath); 180 for II := 0 to Path.Items.Count - 1 do 181 with Path.Items[II] do begin 182 if Affected then Inc(CharIndex); 183 end; 184 Insert(Path, Scope.Items[C].Character); 185 for II := 0 to Path.Items.Count - 1 do 186 with Path.Items[II] do begin 187 Affected := False; 188 end; 189 //Path.Next; 114 190 115 end else begin 191 ExpectedCharacters := ''; 192 for II := 0 to Scope.Items.Count - 1 do 193 ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character; 194 //raise Exception.Create('Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".'); 116 //raise Exception.Create('Parse error. Expected "' + Tokens[I] + '" but found "' + Text[I] + '".'); 195 117 //MainForm.StatusBar1.SimpleText := 'Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".'; 196 118 break; 197 119 end; 198 end;199 120 end; 200 121 Path.Destroy; 201 Scope.Destroy;202 122 end; 203 123
Note:
See TracChangeset
for help on using the changeset viewer.