Skip to content

Allow attributes to make format preserving print piece of cake #11

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

Closed
TomasVotruba opened this issue May 5, 2018 · 4 comments
Closed

Comments

@TomasVotruba
Copy link
Contributor

TomasVotruba commented May 5, 2018

It probably out of scope of PHPStan (for now), but as we talked, it would be great if this package allowed format preserving print.

I already asked nikic how to do it, he explained me the basics. Basically, AbstractNode class with set/get attributes is the first step towards it: nikic/PHP-Parser@4d2a4d0#diff-ee2208021c6e96ff1de44281aa029630R108

After few weeks I managed to make working prototype, but it contains lot of boiler plate code and external storage of tokens positions etc., to make it work.

Having this would also make it easy to configure FQN namespaces, the same way php-parser does, without modifing the output. And other SOLID features :)

@ondrejmirtes
Copy link
Member

Please join forces with @dantleech in #10 - he wrote in two hours ago 😊

@TomasVotruba
Copy link
Contributor Author

TomasVotruba commented May 5, 2018

I've read it, but Tokens (array, chars) and Nodes (rich objects) are different thing

@dantleech
Copy link

dantleech commented May 5, 2018

Quite funny, I was just looking at your packages @TomasVotruba :) My approach for format preserving was to leave the AST read-only, but instead export it to a mutable format, e.g. XML. In the XML document the nodes are represented as elements, and the tokens have node values, e.g.

<PhpDocNode>
  <PhpDocTextNode text="Short description"/>
  <PhpDocTextNode text=""/>
  <PhpDocTextNode text="This is a multi-line description"/>
  <PhpDocTextNode text="of the docblock."/>
  <PhpDocTextNode text=""/>
  <PhpDocTagNode name="@param">
    <ParamTagNode isVariadic="" parameterName="$bar" description="">
      <IdentifierTypeNode name="Fobar"/>
    </ParamTagNode>
  </PhpDocTagNode>
  <PhpDocTagNode name="@param">
    <ParamTagNode isVariadic="" parameterName="$foo" description="">
      <IdentifierTypeNode name="Bar\Foo"/>
    </ParamTagNode>
  </PhpDocTagNode>
  <PhpDocTagNode name="@param">
    <ParamTagNode isVariadic="" parameterName="$baz" description="">
      <IdentifierTypeNode name="int"/>
    </ParamTagNode>
  </PhpDocTagNode>
  <PhpDocTextNode text=""/>
  <PhpDocTagNode name="@throws">
    <ThrowsTagNode description="">
      <IdentifierTypeNode name="Barbar"/>
    </ThrowsTagNode>
  </PhpDocTagNode>
  <PhpDocTagNode name="@method">
    <MethodTagNode isStatic="" methodName="foobar" description="">
      <IdentifierTypeNode name="string"/>
    </MethodTagNode>
  </PhpDocTagNode>
  <PhpDocTextNode text=""/>
  <PhpDocTagNode name="@return">
    <ReturnTagNode description="">
      <IdentifierTypeNode name="Hello"/>
    </ReturnTagNode>
  </PhpDocTagNode>
</PhpDocNode>

Tokens would could look like <Token type="IDENTIFIER">Fully\Qualfiied\Name</token> and would be children of the nodes above. The text value of the document would be docblock with it's format preserved.

That's what I'm working on anyway -- the nice thing is that you can import that XML into an XML of the PHP AST, and modify the whole source tree in one place.

If it's interesting the PHP AST looks like this curently, work-in-progress (based on the Tolerant Parser AST):

<Ast>
  <SourceFileNode>
    <InlineHtml>
      <Token kind="ScriptSectionStartTag" fullStart="0" start="0" length="6">&lt;?php
</Token>
    </InlineHtml>
    <ClassDeclaration>
      <Preamble>
/**
 * @author Daniel
 */
</Preamble>
      <Token kind="ClassKeyword" fullStart="6" start="33" length="32">class</Token>
      <Preamble> </Preamble>
      <Token kind="Name" fullStart="38" start="39" length="13">ExampleClass</Token>
      <ClassMembersNode>
        <Preamble>
</Preamble>
        <Token kind="OpenBraceToken" fullStart="51" start="52" length="2">{</Token>
        <Preamble>
</Preamble>
        <Token kind="CloseBraceToken" fullStart="53" start="54" length="2">}</Token>
      </ClassMembersNode>
    </ClassDeclaration>
    <Preamble>
</Preamble>
    <Token kind="EndOfFileToken" fullStart="55" start="56" length="1"></Token>
  </SourceFileNode>
</Ast>

@github-actions
Copy link

github-actions bot commented May 1, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants