Changeset 33
- Timestamp:
- Apr 25, 2011, 10:41:52 PM (14 years ago)
- Location:
- tools/Grammer
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/Grammer/Grammer.lpi
r31 r33 39 39 </Item2> 40 40 </RequiredPackages> 41 <Units Count=" 4">41 <Units Count="5"> 42 42 <Unit0> 43 43 <Filename Value="Grammer.lpr"/> 44 44 <IsPartOfProject Value="True"/> 45 45 <UnitName Value="Grammer"/> 46 <EditorIndex Value=" 3"/>46 <EditorIndex Value="4"/> 47 47 <WindowIndex Value="0"/> 48 48 <TopLine Value="1"/> … … 50 50 <UsageCount Value="20"/> 51 51 <Loaded Value="True"/> 52 <DefaultSyntaxHighlighter Value="Delphi"/> 52 53 </Unit0> 53 54 <Unit1> … … 60 61 <EditorIndex Value="0"/> 61 62 <WindowIndex Value="0"/> 62 <TopLine Value=" 72"/>63 <CursorPos X=" 11" Y="91"/>63 <TopLine Value="93"/> 64 <CursorPos X="20" Y="98"/> 64 65 <UsageCount Value="20"/> 65 66 <Loaded Value="True"/> 66 67 <LoadedDesigner Value="True"/> 68 <DefaultSyntaxHighlighter Value="Delphi"/> 67 69 </Unit1> 68 70 <Unit2> 69 71 <Filename Value="Test.grammer"/> 70 72 <IsPartOfProject Value="True"/> 71 <EditorIndex Value=" 1"/>73 <EditorIndex Value="2"/> 72 74 <WindowIndex Value="0"/> 73 75 <TopLine Value="1"/> … … 81 83 <IsPartOfProject Value="True"/> 82 84 <UnitName Value="UGrammer"/> 83 <EditorIndex Value="2"/> 84 <WindowIndex Value="0"/> 85 <TopLine Value="25"/> 86 <CursorPos X="14" Y="42"/> 87 <UsageCount Value="20"/> 88 <Loaded Value="True"/> 85 <EditorIndex Value="3"/> 86 <WindowIndex Value="0"/> 87 <TopLine Value="1"/> 88 <CursorPos X="44" Y="12"/> 89 <UsageCount Value="20"/> 90 <Loaded Value="True"/> 91 <DefaultSyntaxHighlighter Value="Delphi"/> 89 92 </Unit3> 93 <Unit4> 94 <Filename Value="Pl0Grammer.pas"/> 95 <IsPartOfProject Value="True"/> 96 <UnitName Value="Pl0Grammer"/> 97 <EditorIndex Value="1"/> 98 <WindowIndex Value="0"/> 99 <TopLine Value="11"/> 100 <CursorPos X="24" Y="21"/> 101 <UsageCount Value="20"/> 102 <Loaded Value="True"/> 103 <DefaultSyntaxHighlighter Value="Delphi"/> 104 </Unit4> 90 105 </Units> 91 106 <JumpHistory Count="30" HistoryIndex="29"> 92 107 <Position1> 93 108 <Filename Value="UMainForm.pas"/> 94 <Caret Line="1 16" Column="17" TopLine="99"/>109 <Caret Line="102" Column="1" TopLine="84"/> 95 110 </Position1> 96 111 <Position2> 97 112 <Filename Value="UMainForm.pas"/> 98 <Caret Line="1 14" Column="15" TopLine="99"/>113 <Caret Line="103" Column="1" TopLine="84"/> 99 114 </Position2> 100 115 <Position3> 101 <Filename Value="U Grammer.pas"/>102 <Caret Line="11 6" Column="22" TopLine="100"/>116 <Filename Value="UMainForm.pas"/> 117 <Caret Line="112" Column="1" TopLine="84"/> 103 118 </Position3> 104 119 <Position4> 105 <Filename Value="U Grammer.pas"/>106 <Caret Line=" 3" Column="14" TopLine="1"/>120 <Filename Value="UMainForm.pas"/> 121 <Caret Line="101" Column="1" TopLine="84"/> 107 122 </Position4> 108 123 <Position5> 109 <Filename Value="U Grammer.pas"/>110 <Caret Line=" 57" Column="1" TopLine="40"/>124 <Filename Value="UMainForm.pas"/> 125 <Caret Line="102" Column="1" TopLine="84"/> 111 126 </Position5> 112 127 <Position6> 113 128 <Filename Value="UMainForm.pas"/> 114 <Caret Line="1 16" Column="17" TopLine="99"/>129 <Caret Line="103" Column="1" TopLine="84"/> 115 130 </Position6> 116 131 <Position7> 117 132 <Filename Value="UMainForm.pas"/> 118 <Caret Line="1 24" Column="14" TopLine="109"/>133 <Caret Line="101" Column="1" TopLine="84"/> 119 134 </Position7> 120 135 <Position8> 121 <Filename Value="U Grammer.pas"/>122 <Caret Line="1 32" Column="19" TopLine="101"/>136 <Filename Value="UMainForm.pas"/> 137 <Caret Line="102" Column="1" TopLine="84"/> 123 138 </Position8> 124 139 <Position9> 125 <Filename Value="U Grammer.pas"/>126 <Caret Line="1 27" Column="33" TopLine="104"/>140 <Filename Value="UMainForm.pas"/> 141 <Caret Line="103" Column="1" TopLine="84"/> 127 142 </Position9> 128 143 <Position10> 129 <Filename Value="U Grammer.pas"/>130 <Caret Line=" 26" Column="34" TopLine="3"/>144 <Filename Value="UMainForm.pas"/> 145 <Caret Line="101" Column="1" TopLine="84"/> 131 146 </Position10> 132 147 <Position11> 133 <Filename Value="U Grammer.pas"/>134 <Caret Line=" 71" Column="14" TopLine="64"/>148 <Filename Value="UMainForm.pas"/> 149 <Caret Line="102" Column="1" TopLine="84"/> 135 150 </Position11> 136 151 <Position12> 137 <Filename Value="U Grammer.pas"/>138 <Caret Line="1 14" Column="15" TopLine="93"/>152 <Filename Value="UMainForm.pas"/> 153 <Caret Line="103" Column="1" TopLine="84"/> 139 154 </Position12> 140 155 <Position13> 141 <Filename Value="U Grammer.pas"/>142 <Caret Line="1 " Column="1" TopLine="1"/>156 <Filename Value="UMainForm.pas"/> 157 <Caret Line="101" Column="1" TopLine="84"/> 143 158 </Position13> 144 159 <Position14> 145 <Filename Value="U Grammer.pas"/>146 <Caret Line="1 36" Column="14" TopLine="116"/>160 <Filename Value="UMainForm.pas"/> 161 <Caret Line="106" Column="11" TopLine="84"/> 147 162 </Position14> 148 163 <Position15> 149 164 <Filename Value="UMainForm.pas"/> 150 <Caret Line="1 17" Column="10" TopLine="109"/>165 <Caret Line="105" Column="96" TopLine="91"/> 151 166 </Position15> 152 167 <Position16> 153 168 <Filename Value="UMainForm.pas"/> 154 <Caret Line=" 118" Column="10" TopLine="109"/>169 <Caret Line="35" Column="48" TopLine="11"/> 155 170 </Position16> 156 171 <Position17> 157 172 <Filename Value="UMainForm.pas"/> 158 <Caret Line="1 34" Column="16" TopLine="111"/>173 <Caret Line="126" Column="18" TopLine="124"/> 159 174 </Position17> 160 175 <Position18> 161 176 <Filename Value="UMainForm.pas"/> 162 <Caret Line="1 35" Column="15" TopLine="111"/>177 <Caret Line="108" Column="26" TopLine="91"/> 163 178 </Position18> 164 179 <Position19> 165 180 <Filename Value="UMainForm.pas"/> 166 <Caret Line=" 134" Column="19" TopLine="111"/>181 <Caret Line="95" Column="21" TopLine="80"/> 167 182 </Position19> 168 183 <Position20> 169 <Filename Value="U Grammer.pas"/>170 <Caret Line=" 42" Column="1" TopLine="25"/>184 <Filename Value="UMainForm.pas"/> 185 <Caret Line="85" Column="29" TopLine="71"/> 171 186 </Position20> 172 187 <Position21> 173 <Filename Value="U Grammer.pas"/>174 <Caret Line=" 146" Column="38" TopLine="125"/>188 <Filename Value="UMainForm.pas"/> 189 <Caret Line="34" Column="47" TopLine="17"/> 175 190 </Position21> 176 191 <Position22> 177 <Filename Value="U Grammer.pas"/>178 <Caret Line=" 42" Column="91" TopLine="42"/>192 <Filename Value="UMainForm.pas"/> 193 <Caret Line="114" Column="57" TopLine="91"/> 179 194 </Position22> 180 195 <Position23> 181 <Filename Value="U Grammer.pas"/>182 <Caret Line="1 48" Column="1" TopLine="125"/>196 <Filename Value="UMainForm.pas"/> 197 <Caret Line="109" Column="73" TopLine="95"/> 183 198 </Position23> 184 199 <Position24> 185 <Filename Value="U Grammer.pas"/>186 <Caret Line="1 46" Column="43" TopLine="125"/>200 <Filename Value="UMainForm.pas"/> 201 <Caret Line="107" Column="63" TopLine="91"/> 187 202 </Position24> 188 203 <Position25> 189 <Filename Value="U Grammer.pas"/>190 <Caret Line=" 42" Column="91" TopLine="42"/>204 <Filename Value="UMainForm.pas"/> 205 <Caret Line="128" Column="62" TopLine="111"/> 191 206 </Position25> 192 207 <Position26> 193 208 <Filename Value="UMainForm.pas"/> 194 <Caret Line="1 09" Column="36" TopLine="81"/>209 <Caret Line="133" Column="1" TopLine="106"/> 195 210 </Position26> 196 211 <Position27> 197 212 <Filename Value="UMainForm.pas"/> 198 <Caret Line="1 72" Column="46" TopLine="167"/>213 <Caret Line="135" Column="15" TopLine="106"/> 199 214 </Position27> 200 215 <Position28> 201 216 <Filename Value="UMainForm.pas"/> 202 <Caret Line=" 186" Column="13" TopLine="164"/>217 <Caret Line="97" Column="24" TopLine="82"/> 203 218 </Position28> 204 219 <Position29> 205 220 <Filename Value="UMainForm.pas"/> 206 <Caret Line=" 89" Column="48" TopLine="73"/>221 <Caret Line="95" Column="5" TopLine="86"/> 207 222 </Position29> 208 223 <Position30> 209 224 <Filename Value="UMainForm.pas"/> 210 <Caret Line=" 90" Column="43" TopLine="73"/>225 <Caret Line="110" Column="51" TopLine="88"/> 211 226 </Position30> 212 227 </JumpHistory> … … 221 236 <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> 222 237 </SearchPaths> 238 <Parsing> 239 <SyntaxOptions> 240 <SyntaxMode Value="Delphi"/> 241 </SyntaxOptions> 242 </Parsing> 223 243 <Linking> 224 244 <Options> … … 229 249 </Linking> 230 250 <Other> 251 <CompilerMessages> 252 <UseMsgFile Value="True"/> 253 </CompilerMessages> 231 254 <CompilerPath Value="$(CompPath)"/> 232 255 </Other> -
tools/Grammer/Grammer.lpr
r31 r33 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UMainForm, UGrammer 10 Forms, UMainForm, UGrammer, Pl0Grammer 11 11 { you can add units after this }; 12 12 -
tools/Grammer/UMainForm.lfm
r31 r33 1 1 object Form1: TForm1 2 Left = 3563 Height = 4214 Top = 1275 Width = 4712 Left = 209 3 Height = 552 4 Top = 43 5 Width = 673 6 6 Caption = 'Grammer transformator' 7 ClientHeight = 4218 ClientWidth = 4717 ClientHeight = 552 8 ClientWidth = 673 9 9 OnCreate = FormCreate 10 10 OnDestroy = FormDestroy … … 14 14 Height = 21 15 15 Top = 25 16 Width = 37516 Width = 577 17 17 Anchors = [akTop, akLeft, akRight] 18 18 TabOrder = 0 … … 28 28 end 29 29 object Button1: TButton 30 Left = 39130 Left = 593 31 31 Height = 25 32 32 Top = 25 … … 49 49 Height = 21 50 50 Top = 72 51 Width = 37451 Width = 576 52 52 Anchors = [akTop, akLeft, akRight] 53 53 TabOrder = 2 … … 55 55 end 56 56 object Button2: TButton 57 Left = 39157 Left = 593 58 58 Height = 25 59 59 Top = 72 … … 64 64 TabOrder = 3 65 65 end 66 object Button 3: TButton66 object ButtonGenerate: TButton 67 67 Left = 11 68 68 Height = 25 … … 70 70 Width = 75 71 71 Caption = 'Generate' 72 OnClick = Button 3Click72 OnClick = ButtonGenerateClick 73 73 TabOrder = 4 74 74 end 75 75 object Memo1: TMemo 76 76 Left = 8 77 Height = 27677 Height = 407 78 78 Top = 140 79 Width = 45879 Width = 660 80 80 Anchors = [akTop, akLeft, akRight, akBottom] 81 Font.Name = 'freemono' 82 ParentFont = False 81 83 ScrollBars = ssAutoBoth 82 84 TabOrder = 5 -
tools/Grammer/UMainForm.pas
r31 r33 1 1 unit UMainForm; 2 2 3 {$mode objfpc}{$H+}3 {$mode Delphi}{$H+} 4 4 5 5 interface … … 16 16 Button1: TButton; 17 17 Button2: TButton; 18 Button 3: TButton;18 ButtonGenerate: TButton; 19 19 ButtonDump: TButton; 20 20 Edit1: TEdit; … … 25 25 procedure Button1Click(Sender: TObject); 26 26 procedure Button2Click(Sender: TObject); 27 procedure Button 3Click(Sender: TObject);27 procedure ButtonGenerateClick(Sender: TObject); 28 28 procedure ButtonDumpClick(Sender: TObject); 29 29 procedure FormCreate(Sender: TObject); 30 30 procedure FormDestroy(Sender: TObject); 31 31 private 32 LastIndex: Integer; 32 33 function DumpGroup(Group: TGrammerRule): string; 34 function GenerateGroup(Group: TGrammerRule; Lines: TStrings): string; 35 function GetFreeName(Name: string): string; 33 36 { private declarations } 34 37 public 35 38 Grammer: TGrammer; 36 procedure InitPascal; 37 end; 39 end; 38 40 39 41 var … … 43 45 44 46 {$R *.lfm} 47 48 uses 49 Pl0Grammer; 45 50 46 51 { TForm1 } … … 68 73 end; 69 74 70 procedure TForm1.Button3Click(Sender: TObject); 71 begin 72 75 procedure TForm1.ButtonGenerateClick(Sender: TObject); 76 var 77 I: Integer; 78 Row: string; 79 begin 80 Memo1.Clear; 81 with Grammer do 82 for I := 0 to Rules.Count - 1 do 83 with TGrammerRule(Rules[I]) do begin 84 Row := 'T' + Name + ' = class' + LineEnding; 85 Row := Row + GenerateGroup(TGrammerRule(Rules[I]), Memo1.Lines); 86 Row := Row + 'end;' + LineEnding; 87 Memo1.Lines.Add(Row); 88 end; 89 end; 90 91 function TForm1.GenerateGroup(Group: TGrammerRule; Lines: TStrings): string; 92 var 93 J: Integer; 94 Item: string; 95 UniqueName: string; 96 SwitchItems: string; 97 GroupContent: string; 98 TypeName: string; 99 begin 100 Result := ''; 101 SwitchItems := ''; 102 with Group do begin 103 for J := 0 to Items.Count - 1 do 104 with TGrammerRuleItem(Items[J]) do begin 105 Item := ''; 106 if ItemType = itText then begin 107 Item := '' 108 end else 109 if ItemType = itReference then begin 110 UniqueName := GetFreeName(Text); 111 if Text = 'Name' then TypeName := 'string' 112 else if Text = 'Number' then TypeName := 'Integer' 113 else TypeName := 'T' + Text; 114 Item := ' ' + UniqueName + ': ' + TypeName + ';' + LineEnding; 115 SwitchItems := SwitchItems + 'si' + UniqueName; 116 if J < (Items.Count - 1) then SwitchItems := SwitchItems + ', '; 117 end else 118 if ItemType = itGroup then begin 119 GroupContent := GenerateGroup(Group, Lines); 120 if GroupContent <> '' then begin 121 UniqueName := GetFreeName('Group'); 122 Item := ' ' + UniqueName + ': '; 123 Lines.Add('T' + UniqueName + ' = class' + LineEnding + 124 GroupContent + 'end' + LineEnding); 125 if Repetition then begin 126 Item := Item + 'TObjectList; // TList<T' + UniqueName + '>' + LineEnding 127 end else Item := Item + 'T' + UniqueName + ';' + LineEnding; 128 129 SwitchItems := SwitchItems + 'si' + UniqueName; 130 if J < (Items.Count - 1) then SwitchItems := SwitchItems + ', '; 131 end; 132 end else Item := ''; 133 134 //else if Optional then Item := '[ ' + Item + ' ]'; 135 Result := Result + Item; 136 end; 137 if (RuleType = rtAlternative) then begin 138 if SwitchItems <> '' then begin 139 Lines.Add('T' + Name + 'Switch = (' + SwitchItems + ');'); 140 Result := Result + ' Switch: T' + Name + 'Switch;' + LineEnding; 141 end; 142 end; 143 end; 144 end; 145 146 function TForm1.GetFreeName(Name: string): string; 147 begin 148 Inc(LastIndex); 149 Result := Name + IntToStr(LastIndex); 73 150 end; 74 151 … … 96 173 Result := Result + Item + ' '; 97 174 end; 98 99 175 end; 100 176 … … 104 180 Row: string; 105 181 begin 182 Memo1.Clear; 106 183 with Grammer do 107 184 for I := 0 to Rules.Count - 1 do … … 116 193 procedure TForm1.FormCreate(Sender: TObject); 117 194 begin 118 Grammer := TGrammer.Create; 119 InitPascal; 195 Grammer := TPl0Grammer.Create; 120 196 end; 121 197 … … 125 201 end; 126 202 127 procedure TForm1.InitPascal;128 begin129 with Grammer do begin130 Rules.Clear;131 with AddRule('program', rtSequence) do begin132 AddItem('block', itReference, False, False);133 AddItem('.', itText, False, False);134 end;135 with AddRule('block', rtSequence) do begin136 AddItem('declaration', itReference, True, False);137 AddItem('statement', itReference, False, False);138 end;139 with AddRule('declaration', rtAlternative) do begin140 AddItem('constant', itReference, False, False);141 AddItem('variable', itReference, False, False);142 AddItem('function', itReference, False, False);143 end;144 with AddRule('constant', rtSequence) do begin145 AddItem('const', itText, False, False);146 AddItem('constant_definition', itReference, False, False);147 with AddGroup(rtSequence, False, True), Items do begin148 AddItem(',', itText, False, False);149 AddItem('constant_definition', itReference, False, False);150 end;151 AddItem(';', itText, False, False);152 end;153 with AddRule('constant_definition', rtSequence) do begin154 AddItem('name', itReference, False, False);155 AddItem('=', itText, False, False);156 AddItem('number', itReference, False, False);157 end;158 with AddRule('variable', rtSequence) do begin159 AddItem('var', itText, False, False);160 AddItem('name', itReference, False, False);161 with AddGroup(rtSequence, False, True), Items do begin162 AddItem(',', itText, False, False);163 AddItem('name', itReference, False, False);164 end;165 AddItem(';', itText, False, False);166 end;167 with AddRule('function', rtSequence) do begin168 AddItem('function', itText, False, False);169 AddItem('name', itReference, False, False);170 AddItem('(', itText, False, False);171 with AddGroup(rtSequence, True, False), Items do begin172 AddItem('name', itReference, False, False);173 with AddGroup(rtSequence, False, True), Items do begin174 AddItem(',', itText, False, False);175 AddItem('name', itReference, False, False);176 end;177 end;178 AddItem(')', itText, False, False);179 AddItem('block', itReference, False, False);180 AddItem(';', itText, False, False);181 end;182 with AddRule('expression', rtSequence) do begin183 with AddGroup(rtAlternative, True, False), Items do begin184 AddItem('-', itText, False, False);185 AddItem('+', itText, False, False);186 end;187 AddItem('term', itReference, False, False);188 with AddGroup(rtSequence, False, True), Items do begin189 with AddGroup(rtAlternative, False, False), Items do begin190 AddItem('-', itText, False, False);191 AddItem('+', itText, False, False);192 end;193 AddItem('term', itReference, False, False);194 end;195 end;196 with AddRule('term', rtSequence) do begin197 AddItem('factor', itReference, False, False);198 with AddGroup(rtSequence, False, True), Items do begin199 with AddGroup(rtAlternative, False, False), Items do begin200 AddItem('*', itText, False, False);201 AddItem('/', itText, False, False);202 end;203 AddItem('factor', itReference, False, False);204 end;205 end;206 with AddRule('factor', rtAlternative) do begin207 with AddGroup(rtSequence, False, False), Items do begin208 AddItem('name', itReference, False, False);209 AddItem('(', itText, False, False);210 with AddGroup(rtSequence, True, False), Items do begin211 AddItem('expression', itReference, False, False);212 with AddGroup(rtSequence, False, True), Items do begin213 AddItem(',', itText, False, False);214 AddItem('expression', itReference, False, False);215 end;216 end;217 AddItem('(', itText, False, False);218 end;219 AddItem('number', itReference, False, False);220 AddItem('name', itReference, False, False);221 with AddGroup(rtSequence, False, False), Items do begin222 AddItem('(', itText, False, False);223 AddItem('expression', itReference, False, False);224 AddItem(')', itText, False, False);225 end;226 end;227 with AddRule('statement', rtSequence) do begin228 with AddGroup(rtAlternative, True, False), Items do begin229 with AddGroup(rtSequence, False, False), Items do begin230 AddItem('name', itReference, False, False);231 AddItem(':=', itText, False, False);232 AddItem('expression', itReference, False, False);233 end;234 with AddGroup(rtSequence, False, False), Items do begin235 AddItem('begin', itText, False, False);236 AddItem('statement', itReference, False, False);237 with AddGroup(rtSequence, False, True), Items do begin238 AddItem(',', itText, False, False);239 AddItem('statement', itReference, False, False);240 end;241 AddItem('end', itText, False, False);242 end;243 with AddGroup(rtSequence, False, False), Items do begin244 AddItem('if', itText, False, False);245 AddItem('condition', itReference, False, False);246 AddItem('then', itText, False, False);247 AddItem('statement', itReference, False, False);248 end;249 with AddGroup(rtSequence, False, False), Items do begin250 AddItem('whilef', itText, False, False);251 AddItem('condition', itReference, False, False);252 AddItem('do', itText, False, False);253 AddItem('statement', itReference, False, False);254 end;255 with AddGroup(rtSequence, False, False), Items do begin256 AddItem('return', itText, False, False);257 AddItem('expression', itReference, False, False);258 end;259 with AddGroup(rtSequence, False, False), Items do begin260 AddItem('write', itText, False, False);261 AddItem('expression', itReference, False, False);262 end;263 end;264 end;265 with AddRule('condition', rtAlternative) do begin266 with AddGroup(rtSequence, False, False), Items do begin267 AddItem('odd', itText, False, False);268 AddItem('expression', itReference, False, False);269 end;270 with AddGroup(rtSequence, False, False), Items do begin271 AddItem('expression', itReference, False, False);272 with AddGroup(rtAlternative, False, False), Items do begin273 AddItem('=', itText, False, False);274 AddItem('<>', itText, False, False);275 AddItem('<', itText, False, False);276 AddItem('<=', itText, False, False);277 AddItem('>', itText, False, False);278 AddItem('=>', itText, False, False);279 end;280 AddItem('expression', itReference, False, False);281 end;282 end;283 284 (*285 condition = ODD expression286 | expression ( '=' | '<>' | '<' | '<=' | '>' | '>=' ) expression .287 *)288 end;289 end;290 291 203 end. 292 204
Note:
See TracChangeset
for help on using the changeset viewer.