Changeset 134
- Timestamp:
- Dec 26, 2017, 12:28:38 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/generator/UBuilder.pas
r133 r134 16 16 LocalFunctions: string; 17 17 LocalFunctionIndex: Integer; 18 function Indent(Count: Integer): string; 18 19 function GetItemString(Item: TRuleItem; Required: Boolean; IndentLevel: Integer): string; 19 20 function StringText(Text: string): string; … … 35 36 implementation 36 37 38 function TBuilder.Indent(Count: Integer): string; 39 begin 40 Result := DupeString(' ', Count); 41 end; 42 37 43 function TBuilder.GetItemString(Item: TRuleItem; Required: Boolean; IndentLevel: Integer): string; 38 44 begin … … 47 53 ritSubItems: begin 48 54 LocalFunctions := LocalFunctions + 'function ParseSubitems' + IntToStr(LocalFunctionIndex) + '(Required: Boolean = False): Boolean;' + LineEnding; 55 LocalFunctions := LocalFunctions + 'var' + LineEnding + ' OldPosition: Integer;' + LineEnding; 49 56 LocalFunctions := LocalFunctions + 'begin' + LineEnding; 50 57 LocalFunctions := LocalFunctions + ' Result := True;' + LineEnding; … … 90 97 Add(' function ExpectRange(CharFrom, CharTo: Char; Required: Boolean = False): Boolean;'); 91 98 for Rule in Grammer.Rules do 92 if Rule.Level = rlParser then99 //if Rule.Level = rlParser then 93 100 Add(' function Parse' + Rule.Name + '(Required: Boolean = False): Boolean;'); 94 101 Add(' constructor Create;'); … … 148 155 LocalFunctions := ''; 149 156 for Rule in Grammer.Rules do 150 if Rule.Level = rlParser then begin 157 //if Rule.Level = rlParser then 158 begin 151 159 FunctionBody := BuildParserItems(Rule.Items, True, 1); 152 160 Text := Text + 'function TParser.Parse' + Rule.Name + '(Required: Boolean = False): Boolean;' + LineEnding; 153 161 Text := Text + LocalFunctions; 162 Text := Text + 'var' + LineBreak + ' OldPosition: Integer;' + LineEnding; 154 163 Text := Text + 'begin' + LineEnding; 155 164 Text := Text + ' Result := True;' + LineEnding; … … 171 180 Line: string; 172 181 begin 173 Result := '';182 Result := Indent(IndentLevel) + 'OldPosition := Position;' + LineEnding; 174 183 I := 0; 175 184 for Item in Items do begin … … 178 187 case Items.RuleType of 179 188 rtOr: begin 180 Line := Line + DupeString(' ',IndentLevel);189 Line := Line + Indent(IndentLevel); 181 190 if I > 0 then Line := Line + 'else '; 182 191 Line := Line + 'if ' + GetItemString(Item, False, IndentLevel) + … … 185 194 rtAnd: begin 186 195 if not Item.Optional then 187 Line := Line + DupeString(' ',IndentLevel) + 'Result := Result and ';196 Line := Line + Indent(IndentLevel) + 'Result := Result and '; 188 197 if Item.Repetitive then begin 189 198 if not Item.Optional then 190 Line := Line + DupeString(' ',IndentLevel) + GetItemString(Item, ItemRequired, IndentLevel) + ';' + LineEnding;191 Line := Line + DupeString(' ', IndentLevel) + 'repeat' + LineEnding + DupeString(' ', IndentLevel) + 'if not ';199 Line := Line + Indent(IndentLevel) + GetItemString(Item, ItemRequired, IndentLevel) + ';' + LineEnding; 200 Line := Line + Indent(IndentLevel) + 'repeat' + LineEnding; 192 201 Inc(IndentLevel); 202 Line := Line + Indent(IndentLevel) + 'if not '; 193 203 ItemRequired := False; 194 end; 204 end else 205 if Item.Optional then Line := Line + Indent(IndentLevel); 195 206 Line := Line + GetItemString(Item, ItemRequired, IndentLevel); 196 207 if Item.Repetitive then begin 208 Dec(IndentLevel); 197 209 Line := Line + ' then Break;' + LineEnding + 198 DupeString(' ', IndentLevel) + 'until False'; 199 Dec(IndentLevel); 210 Indent(IndentLevel) + 'until False'; 200 211 end; 201 212 Line := Line + ';' + LineEnding + 202 DupeString(' ', IndentLevel) + 'if not Result then Exit;'; 213 Indent(IndentLevel) + 'if not Result then begin' + LineEnding + 214 Indent(IndentLevel + 1) + 'Position := OldPosition;' + LineEnding + 215 Indent(IndentLevel + 1) + 'Exit;' + LineEnding + 216 Indent(IndentLevel) + 'end;'; 203 217 end; 204 218 end; … … 208 222 case Items.RuleType of 209 223 rtOr: begin 210 Result := Result + DupeString(' ',IndentLevel) + 'else begin' + LineEnding;211 Result := Result + DupeString(' ',IndentLevel) + ' Error(''Unexpected token'');' + LineEnding;212 Result := Result + DupeString(' ',IndentLevel) + ' Result := False;' + LineEnding;213 Result := Result + DupeString(' ',IndentLevel) + 'end;' + LineEnding;224 Result := Result + Indent(IndentLevel) + 'else begin' + LineEnding; 225 Result := Result + Indent(IndentLevel) + ' Error(''Unexpected token'');' + LineEnding; 226 Result := Result + Indent(IndentLevel) + ' Result := False;' + LineEnding; 227 Result := Result + Indent(IndentLevel) + 'end;' + LineEnding; 214 228 end; 215 229 end; … … 267 281 SourceFile: TStringList; 268 282 TypeSectionStarted: Boolean; 283 I: Integer; 269 284 begin 270 285 SourceFile := TStringList.Create; … … 281 296 TypeSectionStarted := False; 282 297 for Rule in Grammer.Rules do 283 if Rule.CreateSourceNode then begin 298 //if Rule.CreateSourceNode then 299 begin 284 300 if not TypeSectionStarted then begin 285 301 Add('type'); … … 290 306 Add(''); 291 307 for Rule in Grammer.Rules do 292 if Rule.CreateSourceNode then begin 308 //if Rule.CreateSourceNode then 309 begin 293 310 Add(' T' + Rule.Name + ' = class'); 311 I := 1; 294 312 for Item in Rule.Items do begin 295 313 if Item.RuleItemType = ritNonTerminal then 296 314 if Item.Repetitive then 297 Add(' ' + Item.NonTerminal.Name + ': TFPGObjectList<T' + Item.NonTerminal.Name + '>;') 298 else Add(' ' + Item.NonTerminal.Name + ': T' + Item.NonTerminal.Name + ';'); 315 Add(' ' + Item.NonTerminal.Name + IntToStr(I) + ': TFPGObjectList<T' + Item.NonTerminal.Name + '>;') 316 else Add(' ' + Item.NonTerminal.Name + IntToStr(I) + ': T' + Item.NonTerminal.Name + ';'); 317 Inc(I); 299 318 end; 300 319 Add(' end;' + LineEnding);
Note:
See TracChangeset
for help on using the changeset viewer.