Php-parser: Better handle `<?php` and `?>` tags

Created on 20 Mar 2018  路  2Comments  路  Source: glayzzle/php-parser

We have problem when printing statement and inline nodes.
Example:

<?php echo 'test'; ?>
<?php
echo 'test;
echo 'test';
echo 'test';
?>

We need information when php tag opening and closing. Any idea how best we can get this information? Maybe add node.hasOpenTag and node.hasCloseTag (or maybe best names). It is allow output all tags as is.

<?php if (...): ?>
  <div>foo</div>
<?php endif; ?>

But, for control structure it is not good solution :disappointed:


Maybe you can export tags array as comment with position? I think it will be solve our problem :+1:

enhancement question AST

All 2 comments

I could provide an option in order to export tokens into an array, so it would be more generic. By joining all tokens you have exactly the same output as the original file.

The implementation is compliant with this function : http://php.net/manual/en/function.token-get-all.php

Hi @evilebottnawi,

You can try the new tokens extraction here - https://glayzzle.com/php-parser/

Here a sample code that has no nodes, but can be serialized with tokens :

The offsets on tokens are :

  • token name - if it's not defined, it's plain text node
  • text - the token inner text
  • line - the line (keeped from php get_tokens_all)
  • start offset
  • end offset

PHP Script :

<?php /** **/ ?>

AST Structure :

{
  "kind": "program",
  "children": [],
  "errors": [],
  "comments": [
    {
      "kind": "commentblock",
      "value": "/** **/"
    }
  ],
  "tokens": [
    [
      "T_OPEN_TAG",
      "<?php ",
      1,
      0,
      6
    ],
    [
      "T_DOC_COMMENT",
      "/** **/",
      1,
      6,
      13
    ],
    [
      "T_WHITESPACE",
      " ",
      1,
      13,
      14
    ],
    [
      "T_CLOSE_TAG",
      "?>",
      1,
      14,
      16
    ]
  ]
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nevadascout picture nevadascout  路  3Comments

alexander-akait picture alexander-akait  路  7Comments

Rivendall picture Rivendall  路  6Comments

mgrip picture mgrip  路  3Comments

alexander-akait picture alexander-akait  路  3Comments