Ignore:
Timestamp:
Dec 26, 2017, 12:28:38 AM (7 years ago)
Author:
chronos
Message:
  • Modified: Restore start source position if rule item(s) not matched.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/generator/UBuilder.pas

    r133 r134  
    1616    LocalFunctions: string;
    1717    LocalFunctionIndex: Integer;
     18    function Indent(Count: Integer): string;
    1819    function GetItemString(Item: TRuleItem; Required: Boolean; IndentLevel: Integer): string;
    1920    function StringText(Text: string): string;
     
    3536implementation
    3637
     38function TBuilder.Indent(Count: Integer): string;
     39begin
     40  Result := DupeString('  ', Count);
     41end;
     42
    3743function TBuilder.GetItemString(Item: TRuleItem; Required: Boolean; IndentLevel: Integer): string;
    3844begin
     
    4753    ritSubItems: begin
    4854      LocalFunctions := LocalFunctions + 'function ParseSubitems' + IntToStr(LocalFunctionIndex) + '(Required: Boolean = False): Boolean;' + LineEnding;
     55      LocalFunctions := LocalFunctions + 'var' + LineEnding + '  OldPosition: Integer;' + LineEnding;
    4956      LocalFunctions := LocalFunctions + 'begin' + LineEnding;
    5057      LocalFunctions := LocalFunctions + '  Result := True;' + LineEnding;
     
    9097    Add('    function ExpectRange(CharFrom, CharTo: Char; Required: Boolean = False): Boolean;');
    9198    for Rule in Grammer.Rules do
    92     if Rule.Level = rlParser then
     99    //if Rule.Level = rlParser then
    93100      Add('    function Parse' + Rule.Name + '(Required: Boolean = False): Boolean;');
    94101    Add('    constructor Create;');
     
    148155    LocalFunctions := '';
    149156    for Rule in Grammer.Rules do
    150     if Rule.Level = rlParser then begin
     157    //if Rule.Level = rlParser then
     158    begin
    151159      FunctionBody := BuildParserItems(Rule.Items, True, 1);
    152160      Text := Text + 'function TParser.Parse' + Rule.Name + '(Required: Boolean = False): Boolean;' + LineEnding;
    153161      Text := Text + LocalFunctions;
     162      Text := Text + 'var' + LineBreak + '  OldPosition: Integer;' + LineEnding;
    154163      Text := Text + 'begin' + LineEnding;
    155164      Text := Text + '  Result := True;' + LineEnding;
     
    171180  Line: string;
    172181begin
    173   Result := '';
     182  Result := Indent(IndentLevel) + 'OldPosition := Position;' + LineEnding;
    174183  I := 0;
    175184  for Item in Items do begin
     
    178187    case Items.RuleType of
    179188      rtOr: begin
    180         Line := Line + DupeString('  ', IndentLevel);
     189        Line := Line + Indent(IndentLevel);
    181190        if I > 0 then Line := Line + 'else ';
    182191        Line := Line + 'if ' + GetItemString(Item, False, IndentLevel) +
     
    185194      rtAnd: begin
    186195        if not Item.Optional then
    187           Line := Line + DupeString('  ', IndentLevel) + 'Result := Result and ';
     196          Line := Line + Indent(IndentLevel) + 'Result := Result and ';
    188197        if Item.Repetitive then begin
    189198          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;
    192201          Inc(IndentLevel);
     202          Line := Line + Indent(IndentLevel) + 'if not ';
    193203          ItemRequired := False;
    194         end;
     204        end else
     205          if Item.Optional then Line := Line + Indent(IndentLevel);
    195206        Line := Line + GetItemString(Item, ItemRequired, IndentLevel);
    196207        if Item.Repetitive then begin
     208          Dec(IndentLevel);
    197209          Line := Line + ' then Break;' + LineEnding +
    198           DupeString('  ', IndentLevel) + 'until False';
    199           Dec(IndentLevel);
     210            Indent(IndentLevel) + 'until False';
    200211        end;
    201212        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;';
    203217      end;
    204218    end;
     
    208222  case Items.RuleType of
    209223    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;
    214228    end;
    215229  end;
     
    267281  SourceFile: TStringList;
    268282  TypeSectionStarted: Boolean;
     283  I: Integer;
    269284begin
    270285  SourceFile := TStringList.Create;
     
    281296    TypeSectionStarted := False;
    282297    for Rule in Grammer.Rules do
    283     if Rule.CreateSourceNode then begin
     298    //if Rule.CreateSourceNode then
     299    begin
    284300      if not TypeSectionStarted then begin
    285301        Add('type');
     
    290306    Add('');
    291307    for Rule in Grammer.Rules do
    292     if Rule.CreateSourceNode then begin
     308    //if Rule.CreateSourceNode then
     309    begin
    293310      Add('  T' + Rule.Name + ' = class');
     311      I := 1;
    294312      for Item in Rule.Items do begin
    295313        if Item.RuleItemType = ritNonTerminal then
    296314          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);
    299318      end;
    300319      Add('  end;' + LineEnding);
Note: See TracChangeset for help on using the changeset viewer.