Changeset 117 for branches/generator/URules.pas
- Timestamp:
- Nov 27, 2017, 1:09:38 PM (7 years ago)
- Location:
- branches/generator
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/generator
- Property svn:ignore
-
old new 2 2 generator.lps 3 3 lib 4 heaptrclog.trc
-
- Property svn:ignore
-
branches/generator/URules.pas
r116 r117 12 12 TRules = class; 13 13 TGrammer = class; 14 15 TRuleItemType = (ritTerminal, ritNonTerminal); 14 TRuleItems = class; 15 16 TRuleItemType = (ritTerminal, ritNonTerminal, ritSubItems); 16 17 17 18 { TRuleItem } 18 19 19 20 TRuleItem = class 20 ParentRule: TRule; 21 private 22 FGrammer: TGrammer; 23 FParentRule: TRule; 24 procedure SetGrammer(AValue: TGrammer); 25 procedure SetParentRule(AValue: TRule); 26 public 27 Optional: Boolean; 28 Repetitive: Boolean; 21 29 RuleItemType: TRuleItemType; 22 30 Terminal: string; 23 31 NonTerminalName: string; 24 32 NonTerminal: TRule; 25 Grammer: TGrammer;33 SubItems: TRuleItems; 26 34 procedure LoadFromXmlNode(Node: TDOMNode); 27 35 procedure SaveToXmlNode(Node: TDOMNode); 28 36 function GetBNF: string; 37 constructor Create; 38 destructor Destroy; override; 39 property Grammer: TGrammer read FGrammer write SetGrammer; 40 property ParentRule: TRule read FParentRule write SetParentRule; 29 41 end; 30 42 … … 34 46 35 47 TRuleItems = class(TFPGObjectList<TRuleItem>) 36 ParentRule: TRule; 48 private 49 FParentRule: TRule; 50 procedure SetParentRule(AValue: TRule); 51 public 37 52 Grammer: TGrammer; 38 53 RuleType: TRuleType; 39 54 procedure LoadFromXmlNode(Node: TDOMNode); 40 55 procedure SaveToXmlNode(Node: TDOMNode); 41 function GetBNF(RuleType: TRuleType): string; 56 procedure UpdateRuleReference; 57 function GetBNF: string; 58 property ParentRule: TRule read FParentRule write SetParentRule; 42 59 end; 43 60 … … 103 120 RulesNode: TDOMNode; 104 121 Rule: TRule; 105 RuleItem: TRuleItem;106 122 begin 107 123 Self.FileName := FileName; … … 123 139 // Update rule references 124 140 for Rule in Rules do 125 for RuleItem in Rule.Items do 126 if RuleItem.RuleItemType = ritNonTerminal then 127 RuleItem.NonTerminal := Rules.FindName(RuleItem.NonTerminalName); 141 Rule.Items.UpdateRuleReference; 128 142 end; 129 143 … … 170 184 171 185 procedure TRuleItem.SaveToXmlNode(Node: TDOMNode); 186 var 187 SubItemsNode: TDOMNode; 172 188 begin 173 189 WriteInteger(Node, 'Type', Integer(RuleItemType)); 174 if RuleItemType = ritTerminal then WriteString(Node, 'Terminal', Terminal) 175 else if RuleItemType = ritNonTerminal then WriteString(Node, 'NonTerminal', NonTerminal.Name) 190 WriteBoolean(Node, 'Optional', Optional); 191 WriteBoolean(Node, 'Repetitive', Repetitive); 192 193 case RuleItemType of 194 ritTerminal: WriteString(Node, 'Terminal', Terminal); 195 ritNonTerminal: WriteString(Node, 'NonTerminal', NonTerminal.Name); 196 ritSubItems: begin 197 SubItemsNode := Node.OwnerDocument.CreateElement('SubItems'); 198 Node.AppendChild(SubItemsNode); 199 SubItems.SaveToXmlNode(SubItemsNode); 200 end; 201 end; 176 202 end; 177 203 178 204 function TRuleItem.GetBNF: string; 179 205 begin 180 if RuleItemType = ritTerminal then Result := '"' + Terminal + '"' 181 else if RuleItemType = ritNonTerminal then Result := NonTerminal.Name; 206 case RuleItemType of 207 ritTerminal: Result := '"' + Terminal + '"'; 208 ritNonTerminal: Result := NonTerminal.Name; 209 ritSubItems: Result := '(' + SubItems.GetBNF + ')'; 210 end; 211 if Optional then Result := '+' + Result; 212 if Repetitive then Result := '*' + Result; 213 end; 214 215 constructor TRuleItem.Create; 216 begin 217 SubItems := TRuleItems.Create; 218 SubItems.Grammer := Grammer; 219 end; 220 221 destructor TRuleItem.Destroy; 222 begin 223 FreeAndNil(SubItems); 224 inherited Destroy; 225 end; 226 227 procedure TRuleItem.SetParentRule(AValue: TRule); 228 begin 229 if FParentRule = AValue then Exit; 230 FParentRule := AValue; 231 SubItems.ParentRule := AValue; 232 end; 233 234 procedure TRuleItem.SetGrammer(AValue: TGrammer); 235 begin 236 if FGrammer = AValue then Exit; 237 FGrammer := AValue; 238 SubItems.Grammer := AValue; 182 239 end; 183 240 184 241 procedure TRuleItem.LoadFromXmlNode(Node: TDOMNode); 242 var 243 SubItemsNode: TDOMNode; 185 244 begin 186 245 RuleItemType := TRuleItemType(ReadInteger(Node, 'Type', Integer(ritTerminal))); 187 if RuleItemType = ritTerminal then Terminal := ReadString(Node, 'Terminal', '') 188 else if RuleItemType = ritNonTerminal then 189 NonTerminalName := ReadString(Node, 'NonTerminal', ''); 246 Optional := ReadBoolean(Node, 'Optional', False); 247 Repetitive := ReadBoolean(Node, 'Repetitive', False); 248 case RuleItemType of 249 ritTerminal: Terminal := ReadString(Node, 'Terminal', ''); 250 ritNonTerminal: NonTerminalName := ReadString(Node, 'NonTerminal', ''); 251 ritSubItems: begin 252 SubItemsNode := Node.FindNode('SubItems'); 253 if Assigned(SubItemsNode) then SubItems.LoadFromXmlNode(SubItemsNode); 254 end; 255 end; 190 256 end; 191 257 … … 206 272 end; 207 273 208 function TRuleItems.GetBNF(RuleType: TRuleType): string; 274 procedure TRuleItems.UpdateRuleReference; 275 var 276 RuleItem: TRuleItem; 277 begin 278 for RuleItem in Self do 279 case RuleItem.RuleItemType of 280 ritNonTerminal: RuleItem.NonTerminal := Grammer.Rules.FindName(RuleItem.NonTerminalName); 281 ritSubItems: RuleItem.SubItems.UpdateRuleReference; 282 end; 283 end; 284 285 function TRuleItems.GetBNF: string; 209 286 var 210 287 Item: TRuleItem; … … 220 297 end; 221 298 299 procedure TRuleItems.SetParentRule(AValue: TRule); 300 begin 301 if FParentRule = AValue then Exit; 302 FParentRule := AValue; 303 end; 304 222 305 procedure TRuleItems.LoadFromXmlNode(Node: TDOMNode); 223 306 var … … 232 315 RuleItem := TRuleItem.Create; 233 316 RuleItem.Grammer := Grammer; 317 RuleItem.ParentRule := ParentRule; 234 318 RuleItem.LoadFromXmlNode(RuleItemNode); 235 319 Add(RuleItem); … … 254 338 function TRule.GetBNF: string; 255 339 begin 256 Result := Name + ' ::= ' + Items.GetBNF (Items.RuleType);340 Result := Name + ' ::= ' + Items.GetBNF; 257 341 end; 258 342
Note:
See TracChangeset
for help on using the changeset viewer.