Ignore:
Timestamp:
Dec 24, 2017, 3:54:08 PM (7 years ago)
Author:
chronos
Message:
  • Added: Open recent files menu action.
  • Added: Generate BNF and EBNF output separately.
  • Added: Allow to hide main toolbar.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/generator/UGrammer.pas

    r130 r131  
    1515
    1616  TRuleItemType = (ritTerminal, ritNonTerminal, ritSubItems, ritTerminalRange);
     17  TGrammerNotation = (gnBnf, gnEbnf);
    1718
    1819  { TRuleItem }
     
    3940    procedure SaveToXmlNode(Node: TDOMNode);
    4041    function GetString: string;
     42    function ExportAsString(Notation: TGrammerNotation): string;
    4143    constructor Create;
    4244    destructor Destroy; override;
     
    6163    procedure UpdateRuleReference;
    6264    function GetString: string;
     65    function ExportAsString(Notation: TGrammerNotation): string;
    6366    property ParentRule: TRule read FParentRule write SetParentRule;
    6467  end;
     
    9598    procedure SaveToXmlNode(Node: TDOMNode);
    9699    function GetString: string;
     100    function ExportAsString(Notation: TGrammerNotation): string;
    97101    property Grammer: TGrammer read FGrammer write SetGrammer;
    98102  end;
     
    106110    procedure SaveToXmlNode(Node: TDOMNode);
    107111    function GetString: string;
     112    function ExportAsString(Notation: TGrammerNotation): string;
    108113  end;
    109114
     
    129134    procedure GetUsedByRule(RefRule: TRule; UsedByRules: TStrings);
    130135    function GetString: string;
     136    function ExportAsString(Notation: TGrammerNotation): string;
    131137    procedure Change;
    132138    property Modified: Boolean read FModified write SetModified;
     
    248254begin
    249255  Result := Rules.GetString;
     256end;
     257
     258function TGrammer.ExportAsString(Notation: TGrammerNotation): string;
     259begin
     260  Result := Rules.ExportAsString(Notation);
    250261end;
    251262
     
    292303  if Repetitive then Result := '*' + Result;
    293304  if AnyExcept then Result := '!' + Result;
     305end;
     306
     307function TRuleItem.ExportAsString(Notation: TGrammerNotation): string;
     308begin
     309  case Notation of
     310    gnBnf: begin
     311      case RuleItemType of
     312        ritTerminal: Result := Terminal;
     313        ritNonTerminal: Result := '<' + NonTerminal.Name + '>';
     314        ritSubItems: begin
     315          if not Optional and not Repetitive then
     316            Result := '(' + SubItems.ExportAsString(Notation) + ')'
     317            else Result := SubItems.ExportAsString(Notation);
     318        end;
     319        ritTerminalRange: Result := '(' + TerminalFrom + ' .. ' + TerminalTo + ')';
     320      end;
     321      if Optional and not Repetitive then Result := '[' + Result + ']';
     322      if Repetitive then Result := '{' + Result + '}';
     323      if AnyExcept then Result := 'other then ' + Result;
     324    end;
     325    gnEbnf: begin
     326      case RuleItemType of
     327        ritTerminal: Result := '"' + Terminal + '"';
     328        ritNonTerminal: Result := NonTerminal.Name;
     329        ritSubItems: begin
     330          if not Optional and not Repetitive then
     331            Result := '(' + SubItems.ExportAsString(Notation) + ')'
     332            else Result := SubItems.ExportAsString(Notation);
     333        end;
     334        ritTerminalRange: Result := '(' + TerminalFrom + ' .. ' + TerminalTo + ')';
     335      end;
     336      if Optional and not Repetitive then Result := '[' + Result + ']';
     337      if Repetitive then Result := '{' + Result + '}';
     338      if AnyExcept then Result := 'other then ' + Result;
     339    end;
     340  end;
    294341end;
    295342
     
    395442end;
    396443
     444function TRuleItems.ExportAsString(Notation: TGrammerNotation): string;
     445var
     446  Item: TRuleItem;
     447begin
     448  Result := '';
     449  for Item in Self do begin
     450    if Item <> First then begin
     451      if RuleType = rtAnd then begin
     452        case Notation of
     453          gnBnf: Result := Result + ' ';
     454          gnEbnf: Result := Result + ', ';
     455        end;
     456      end else
     457      if RuleType = rtOr then begin
     458        case Notation of
     459          gnBnf: Result := Result + ' | ';
     460          gnEbnf: Result := Result + ' | ';
     461        end;
     462      end;
     463    end;
     464    Result := Result + Item.ExportAsString(Notation);
     465  end;
     466end;
     467
    397468procedure TRuleItems.SetParentRule(AValue: TRule);
    398469begin
     
    456527end;
    457528
     529function TRule.ExportAsString(Notation: TGrammerNotation): string;
     530begin
     531  case Notation of
     532    gnBnf: Result := Name + ' ::= ' + Items.ExportAsString(Notation);
     533    gnEbnf: Result := Name + ' = ' + Items.ExportAsString(Notation);
     534  end;
     535end;
     536
    458537procedure TRule.SetGrammer(AValue: TGrammer);
    459538begin
     
    553632end;
    554633
     634function TRules.ExportAsString(Notation: TGrammerNotation): string;
     635var
     636  Rule: TRule;
     637begin
     638  Result := '';
     639  for Rule in Self do begin
     640    Result := Result + Rule.ExportAsString(Notation) + LineEnding;
     641  end;
     642end;
     643
    555644end.
    556645
Note: See TracChangeset for help on using the changeset viewer.