Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[String_] Add rawValue attribute #831

Merged
merged 6 commits into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add String_::fromString() method by convention
  • Loading branch information
TomasVotruba committed May 15, 2022
commit 8c3d7902c7f6bf968b5673acbcd23b1aef20c760
6 changes: 2 additions & 4 deletions grammar/php5.y
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,7 @@ scalar_dereference:
| T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']'
{ $attrs = attributes();
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved
$attrs['kind'] = strKind($1);
nikic marked this conversation as resolved.
Show resolved Hide resolved
$attrs['rawValue'] = $1;
$$ = Expr\ArrayDimFetch[new Scalar\String_(Scalar\String_::parse($1), $attrs), $3]; }
$$ = Expr\ArrayDimFetch[Scalar\String_::fromString($1, $attrs), $3]; }
| constant '[' dim_offset ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
| scalar_dereference '[' dim_offset ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
/* alternative array syntax missing intentionally */
Expand Down Expand Up @@ -799,8 +798,7 @@ common_scalar:
| T_CONSTANT_ENCAPSED_STRING
{ $attrs = attributes();
$attrs['kind'] = strKind($1);
$attrs['rawValue'] = $1;
$$ = new Scalar\String_(Scalar\String_::parse($1, false), $attrs); }
$$ = Scalar\String_::fromString($1, $attrs, false); }
| T_LINE { $$ = Scalar\MagicConst\Line[]; }
| T_FILE { $$ = Scalar\MagicConst\File[]; }
| T_DIR { $$ = Scalar\MagicConst\Dir[]; }
Expand Down
3 changes: 1 addition & 2 deletions grammar/php7.y
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,7 @@ dereferencable_scalar:
| T_CONSTANT_ENCAPSED_STRING
{ $attrs = attributes();
$attrs['kind'] = strKind($1);
$attrs['rawValue'] = $1;
$$ = new Scalar\String_(Scalar\String_::parse($1), $attrs); }
$$ = Scalar\String_::fromString($1, $attrs); }
| '"' encaps_list '"'
{ $attrs = attributes(); $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
parseEncapsed($2, '"', true); $$ = new Scalar\Encapsed($2, $attrs); }
Expand Down
11 changes: 11 additions & 0 deletions lib/PhpParser/Node/Scalar/String_.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ public function getSubNodeNames() : array {
return ['value'];
}

/**
* @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
*/
public static function fromString(string $str, array $attributes = [], bool $parseUnicodeEscape = true): self
{
$attributes['rawValue'] = $str;
$string = self::parse($str, $parseUnicodeEscape);

return new self($string, $attributes);
}

/**
* @internal
*
Expand Down
6 changes: 2 additions & 4 deletions lib/PhpParser/Parser/Php5.php
Original file line number Diff line number Diff line change
Expand Up @@ -2149,8 +2149,7 @@ protected function initReduceCallbacks() {
392 => function ($stackPos) {
$attrs = $this->startAttributeStack[$stackPos-(4-1)] + $this->endAttributes;
$attrs['kind'] = ($this->semStack[$stackPos-(4-1)][0] === "'" || ($this->semStack[$stackPos-(4-1)][1] === "'" && ($this->semStack[$stackPos-(4-1)][0] === 'b' || $this->semStack[$stackPos-(4-1)][0] === 'B')) ? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED);
$attrs['rawValue'] = $this->semStack[$stackPos-(4-1)];
$this->semValue = new Expr\ArrayDimFetch(new Scalar\String_(Scalar\String_::parse($this->semStack[$stackPos-(4-1)]), $attrs), $this->semStack[$stackPos-(4-3)], $this->startAttributeStack[$stackPos-(4-1)] + $this->endAttributes);
$this->semValue = new Expr\ArrayDimFetch(Scalar\String_::fromString($this->semStack[$stackPos-(4-1)], $attrs), $this->semStack[$stackPos-(4-3)], $this->startAttributeStack[$stackPos-(4-1)] + $this->endAttributes);
},
393 => function ($stackPos) {
$this->semValue = new Expr\ArrayDimFetch($this->semStack[$stackPos-(4-1)], $this->semStack[$stackPos-(4-3)], $this->startAttributeStack[$stackPos-(4-1)] + $this->endAttributes);
Expand Down Expand Up @@ -2282,8 +2281,7 @@ protected function initReduceCallbacks() {
435 => function ($stackPos) {
$attrs = $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes;
$attrs['kind'] = ($this->semStack[$stackPos-(1-1)][0] === "'" || ($this->semStack[$stackPos-(1-1)][1] === "'" && ($this->semStack[$stackPos-(1-1)][0] === 'b' || $this->semStack[$stackPos-(1-1)][0] === 'B')) ? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED);
$attrs['rawValue'] = $this->semStack[$stackPos-(1-1)];
$this->semValue = new Scalar\String_(Scalar\String_::parse($this->semStack[$stackPos-(1-1)], false), $attrs);
$this->semValue = Scalar\String_::fromString($this->semStack[$stackPos-(1-1)], $attrs, false);
},
436 => function ($stackPos) {
$this->semValue = new Scalar\MagicConst\Line($this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
Expand Down
3 changes: 1 addition & 2 deletions lib/PhpParser/Parser/Php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2545,8 +2545,7 @@ protected function initReduceCallbacks() {
512 => function ($stackPos) {
$attrs = $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes;
$attrs['kind'] = ($this->semStack[$stackPos-(1-1)][0] === "'" || ($this->semStack[$stackPos-(1-1)][1] === "'" && ($this->semStack[$stackPos-(1-1)][0] === 'b' || $this->semStack[$stackPos-(1-1)][0] === 'B')) ? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED);
$attrs['rawValue'] = $this->semStack[$stackPos-(1-1)];
$this->semValue = new Scalar\String_(Scalar\String_::parse($this->semStack[$stackPos-(1-1)]), $attrs);
$this->semValue = Scalar\String_::fromString($this->semStack[$stackPos-(1-1)], $attrs);
},
513 => function ($stackPos) {
$attrs = $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes; $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
Expand Down