155 lines
5.9 KiB
ObjectPascal
155 lines
5.9 KiB
ObjectPascal
// ----------------------------------------------------------------------------
|
|
// A GrammarElement is a generic node in our data structure that holds a
|
|
// grammar in memory. This data structure can be used for static analysis or
|
|
// for dynamic analysis (during parsing).
|
|
// Every node must know which grammar owns it, how to generate code, and how
|
|
// to do analysis.
|
|
// ----------------------------------------------------------------------------
|
|
unit dpglib.GrammarElem;
|
|
|
|
interface
|
|
uses
|
|
dpgrtl.types,
|
|
dpglib.types;
|
|
|
|
type
|
|
TGrammarElem = class( TInterfacedObject, IGrammarElem)
|
|
protected
|
|
fGrammar : IGrammar;
|
|
fLine : integer;
|
|
fColumn : integer;
|
|
|
|
// ----------------------------------------------------------------------
|
|
// IGrammarElem methods
|
|
// ----------------------------------------------------------------------
|
|
protected
|
|
function GetLine : integer;
|
|
function GetColumn : integer;
|
|
|
|
procedure SetLine( Line : integer);
|
|
procedure SetColumn( Colunm : integer);
|
|
|
|
procedure Generate;
|
|
function AsString: AnsiString;
|
|
function Look( k: integer): ILookahead;
|
|
|
|
// ----------------------------------------------------------------------
|
|
// Construction/destruction
|
|
// ----------------------------------------------------------------------
|
|
public
|
|
constructor Create( Grammar: IGrammar); overload;
|
|
constructor Create( Grammar: IGrammar; Start: IToken); overload;
|
|
destructor Destroy; override;
|
|
end;
|
|
|
|
implementation
|
|
|
|
// @@@: Construction/destruction ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//
|
|
// Construction/destruction
|
|
//
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ================================================================================================
|
|
// Constructor
|
|
// ================================================================================================
|
|
constructor TGrammarElem.Create(Grammar: IGrammar);
|
|
begin
|
|
inherited Create;
|
|
|
|
fGrammar := Grammar;
|
|
fLine := -1;
|
|
fColumn := -1;
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Constructor
|
|
// ================================================================================================
|
|
constructor TGrammarElem.Create(Grammar: IGrammar; Start: IToken);
|
|
begin
|
|
inherited Create;
|
|
|
|
fGrammar := Grammar;
|
|
fLine := Start.TokenLine;
|
|
fColumn := Start.TokenColumn;
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Destructor
|
|
// ================================================================================================
|
|
destructor TGrammarElem.Destroy;
|
|
begin
|
|
fGrammar := nil;
|
|
inherited;
|
|
end;
|
|
|
|
// @@@: IGrammarElem implementation +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//
|
|
// IGrammarElem implementation
|
|
//
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ================================================================================================
|
|
// Get Line
|
|
// ================================================================================================
|
|
function TGrammarElem.GetLine: integer;
|
|
begin
|
|
result := fLine;
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Get Column
|
|
// ================================================================================================
|
|
function TGrammarElem.GetColumn: integer;
|
|
begin
|
|
result := fColumn;
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Set Line
|
|
// ================================================================================================
|
|
procedure TGrammarElem.SetLine(Line: integer);
|
|
begin
|
|
fLine := Line
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Set Column
|
|
// ================================================================================================
|
|
procedure TGrammarElem.SetColumn(Colunm: integer);
|
|
begin
|
|
fColumn := Colunm
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Generate
|
|
// ================================================================================================
|
|
procedure TGrammarElem.Generate;
|
|
begin
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Look
|
|
// ================================================================================================
|
|
function TGrammarElem.Look(k: integer): ILookahead;
|
|
begin
|
|
result := nil;
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// AsString
|
|
// ================================================================================================
|
|
function TGrammarElem.AsString: AnsiString;
|
|
begin
|
|
result := ''
|
|
end;
|
|
|
|
|
|
end.
|