Skip to content

Parse template strings correctly - #168

Merged
roblourens merged 5 commits into
masterfrom
roblou/fixTemplateStringParse
Aug 11, 2017
Merged

roblourens merged 5 commits into
masterfrom
roblou/fixTemplateStringParse

Conversation

@roblourens

@roblourens roblourens commented Aug 10, 2017 •

Copy link
Copy Markdown
Member

Fixes #12

Removes the old template string implementation, adds a new one.

There's probably more that could be done to make the string representation more sophisticated, per #11, but I'm not sure what that should be. This is simple and works quite well.

@roblourens
roblourens merged commit 1fd9de1 into master Aug 11, 2017
Comment thread src/Parser.php
$postfixExpression->parent = $subscriptExpression;

$subscriptExpression->postfixExpression = $postfixExpression;
$subscriptExpression->openBracketOrBrace = $this->eat(TokenKind::OpenBracketToken); // Only [] syntax is supported, not {}

@jens1o Jens Hausdorf (jens1o) Aug 11, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with the comment?

<?php
echo "{$myFancyArray[5]}"

is valid too, and will output the sixth element of the array called myFancyArray,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, "$a[0]" works, but "$a{0}" doesn't.

@jens1o Jens Hausdorf (jens1o) Aug 11, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay. Now I got that. Thanks!

Comment thread src/TokenKind.php
const TemplateStringEnd = 313;
const NoSubstitutionTemplateLiteral = 314;
const UnterminatedNoSubstitutionTemplateLiteral = 315;
const UnterminatedTemplateStringEnd = 316;

@jens1o Jens Hausdorf (jens1o) Aug 11, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaking changes here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were never really used.

if (
$this->name instanceof Token &&
$name = substr($this->name->getText($this->getFileContents()), 1)
$name = ltrim($this->name->getText($this->getFileContents()), '$')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this trims all $ from the left - what about variable variables? e.g. $$var

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getName wouldn't work on $$var, because of the check above - name is a Variable node here. You would have to use $var->name->getName().

I made this change for "${x}". x is a variable that doesn't have a dollar sign, so I only want to remove a dollar sign if it exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants