Changeset 119 for branches/generator/Forms/UFormMain.pas
- Timestamp:
- Nov 29, 2017, 12:31:26 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/generator/Forms/UFormMain.pas
r118 r119 15 15 TFormMain = class(TForm) 16 16 ABuildCompiler: TAction; 17 ANew: TAction; 18 ASave: TAction; 17 19 AExit: TAction; 18 20 AOpen: TAction; … … 24 26 MemoBNF: TMemo; 25 27 MenuItem1: TMenuItem; 28 MenuItem10: TMenuItem; 29 MenuItem11: TMenuItem; 26 30 MenuItem2: TMenuItem; 27 31 MenuItem3: TMenuItem; … … 39 43 procedure ABuildCompilerExecute(Sender: TObject); 40 44 procedure AExitExecute(Sender: TObject); 45 procedure ANewExecute(Sender: TObject); 41 46 procedure AOpenExecute(Sender: TObject); 42 47 procedure ARulesExecute(Sender: TObject); 43 48 procedure ASaveAsExecute(Sender: TObject); 49 procedure ASaveExecute(Sender: TObject); 44 50 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 45 51 procedure FormShow(Sender: TObject); 46 52 private 47 { private declarations }53 procedure ReloadMemo; 48 54 public 49 55 { public declarations } … … 70 76 FormRules.Rules := Core.Grammer.Rules; 71 77 FormRules.ShowModal; 72 MemoBNF.Lines.Text := Core.Grammer.GetBNF;78 ReloadMemo; 73 79 FreeAndNil(FormRules); 74 80 end; … … 82 88 end; 83 89 90 procedure TFormMain.ASaveExecute(Sender: TObject); 91 begin 92 Core.Grammer.SaveToXmlFile(Core.Grammer.FileName); 93 end; 94 84 95 procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction); 85 96 begin … … 90 101 begin 91 102 Core.PersistentForm1.Load(Self, True); 92 MemoBNF.Lines.Text := Core.Grammer.GetBNF; 103 ReloadMemo; 104 end; 105 106 procedure TFormMain.ReloadMemo; 107 begin 108 MemoBNF.Lines.Text := Core.Grammer.GetString; 93 109 end; 94 110 … … 98 114 end; 99 115 116 procedure TFormMain.ANewExecute(Sender: TObject); 117 begin 118 Core.Grammer.Rules.Clear; 119 ReloadMemo; 120 end; 121 100 122 procedure TFormMain.ABuildCompilerExecute(Sender: TObject); 101 var102 ParserFile: TStringList;103 SourceFile: TStringList;104 Rule: TRule;105 Item: TRuleItem;106 OutputDir: string;107 Line: string;108 I: Integer;109 123 begin 110 OutputDir := 'Generated'; 111 ForceDirectories(OutputDir); 112 113 ParserFile := TStringList.Create; 114 with ParserFile do begin 115 Add('program Compiler;'); 116 Add(''); 117 Add('{$MODE Delphi}'); 118 Add(''); 119 Add('uses'); 120 Add(' Source;'); 121 Add(''); 122 Add('procedure Compile;'); 123 Add('begin'); 124 Add('end;'); 125 Add(''); 126 Add('begin'); 127 Add(' Compile;'); 128 Add('end.'); 129 SaveToFile(OutputDir + DirectorySeparator + 'Compiler.pas'); 130 end; 131 FreeAndNil(ParserFile); 132 133 SourceFile := TStringList.Create; 134 with SourceFile do begin 135 Add('unit Source;'); 136 Add(''); 137 Add('{$MODE Delphi}'); 138 Add(''); 139 Add('interface'); 140 Add(''); 141 Add('uses'); 142 Add(' fgl;'); 143 Add(''); 144 Add('type'); 145 for Rule in Core.Grammer.Rules do begin 146 Add(' T' + Rule.Name + ' = class;'); 147 end; 148 Add(''); 149 for Rule in Core.Grammer.Rules do begin 150 Add(' T' + Rule.Name + ' = class'); 151 for Item in Rule.Items do begin 152 if Item.RuleItemType = ritNonTerminal then 153 if Item.Repetitive then 154 Add(' ' + Item.NonTerminal.Name + ': TFPGObjectList<T' + Item.NonTerminal.Name + '>;') 155 else Add(' ' + Item.NonTerminal.Name + ': T' + Item.NonTerminal.Name + ';'); 156 end; 157 Add(' end;' + LineEnding); 158 end; 159 Add(''); 160 Add('implementation'); 161 Add(''); 162 for Rule in Core.Grammer.Rules do begin 163 Add('function Parse' + Rule.Name + ': Boolean;'); 164 Add('begin'); 165 I := 0; 166 for Item in Rule.Items do begin 167 Line := ' '; 168 if Rule.Items.RuleType = rtOr then begin 169 if I > 0 then Line := Line + 'else '; 170 Line := Line + 'if '; 171 end else 172 if Rule.Items.RuleType = rtAnd then begin 173 Line := Line + ''; 174 end; 175 if Item.RuleItemType = ritTerminal then 176 Line := Line + 'Expect(''' + Item.Terminal + ''')' 177 else if Item.RuleItemType = ritNonTerminal then 178 Line := Line + 'Parse' + Item.NonTerminal.Name; 179 if Rule.Items.RuleType = rtOr then begin 180 Line := Line + ' then '; 181 end else 182 if Rule.Items.RuleType = rtAnd then begin 183 Line := Line + ';'; 184 end; 185 Add(Line); 186 Inc(I); 187 end; 188 if Rule.Items.RuleType = rtOr then begin 189 Add(' else ShowError(''Unexpected token'');'); 190 end; 191 Add('end;'); 192 Add(''); 193 end; 194 195 Add('end.'); 196 197 SaveToFile(OutputDir + DirectorySeparator +'Source.pas'); 198 end; 199 FreeAndNil(SourceFile); 124 Core.Grammer.BuildCompiler; 200 125 end; 201 126 … … 205 130 if OpenDialog1.Execute then begin 206 131 Core.Grammer.LoadFromXmlFile(OpenDialog1.FileName); 132 ReloadMemo; 207 133 end; 208 134 end;
Note:
See TracChangeset
for help on using the changeset viewer.