nikic/php-ast

Is it possible to expose the line numbers associated with a scalar?

Closed this issue · 2 comments

Example: In Phan, I would want to know the line number of an invalid scalar (e.g. a string passed to a binary operator, an integer passed in a function call when something else is expected, a no-op scalar statement (<?php 2;))

Desired: Add a config flag (e.g. a bitmask or options array) to \ast\parse_code() that would make php-ast return an AST_SCALAR node wrapping the scalar value (e.g. new ast\Node(ast\AST_SCALAR, 0, lineno, 2.5) instead of 2.5)

  • E.g. ast\parse_code($code, 50, \ast\options\AST_NODE_FOR_SCALAR)

If the internal AST doesn't expose this information, feel free to close this.

nikic commented

This should be technically possible.

It looked like it was possible with ast_to_zval, but this seems like the resulting ast\Node would be hard to get existing code to work with. It also included strings that weren't string literals (e.g. variable names), so some extra handling would be required to avoid cases where this would never give extra line information.

Half of the things that need precise line numbers (e.g. method names) would also benefit from columns.