157 lines
6.2 KiB
ObjectPascal
157 lines
6.2 KiB
ObjectPascal
// TODO: jo lenne cache-elni az LA(x) karaktereket...
|
|
|
|
unit dpgrtl.charbuffer;
|
|
|
|
interface
|
|
uses
|
|
System.Classes,
|
|
dpgrtl.types,
|
|
dpgrtl.inputbuffer,
|
|
dpgrtl.charqueue;
|
|
|
|
type
|
|
TCharBuffer = class( TInputBuffer, IInputBuffer, ICharBuffer)
|
|
protected
|
|
fStream : TStream;
|
|
fQueue : TCharQueue;
|
|
|
|
protected
|
|
function GetNext: AnsiChar;
|
|
procedure Fill( Amount: integer);
|
|
|
|
// ------------------------------------------------------------
|
|
// TInputBuffer overrides
|
|
// ------------------------------------------------------------
|
|
protected
|
|
procedure Remove( ACount: integer); override;
|
|
|
|
// ------------------------------------------------------------
|
|
// ICharBuffer
|
|
// ------------------------------------------------------------
|
|
protected
|
|
function LA( i: integer): AnsiChar;
|
|
|
|
// ------------------------------------------------------------
|
|
// Construction/destruction
|
|
// ------------------------------------------------------------
|
|
public
|
|
constructor Create( AStream: TStream);
|
|
|
|
procedure AfterConstruction; override;
|
|
procedure BeforeDestruction; override;
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TCharBuffer }
|
|
|
|
// @@@: Construction/destruction ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//
|
|
// Construction/destruction
|
|
//
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ================================================================================================
|
|
// Constructor
|
|
// ================================================================================================
|
|
constructor TCharBuffer.Create(AStream: TStream);
|
|
begin
|
|
inherited Create;
|
|
fStream := AStream
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// After Destruction
|
|
// ================================================================================================
|
|
procedure TCharBuffer.AfterConstruction;
|
|
begin
|
|
inherited;
|
|
|
|
fMarkerCount := 0;
|
|
fMarkerOffset := 0;
|
|
fNumToConsume := 0;
|
|
|
|
fQueue := TCharQueue.Create
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Before Destruction
|
|
// ================================================================================================
|
|
procedure TCharBuffer.BeforeDestruction;
|
|
begin
|
|
fQueue.Free;
|
|
inherited
|
|
end;
|
|
|
|
// @@@: Interface +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//
|
|
// Interface
|
|
//
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ================================================================================================
|
|
// LA
|
|
//
|
|
// Ensures that the 'fQueue' object holds sufficient characters, and gets the 'i'th Look Ahead
|
|
// character from the 'fQueue'.
|
|
// ================================================================================================
|
|
function TCharBuffer.LA(i: integer): AnsiChar;
|
|
begin
|
|
Fill(i);
|
|
result := fQueue.Items[fMarkerOffset +i]
|
|
end;
|
|
|
|
// @@@: Internals +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//
|
|
// Internals
|
|
//
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ================================================================================================
|
|
// Get Next
|
|
// ================================================================================================
|
|
function TCharBuffer.GetNext: AnsiChar;
|
|
begin
|
|
if fStream.Read( result, 1) <> 1 then
|
|
result := #0
|
|
end;
|
|
|
|
// ================================================================================================
|
|
// Fill
|
|
// ================================================================================================
|
|
procedure TCharBuffer.Fill(Amount: integer);
|
|
begin
|
|
SyncConsume;
|
|
|
|
while fQueue.Count < (Amount +fMarkerOffset) do
|
|
fQueue.Add(GetNext)
|
|
end;
|
|
|
|
// @@@: Overrides +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
//
|
|
// Overrides
|
|
//
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// ================================================================================================
|
|
// Remove
|
|
// ================================================================================================
|
|
procedure TCharBuffer.Remove(ACount: integer);
|
|
begin
|
|
fQueue.Remove(ACount)
|
|
end;
|
|
|
|
end.
|