Page MenuHomePhabricator

Improve parsing of variables embedded in strings in XHPAST
Open, WishlistPublic

Description

It seems that XHPAST doesn't properly parse strings contains interpolated variables. Specifically, "This string contains no variables" and "This string contains $variables" are both parsed as n_STRING_SCALAR. See https://secure.phabricator.com/xhpast/view/729/ for example... tokenizing this same file with token_get_all produces the following:

array (
  0 => 
  array (
    0 => 374,
    1 => '<?php ',
    2 => 1,
  ),
  1 => 
  array (
    0 => 377,
    1 => '

',
    2 => 1,
  ),
  2 => 
  array (
    0 => 317,
    1 => 'echo',
    2 => 3,
  ),
  3 => 
  array (
    0 => 377,
    1 => ' ',
    2 => 3,
  ),
  4 => 
  array (
    0 => 316,
    1 => '"This string contains no variables."',
    2 => 3,
  ),
  5 => ';',
  6 => 
  array (
    0 => 377,
    1 => '
',
    2 => 3,
  ),
  7 => 
  array (
    0 => 317,
    1 => 'echo',
    2 => 4,
  ),
  8 => 
  array (
    0 => 377,
    1 => ' ',
    2 => 4,
  ),
  9 => '"',
  10 => 
  array (
    0 => 315,
    1 => 'This string contains ',
    2 => 4,
  ),
  11 => 
  array (
    0 => 310,
    1 => '$variables',
    2 => 4,
  ),
  12 => 
  array (
    0 => 315,
    1 => '.',
    2 => 4,
  ),
  13 => '"',
  14 => ';',
)

More succinctly:

// "This string contains no variables."
array(
  array(
    0 => T_CONSTANT_ENCAPSED_STRING,
    1 => '"This string contains no variables."',
    2 => 3,
  ),
);

// "This string contains $variables."
array(
  '"',
  array(
    0 => T_ENCAPSED_AND_WHITESPACE,
    1 => 'This string contains ',
    2 => 4,
  ),
  array(
    0 => T_VARIABLE,
    1 => '$variables',
    2 => 4,
  ),
  array(
    0 => T_ENCAPSED_AND_WHITESPACE,
    1 => '.',
    2 => 4,
  ),
  '"',
);