1 { *************************************************************************** } 2 { } 3 { EControl Syntax Editor SDK } 4 { } 5 { Copyright (c) 2004 - 2015 EControl Ltd., Zaharov Michael } 6 { www.econtrol.ru } 7 { support@econtrol.ru } 8 { } 9 { Changes in Lazarus port: by Alexey Torgashin (CudaText) } 10 { } 11 { *************************************************************************** } 12 13 unit ec_syntax_rule; 14 15 {$mode Delphi}{$H+} 16 17 interface 18 19 uses 20 Classes, SysUtils, Contnrs; 21 22 type 23 TParserRuleItem = class; 24 TParserRule = class; 25 TParserRuleBranch = class; 26 27 TParserItemType = (itTerminal, // "aaa" 28 itTerminalNoCase, // 'aaa' 29 itTokenRule, // <aaa> 30 itParserRule); // aaa 31 32 TParserRuleItem = class 33 private 34 FItemType: TParserItemType; 35 FTerminal: string; 36 FTokenType: integer; 37 FParserRule: TParserRule; 38 FBranch: TParserRuleBranch; 39 FRepMin: integer; 40 FRepMax: integer; 41 FOwnRule: Boolean; 42 public 43 constructor Create; 44 destructor Destroy; override; IsValidnull45 function IsValid: Boolean; 46 47 property ItemType: TParserItemType read FItemType write FItemType; 48 property Terminal: string read FTerminal write FTerminal; 49 property TokenType: integer read FTokenType write FTokenType; 50 property ParserRule: TParserRule read FParserRule write FParserRule; 51 property Branch: TParserRuleBranch read FBranch write FBranch; 52 property RepMin: integer read FRepMin write FRepMin; 53 property RepMax: integer read FRepMax write FRepMax; 54 property IsSubRule: Boolean read FOwnRule write FOwnRule; 55 end; 56 57 TParserRuleBranch = class 58 private 59 FItems: TFPObjectList; 60 FRule: TParserRule; GetCountnull61 function GetCount: integer; GetItemsnull62 function GetItems(Index: integer): TParserRuleItem; 63 public 64 constructor Create; 65 destructor Destroy; override; IsValidnull66 function IsValid: Boolean; 67 68 property Count: integer read GetCount; 69 property ItemsList: TFPObjectList read FItems; 70 property Items[Index: integer]: TParserRuleItem read GetItems; 71 property Rule: TParserRule read FRule write FRule; 72 end; 73 74 TParserRule = class 75 private 76 FBranches: TFPObjectList; 77 FName: string; 78 FIndex: integer; GetCountnull79 function GetCount: integer; GetBranchesnull80 function GetBranches(Index: integer): TParserRuleBranch; 81 public 82 constructor Create; 83 destructor Destroy; override; IsValidnull84 function IsValid: Boolean; 85 86 property Count: integer read GetCount; 87 property BranchesList: TFPObjectList read FBranches; 88 property Branches[Index: integer]: TParserRuleBranch read GetBranches; 89 property Name: string read FName write FName; 90 property Index: integer read FIndex write FIndex; 91 end; 92 93 implementation 94 95 { TParserRuleItem } 96 97 constructor TParserRuleItem.Create; 98 begin 99 inherited Create; 100 FTokenType := -1; 101 FRepMin := 1; 102 FRepMax := 1; 103 FOwnRule := False; 104 end; 105 106 destructor TParserRuleItem.Destroy; 107 begin 108 if FOwnRule then 109 FParserRule.Free; 110 inherited; 111 end; 112 TParserRuleItem.IsValidnull113function TParserRuleItem.IsValid: Boolean; 114 begin 115 case FItemType of 116 itTerminal, 117 itTerminalNoCase: Result := FTerminal <> ''; 118 itTokenRule: Result := FTokenType <> -1; 119 itParserRule: Result := FParserRule <> nil; 120 else Result := False; 121 end; 122 end; 123 124 { TParserRuleBranch } 125 126 constructor TParserRuleBranch.Create; 127 begin 128 inherited Create; 129 FItems := TFPObjectList.Create; 130 end; 131 132 destructor TParserRuleBranch.Destroy; 133 begin 134 FItems.Free; 135 inherited; 136 end; 137 GetCountnull138function TParserRuleBranch.GetCount: integer; 139 begin 140 Result := FItems.Count; 141 end; 142 TParserRuleBranch.GetItemsnull143function TParserRuleBranch.GetItems(Index: integer): TParserRuleItem; 144 begin 145 Result := TParserRuleItem(FItems[Index]); 146 end; 147 TParserRuleBranch.IsValidnull148function TParserRuleBranch.IsValid: Boolean; 149 var i: integer; 150 begin 151 for i := 0 to FItems.Count - 1 do 152 if not Items[i].IsValid then 153 begin 154 Result := False; 155 Exit; 156 end; 157 Result := True; 158 end; 159 160 { TParserRule } 161 162 constructor TParserRule.Create; 163 begin 164 inherited Create; 165 FBranches := TFPObjectList.Create; 166 end; 167 168 destructor TParserRule.Destroy; 169 begin 170 FBranches.Free; 171 inherited; 172 end; 173 TParserRule.GetBranchesnull174function TParserRule.GetBranches(Index: integer): TParserRuleBranch; 175 begin 176 Result := TParserRuleBranch(FBranches[Index]); 177 end; 178 TParserRule.GetCountnull179function TParserRule.GetCount: integer; 180 begin 181 Result := FBranches.Count; 182 end; 183 TParserRule.IsValidnull184function TParserRule.IsValid: Boolean; 185 var i: integer; 186 begin 187 for i := 0 to Count - 1 do 188 if not Branches[i].IsValid then 189 begin 190 Result := False; 191 Exit; 192 end; 193 Result := (Count > 1) or (Count = 1) and (Branches[0].Count > 0); 194 end; 195 196 end. 197