Skip to content

Add a utility and/or library to dump a compact representation of a node? #182

Open

Description

E.g. to tools or bin

This will help people who are developing libraries using this parser, as well as trying to debug the behavior on a given file/snippet.

I didn't see any similar issues mentioned elsewhere in the issues, readme, or tools directory.

Currently, I'm using this snippet: https://gist.github.com/TysonAndre/3c0619efc9695ef9f2465f4a55e6e6ee
Previously, I was using var_export(), which was several times longer.

» php dump.php "const X = 2*3+1;"
Node\SourceFileNode
    statementList: Node\Statement\ConstDeclaration
        constKeyword: Token: ConstKeyword(111): "const"
        constElements: Node\DelimitedList\ConstElementList
            children: Node\ConstElement
                name: Token: Name(2): " X"
                equalsToken: Token: EqualsToken(238): " ="
                assignment: Node\Expression\BinaryExpression
                    leftOperand: Node\Expression\BinaryExpression
                        leftOperand: Node\NumericLiteral
                            children: Token: IntegerLiteralToken(416): " 2"
                        operator: Token: AsteriskToken(212): "*"
                        rightOperand: Node\NumericLiteral
                            children: Token: IntegerLiteralToken(416): "3"
                    operator: Token: PlusToken(213): "+"
                    rightOperand: Node\NumericLiteral
                        children: Token: IntegerLiteralToken(416): "1"
        semicolon: Token: SemicolonToken(237): ";"
    endOfFileToken: Token: EndOfFileToken(1): ""

EDIT: I've moved that snippet to https://github.com/phan/phan/blob/master/internal/dump_fallback_ast.php

Activity

  1. roblourens commented on Sep 19, 2017

    @roblourens
    Member

    Sort of related to what the syntax-visualizer does: https://github.com/Microsoft/tolerant-php-parser/tree/master/syntax-visualizer which is just running the parsed file through json_encode

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions