Skip to content

Commit

Permalink
add String_::fromString() method by convention
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 15, 2022
1 parent 2f90510 commit 1cdfe02
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
6 changes: 2 additions & 4 deletions grammar/php5.y
Expand Up @@ -692,8 +692,7 @@ scalar_dereference:
| T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']'
{ $attrs = attributes();
$attrs['kind'] = strKind($1);
$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
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
8 changes: 8 additions & 0 deletions lib/PhpParser/Node/Scalar/String_.php
Expand Up @@ -42,6 +42,14 @@ public function getSubNodeNames() : array {
return ['value'];
}

public 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
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
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

0 comments on commit 1cdfe02

Please sign in to comment.