Changeset 111 for branches


Ignore:
Timestamp:
Aug 9, 2017, 12:09:47 AM (7 years ago)
Author:
chronos
Message:
  • Added: RuleItem can be selected as terminal or non-terminal node.
Location:
branches/generator
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/generator/Forms/UFormMain.pas

    r110 r111  
    5656procedure TFormMain.ARulesExecute(Sender: TObject);
    5757begin
    58   FormRules.Rules := Core.Rules;
     58  FormRules.Rules := Core.Grammer.Rules;
    5959  FormRules.ShowModal;
    60   MemoBNF.Lines.Text := Core.Rules.GetBNF;
     60  MemoBNF.Lines.Text := Core.Grammer.GetBNF;
    6161end;
    6262
    6363procedure TFormMain.ASaveAsExecute(Sender: TObject);
    6464begin
    65   SaveDialog1.FileName := Core.Rules.FileName;
     65  SaveDialog1.FileName := Core.Grammer.FileName;
    6666  if SaveDialog1.Execute then begin
    67     Core.Rules.SaveToXmlFile(SaveDialog1.FileName);
     67    Core.Grammer.SaveToXmlFile(SaveDialog1.FileName);
    6868  end;
    6969end;
     
    7171procedure TFormMain.FormShow(Sender: TObject);
    7272begin
    73   MemoBNF.Lines.Text := Core.Rules.GetBNF;
     73  MemoBNF.Lines.Text := Core.Grammer.GetBNF;
    7474end;
    7575
     
    8181procedure TFormMain.AOpenExecute(Sender: TObject);
    8282begin
    83   OpenDialog1.FileName := Core.Rules.FileName;
     83  OpenDialog1.FileName := Core.Grammer.FileName;
    8484  if OpenDialog1.Execute then begin
    85     Core.Rules.LoadFromXmlFile(OpenDialog1.FileName);
     85    Core.Grammer.LoadFromXmlFile(OpenDialog1.FileName);
    8686  end;
    8787end;
  • branches/generator/Forms/UFormRule.lfm

    r110 r111  
    4747  object ListView: TListView
    4848    Left = 16
    49     Height = 264
    50     Top = 80
     49    Height = 232
     50    Top = 112
    5151    Width = 624
    5252    Anchors = [akTop, akLeft, akBottom]
     
    6767    OnKeyPress = ListViewKeyPress
    6868    OnSelectItem = ListViewSelectItem
     69  end
     70  object Label2: TLabel
     71    Left = 24
     72    Height = 24
     73    Top = 64
     74    Width = 82
     75    Caption = 'Operator:'
     76    ParentColor = False
     77  end
     78  object ComboBox1: TComboBox
     79    Left = 186
     80    Height = 36
     81    Top = 61
     82    Width = 390
     83    ItemHeight = 0
     84    Items.Strings = (
     85      'And'
     86      'Or'
     87    )
     88    Style = csDropDownList
     89    TabOrder = 4
    6990  end
    7091  object ActionList1: TActionList
  • branches/generator/Forms/UFormRule.pas

    r110 r111  
    2020    ButtonOk: TButton;
    2121    ButtonCancel: TButton;
     22    ComboBox1: TComboBox;
    2223    EditName: TEdit;
    2324    Label1: TLabel;
     25    Label2: TLabel;
    2426    ListView: TListView;
    2527    MenuItem1: TMenuItem;
     
    3941    { private declarations }
    4042  public
    41     RuleItems: TRuleItems;
     43    Rule: TRule;
    4244    procedure UpdateInterface;
    4345    procedure ReloadList;
     
    6870procedure TFormRule.ListViewData(Sender: TObject; Item: TListItem);
    6971begin
    70   if (Item.Index >= 0) and (Item.Index < RuleItems.Count) then begin
    71     Item.Caption := RuleItems[Item.Index].Name;
    72     Item.Data := RuleItems[Item.Index];
     72  if (Item.Index >= 0) and (Item.Index < Rule.Items.Count) then begin
     73    Item.Caption := Rule.Items[Item.Index].GetBNF;
     74    Item.Data := Rule.Items[Item.Index];
    7375  end;
    7476end;
     
    9597begin
    9698  NewRuleItem := TRuleItem.Create;
     99  NewRuleItem.Grammer := Rule.Grammer;
    97100  FormRuleItem.LoadData(NewRuleItem);
    98101  if FormRuleItem.ShowModal = mrOk then begin
    99102    FormRuleItem.SaveData(NewRuleItem);
    100     RuleItems.Add(NewRuleItem);
     103    NewRuleItem.ParentRule := Rule;
     104    Rule.Items.Add(NewRuleItem);
    101105    ReloadList;
    102106  end else FreeAndNil(NewRuleItem);
     
    121125  for Item in ListView.Items do
    122126  if Item.Selected then
    123     RuleItems.Remove(TRuleItem(Item.Data));
     127    Rule.Items.Remove(TRuleItem(Item.Data));
    124128  ReloadList;
    125129end;
     
    133137procedure TFormRule.ReloadList;
    134138begin
    135   ListView.Items.Count := RuleItems.Count;
     139  ListView.Items.Count := Rule.Items.Count;
    136140  ListView.Refresh;
    137141end;
     
    139143procedure TFormRule.LoadData(Rule: TRule);
    140144begin
     145  Self.Rule := Rule;
     146  ComboBox1.ItemIndex := Integer(Rule.RuleType);
    141147  EditName.Text := Rule.Name;
    142   RuleItems := Rule.Items;
     148  Rule.Items := Rule.Items;
    143149end;
    144150
    145151procedure TFormRule.SaveData(Rule: TRule);
    146152begin
     153  Rule.RuleType := TRuleType(ComboBox1.ItemIndex);
    147154  Rule.Name := EditName.Text;
    148155end;
  • branches/generator/Forms/UFormRuleItem.lfm

    r110 r111  
    99  ClientWidth = 656
    1010  LCLVersion = '1.6.4.0'
    11   object Label1: TLabel
    12     Left = 24
     11  object LabelTerminal: TLabel
     12    Left = 16
    1313    Height = 24
    14     Top = 24
    15     Width = 56
    16     Caption = 'Name:'
     14    Top = 64
     15    Width = 79
     16    Caption = 'Terminal:'
    1717    ParentColor = False
    1818  end
    1919  object EditName: TEdit
    20     Left = 132
     20    Left = 160
    2121    Height = 34
    22     Top = 19
    23     Width = 428
     22    Top = 64
     23    Width = 400
    2424    TabOrder = 0
    2525  end
     
    4242    TabOrder = 2
    4343  end
     44  object ComboBoxType: TComboBox
     45    Left = 160
     46    Height = 34
     47    Top = 16
     48    Width = 400
     49    ItemHeight = 0
     50    Items.Strings = (
     51      'Terminal'
     52      'Non-terminal'
     53    )
     54    OnChange = ComboBoxTypeChange
     55    Style = csDropDownList
     56    TabOrder = 3
     57  end
     58  object Label2: TLabel
     59    Left = 16
     60    Height = 24
     61    Top = 24
     62    Width = 45
     63    Caption = 'Type:'
     64    ParentColor = False
     65  end
     66  object LabelNonTerminal: TLabel
     67    Left = 16
     68    Height = 24
     69    Top = 112
     70    Width = 119
     71    Caption = 'Non-terminal:'
     72    ParentColor = False
     73  end
     74  object ComboBoxNonTerminal: TComboBox
     75    Left = 160
     76    Height = 36
     77    Top = 104
     78    Width = 404
     79    ItemHeight = 0
     80    Style = csDropDownList
     81    TabOrder = 4
     82  end
    4483end
  • branches/generator/Forms/UFormRuleItem.pas

    r110 r111  
    1616    ButtonCancel: TButton;
    1717    ButtonOk: TButton;
     18    ComboBoxType: TComboBox;
     19    ComboBoxNonTerminal: TComboBox;
    1820    EditName: TEdit;
    19     Label1: TLabel;
     21    LabelTerminal: TLabel;
     22    Label2: TLabel;
     23    LabelNonTerminal: TLabel;
     24    procedure ComboBoxTypeChange(Sender: TObject);
    2025  private
    21     { private declarations }
     26    procedure ReloadNonTerminals(Grammer: TGrammer);
    2227  public
     28    RuleItem: TRuleItem;
     29    procedure UpdateInterface;
    2330    procedure LoadData(RuleItem: TRuleItem);
    2431    procedure SaveData(RuleItem: TRuleItem);
     
    3441{ TFormRuleItem }
    3542
     43procedure TFormRuleItem.ComboBoxTypeChange(Sender: TObject);
     44begin
     45  RuleItem.RuleItemType := TRuleItemType(ComboBoxType.ItemIndex);
     46  UpdateInterface;
     47end;
     48
     49procedure TFormRuleItem.ReloadNonTerminals(Grammer: TGrammer);
     50var
     51  Rule: TRule;
     52begin
     53  ComboBoxNonTerminal.Items.Clear;
     54  for Rule in Grammer.Rules do begin
     55    ComboBoxNonTerminal.Items.AddObject(Rule.Name, Rule);
     56  end;
     57end;
     58
     59procedure TFormRuleItem.UpdateInterface;
     60begin
     61  EditName.Visible := RuleItem.RuleItemType = ritTerminal;
     62  LabelTerminal.Visible := RuleItem.RuleItemType = ritTerminal;
     63  ComboBoxNonTerminal.Visible := RuleItem.RuleItemType = ritNonTerminal;
     64  LabelNonTerminal.Visible := RuleItem.RuleItemType = ritNonTerminal;
     65end;
     66
    3667procedure TFormRuleItem.LoadData(RuleItem: TRuleItem);
    3768begin
    38   EditName.Text := RuleItem.Name;
     69  Self.RuleItem := RuleItem;
     70  ReloadNonTerminals(RuleItem.Grammer);
     71  ComboBoxType.ItemIndex := Integer(RuleItem.RuleItemType);
     72  ComboBoxNonTerminal.ItemIndex := ComboBoxNonTerminal.Items.IndexOfObject(RuleItem.NonTerminal);
     73  EditName.Text := RuleItem.Terminal;
     74  UpdateInterface;
    3975end;
    4076
    4177procedure TFormRuleItem.SaveData(RuleItem: TRuleItem);
    4278begin
    43   RuleItem.Name := EditName.Text;
     79  RuleItem.RuleItemType := TRuleItemType(ComboBoxType.ItemIndex);
     80  if RuleItem.RuleItemType = ritTerminal then RuleItem.Terminal := EditName.Text
     81  else if RuleItem.RuleItemType = ritNonTerminal then
     82    RuleItem.NonTerminal := TRule(ComboBoxNonTerminal.Items.Objects[ComboBoxNonTerminal.ItemIndex]);
    4483end;
    4584
  • branches/generator/Forms/UFormRules.lfm

    r109 r111  
    2727      Height = 24
    2828      Top = 5
    29       Width = 70
     29      Width = 150
    3030      Align = alNone
    3131      AutoSize = True
  • branches/generator/Forms/UFormRules.pas

    r109 r111  
    6969begin
    7070  NewRule := TRule.Create;
     71  NewRule.Grammer := Rules.Grammer;
    7172  FormRule.LoadData(NewRule);
    7273  if FormRule.ShowModal = mrOk then begin
  • branches/generator/UCore.pas

    r109 r111  
    1919    { private declarations }
    2020  public
    21     Rules: TRules;
     21    Grammer: TGrammer;
    2222  end;
    2323
     
    3333procedure TCore.DataModuleCreate(Sender: TObject);
    3434begin
    35   Rules := TRules.Create;
    36   Rules.LoadFromXmlFile('pascal.grm');
     35  Grammer := TGrammer.Create;
     36  Grammer.LoadFromXmlFile('pascal.grm');
    3737end;
    3838
    3939procedure TCore.DataModuleDestroy(Sender: TObject);
    4040begin
    41   Rules.SaveToXmlFile('pascal.grm');
    42   FreeAndNil(Rules);
     41  Grammer.SaveToXmlFile('pascal.grm');
     42  FreeAndNil(Grammer);
    4343end;
    4444
  • branches/generator/URules.pas

    r110 r111  
    99
    1010type
     11  TRule = class;
     12  TRules = class;
     13  TGrammer = class;
     14
     15  TRuleItemType = (ritTerminal, ritNonTerminal);
    1116
    1217  { TRuleItem }
    1318
    1419  TRuleItem = class
    15     Name: string;
     20    ParentRule: TRule;
     21    RuleItemType: TRuleItemType;
     22    Terminal: string;
     23    NonTerminalName: string;
     24    NonTerminal: TRule;
     25    Grammer: TGrammer;
    1626    procedure LoadFromXmlNode(Node: TDOMNode);
    1727    procedure SaveToXmlNode(Node: TDOMNode);
     28    function GetBNF: string;
    1829  end;
    1930
     
    2132
    2233  TRuleItems = class(TFPGObjectList<TRuleItem>)
     34    Grammer: TGrammer;
    2335    procedure LoadFromXmlNode(Node: TDOMNode);
    2436    procedure SaveToXmlNode(Node: TDOMNode);
    2537  end;
    2638
     39  TRuleType = (rtAnd, rtOr);
     40
    2741  { TRule }
    2842
    2943  TRule = class
     44  private
     45    function GetGrammer: TGrammer;
     46    procedure SetGrammer(AValue: TGrammer);
     47  public
    3048    Name: string;
    3149    Items: TRuleItems;
     50    RuleType: TRuleType;
     51    property Grammer: TGrammer read GetGrammer write SetGrammer;
    3252    constructor Create;
    3353    destructor Destroy; override;
     
    4060
    4161  TRules = class(TFPGObjectList<TRule>)
    42     FileName: string;
     62    Grammer: TGrammer;
     63    function FindName(Name: string): TRule;
    4364    procedure LoadFromXmlNode(Node: TDOMNode);
    4465    procedure SaveToXmlNode(Node: TDOMNode);
     66    function GetBNF: string;
     67  end;
     68
     69  { TGrammer }
     70
     71  TGrammer = class
     72    FileName: string;
     73    Rules: TRules;
    4574    procedure LoadFromXmlFile(FileName: string);
    4675    procedure SaveToXmlFile(FileName: string);
     76    constructor Create;
     77    destructor Destroy; override;
    4778    function GetBNF: string;
    4879  end;
     
    5081implementation
    5182
    52 { TRuleItem }
    53 
    54 procedure TRuleItem.SaveToXmlNode(Node: TDOMNode);
    55 begin
    56   WriteString(Node, 'Name', Name);
    57 end;
    58 
    59 procedure TRuleItem.LoadFromXmlNode(Node: TDOMNode);
    60 begin
    61   Name := ReadString(Node, 'Name', '');
    62 end;
    63 
    64 { TRuleItems }
    65 
    66 procedure TRuleItems.SaveToXmlNode(Node: TDOMNode);
    67 var
    68   RuleItem: TRuleItem;
    69   RuleItemNode: TDOMNode;
    70 begin
    71   for RuleItem in Self do begin
    72     RuleItemNode := Node.OwnerDocument.CreateElement('RuleItem');
    73     RuleItem.SaveToXmlNode(RuleItemNode);
    74     Node.AppendChild(RuleItemNode);
    75   end;
    76 end;
    77 
    78 procedure TRuleItems.LoadFromXmlNode(Node: TDOMNode);
    79 var
    80   RuleItemNode: TDOMNode;
    81   RuleItem: TRuleItem;
    82 begin
    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;
    92 end;
    93 
    94 { TRule }
    95 
    96 procedure TRule.SaveToXmlNode(Node: TDOMNode);
    97 var
    98   RuleItemsNode: TDOMNode;
    99 begin
    100   WriteString(Node, 'Name', Name);
    101 
    102   RuleItemsNode := Node.OwnerDocument.CreateElement('RuleItems');
    103   Node.AppendChild(RuleItemsNode);
    104   Items.SaveToXmlNode(RuleItemsNode);
    105 end;
    106 
    107 function TRule.GetBNF: string;
    108 var
    109   Item: TRuleItem;
    110 begin
    111   Result := Name + ' ::=';
    112   for Item in Items do begin
    113     Result := Result + ' ' + Item.Name;
    114   end;
    115 end;
    116 
    117 constructor TRule.Create;
    118 begin
    119   Items := TRuleItems.Create;
    120 end;
    121 
    122 destructor TRule.Destroy;
    123 begin
    124   FreeAndNil(Items);
    125   inherited Destroy;
    126 end;
    127 
    128 procedure TRule.LoadFromXmlNode(Node: TDOMNode);
    129 var
    130   ItemsNode: TDOMNode;
    131 begin
    132   Name := ReadString(Node, 'Name', '');
    133 
    134   ItemsNode := Node.FindNode('RuleItems');
    135   if Assigned(ItemsNode) then begin
    136     Items.LoadFromXmlNode(ItemsNode);
    137   end;
    138 end;
    139 
    140 { TRules }
    141 
    142 procedure TRules.SaveToXmlNode(Node: TDOMNode);
    143 var
    144   Rule: TRule;
    145   RuleNode: TDOMNode;
    146 begin
    147   for Rule in Self do begin
    148     RuleNode := Node.OwnerDocument.CreateElement('Rule');
    149     Rule.SaveToXmlNode(RuleNode);
    150     Node.AppendChild(RuleNode);
    151   end;
    152 end;
    153 
    154 procedure TRules.LoadFromXmlNode(Node: TDOMNode);
    155 var
    156   RuleNode: TDOMNode;
    157   Rule: TRule;
    158 begin
    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;
    168 end;
    169 
    170 procedure TRules.LoadFromXmlFile(FileName: string);
     83{ TGrammer }
     84
     85procedure TGrammer.LoadFromXmlFile(FileName: string);
    17186var
    17287  Doc: TXMLDocument;
    17388  RootNode: TDOMNode;
    17489  RulesNode: TDOMNode;
     90  Rule: TRule;
     91  RuleItem: TRuleItem;
    17592begin
    17693  Self.FileName := FileName;
    177   Clear;
     94  Rules.Clear;
    17895  try
    17996    ReadXMLFile(Doc, FileName);
     
    183100      RulesNode := RootNode.FindNode('Rules');
    184101      if Assigned(RulesNode) then begin
    185         LoadFromXmlNode(RulesNode);
     102        Rules.LoadFromXmlNode(RulesNode);
    186103      end;
    187104    end;
     
    189106    Doc.Free;
    190107  end;
    191 end;
    192 
    193 procedure TRules.SaveToXmlFile(FileName: string);
     108
     109  // Update rule references
     110  for Rule in Rules do
     111    for RuleItem in Rule.Items do
     112      if RuleItem.RuleItemType = ritNonTerminal then
     113        RuleItem.NonTerminal := Rules.FindName(RuleItem.NonTerminalName);
     114end;
     115
     116procedure TGrammer.SaveToXmlFile(FileName: string);
    194117var
    195118  Doc: TXMLDocument;
     
    205128    RulesNode := Doc.CreateElement('Rules');
    206129    RootNode.AppendChild(RulesNode);
    207     SaveToXmlNode(RulesNode);
     130    Rules.SaveToXmlNode(RulesNode);
    208131
    209132    WriteXMLFile(Doc, FileName);
     
    213136end;
    214137
     138constructor TGrammer.Create;
     139begin
     140  Rules := TRules.Create;
     141  Rules.Grammer := Self;
     142end;
     143
     144destructor TGrammer.Destroy;
     145begin
     146  FreeAndNil(Rules);
     147  inherited Destroy;
     148end;
     149
     150function TGrammer.GetBNF: string;
     151begin
     152  Result := Rules.GetBNF;
     153end;
     154
     155{ TRuleItem }
     156
     157procedure TRuleItem.SaveToXmlNode(Node: TDOMNode);
     158begin
     159  WriteInteger(Node, 'Type', Integer(RuleItemType));
     160  if RuleItemType = ritTerminal then WriteString(Node, 'Terminal', Terminal)
     161  else if RuleItemType = ritNonTerminal then WriteString(Node, 'NonTerminal', NonTerminal.Name)
     162end;
     163
     164function TRuleItem.GetBNF: string;
     165begin
     166  if RuleItemType = ritTerminal then Result := '"' + Terminal + '"'
     167    else if RuleItemType = ritNonTerminal then Result := NonTerminal.Name;
     168end;
     169
     170procedure TRuleItem.LoadFromXmlNode(Node: TDOMNode);
     171begin
     172  RuleItemType := TRuleItemType(ReadInteger(Node, 'Type', Integer(ritTerminal)));
     173  if RuleItemType = ritTerminal then Terminal := ReadString(Node, 'Terminal', '')
     174  else if RuleItemType = ritNonTerminal then
     175    NonTerminalName := ReadString(Node, 'NonTerminal', '');
     176end;
     177
     178{ TRuleItems }
     179
     180procedure TRuleItems.SaveToXmlNode(Node: TDOMNode);
     181var
     182  RuleItem: TRuleItem;
     183  RuleItemNode: TDOMNode;
     184begin
     185  for RuleItem in Self do begin
     186    RuleItemNode := Node.OwnerDocument.CreateElement('RuleItem');
     187    RuleItem.SaveToXmlNode(RuleItemNode);
     188    Node.AppendChild(RuleItemNode);
     189  end;
     190end;
     191
     192procedure TRuleItems.LoadFromXmlNode(Node: TDOMNode);
     193var
     194  RuleItemNode: TDOMNode;
     195  RuleItem: TRuleItem;
     196begin
     197  RuleItemNode := Node.FirstChild;
     198  while Assigned(RuleItemNode) do begin
     199    if RuleItemNode.NodeName = 'RuleItem' then begin
     200      RuleItem := TRuleItem.Create;
     201      RuleItem.Grammer := Grammer;
     202      RuleItem.LoadFromXmlNode(RuleItemNode);
     203      Add(RuleItem);
     204    end;
     205    RuleItemNode := RuleItemNode.NextSibling;
     206  end;
     207end;
     208
     209{ TRule }
     210
     211procedure TRule.SaveToXmlNode(Node: TDOMNode);
     212var
     213  RuleItemsNode: TDOMNode;
     214begin
     215  WriteString(Node, 'Name', Name);
     216
     217  RuleItemsNode := Node.OwnerDocument.CreateElement('RuleItems');
     218  Node.AppendChild(RuleItemsNode);
     219  Items.SaveToXmlNode(RuleItemsNode);
     220end;
     221
     222function TRule.GetBNF: string;
     223var
     224  Item: TRuleItem;
     225begin
     226  Result := Name + ' ::= ';
     227  for Item in Items do begin
     228    if Item <> Items.First then begin
     229      if RuleType = rtAnd then Result := Result + ' '
     230      else if RuleType = rtOr then Result := Result + ' | ';
     231    end;
     232    Result := Result + Item.GetBNF;
     233  end;
     234end;
     235
     236function TRule.GetGrammer: TGrammer;
     237begin
     238  Result := Items.Grammer;
     239end;
     240
     241procedure TRule.SetGrammer(AValue: TGrammer);
     242begin
     243  Items.Grammer := AValue;
     244end;
     245
     246constructor TRule.Create;
     247begin
     248  Items := TRuleItems.Create;
     249end;
     250
     251destructor TRule.Destroy;
     252begin
     253  FreeAndNil(Items);
     254  inherited Destroy;
     255end;
     256
     257procedure TRule.LoadFromXmlNode(Node: TDOMNode);
     258var
     259  ItemsNode: TDOMNode;
     260begin
     261  Name := ReadString(Node, 'Name', '');
     262
     263  ItemsNode := Node.FindNode('RuleItems');
     264  if Assigned(ItemsNode) then begin
     265    Items.LoadFromXmlNode(ItemsNode);
     266  end;
     267end;
     268
     269{ TRules }
     270
     271procedure TRules.SaveToXmlNode(Node: TDOMNode);
     272var
     273  Rule: TRule;
     274  RuleNode: TDOMNode;
     275begin
     276  for Rule in Self do begin
     277    RuleNode := Node.OwnerDocument.CreateElement('Rule');
     278    Rule.SaveToXmlNode(RuleNode);
     279    Node.AppendChild(RuleNode);
     280  end;
     281end;
     282
     283function TRules.FindName(Name: string): TRule;
     284var
     285  I: Integer;
     286begin
     287  I := 0;
     288  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
     289  if I < Count then Result := Items[I]
     290    else Result := nil;
     291end;
     292
     293procedure TRules.LoadFromXmlNode(Node: TDOMNode);
     294var
     295  RuleNode: TDOMNode;
     296  Rule: TRule;
     297begin
     298  RuleNode := Node.FirstChild;
     299  while Assigned(RuleNode) do begin
     300    if RuleNode.NodeName = 'Rule' then begin
     301      Rule := TRule.Create;
     302      Rule.Grammer := Grammer;
     303      Rule.LoadFromXmlNode(RuleNode);
     304      Add(Rule);
     305    end;
     306    RuleNode := RuleNode.NextSibling;
     307  end;
     308end;
     309
    215310function TRules.GetBNF: string;
    216311var
  • branches/generator/pascal.grm

    r110 r111  
    66      <RuleItems>
    77        <RuleItem>
    8           <Name>begin</Name>
     8          <Type>0</Type>
     9          <Terminal>begin</Terminal>
    910        </RuleItem>
    1011        <RuleItem>
    11           <Name>command</Name>
     12          <Type>1</Type>
     13          <NonTerminal>Command</NonTerminal>
    1214        </RuleItem>
    1315        <RuleItem>
    14           <Name>end</Name>
     16          <Type>0</Type>
     17          <Terminal>end</Terminal>
    1518        </RuleItem>
    1619      </RuleItems>
     
    1821    <Rule>
    1922      <Name>IfThenElse</Name>
    20       <RuleItems/>
     23      <RuleItems>
     24        <RuleItem>
     25          <Type>0</Type>
     26          <Terminal>if</Terminal>
     27        </RuleItem>
     28        <RuleItem>
     29          <Type>1</Type>
     30          <NonTerminal>Expression</NonTerminal>
     31        </RuleItem>
     32        <RuleItem>
     33          <Type>0</Type>
     34          <Terminal>then</Terminal>
     35        </RuleItem>
     36        <RuleItem>
     37          <Type>1</Type>
     38          <NonTerminal>Command</NonTerminal>
     39        </RuleItem>
     40        <RuleItem>
     41          <Type>0</Type>
     42          <Terminal>else</Terminal>
     43        </RuleItem>
     44        <RuleItem>
     45          <Type>1</Type>
     46          <NonTerminal>Command</NonTerminal>
     47        </RuleItem>
     48        <RuleItem>
     49          <Type>0</Type>
     50          <Terminal>end</Terminal>
     51        </RuleItem>
     52      </RuleItems>
    2153    </Rule>
    2254    <Rule>
    2355      <Name>WhileDo</Name>
    24       <RuleItems/>
     56      <RuleItems>
     57        <RuleItem>
     58          <Type>0</Type>
     59          <Terminal>while</Terminal>
     60        </RuleItem>
     61        <RuleItem>
     62          <Type>1</Type>
     63          <NonTerminal>Expression</NonTerminal>
     64        </RuleItem>
     65        <RuleItem>
     66          <Type>0</Type>
     67          <Terminal>do</Terminal>
     68        </RuleItem>
     69        <RuleItem>
     70          <Type>1</Type>
     71          <NonTerminal>Command</NonTerminal>
     72        </RuleItem>
     73      </RuleItems>
    2574    </Rule>
    2675    <Rule>
    2776      <Name>RepeatUntil</Name>
    28       <RuleItems/>
     77      <RuleItems>
     78        <RuleItem>
     79          <Type>0</Type>
     80          <Terminal>repeat</Terminal>
     81        </RuleItem>
     82        <RuleItem>
     83          <Type>1</Type>
     84          <NonTerminal>Command</NonTerminal>
     85        </RuleItem>
     86        <RuleItem>
     87          <Type>0</Type>
     88          <Terminal>until</Terminal>
     89        </RuleItem>
     90        <RuleItem>
     91          <Type>1</Type>
     92          <NonTerminal>Expression</NonTerminal>
     93        </RuleItem>
     94      </RuleItems>
    2995    </Rule>
    3096    <Rule>
     
    42108    <Rule>
    43109      <Name>Assignment</Name>
    44       <RuleItems/>
     110      <RuleItems>
     111        <RuleItem>
     112          <Type>1</Type>
     113          <NonTerminal>Variable</NonTerminal>
     114        </RuleItem>
     115        <RuleItem>
     116          <Type>0</Type>
     117          <Terminal>:=</Terminal>
     118        </RuleItem>
     119        <RuleItem>
     120          <Type>1</Type>
     121          <NonTerminal>Expression</NonTerminal>
     122        </RuleItem>
     123      </RuleItems>
    45124    </Rule>
    46125    <Rule>
     
    48127      <RuleItems/>
    49128    </Rule>
     129    <Rule>
     130      <Name>Expression</Name>
     131      <RuleItems/>
     132    </Rule>
     133    <Rule>
     134      <Name>Variable</Name>
     135      <RuleItems/>
     136    </Rule>
    50137  </Rules>
    51138</GrammerProject>
Note: See TracChangeset for help on using the changeset viewer.