Ignore:
Timestamp:
Aug 8, 2017, 11:15:41 PM (7 years ago)
Author:
chronos
Message:
  • Added: Rule items can be managed.
  • Added: Show BNF syntax on main form.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/generator/URules.pas

    r109 r110  
    1010type
    1111
     12  { TRuleItem }
     13
     14  TRuleItem = class
     15    Name: string;
     16    procedure LoadFromXmlNode(Node: TDOMNode);
     17    procedure SaveToXmlNode(Node: TDOMNode);
     18  end;
     19
     20  { TRuleItems }
     21
     22  TRuleItems = class(TFPGObjectList<TRuleItem>)
     23    procedure LoadFromXmlNode(Node: TDOMNode);
     24    procedure SaveToXmlNode(Node: TDOMNode);
     25  end;
     26
    1227  { TRule }
    1328
    1429  TRule = class
    1530    Name: string;
    16     procedure SaveToXmlNode(Node: TDOMNode);
    17     procedure LoadFromXmlNode(Node: TDOMNode);
     31    Items: TRuleItems;
     32    constructor Create;
     33    destructor Destroy; override;
     34    procedure LoadFromXmlNode(Node: TDOMNode);
     35    procedure SaveToXmlNode(Node: TDOMNode);
     36    function GetBNF: string;
    1837  end;
    1938
     
    2140
    2241  TRules = class(TFPGObjectList<TRule>)
     42    FileName: string;
     43    procedure LoadFromXmlNode(Node: TDOMNode);
     44    procedure SaveToXmlNode(Node: TDOMNode);
    2345    procedure LoadFromXmlFile(FileName: string);
    2446    procedure SaveToXmlFile(FileName: string);
     47    function GetBNF: string;
    2548  end;
    2649
    2750implementation
    2851
     52{ TRuleItem }
     53
     54procedure TRuleItem.SaveToXmlNode(Node: TDOMNode);
     55begin
     56  WriteString(Node, 'Name', Name);
     57end;
     58
     59procedure TRuleItem.LoadFromXmlNode(Node: TDOMNode);
     60begin
     61  Name := ReadString(Node, 'Name', '');
     62end;
     63
     64{ TRuleItems }
     65
     66procedure TRuleItems.SaveToXmlNode(Node: TDOMNode);
     67var
     68  RuleItem: TRuleItem;
     69  RuleItemNode: TDOMNode;
     70begin
     71  for RuleItem in Self do begin
     72    RuleItemNode := Node.OwnerDocument.CreateElement('RuleItem');
     73    RuleItem.SaveToXmlNode(RuleItemNode);
     74    Node.AppendChild(RuleItemNode);
     75  end;
     76end;
     77
     78procedure TRuleItems.LoadFromXmlNode(Node: TDOMNode);
     79var
     80  RuleItemNode: TDOMNode;
     81  RuleItem: TRuleItem;
     82begin
     83  RuleItemNode := Node.FirstChild;
     84  while Assigned(RuleItemNode) do begin
     85    if RuleItemNode.NodeName = 'RuleItem' then begin
     86      RuleItem := TRuleItem.Create;
     87      RuleItem.LoadFromXmlNode(RuleItemNode);
     88      Add(RuleItem);
     89    end;
     90    RuleItemNode := RuleItemNode.NextSibling;
     91  end;
     92end;
     93
    2994{ TRule }
    3095
    3196procedure TRule.SaveToXmlNode(Node: TDOMNode);
     97var
     98  RuleItemsNode: TDOMNode;
    3299begin
    33100  WriteString(Node, 'Name', Name);
     101
     102  RuleItemsNode := Node.OwnerDocument.CreateElement('RuleItems');
     103  Node.AppendChild(RuleItemsNode);
     104  Items.SaveToXmlNode(RuleItemsNode);
     105end;
     106
     107function TRule.GetBNF: string;
     108var
     109  Item: TRuleItem;
     110begin
     111  Result := Name + ' ::=';
     112  for Item in Items do begin
     113    Result := Result + ' ' + Item.Name;
     114  end;
     115end;
     116
     117constructor TRule.Create;
     118begin
     119  Items := TRuleItems.Create;
     120end;
     121
     122destructor TRule.Destroy;
     123begin
     124  FreeAndNil(Items);
     125  inherited Destroy;
    34126end;
    35127
    36128procedure TRule.LoadFromXmlNode(Node: TDOMNode);
     129var
     130  ItemsNode: TDOMNode;
    37131begin
    38132  Name := ReadString(Node, 'Name', '');
     133
     134  ItemsNode := Node.FindNode('RuleItems');
     135  if Assigned(ItemsNode) then begin
     136    Items.LoadFromXmlNode(ItemsNode);
     137  end;
    39138end;
    40139
    41140{ TRules }
     141
     142procedure TRules.SaveToXmlNode(Node: TDOMNode);
     143var
     144  Rule: TRule;
     145  RuleNode: TDOMNode;
     146begin
     147  for Rule in Self do begin
     148    RuleNode := Node.OwnerDocument.CreateElement('Rule');
     149    Rule.SaveToXmlNode(RuleNode);
     150    Node.AppendChild(RuleNode);
     151  end;
     152end;
     153
     154procedure TRules.LoadFromXmlNode(Node: TDOMNode);
     155var
     156  RuleNode: TDOMNode;
     157  Rule: TRule;
     158begin
     159  RuleNode := Node.FirstChild;
     160  while Assigned(RuleNode) do begin
     161    if RuleNode.NodeName = 'Rule' then begin
     162      Rule := TRule.Create;
     163      Rule.LoadFromXmlNode(RuleNode);
     164      Add(Rule);
     165    end;
     166    RuleNode := RuleNode.NextSibling;
     167  end;
     168end;
    42169
    43170procedure TRules.LoadFromXmlFile(FileName: string);
     
    46173  RootNode: TDOMNode;
    47174  RulesNode: TDOMNode;
    48   RuleNode: TDOMNode;
    49   Rule: TRule;
    50 begin
     175begin
     176  Self.FileName := FileName;
    51177  Clear;
    52178  try
     
    57183      RulesNode := RootNode.FindNode('Rules');
    58184      if Assigned(RulesNode) then begin
    59         RuleNode := RulesNode.FirstChild;
    60         while Assigned(RuleNode) do begin
    61           if RuleNode.NodeName = 'Rule' then begin
    62             Rule := TRule.Create;
    63             Rule.LoadFromXmlNode(RuleNode);
    64             Add(Rule);
    65           end;
    66           RuleNode := RuleNode.NextSibling;
    67         end;
     185        LoadFromXmlNode(RulesNode);
    68186      end;
    69187    end;
     
    78196  RootNode: TDOMNode;
    79197  RulesNode: TDOMNode;
    80   RuleNode: TDOMNode;
    81   Rule: TRule;
    82 begin
     198begin
     199  Self.FileName := FileName;
    83200  Doc := TXMLDocument.Create;
    84201  try
     
    88205    RulesNode := Doc.CreateElement('Rules');
    89206    RootNode.AppendChild(RulesNode);
    90 
    91     for Rule in Self do begin
    92       RuleNode := Doc.CreateElement('Rule');
    93       Rule.SaveToXmlNode(RuleNode);
    94       RulesNode.AppendChild(RuleNode);
    95     end;
     207    SaveToXmlNode(RulesNode);
    96208
    97209    WriteXMLFile(Doc, FileName);
     
    101213end;
    102214
     215function TRules.GetBNF: string;
     216var
     217  Rule: TRule;
     218begin
     219  Result := '';
     220  for Rule in Self do begin
     221    Result := Result + Rule.GetBNF + LineEnding;
     222  end;
     223end;
     224
    103225end.
    104226
Note: See TracChangeset for help on using the changeset viewer.