Changeset 7 for UGrammer.pas
- Timestamp:
- Oct 12, 2007, 1:43:10 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
UGrammer.pas
r6 r7 4 4 5 5 uses 6 Classes, ComCtrls ;6 Classes, ComCtrls, SysUtils; 7 7 8 8 type … … 50 50 procedure DeleteEmpty; 51 51 procedure MergeNonterminal(ARule: TGrammerRule); 52 procedure Join(ARule: TGrammerRule; ItemIndex: Integer); 52 53 function MergeToTerminal: string; 53 54 constructor Create; 54 55 destructor Destroy; override; 56 procedure Assign(Source: TProgramItem); 55 57 end; 56 58 … … 65 67 TGrammerItem = record 66 68 private 69 Processed: Boolean; 67 70 Parent: TGrammerRule; 68 Processed: Boolean;69 71 public 70 72 ItemType: TRuleItemType; 71 73 Character: Char; 72 74 Rule: TGrammerRule; 75 RuleName: string; 73 76 Optional: Boolean; 74 77 Repetition: Boolean; … … 80 83 private 81 84 public 85 Ownership: TGrammerRule; 82 86 Name: string; 83 87 RuleType: TRuleType; 84 88 Items: array of TGrammerItem; 85 89 procedure ClearProcessed; 90 function Add: TGrammerItem; 86 91 procedure AddTerminal(Character: Char; Optional, Repetition: Boolean); 87 92 procedure AddTerminalText(Text: string); … … 100 105 procedure ClearProcessed; 101 106 procedure Parse(Text: string; var ParsedProgram: TProgram); 107 procedure CorrectRuleLinks; 102 108 destructor Destroy; override; 103 109 end; … … 106 112 implementation 107 113 114 uses 115 UMainForm; 116 108 117 { TGrammerRule } 118 119 function TGrammerRule.Add: TGrammerItem; 120 begin 121 SetLength(Items, Length(Items) + 1); 122 Result := Items[High(Items)]; 123 Result.Parent := Self; 124 end; 109 125 110 126 procedure TGrammerRule.AddRule(Rule: TGrammerRule; Optional, … … 147 163 constructor TGrammerRule.Create; 148 164 begin 149 165 Ownership := nil; 150 166 end; 151 167 … … 225 241 for I := 0 to Rules.Count - 1 do with TGrammerRule(Rules[I]) do begin 226 242 ClearProcessed; 243 end; 244 end; 245 246 procedure TGrammer.CorrectRuleLinks; 247 var 248 I: Integer; 249 II: Integer; 250 J: Integer; 251 begin 252 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 begin 254 if (ItemType = itNonterminal) and (Rule = nil) then begin 255 J := 0; 256 while (J < Rules.Count) and (TGrammerRule(Rules[J]).Name <> RuleName) do Inc(J); 257 if J < Rules.Count then Rule := Rules[J] else 258 raise Exception.Create('Rule link correction failed on rule ' + 259 IntToStr(I) + ' item ' + IntToStr(II)); 260 end; 261 end; 227 262 end; 228 263 end; … … 294 329 ExpectedCharacters := ExpectedCharacters + Scope.Items[II].Character; 295 330 //raise Exception.Create('Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".'); 331 MainForm.Label1.Caption := 'Parse error. Expected "' + ExpectedCharacters + '" but found "' + Text[I] + '".'; 296 332 break; 297 333 end; … … 361 397 { TProgramItem } 362 398 399 procedure TProgramItem.Assign(Source: TProgramItem); 400 begin 401 402 end; 403 363 404 constructor TProgramItem.Create; 364 405 begin … … 423 464 Items.Free; 424 465 inherited; 466 end; 467 468 procedure TProgramItem.Join(ARule: TGrammerRule; ItemIndex: Integer); 469 var 470 SubProgramItem: TProgramItem; 471 I: Integer; 472 begin 473 if (ItemType = itNonterminal) and (Rule = ARule) then begin 474 SubProgramItem := TProgramItem(Items[ItemIndex]); 475 RuleBefore := SubProgramItem.RuleBefore; 476 Rule := SubProgramItem.Rule; 477 ItemType := SubProgramItem.ItemType; 478 Value := SubProgramItem.Value; 479 Items[ItemIndex] := nil; 480 for I := 0 to Items.Count - 1 do 481 if Assigned(Items[I]) then TProgramItem(Items[I]).Free; 482 Items := SubProgramItem.Items; 483 end; 484 for I := 0 to Items.Count - 1 do 485 TProgramItem(Items[I]).Join(ARule, ItemIndex); 425 486 end; 426 487
Note:
See TracChangeset
for help on using the changeset viewer.