Ignore:
Timestamp:
Dec 27, 2017, 6:05:11 PM (7 years ago)
Author:
chronos
Message:
  • Added: Lookup tables can be defined and their usage set from grammer rules.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/generator/UBuilder.pas

    r136 r137  
    163163    Add('    Position := StartPos;');
    164164    Add('    if Required then Error(''Expected "'' + Text + ''" but found "'' + ReadText + ''".'');');
    165     Add('  end;');
     165    Add('  end else WriteLn(''Expect: '' + Text);');
    166166    Add('end;');
    167167    Add('');
     
    178178    Add('    Position := StartPos;');
    179179    Add('    if Required then Error(''Expected "'' + CharFrom + ''" to "'' + CharTo + ''" but found "'' + ReadChar + ''".'');');
    180     Add('  end;');
     180    Add('  end else WriteLn(''ExpectRange: '' + CharFrom + ''..'' + CharTo + '': '' + ReadChar);');
    181181    Add('end;');
    182182    Add('');
     
    192192      Text := Text + '  OldPosition: TPosition;' + LineEnding;
    193193      Text := Text + 'begin' + LineEnding;
     194      //Text := Text + '  WriteLn(''Rule check: ' + Rule.Name + ''');';
    194195      Text := Text + '  Result := True;' + LineEnding;
    195196      Text := Text + FunctionBody;
     197      Text := Text + '  if Result then WriteLn(''Rule: ' + Rule.Name + ''');';
    196198      Text := Text + 'end;' + LineEnding + LineEnding;
    197199    end;
     
    223225      end;
    224226      rtAnd: begin
    225         if Assigned(Items.Grammer.WhiteSpaceRule) and Items.WithWhiteSpeaces
    226         and (I > 0) then begin
    227           Line := Line + Indent(IndentLevel) + 'repeat' + LineEnding;
    228           Inc(IndentLevel);
    229           Line := Line + Indent(IndentLevel) + 'if not Parse' +
    230             Items.Grammer.WhiteSpaceRule.Name + '(False)' +
    231             ' then Break;' + LineEnding;
    232           Dec(IndentLevel);
    233           Line := Line + Indent(IndentLevel) + 'until False;' + LineEnding;
    234         end;
    235 
    236227        if not Item.Optional then
    237228          Line := Line + Indent(IndentLevel) + 'Result := Result and ';
     
    256247          Indent(IndentLevel + 1) + 'Exit;' + LineEnding +
    257248          Indent(IndentLevel) + 'end;';
     249
     250        // Check white spaces
     251        if Assigned(Items.Grammer.WhiteSpaceRule) and Items.WithWhiteSpeaces
     252        then begin
     253          Line := Line + Indent(IndentLevel) + 'repeat' + LineEnding;
     254          Inc(IndentLevel);
     255          Line := Line + Indent(IndentLevel) + 'if not Parse' +
     256            Items.Grammer.WhiteSpaceRule.Name + '(False)' +
     257            ' then Break;' + LineEnding;
     258          Dec(IndentLevel);
     259          Line := Line + Indent(IndentLevel) + 'until False;' + LineEnding;
     260        end;
    258261      end;
    259262    end;
     
    302305    Add('  CloseFile(SourceFile);');
    303306    if Assigned(Grammer.TopRule) then
    304       Add('  Parser.Parse' + Grammer.TopRule.Name + ';');
     307      Add('  Parser.Parse' + Grammer.TopRule.Name + '(True);');
    305308    Add('  FreeAndNil(Parser);');
    306309    Add('end;');
     
    374377var
    375378  Item: TRuleItem;
     379  Line: string;
    376380begin
    377381  Result := '';
     382  Line := '';
    378383  with SourceFile do begin
    379384    for Item in Items do begin
    380385      case Item.RuleItemType of
    381         ritTerminal: Result := Result + '(Value = ''' + StringText(Item.Terminal, Item.EscapedStrings) + ''')';
    382         ritNonTerminal: Result := Result + 'Is' + Item.NonTerminalName + '(Value)';
    383         ritTerminalRange: Result := Result + '((Value > ''' + StringText(Item.TerminalFrom, Item.EscapedStrings) + ''') and (Value < ''' + StringText(Item.TerminalTo, Item.EscapedStrings) + '''))';
    384         ritSubItems: Result := Result + '(' + BuildTokenizerItems(SourceFile, Item.SubItems) + ')';
     386        ritTerminal: Line := Line + '(Value = ''' + StringText(Item.Terminal, Item.EscapedStrings) + ''')';
     387        ritNonTerminal: Line := Line + 'Is' + Item.NonTerminalName + '(Value)';
     388        ritTerminalRange: Line := Line + '((Value > ''' + StringText(Item.TerminalFrom, Item.EscapedStrings) + ''') and (Value < ''' + StringText(Item.TerminalTo, Item.EscapedStrings) + '''))';
     389        ritSubItems: Line := Line + '(' + BuildTokenizerItems(SourceFile, Item.SubItems) + ')';
    385390      end;
    386391      if Item <> Items[Items.Count - 1] then
    387         Result := Result + ' ' + RuleTypeText[Items.RuleType] + ' ';
    388     end;
     392        Line := Line + ' ' + RuleTypeText[Items.RuleType] + ' ';
     393      if Length(Line) > 80 then begin
     394        Result := Result + Line + LineEnding;
     395        Line := '';
     396      end;
     397    end;
     398    Result := Result + Line;
    389399  end;
    390400end;
Note: See TracChangeset for help on using the changeset viewer.