- Timestamp:
- Dec 25, 2017, 12:23:39 PM (7 years ago)
- Location:
- branches/generator
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/generator/Grammers/Test Subitems.grm
r132 r133 13 13 <Repetitive>0</Repetitive> 14 14 <AnyExcept>0</AnyExcept> 15 <Terminal> d</Terminal>15 <Terminal>a</Terminal> 16 16 </RuleItem> 17 17 <RuleItem> 18 18 <Type>2</Type> 19 <Optional> -1</Optional>19 <Optional>0</Optional> 20 20 <Repetitive>0</Repetitive> 21 21 <AnyExcept>0</AnyExcept> … … 27 27 <Repetitive>0</Repetitive> 28 28 <AnyExcept>0</AnyExcept> 29 <Terminal> a</Terminal>29 <Terminal>b</Terminal> 30 30 </RuleItem> 31 31 <RuleItem> … … 34 34 <Repetitive>0</Repetitive> 35 35 <AnyExcept>0</AnyExcept> 36 <Terminal> b</Terminal>36 <Terminal>c</Terminal> 37 37 </RuleItem> 38 38 </SubItems> … … 43 43 <Repetitive>0</Repetitive> 44 44 <AnyExcept>0</AnyExcept> 45 <Terminal> c</Terminal>45 <Terminal>d</Terminal> 46 46 </RuleItem> 47 47 </RuleItems> -
branches/generator/UBuilder.pas
r132 r133 14 14 TBuilder = class 15 15 private 16 LocalFunctions: string; 17 LocalFunctionIndex: Integer; 16 18 function GetItemString(Item: TRuleItem; Required: Boolean; IndentLevel: Integer): string; 17 19 function StringText(Text: string): string; 18 20 procedure BuildMain(FileName: string); 19 21 procedure BuildParser(FileName: string); 20 function BuildParserItems(Items: TRuleItems; IndentLevel: Integer): string;22 function BuildParserItems(Items: TRuleItems; Required: Boolean; IndentLevel: Integer): string; 21 23 procedure BuildSource(FileName: string); 22 24 procedure BuildTokenizer(FileName: string); … … 44 46 StringText(Item.TerminalTo) + ''', ' + BooleanText[Required] + ')'; 45 47 ritSubItems: begin 46 Result := DupeString(' ', IndentLevel) + 'begin' + LineEnding; 47 Result := Result + BuildParserItems(Item.SubItems, IndentLevel + 1); 48 Result := Result + DupeString(' ', IndentLevel) + 'end'; 48 LocalFunctions := LocalFunctions + 'function ParseSubitems' + IntToStr(LocalFunctionIndex) + '(Required: Boolean = False): Boolean;' + LineEnding; 49 LocalFunctions := LocalFunctions + 'begin' + LineEnding; 50 LocalFunctions := LocalFunctions + ' Result := True;' + LineEnding; 51 LocalFunctions := LocalFunctions + BuildParserItems(Item.SubItems, Required, 1); 52 LocalFunctions := LocalFunctions + 'end;' + LineEnding + LineEnding; 53 54 Result := Result + 'ParseSubitems' + IntToStr(LocalFunctionIndex) + '(' + BooleanText[Required] + ')'; 55 Inc(LocalFunctionIndex); 49 56 end; 50 57 end; … … 60 67 ParserFile: TStringList; 61 68 Rule: TRule; 69 FunctionBody: string; 62 70 begin 63 71 ParserFile := TStringList.Create; … … 137 145 Add('end;'); 138 146 Add(''); 147 LocalFunctionIndex := 1; 148 LocalFunctions := ''; 139 149 for Rule in Grammer.Rules do 140 150 if Rule.Level = rlParser then begin 141 Add('function TParser.Parse' + Rule.Name + '(Required: Boolean = False): Boolean;'); 142 Add('begin'); 143 Add(' Result := True;'); 144 Text := Text + BuildParserItems(Rule.Items, 1); 145 Add('end;'); 146 Add(''); 151 FunctionBody := BuildParserItems(Rule.Items, True, 1); 152 Text := Text + 'function TParser.Parse' + Rule.Name + '(Required: Boolean = False): Boolean;' + LineEnding; 153 Text := Text + LocalFunctions; 154 Text := Text + 'begin' + LineEnding; 155 Text := Text + ' Result := True;' + LineEnding; 156 Text := Text + FunctionBody; 157 Text := Text + 'end;' + LineEnding + LineEnding; 147 158 end; 148 159 Add(''); … … 153 164 end; 154 165 155 function TBuilder.BuildParserItems(Items: TRuleItems; IndentLevel: Integer): string;166 function TBuilder.BuildParserItems(Items: TRuleItems; Required: Boolean; IndentLevel: Integer): string; 156 167 var 157 168 I: Integer; 158 169 Item: TRuleItem; 159 Required: Boolean;170 ItemRequired: Boolean; 160 171 Line: string; 161 172 begin … … 163 174 I := 0; 164 175 for Item in Items do begin 165 Required := not Item.Optional;176 ItemRequired := not Item.Optional and Required; 166 177 Line := ''; 167 178 case Items.RuleType of … … 173 184 end; 174 185 rtAnd: begin 175 if not Item.Optional and not (Item.RuleItemType = ritSubItems)then186 if not Item.Optional then 176 187 Line := Line + DupeString(' ', IndentLevel) + 'Result := Result and '; 177 188 if Item.Repetitive then begin 178 189 if not Item.Optional then 179 Line := Line + DupeString(' ', IndentLevel) + GetItemString(Item, Required, IndentLevel) + ';' + LineEnding;190 Line := Line + DupeString(' ', IndentLevel) + GetItemString(Item, ItemRequired, IndentLevel) + ';' + LineEnding; 180 191 Line := Line + DupeString(' ', IndentLevel) + 'repeat' + LineEnding + DupeString(' ', IndentLevel) + 'if not '; 181 Required := False; 192 Inc(IndentLevel); 193 ItemRequired := False; 182 194 end; 183 Line := Line + GetItemString(Item, Required, IndentLevel);184 if Item.Repetitive then 195 Line := Line + GetItemString(Item, ItemRequired, IndentLevel); 196 if Item.Repetitive then begin 185 197 Line := Line + ' then Break;' + LineEnding + 186 198 DupeString(' ', IndentLevel) + 'until False'; 199 Dec(IndentLevel); 200 end; 187 201 Line := Line + ';' + LineEnding + 188 202 DupeString(' ', IndentLevel) + 'if not Result then Exit;';
Note:
See TracChangeset
for help on using the changeset viewer.