Changeset 30 for branches/Analyzátor gramatiky/UGrammer.pas
- Timestamp:
- Nov 11, 2009, 12:56:32 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Analyzátor gramatiky/UGrammer.pas
r29 r30 20 20 end; 21 21 22 TPathItemList = specialize TFPGObjectList<TPathItem>; 23 22 24 TGrammerPath = class 23 Items: array of TPathItem;25 Items: TPathItemList; 24 26 procedure Assign(Source: TGrammerPath); 25 27 procedure Next; 28 constructor Create; 29 destructor Destroy; override; 26 30 end; 27 31 … … 32 36 end; 33 37 38 TPossibleCharacterList = specialize TFPGObjectList<TPossibleCharacter>; 39 40 { TPossibleCharacters } 41 34 42 TPossibleCharacters = class 35 Items: array of TPossibleCharacter;43 Items: TPossibleCharacterList; 36 44 procedure Assign(Source: TPossibleCharacters); 45 constructor Create; 46 destructor Destroy; override; 37 47 end; 38 48 … … 135 145 I: Integer; 136 146 begin 137 for I := 0 to Items.Count - 1 do with TGrammerItem(Items[I]) do begin 147 for I := 0 to Items.Count - 1 do 148 with Items[I] do begin 138 149 Processed := False; 139 150 end; … … 160 171 TempPath: TGrammerPath; 161 172 begin 162 SetLength(Path.Items, Length(Path.Items) + 1);163 with Path.Items[ High(Path.Items)] do begin173 TempPath := TGrammerPath.Create; 174 with Path.Items[Path.Items.Add(TPathItem.Create)] do begin 164 175 Rule := Self; 165 176 ItemIndex := UseIndex; … … 172 183 if UseIndex > 0 then begin 173 184 // Forward generation to upper item 174 SetLength(Path.Items, Length(Path.Items) - 1); 175 with Path.Items[High(Path.Items)] do begin 185 with Path.Items[Path.Items.Add(TPathItem.Create)] do begin 176 186 NextItemIndex := ItemIndex; 177 187 NextCharIndex := CharIndex; 178 188 NextRule := Rule; 179 189 end; 180 SetLength(Path.Items, Length(Path.Items)- 1);190 Path.Items.Delete(Path.Items.Count - 1); 181 191 NextRule.GetPossibleCharacters(Path, Characters, NextItemIndex + 1, NextCharIndex); 182 192 end else begin 183 193 // Generate alternatives 184 194 for I := 0 to Items.Count - 1 do begin 185 Path.Items[ High(Path.Items)].ItemIndex := I;195 Path.Items[Path.Items.Count - 1].ItemIndex := I; 186 196 //Inc(Path.Items[High(Path.Items)].CharIndex); 187 197 Items[I].GetPossibleCharacters(Path, Characters); … … 193 203 if UseIndex >= Items.Count then begin 194 204 // Forward generation to upper item 195 SetLength(Path.Items, Length(Path.Items)- 1);196 with Path.Items[ High(Path.Items)] do begin205 Path.Items.Delete(Path.Items.Count - 1); 206 with Path.Items[Path.Items.Count - 1] do begin 197 207 NextItemIndex := ItemIndex; 198 208 NextCharIndex := CharIndex; 199 209 NextRule := Rule; 200 210 end; 201 SetLength(Path.Items, Length(Path.Items)- 1);211 Path.Items.Delete(Path.Items.Count - 1); 202 212 NextRule.GetPossibleCharacters(Path, Characters, NextItemIndex + 1, NextCharIndex); 203 213 end else begin 204 Path.Items[ High(Path.Items)].ItemIndex := UseIndex;214 Path.Items[Path.Items.Count - 1].ItemIndex := UseIndex; 205 215 Items[UseIndex].GetPossibleCharacters(Path, Characters); 206 216 end; … … 208 218 if (UseIndex > 0) and not Items[UseIndex - 1].Processed then 209 219 if Items[UseIndex - 1].Repetition then begin 210 TempPath.Items[ High(TempPath.Items)].ItemIndex := UseIndex - 1;220 TempPath.Items[TempPath.Items.Count - 1].ItemIndex := UseIndex - 1; 211 221 Items[UseIndex - 1].GetPossibleCharacters(TempPath, Characters); 212 222 end; 213 223 end; 214 224 end; 225 TempPath.Destroy; 215 226 end; 216 227 … … 225 236 I: Integer; 226 237 begin 227 for I := 0 to Rules.Count - 1 do with Rules[I] do begin 238 for I := 0 to Rules.Count - 1 do 239 with Rules[I] do begin 228 240 ClearProcessed; 229 241 end; … … 278 290 case ItemType of 279 291 itTerminal: begin 280 SetLength(Characters.Items, Length(Characters.Items) + 1);281 Characters.Items[ High(Characters.Items)].Character := Character;282 Characters.Items[ High(Characters.Items)].RulePath.Assign(Path);283 with Characters.Items[ High(Characters.Items)].RulePath do begin284 Inc(Items[ High(Items)].ItemIndex);292 Characters.Items.Add(TPossibleCharacter.Create); 293 Characters.Items[Characters.Items.Count - 1].Character := Character; 294 Characters.Items[Characters.Items.Count - 1].RulePath.Assign(Path); 295 with Characters.Items[Characters.Items.Count - 1].RulePath do begin 296 Inc(Items[Items.Count - 1].ItemIndex); 285 297 end; 286 298 end; … … 292 304 // Forward generation to upper item 293 305 //SetLength(Path.Items, Length(Path.Items) - 1); 294 with Path.Items[ High(Path.Items)] do begin306 with Path.Items[Path.Items.Count - 1] do begin 295 307 NextItemIndex := ItemIndex; 296 308 NextCharIndex := CharIndex; 297 309 NextRule := Rule; 298 310 end; 299 SetLength(Path.Items, Length(Path.Items) - 1);311 Path.Items.Count := Path.Items.Count - 1; 300 312 NextRule.GetPossibleCharacters(Path, Characters, NextItemIndex + 1, NextCharIndex); 301 313 end; … … 308 320 I: Integer; 309 321 begin 310 SetLength(Items, Length(Source.Items)); 311 for I := 0 to High(Items) do Items[I].Assign(Source.Items[I]); 322 for I := 0 to Items.Count - 1 do 323 Items[I].Destroy; 324 Items.Count := Source.Items.Count; 325 for I := 0 to Items.Count - 1 do begin 326 Items[I] := TPathItem.Create; 327 Items[I].Assign(Source.Items[I]); 328 end; 312 329 end; 313 330 … … 318 335 begin 319 336 Success := False; 320 Index := High(Items);337 Index := Items.Count - 1; 321 338 while not Success and (Index >= 0) do begin 322 339 with Items[Index] do if Rule.Items[ItemIndex].Repetition then begin … … 331 348 end; 332 349 end; 333 SetLength(Items, Index + 1); 350 Items.Count := Index + 1; 351 end; 352 353 constructor TGrammerPath.Create; 354 begin 355 Items := TPathItemList.Create; 356 end; 357 358 destructor TGrammerPath.Destroy; 359 begin 360 Items.Destroy; 361 inherited Destroy; 334 362 end; 335 363 … … 358 386 I: Integer; 359 387 begin 360 SetLength(Items, Length(Source.Items)); 361 for I := 0 to High(Items) do Items[I].Assign(Source.Items[I]); 388 Items.Count := Source.Items.Count; 389 for I := 0 to Items.Count - 1 do 390 Items[I].Assign(Source.Items[I]); 391 end; 392 393 constructor TPossibleCharacters.Create; 394 begin 395 Items := TPossibleCharacterList.Create; 396 end; 397 398 destructor TPossibleCharacters.Destroy; 399 begin 400 Items.Destroy; 401 inherited Destroy; 362 402 end; 363 403
Note:
See TracChangeset
for help on using the changeset viewer.