Files
bds.mr.dpg/src.lib/dpglib.TreeElem.pas
T
2026-01-03 18:33:48 +01:00

146 lines
5.2 KiB
ObjectPascal

// A TreeElement is a block with one alternative and a root node
unit dpglib.TreeElem;
interface
uses
dpgrtl.types,
dpglib.types,
dpglib.AlternativeBlock;
type
TTreeElem = class( TAlternativeBlock,
ITreeElem,
IGrammarElem)
protected
fRoot : IGrammarAtom;
// ---------------------------------------------------------------
// ITreeElem
// ---------------------------------------------------------------
protected
function GetRoot: IGrammarAtom;
procedure SetRoot( pRoot: IGrammarAtom);
// ---------------------------------------------------------------
// IGrammarElem
// ---------------------------------------------------------------
public
procedure Generate;
function Look( pK: integer): ILookahead;
function AsString : AnsiString;
public
constructor Create( Grammar : IGrammar;
Start : IToken;
Invert : boolean);
end;
implementation
{ TTreeElem }
// @@@: Construction / destruction ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Construction / destruction
//
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ================================================================================================
// Constructor
// ================================================================================================
constructor TTreeElem.Create(Grammar: IGrammar; Start: IToken; Invert: boolean);
begin
inherited
end;
// @@@: ITreeElem implementation ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// ITreeElem implementation
//
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ================================================================================================
// Get Root
// ================================================================================================
function TTreeElem.GetRoot: IGrammarAtom;
begin
result := fRoot
end;
// ================================================================================================
// Set Root
// ================================================================================================
procedure TTreeElem.SetRoot(pRoot: IGrammarAtom);
begin
fRoot := pRoot
end;
// @@@: IGrammarElem overrides ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// IGrammarElem overrides
//
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ================================================================================================
// Look
// ================================================================================================
function TTreeElem.Look(pK: integer): ILookahead;
begin
result := fGrammar.LLkAnalyzer.Look( pK, self);
end;
// ================================================================================================
// Generate
// ================================================================================================
procedure TTreeElem.Generate;
begin
fGrammar.Generator.Gen( self);
end;
// ================================================================================================
// As String
// ================================================================================================
function TTreeElem.AsString: AnsiString;
var
alt : IAlternative;
elem : IAlternativeElem;
begin
result := ' #(';
if Assigned(fRoot) then
result := result +fRoot.AsString;
alt := fAlternatives.Items[0] as IAlternative;
elem := alt.Head;
while Assigned(elem) do
begin
result := result + elem.AsString;
elem := elem.Next;
end;
result := result +')';
end;
end.