tolerant-php-parser icon indicating copy to clipboard operation
tolerant-php-parser copied to clipboard

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

Open TysonAndre opened this issue 8 years ago • 1 comments
trafficstars

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

TysonAndre avatar Sep 19 '17 03:09 TysonAndre

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

roblourens avatar Sep 19 '17 03:09 roblourens