Skip to content

Commit

Permalink
improved whitespace support
Browse files Browse the repository at this point in the history
  • Loading branch information
darnocian committed Sep 22, 2024
1 parent 6b319ed commit a5d2b25
Show file tree
Hide file tree
Showing 12 changed files with 1,415 additions and 987 deletions.
46 changes: 31 additions & 15 deletions src/Sempare.Template.AST.pas
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ ETemplate = class(Exception);

TStripAction = ( //
saWhitespace, //
saNL, //
saKeepOneSpace //
saNL //
);

TStripActionSet = set of TStripAction;
Expand Down Expand Up @@ -150,10 +149,8 @@ ETemplate = class(Exception);
vsBlock, //

vsNewLine, //
vsWhiteSpace, //
vsWhiteSpace //

vsSingleton, //
vsValidate //
);

TTemplateSymbolSet = set of TTemplateSymbol;
Expand Down Expand Up @@ -201,9 +198,13 @@ ETemplate = class(Exception);
['{8C539211-ED84-4963-B894-C569C2F7B2FE}']
end;

TParserOption = (poAllowEnd, poAllowElse, poAllowElIf, poHasElse, poInLoop, poStripNL, poStripWS, poStripRecurringNL);
TParserOptions = set of TParserOption;

IStmt = interface(ITemplateVisitorHost)
['{6D37028E-A0C0-41F1-8A59-EDC0C9ADD9C7}']
function Flatten: TArray<IStmt>;
procedure OptimiseTemplate(const AOptions: TParserOptions; const ANewLine: string);
function GetHasEnd: boolean;
property HasEnd: boolean read GetHasEnd;
end;
Expand All @@ -214,22 +215,19 @@ ETemplate = class(Exception);
property Stmt: IStmt read GetStmt;
end;

TParserOption = (poAllowEnd, poAllowElse, poAllowElIf, poHasElse, poInLoop, poStripNL, poStripWS);
TParserOptions = set of TParserOption;

ITemplate = interface(ITemplateVisitorHost)
['{93AAB971-5B4B-4959-93F2-6C7DAE15C91B}']
function GetItem(const AOffset: integer): IStmt;
function GetCount: integer;
function GetLastItem: IStmt;
procedure FlattenTemplate;
procedure OptimiseTemplate(const AOptions: TParserOptions);
procedure OptimiseTemplate(const AOptions: TParserOptions; const ANewLine: string);
property Items[const AOffset: integer]: IStmt read GetItem;
property Count: integer read GetCount;
property LastItem: IStmt read GetLastItem;
end;

TAddLocation = (alLast, alBeforeNL, alAfterNL);
TAddLocation = (alLast, alFront);

ITemplateAdd = interface(ITemplate)
['{64465D68-0E9D-479F-9EF3-A30E75967809}']
Expand Down Expand Up @@ -260,7 +258,7 @@ ETemplate = class(Exception);
['{FB4CC3AB-BFEC-4189-B555-153DDA490D15}']
end;

TStripDirection = (sdEnd, sdLeft, sdRight, sdBeforeNewLine, sdAfterNewLine);
TStripDirection = (sdLeft, sdRight);

IStripStmt = interface(IStmt)
['{3313745B-D635-4453-9808-660DC462E15C}']
Expand Down Expand Up @@ -369,6 +367,16 @@ ETemplate = class(Exception);
property Container: ITemplate read GetContainer;
end;

IIgnoreNLStmt = interface(IStmt)
function GetContainer: ITemplate;
property Container: ITemplate read GetContainer;
end;

IIgnoreWSStmt = interface(IStmt)
function GetContainer: ITemplate;
property Container: ITemplate read GetContainer;
end;

ILoopStmt = interface(IStmt)
['{D6C26A41-3250-4EB9-A776-8952DE3931BD}']
function GetOnBeginContainer: ITemplate;
Expand Down Expand Up @@ -580,7 +588,10 @@ ETemplate = class(Exception);
procedure Visit(const AStmt: IBlockStmt); overload;
procedure Visit(const AStmt: IExtendsStmt); overload;
procedure Visit(const AStmt: ICompositeStmt); overload;
procedure Visit(const AStmt: INoopStmt); overload;
procedure Visit(const AStmt: IStripStmt); overload;
procedure Visit(const AStmt: IIgnoreNLStmt); overload;
procedure Visit(const AStmt: IIgnoreWSStmt); overload;
end;

IEvaluationTemplateVisitor = interface(ITemplateVisitor)
Expand Down Expand Up @@ -614,6 +625,7 @@ TAbstractStmt = class abstract(TAbstractBase, IStmt)
protected
function Flatten: TArray<IStmt>; virtual;
function GetHasEnd: boolean; virtual;
procedure OptimiseTemplate(const AOptions: TParserOptions; const ANewLine: string); virtual;
public
constructor Create(const APosition: IPosition);
end;
Expand All @@ -632,12 +644,11 @@ TMapExpr = class(TAbstractExpr, IMapExpr)

const
StripDirectionStr: array [TStripDirection] of string = ( //
'sdEnd', 'sdLeft', 'sdRight', 'sdBeforeNewLine', 'sdAfterNewLine');
'sdLeft', 'sdRight' //
);

StripActionStr: array [TStripAction] of string = ( //
'saWhitespace', //
'saNL', //
'saKeepOneSpace' //
'saWhitespace', 'saNL' //
);

type
Expand Down Expand Up @@ -744,4 +755,9 @@ function TAbstractStmt.GetHasEnd: boolean;
exit(false);
end;

procedure TAbstractStmt.OptimiseTemplate(const AOptions: TParserOptions; const ANewLine: string);
begin

end;

end.
20 changes: 8 additions & 12 deletions src/Sempare.Template.Context.pas
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ interface
procedure SetPrettyPrintOutput(const APrettyPrintOutput: TPrettyPrintOutput);
function GetPrettyPrintOutput: TPrettyPrintOutput;

function GetWhitespace: char;
procedure SetWhiteSpace(const AWS: char);
function GetWhitespace: string;
procedure SetWhiteSpace(const AWS: string);

function GetVariableResolver: TTemplateVariableResolver;
procedure SetVariableResolver(const AResolver: TTemplateVariableResolver);
Expand All @@ -181,7 +181,7 @@ interface
property RttiContext: TGetRttiContext read GetRttiContext write SetRttiContext;
property Functions: ITemplateFunctions read GetFunctions write SetFunctions;
property NewLine: string read GetNewLine write SetNewLine;
property WhitespaceChar: char read GetWhitespace write SetWhiteSpace;
property WhitespaceChar: string read GetWhitespace write SetWhiteSpace;
property TemplateResolver: TTemplateResolver read GetTemplateResolver write SetTemplateResolver;
property TemplateResolverWithContext: TTemplateResolverWithContext read GetTemplateResolverWithContext write SetTemplateResolverWithContext;
property MaxRunTimeMs: integer read GetMaxRunTimeMs write SetMaxRunTimeMs;
Expand Down Expand Up @@ -227,8 +227,6 @@ function CreateTemplateContext(const AOptions: TTemplateEvaluationOptions = []):
GUTF8WithoutPreambleEncoding: TUTF8WithoutPreambleEncoding;
GStreamWriterProvider: TStreamWriterProvider;
GPrettyPrintOutput: TPrettyPrintOutput;
GDefaultOpenStripWSTag: string = '<|';
GDefaultCloseWSTag: string = '|>';

implementation

Expand Down Expand Up @@ -286,7 +284,7 @@ TTemplateContext = class(TInterfacedObject, ITemplateContext, ITemplateContext
FFormatSettings: TFormatSettings;
FDebugFormat: string;
FPrettyPrintOutput: TPrettyPrintOutput;
FWhiteSpace: char;
FWhiteSpace: string;
FVariableResolver: TTemplateVariableResolver;
FRttiContext: TGetRttiContext;
public
Expand Down Expand Up @@ -376,8 +374,8 @@ TTemplateContext = class(TInterfacedObject, ITemplateContext, ITemplateContext
function GetDebugErrorFormat: string;
procedure SetDebugErrorFormat(const AFormat: string);

function GetWhitespace: char;
procedure SetWhiteSpace(const AWS: char);
function GetWhitespace: string;
procedure SetWhiteSpace(const AWS: string);

function GetVariableResolver: TTemplateVariableResolver;
procedure SetVariableResolver(const AResolver: TTemplateVariableResolver);
Expand Down Expand Up @@ -438,8 +436,6 @@ constructor TTemplateContext.Create(const AOptions: TTemplateEvaluationOptions);
SetEncoding(GDefaultEncoding);
FStartToken := GDefaultOpenTag;
FEndToken := GDefaultCloseTag;
FStartStripToken := GDefaultOpenStripWSTag;
FEndStripToken := GDefaultCloseWSTag;
FTemplates := TDictionary<string, ITemplate>.Create;
FVariables := TTemplateVariables.Create;
FFunctions := CreateTemplateFunctions(self);
Expand Down Expand Up @@ -579,7 +575,7 @@ function TTemplateContext.GetVariables: ITemplateVariables;
exit(FVariables);
end;

function TTemplateContext.GetWhitespace: char;
function TTemplateContext.GetWhitespace: string;
begin
exit(FWhiteSpace);
end;
Expand Down Expand Up @@ -738,7 +734,7 @@ procedure TTemplateContext.SetVariableResolver(const AResolver: TTemplateVariabl
FVariableResolver := AResolver;
end;

procedure TTemplateContext.SetWhiteSpace(const AWS: char);
procedure TTemplateContext.SetWhiteSpace(const AWS: string);
begin
FWhiteSpace := AWS;
end;
Expand Down
Loading

0 comments on commit a5d2b25

Please sign in to comment.