Changeset 8 for UGrammer.pas
- Timestamp:
- Oct 15, 2007, 8:28:00 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
UGrammer.pas
r7 r8 65 65 end; 66 66 67 TGrammerItem = record67 TGrammerItem = class 68 68 private 69 69 Processed: Boolean; … … 77 77 Repetition: Boolean; 78 78 procedure GetPossibleCharacters(Path: TGrammerPath; var Characters: TPossibleCharacters); 79 constructor Create (ItemType: TRuleType);79 constructor Create; 80 80 end; 81 81 … … 86 86 Name: string; 87 87 RuleType: TRuleType; 88 Items: arrayof TGrammerItem;88 Items: TList; // of TGrammerItem; 89 89 procedure ClearProcessed; 90 90 function Add: TGrammerItem; 91 procedure AddTerminal( Character: Char; Optional,Repetition: Boolean);91 procedure AddTerminal(ACharacter: Char; AOptional, ARepetition: Boolean); 92 92 procedure AddTerminalText(Text: string); 93 procedure AddRule( Rule: TGrammerRule; Optional,Repetition: Boolean);93 procedure AddRule(ARule: TGrammerRule; AOptional, ARepetition: Boolean); 94 94 procedure ProcessCharacter(Character: Char); 95 95 procedure GetPossibleCharacters(Path: TGrammerPath; 96 96 var Characters: TPossibleCharacters; UseIndex: Integer = 0; UseCharIndex: Integer = -1); 97 97 constructor Create; 98 destructor Destroy; override; 98 99 end; 99 100 … … 119 120 function TGrammerRule.Add: TGrammerItem; 120 121 begin 121 SetLength(Items, Length(Items) + 1);122 Result := Items[High(Items)];122 Result := TGrammerItem.Create; 123 Items.Add(Result); 123 124 Result.Parent := Self; 124 125 end; 125 126 126 procedure TGrammerRule.AddRule(Rule: TGrammerRule; Optional, 127 Repetition: Boolean); 128 begin 129 SetLength(Items, Length(Items) + 1); 130 Items[High(Items)].ItemType := itNonterminal; 131 Items[High(Items)].Rule := Rule; 132 Items[High(Items)].Optional := Optional; 133 Items[High(Items)].Repetition := Repetition; 134 Items[High(Items)].Parent := Self; 135 end; 136 137 procedure TGrammerRule.AddTerminal(Character: Char; Optional, Repetition: Boolean); 138 begin 139 SetLength(Items, Length(Items) + 1); 140 Items[High(Items)].ItemType := itTerminal; 141 Items[High(Items)].Character := Character; 142 Items[High(Items)].Optional := Optional; 143 Items[High(Items)].Repetition := Repetition; 144 Items[High(Items)].Parent := Self; 127 procedure TGrammerRule.AddRule(ARule: TGrammerRule; AOptional, 128 ARepetition: Boolean); 129 begin 130 with Add do begin 131 ItemType := itNonterminal; 132 Rule := ARule; 133 Optional := AOptional; 134 Repetition := ARepetition; 135 Parent := Self; 136 end; 137 end; 138 139 procedure TGrammerRule.AddTerminal(ACharacter: Char; AOptional, ARepetition: Boolean); 140 begin 141 with Add do begin 142 ItemType := itTerminal; 143 Character := ACharacter; 144 Optional := AOptional; 145 Repetition := ARepetition; 146 Parent := Self; 147 end; 145 148 end; 146 149 … … 156 159 I: Integer; 157 160 begin 158 for I := 0 to High(Items) do with Items[I]do begin161 for I := 0 to Items.Count - 1 do with TGrammerItem(Items[I]) do begin 159 162 Processed := False; 160 163 end; … … 164 167 begin 165 168 Ownership := nil; 169 Items := TList.Create; 170 end; 171 172 destructor TGrammerRule.Destroy; 173 var 174 I: Integer; 175 begin 176 for I := 0 to Items.Count - 1 do 177 TGrammerItem(Items[I]).Free; 178 Items.Free; 179 inherited; 166 180 end; 167 181 … … 196 210 end else begin 197 211 // Generate alternatives 198 for I := 0 to High(Items)do begin212 for I := 0 to Items.Count - 1 do begin 199 213 Path.Items[High(Path.Items)].ItemIndex := I; 200 214 //Inc(Path.Items[High(Path.Items)].CharIndex); 201 Items[I].GetPossibleCharacters(Path, Characters);215 TGrammerItem(Items[I]).GetPossibleCharacters(Path, Characters); 202 216 end; 203 217 end; … … 205 219 rtSequence: begin 206 220 TempPath.Assign(Path); 207 if UseIndex >= Length(Items)then begin221 if UseIndex >= Items.Count then begin 208 222 // Forward generation to upper item 209 223 SetLength(Path.Items, Length(Path.Items) - 1); … … 217 231 end else begin 218 232 Path.Items[High(Path.Items)].ItemIndex := UseIndex; 219 Items[UseIndex].GetPossibleCharacters(Path, Characters);233 TGrammerItem(Items[UseIndex]).GetPossibleCharacters(Path, Characters); 220 234 end; 221 235 // Check repetition 222 if (UseIndex > 0) and not Items[UseIndex - 1].Processed then223 if Items[UseIndex - 1].Repetition then begin236 if (UseIndex > 0) and not TGrammerItem(Items[UseIndex - 1]).Processed then 237 if TGrammerItem(Items[UseIndex - 1]).Repetition then begin 224 238 TempPath.Items[High(TempPath.Items)].ItemIndex := UseIndex - 1; 225 Items[UseIndex - 1].GetPossibleCharacters(TempPath, Characters);239 TGrammerItem(Items[UseIndex - 1]).GetPossibleCharacters(TempPath, Characters); 226 240 end; 227 241 end; … … 251 265 begin 252 266 for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do begin 253 for II := 0 to Length(Items) - 1 do with Items[II]do begin267 for II := 0 to Items.Count - 1 do with TGrammerItem(Items[II]) do begin 254 268 if (ItemType = itNonterminal) and (Rule = nil) then begin 255 269 J := 0; … … 499 513 { TGrammerItem } 500 514 501 constructor TGrammerItem.Create (ItemType: TRuleType);515 constructor TGrammerItem.Create; 502 516 begin 503 517 Rule := nil; … … 557 571 Index := High(Items); 558 572 while not Success and (Index >= 0) do begin 559 with Items[Index] do if Rule.Items[ItemIndex].Repetition then begin573 with Items[Index] do if TGrammerItem(Rule.Items[ItemIndex]).Repetition then begin 560 574 Success := True; 561 575 //Inc(CharIndex); 562 576 end else begin 563 if ((ItemIndex + 1) < Length(Rule.Items)) and (Rule.RuleType = rtSequence) then begin577 if ((ItemIndex + 1) < Rule.Items.Count) and (Rule.RuleType = rtSequence) then begin 564 578 Inc(ItemIndex); 565 579 //Inc(CharIndex);
Note:
See TracChangeset
for help on using the changeset viewer.