Changeset 137 for branches/generator/UGrammer.pas
- Timestamp:
- Dec 27, 2017, 6:05:11 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/generator/UGrammer.pas
r136 r137 13 13 TGrammer = class; 14 14 TRuleItems = class; 15 TLookupTable = class; 15 16 16 17 TRuleItemType = (ritTerminal, ritNonTerminal, ritSubItems, ritTerminalRange); 17 18 TGrammerNotation = (gnBnf, gnEbnf); 19 TLookupTableAction = (taCreate, taReference); 18 20 19 21 { TRuleItem } … … 37 39 NonTerminal: TRule; 38 40 SubItems: TRuleItems; 41 LookupTableUsed: Boolean; 42 LookupTable: TLookupTable; 43 LookupTableName: string; 44 LookupTableAction: TLookupTableAction; 39 45 function GetCharLength: Integer; 40 46 procedure LoadFromXmlNode(Node: TDOMNode); … … 115 121 end; 116 122 123 TLookupTableItem = class 124 Name: string; 125 end; 126 127 { TLookupTable } 128 129 TLookupTable = class 130 Name: string; 131 Grammer: TGrammer; 132 Items: TFPGObjectList<TLookupTableItem>; 133 procedure LoadFromXmlNode(Node: TDOMNode); 134 procedure SaveToXmlNode(Node: TDOMNode); 135 constructor Create; 136 destructor Destroy; override; 137 end; 138 139 { TLookupTables } 140 141 TLookupTables = class(TFPGObjectList<TLookupTable>) 142 Grammer: TGrammer; 143 function FindName(Name: string): TLookupTable; 144 procedure LoadFromXmlNode(Node: TDOMNode); 145 procedure SaveToXmlNode(Node: TDOMNode); 146 end; 147 117 148 { TGrammer } 118 149 … … 128 159 FileName: string; 129 160 Rules: TRules; 161 LookupTables: TLookupTables; 130 162 TopRule: TRule; 131 163 WhiteSpaceRule: TRule; … … 151 183 implementation 152 184 185 { TLookupTables } 186 187 function TLookupTables.FindName(Name: string): TLookupTable; 188 var 189 I: Integer; 190 begin 191 I := 0; 192 while (I < Count) and (Items[I].Name <> Name) do Inc(I); 193 if I < Count then Result := Items[I] 194 else Result := nil; 195 end; 196 197 procedure TLookupTables.LoadFromXmlNode(Node: TDOMNode); 198 var 199 RuleNode: TDOMNode; 200 LookupTable: TLookupTable; 201 begin 202 RuleNode := Node.FirstChild; 203 while Assigned(RuleNode) do begin 204 if RuleNode.NodeName = 'LookupTable' then begin 205 LookupTable := TLookupTable.Create; 206 LookupTable.Grammer := Grammer; 207 LookupTable.LoadFromXmlNode(RuleNode); 208 Add(LookupTable); 209 end; 210 RuleNode := RuleNode.NextSibling; 211 end; 212 end; 213 214 procedure TLookupTables.SaveToXmlNode(Node: TDOMNode); 215 var 216 LookupTable: TLookupTable; 217 RuleNode: TDOMNode; 218 begin 219 for LookupTable in Self do begin 220 RuleNode := Node.OwnerDocument.CreateElement('LookupTable'); 221 LookupTable.SaveToXmlNode(RuleNode); 222 Node.AppendChild(RuleNode); 223 end; 224 end; 225 226 { TLookupTable } 227 228 procedure TLookupTable.LoadFromXmlNode(Node: TDOMNode); 229 begin 230 Name := ReadString(Node, 'Name', ''); 231 end; 232 233 procedure TLookupTable.SaveToXmlNode(Node: TDOMNode); 234 begin 235 WriteString(Node, 'Name', Name); 236 end; 237 238 constructor TLookupTable.Create; 239 begin 240 Items := TFPGObjectList<TLookupTableItem>.Create; 241 end; 242 243 destructor TLookupTable.Destroy; 244 begin 245 FreeAndNil(Items); 246 inherited Destroy; 247 end; 248 153 249 { TGrammer } 154 250 … … 188 284 Rules.LoadFromXmlNode(RulesNode); 189 285 end; 286 RulesNode := RootNode.FindNode('LookupTables'); 287 if Assigned(RulesNode) then begin 288 LookupTables.LoadFromXmlNode(RulesNode); 289 end; 190 290 191 291 TopRule := Rules.FindName(ReadString(RootNode, 'TopRule', '')); … … 217 317 Rules.SaveToXmlNode(RulesNode); 218 318 319 RulesNode := Doc.CreateElement('LookupTables'); 320 RootNode.AppendChild(RulesNode); 321 LookupTables.SaveToXmlNode(RulesNode); 322 219 323 if Assigned(TopRule) then 220 324 WriteString(RootNode, 'TopRule', TopRule.Name); … … 239 343 Rules := TRules.Create; 240 344 Rules.Grammer := Self; 345 LookupTables := TLookupTables.Create; 346 LookupTables.Grammer := Self; 241 347 end; 242 348 … … 244 350 begin 245 351 DoDestroy; 352 FreeAndNil(LookupTables); 246 353 FreeAndNil(Rules); 247 354 inherited Destroy; … … 297 404 end; 298 405 end; 406 WriteBoolean(Node, 'LookupTableUsed', LookupTableUsed); 407 WriteString(Node, 'LookupTable', LookupTableName); 408 WriteInteger(Node, 'LookupTableAction', Integer(LookupTableAction)); 299 409 end; 300 410 … … 407 517 end; 408 518 end; 519 LookupTableUsed := ReadBoolean(Node, 'LookupTableUsed', False); 520 LookupTableName := ReadString(Node, 'LookupTable', ''); 521 LookupTableAction := TLookupTableAction(ReadInteger(Node, 'LookupTableAction', 0)); 409 522 end; 410 523 … … 430 543 RuleItem: TRuleItem; 431 544 begin 432 for RuleItem in Self do 545 for RuleItem in Self do begin 433 546 case RuleItem.RuleItemType of 434 547 ritNonTerminal: RuleItem.NonTerminal := Grammer.Rules.FindName(RuleItem.NonTerminalName); 435 548 ritSubItems: RuleItem.SubItems.UpdateRuleReference; 436 549 end; 550 RuleItem.LookupTable := Grammer.LookupTables.FindName(RuleItem.LookupTableName); 551 end; 437 552 end; 438 553
Note:
See TracChangeset
for help on using the changeset viewer.