Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit TSInfoDSL;
- {$mode objfpc}{$H+}
- interface
- uses
- TSInfoConsts, TSInfoTypes, TSInfoUtils, TSInfoFiles;
- { ----- types ----------------------------------------------------------------------------------------------------- }
- { ----- simple types -------------------------------------- }
- type
- TName = type String;
- TValue = type String;
- TDecComment = type String;
- TDefComment = type String;
- { ----- basic elements ------------------------------------ }
- type
- TAttribute = record
- Reference: Boolean;
- Name: TName;
- Value: TValue;
- Comment: TComment;
- end;
- type
- TChildNode = record
- Reference: Boolean;
- Name: TName;
- Comment: TComment;
- Attributes: array of TAttribute;
- ChildNodes: array of TChildNode;
- end;
- { ----- global functions ------------------------------------------------------------------------------------------ }
- { ----- for simple types ---------------------------------- }
- function Name(const AName: String): TName;
- function Value(const AValue: String): TValue;
- function Value(const AValues: array of String): TValue;
- function Comment(const AComment: String): TDecComment;
- function Comment(const AComments: array of String): TDecComment;
- function Comment(const AComment: String): TDefComment;
- function Comment(const AComments: array of String): TDefComment;
- function DecComment(const AComment: String): TDecComment;
- function DecComment(const AComments: array of String): TDecComment;
- function DefComment(const AComment: String): TDefComment;
- function DefComment(const AComments: array of String): TDefComment;
- { ----- for standard attributes --------------------------- }
- function Attr(const AName: TName; const AValue: TValue = ''): TAttribute;
- function Attr(const AComment: TDecComment; const AName: TName; const AValue: TValue = ''): TAttribute;
- { ----- for standard child nodes -------------------------- }
- function Node(const AName: TName): TChildNode;
- function Node(const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- function Node(const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- function Node(const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- function Node(const AComment: TDecComment; const AName: TName): TChildNode;
- function Node(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- function Node(const AComment: TDecComment; const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- function Node(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- { ----- for referenced atrributes ------------------------- }
- function RefAttr(const AName: TName; const AValue: TValue = ''): TAttribute;
- function RefAttr(const AComment: TDecComment; const AName: TName; const AValue: TValue = ''): TAttribute;
- function RefAttr(const AComment: TDefComment; const AName: TName; const AValue: TValue = ''): TAttribute;
- function RefAttr(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName; const AValue: TValue = ''): TAttribute;
- { ----- for referenced child nodes ------------------------ }
- function RefNode(const AName: TName): TChildNode;
- function RefNode(const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- function RefNode(const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- function RefNode(const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- function RefNode(const AComment: TDecComment; const AName: TName): TChildNode;
- function RefNode(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- function RefNode(const AComment: TDecComment; const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- function RefNode(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- function RefNode(const AComment: TDefComment; const AName: TName): TChildNode;
- function RefNode(const AComment: TDefComment; const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- function RefNode(const AComment: TDefComment; const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- function RefNode(const AComment: TDefComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- function RefNode(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName): TChildNode;
- function RefNode(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- function RefNode(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- function RefNode(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- { ----- for main function --------------------------------- }
- function TreeStructInfo(const AName: TName = ''): TTSInfoTree;
- function TreeStructInfo(const AComment: TDecComment; const AName: TName = ''): TTSInfoTree;
- function TreeStructInfo(const AAttributes: array of TAttribute): TTSInfoTree;
- function TreeStructInfo(const AChildNodes: array of TChildNode): TTSInfoTree;
- function TreeStructInfo(const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- function TreeStructInfo(const AName: TName; const AAttributes: array of TAttribute): TTSInfoTree;
- function TreeStructInfo(const AName: TName; const AChildNodes: array of TChildNode): TTSInfoTree;
- function TreeStructInfo(const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- function TreeStructInfo(const AComment: TDecComment; const AAttributes: array of TAttribute): TTSInfoTree;
- function TreeStructInfo(const AComment: TDecComment; const AChildNodes: array of TChildNode): TTSInfoTree;
- function TreeStructInfo(const AComment: TDecComment; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- function TreeStructInfo(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute): TTSInfoTree;
- function TreeStructInfo(const AComment: TDecComment; const AName: TName; const AChildNodes: array of TChildNode): TTSInfoTree;
- function TreeStructInfo(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- { ----- end interface --------------------------------------------------------------------------------------------- }
- implementation
- { ----- internal functions ---------------------------------------------------------------------------------------- }
- { ----- simple types -------------------------------------- }
- function GlueStrings(const AStrings: array of String): String;
- var
- strToken: String;
- begin
- Result := '';
- for strToken in AStrings do
- Result += strToken + VALUES_DELIMITER;
- SetLength(Result, Length(Result) - 1);
- end;
- { ----- basic elements ------------------------------------ }
- function InternalAttr(const AReference: Boolean; const AName: TName; const AValue: TValue; const AComment: TComment): TAttribute; inline;
- begin
- Result.Reference := AReference;
- Result.Name := AName;
- Result.Value := AValue;
- Result.Comment := AComment;
- end;
- function InternalNode(const AReference: Boolean; const AName: TName; const AComment: TComment; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- var
- intElementIdx: Integer;
- begin
- Result.Reference := AReference;
- Result.Name := AName;
- Result.Comment := AComment;
- SetLength(Result.Attributes, Length(AAttributes));
- SetLength(Result.ChildNodes, Length(AChildNodes));
- for intElementIdx := 0 to High(AAttributes) do
- Result.Attributes[intElementIdx] := AAttributes[intElementIdx];
- for intElementIdx := 0 to High(AChildNodes) do
- Result.ChildNodes[intElementIdx] := AChildNodes[intElementIdx];
- end;
- procedure InternalAddElements(ATSInfoTree: TTSInfoTree; AAttributes: array of TAttribute; AChildNodes: array of TChildNode);
- var
- attrAdd: TAttribute;
- nodeAdd: TChildNode;
- begin
- for attrAdd in AAttributes do
- begin
- ATSInfoTree.CreateAttribute('~', attrAdd.Reference, attrAdd.Name);
- ATSInfoTree.WriteString(attrAdd.Name, attrAdd.Value);
- ATSInfoTree.WriteAttributeComment(attrAdd.Name, attrAdd.Comment[ctDeclaration], '', ctDeclaration);
- ATSInfoTree.WriteAttributeComment(attrAdd.Name, attrAdd.Comment[ctDefinition], '', ctDefinition);
- end;
- for nodeAdd in AChildNodes do
- begin
- ATSInfoTree.CreateChildNode('~', nodeAdd.Reference, nodeAdd.Name);
- ATSInfoTree.WriteChildNodeComment(nodeAdd.Name, nodeAdd.Comment[ctDeclaration], '', ctDeclaration);
- ATSInfoTree.WriteChildNodeComment(nodeAdd.Name, nodeAdd.Comment[ctDefinition], '', ctDefinition);
- ATSInfoTree.OpenChildNode(nodeAdd.Name);
- InternalAddElements(ATSInfoTree, nodeAdd.Attributes, nodeAdd.ChildNodes);
- ATSInfoTree.GoToParentNode();
- end;
- end;
- function InternalTreeStructInfo(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := TTSInfoTree.Create();
- Result.WriteTreeComment(AComment, '');
- Result.RenameTree(AName);
- InternalAddElements(Result, AAttributes, AChildNodes);
- end;
- { ----- global functions ------------------------------------------------------------------------------------------ }
- { ----- for simple types ---------------------------------- }
- function Name(const AName: String): TName;
- begin
- Result := AName;
- end;
- function Value(const AValue: String): TValue;
- begin
- Result := AValue;
- end;
- function Value(const AValues: array of String): TValue;
- begin
- Result := GlueStrings(AValues);
- end;
- function Comment(const AComment: String): TDecComment;
- begin
- Result := AComment;
- end;
- function Comment(const AComments: array of String): TDecComment;
- begin
- Result := GlueStrings(AComments);
- end;
- function Comment(const AComment: String): TDefComment;
- begin
- Result := AComment;
- end;
- function Comment(const AComments: array of String): TDefComment;
- begin
- Result := GlueStrings(AComments);
- end;
- function DecComment(const AComment: String): TDecComment;
- begin
- Result := AComment;
- end;
- function DefComment(const AComment: String): TDefComment;
- begin
- Result := AComment;
- end;
- function DecComment(const AComments: array of String): TDecComment;
- begin
- Result := GlueStrings(AComments);
- end;
- function DefComment(const AComments: array of String): TDefComment;
- begin
- Result := GlueStrings(AComments);
- end;
- { ----- for standard attributes --------------------------- }
- function Attr(const AName: TName; const AValue: TValue): TAttribute;
- begin
- Result := InternalAttr(False, AName, AValue, TSInfoUtils.Comment('', ''));
- end;
- function Attr(const AComment: TDecComment; const AName: TName; const AValue: TValue): TAttribute;
- begin
- Result := InternalAttr(False, AName, AValue, TSInfoUtils.Comment(AComment, ''));
- end;
- { ----- for standard child nodes -------------------------- }
- function Node(const AName: TName): TChildNode;
- begin
- Result := InternalNode(False, AName, TSInfoUtils.Comment('', ''), [], []);
- end;
- function Node(const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- begin
- Result := InternalNode(False, AName, TSInfoUtils.Comment('', ''), AAttributes, []);
- end;
- function Node(const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(False, AName, TSInfoUtils.Comment('', ''), [], AChildNodes);
- end;
- function Node(const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(False, AName, TSInfoUtils.Comment('', ''), AAttributes, AChildNodes);
- end;
- function Node(const AComment: TDecComment; const AName: TName): TChildNode;
- begin
- Result := InternalNode(False, AName, TSInfoUtils.Comment(AComment, ''), [], []);
- end;
- function Node(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- begin
- Result := InternalNode(False, AName, TSInfoUtils.Comment(AComment, ''), AAttributes, []);
- end;
- function Node(const AComment: TDecComment; const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(False, AName, TSInfoUtils.Comment(AComment, ''), [], AChildNodes);
- end;
- function Node(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(False, AName, TSInfoUtils.Comment(AComment, ''), AAttributes, AChildNodes);
- end;
- { ----- for referenced attributes ------------------------- }
- function RefAttr(const AName: TName; const AValue: TValue): TAttribute;
- begin
- Result := InternalAttr(True, AName, AValue, TSInfoUtils.Comment('', ''));
- end;
- function RefAttr(const AComment: TDecComment; const AName: TName; const AValue: TValue): TAttribute;
- begin
- Result := InternalAttr(True, AName, AValue, TSInfoUtils.Comment(AComment, ''));
- end;
- function RefAttr(const AComment: TDefComment; const AName: TName; const AValue: TValue): TAttribute;
- begin
- Result := InternalAttr(True, AName, AValue, TSInfoUtils.Comment('', AComment));
- end;
- function RefAttr(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName; const AValue: TValue): TAttribute;
- begin
- Result := InternalAttr(True, AName, AValue, TSInfoUtils.Comment(ADecComment, ADefComment));
- end;
- { ----- for referenced child nodes ------------------------ }
- function RefNode(const AName: TName): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment('', ''), [], []);
- end;
- function RefNode(const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment('', ''), AAttributes, []);
- end;
- function RefNode(const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment('', ''), [], AChildNodes);
- end;
- function RefNode(const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment('', ''), AAttributes, AChildNodes);
- end;
- function RefNode(const AComment: TDecComment; const AName: TName): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment(AComment, ''), [], []);
- end;
- function RefNode(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment(AComment, ''), AAttributes, []);
- end;
- function RefNode(const AComment: TDecComment; const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment(AComment, ''), [], AChildNodes);
- end;
- function RefNode(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment(AComment, ''), AAttributes, AChildNodes);
- end;
- function RefNode(const AComment: TDefComment; const AName: TName): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment('', AComment), [], []);
- end;
- function RefNode(const AComment: TDefComment; const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment('', AComment), AAttributes, []);
- end;
- function RefNode(const AComment: TDefComment; const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment('', AComment), [], AChildNodes);
- end;
- function RefNode(const AComment: TDefComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment('', AComment), AAttributes, AChildNodes);
- end;
- function RefNode(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment(ADecComment, ADefComment), [], []);
- end;
- function RefNode(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName; const AAttributes: array of TAttribute): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment(ADecComment, ADefComment), AAttributes, []);
- end;
- function RefNode(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment(ADecComment, ADefComment), [], AChildNodes);
- end;
- function RefNode(const ADecComment: TDecComment; const ADefComment: TDefComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TChildNode;
- begin
- Result := InternalNode(True, AName, TSInfoUtils.Comment(ADecComment, ADefComment), AAttributes, AChildNodes);
- end;
- { ----- for main function --------------------------------- }
- function TreeStructInfo(const AName: TName): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo('', AName, [], []);
- end;
- function TreeStructInfo(const AComment: TDecComment; const AName: TName): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo(AComment, AName, [], []);
- end;
- function TreeStructInfo(const AAttributes: array of TAttribute): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo('', '', AAttributes, []);
- end;
- function TreeStructInfo(const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo('', '', [], AChildNodes);
- end;
- function TreeStructInfo(const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo('', '', AAttributes, AChildNodes);
- end;
- function TreeStructInfo(const AName: TName; const AAttributes: array of TAttribute): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo('', AName, AAttributes, []);
- end;
- function TreeStructInfo(const AName: TName; const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo('', AName, [], AChildNodes);
- end;
- function TreeStructInfo(const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo('', AName, AAttributes, AChildNodes);
- end;
- function TreeStructInfo(const AComment: TDecComment; const AAttributes: array of TAttribute): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo(AComment, '', AAttributes, []);
- end;
- function TreeStructInfo(const AComment: TDecComment; const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo(AComment, '', [], AChildNodes);
- end;
- function TreeStructInfo(const AComment: TDecComment; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo(AComment, '', AAttributes, AChildNodes);
- end;
- function TreeStructInfo(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo(AComment, AName, AAttributes, []);
- end;
- function TreeStructInfo(const AComment: TDecComment; const AName: TName; const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo(AComment, AName, [], AChildNodes);
- end;
- function TreeStructInfo(const AComment: TDecComment; const AName: TName; const AAttributes: array of TAttribute; const AChildNodes: array of TChildNode): TTSInfoTree;
- begin
- Result := InternalTreeStructInfo(AComment, AName, AAttributes, AChildNodes);
- end;
- { ----- end implementation ---------------------------------------------------------------------------------------- }
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement