Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to include and exclude specific constructs in AST #38

Open
LaKraven opened this issue Jan 22, 2015 · 2 comments
Open

Ability to include and exclude specific constructs in AST #38

LaKraven opened this issue Jan 22, 2015 · 2 comments

Comments

@LaKraven
Copy link
Contributor

It would be useful to provide the implementing developer with the ability to specify what constructs of the source to include within the Syntax Tree.

This could be done as a Set, so the implementing developer can choose to, for example, exclude the complete STATEMENTS section from each method implementation from the Syntax Tree, or to exclude global variables/constants etc.

This way, if a tool using the AST is only interested in a particular area of the source, they can omit the rest and make the subsequent scraping/transformation operation(s) of their implementing tool quicker.

@RomanYankovsky
Copy link
Owner

Agree. This would be useful in many cases.

@JBontes
Copy link

JBontes commented May 6, 2015

This is difficult to do in the actual AST building, but very easy to do in the generation of the output XML:

procedure NodeToXMLInternal(const Node: TSyntaxNode; const Indent: string);
  var
    HasChildren: Boolean;
    NewIndent: string;
    Attr: TPair<TAttributeType, string>;
    ChildNode: TSyntaxNode;
    NodeOK: boolean;
  begin
    NodeOK:= not(Node.Typ in Exclude) and (Node.Typ in InternalInclude);
    HasChildren := Node.HasChildren;
    if NodeOK then begin
      if Formatted then begin
        NewIndent:= Indent + '  ';
        Builder.Append(Indent);
      end;
      Builder.Append('<' + UpperCase(SyntaxNodeNames[Node.Typ]));

      if (Node is TCompoundSyntaxNode) then begin
        Builder.Append(' begin_line="' + IntToStr(TCompoundSyntaxNode(Node).Line) + '"');
        Builder.Append(' begin_col="' + IntToStr(TCompoundSyntaxNode(Node).Col) + '"');
        Builder.Append(' end_line="' + IntToStr(TCompoundSyntaxNode(Node).EndLine) + '"');
        Builder.Append(' end_col="' + IntToStr(TCompoundSyntaxNode(Node).EndCol) + '"');
      end else begin
        Builder.Append(' line="' + IntToStr(Node.Line) + '"');
        Builder.Append(' col="' + IntToStr(Node.Col) + '"');
      end;

      for Attr in Node.Attributes do Builder.Append(' ' + AttributeName[Attr.Key] + '="' + XMLEncode(Attr.Value) + '"');
      if HasChildren then Builder.Append('>')
      else Builder.Append('/>');
      if Formatted then Builder.AppendLine;
    end;
    for ChildNode in Node.ChildNodes do NodeToXMLInternal(ChildNode, NewIndent);
    if NodeOK then begin
      if HasChildren then begin
        if Formatted then Builder.Append(Indent);
        Builder.Append('</' + UpperCase(SyntaxNodeNames[Node.Typ]) + '>');
        if Formatted then Builder.AppendLine;
      end;
    end;
  end;

begin
  if Include = [] then InternalInclude:= [ntUnknown..ntWrite]
  else InternalInclude:= Include;
  NodeToXMLInternal(Node, '');
end;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants