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

Add support for PHP 7 #3107

Merged
merged 8 commits into from
Nov 29, 2016
Merged

Add support for PHP 7 #3107

merged 8 commits into from
Nov 29, 2016

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Oct 1, 2016

Update the parser tables for PHP 7, add lexer support for new tokens. This includes support for PHP 7.1.

I have removed the AST node definitions and emptied the semantic actions of the parser. ACE does not use the AST and porting the semantic actions to JS would be way too much work.

For future reference, this is the rebuildParser.php I used:

<?php

const GRAMMAR_FILE = './php7.y';
const TMP_FILE     = './tmp_parser.jsy';
const RESULT_FILE  = './tmp_parser.js';
const TOKENS_FILE  = './tokens.y';

$tokens = file_get_contents(TOKENS_FILE);
$grammarCode = file_get_contents(GRAMMAR_FILE);
$grammarCode = str_replace('%tokens', $tokens, $grammarCode);

file_put_contents(TMP_FILE, $grammarCode);

echo 'Building parser. Output: "',
     trim(shell_exec('kmyacc -l -m kmyacc.js.parser ' . TMP_FILE . ' 2>&1')),
     '"', "\n";

preg_match_all('/T_\w+/', $tokens, $matches);
foreach ($matches[0] as $match) {
    echo '"' . $match . '",';
}

The skeleton file is https://github.com/niklasvh/php.js/blob/master/grammar/kmyacc.js.parser. The grammar and token files are from https://github.com/nikic/PHP-Parser/tree/master/grammar.

nikic added 5 commits October 1, 2016 21:02
Drop contents of semantics actions. ACE does not use the generated
AST and updating this would be too much work.
@nikic nikic mentioned this pull request Oct 1, 2016
@SjonHortensius
Copy link
Contributor

SjonHortensius commented Oct 2, 2016

I noticed a discrepancy on https://3v4l.org/mo9V4 where PHP accepts a syntax and the worker doesn't:

$obj = (Factory::Generate('MyObj'))($someData);

throws Syntax error, unexpected ???

Same thing happens on https://3v4l.org/WThln

var_dump( DateTimeZone::listIdentifiers());

throws Syntax error, unexpected ???, expecting ',' or ')'

@adamjimenez
Copy link
Contributor

I found some other code it doesn't like:

$cb = \Codebird\Codebird::getInstance();

Drop special handling of T_DOUBLE_COLON, always use
T_PAAMAYIM_NEKUDOTAYIM instead.
@nikic
Copy link
Contributor Author

nikic commented Oct 2, 2016

@SjonHortensius Thanks for testing! Handling of :: should be fixed now.

The issue was that the lexer used the T_DOUBLE_COLON alias of T_PAAMAYIM_NEKUDOTAYIM, which was lost during the update. I've now dropped the alias entirely in the interest of simplifying future updates.

@SjonHortensius
Copy link
Contributor

Thanks; that fixes all three indeed 👍

@adamjimenez
Copy link
Contributor

Thanks, this is working great so far.

@adamjimenez
Copy link
Contributor

adamjimenez commented Oct 2, 2016

Seeing as you're on a roll, here's something else that throws an error when it shouldn't:

print "$_SERVER[DOCUMENT_ROOT]";

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

These are also still bust:
niklasvh/php.js#55
niklasvh/php.js#46

@SjonHortensius
Copy link
Contributor

var_dump(`ls`);

throws Syntax error, unexpected $EOF.

Maybe these should be reported as separate issues btw; it is not caused by this PR. This PR fixes 99% of all php7 issues that were in the previous syntax-checker.

@nikic
Copy link
Contributor Author

nikic commented Oct 3, 2016

I started working on a lexer update that should fix these issues, but it will take a while. I'll send a separate PR.

Drop semantic actions entirely instead of just emptying them.
@nikic
Copy link
Contributor Author

nikic commented Oct 16, 2016

Forgot to link, new lexer is at https://gist.github.com/nikic/91ee0bc7ac74659366a8ae90b8f2d15e.

@SjonHortensius
Copy link
Contributor

Not sure if (or why not) you committed this on a branch somewhere but I manually inserted your lexer in the existing php.js and it seems to work fine. It does fix indeed all mentioned issues 👍

@SjonHortensius
Copy link
Contributor

@nikic I think you should include the Lexer changes in this PR as well. Also; I guess you'll need to sign the CLA here and then this should be ready to be pulled and included in a release

Can you confirm @nightwing ?

@nikic
Copy link
Contributor Author

nikic commented Oct 20, 2016

Pulled in the lexer updates, this fixes the issues above as well as #2232, #2285. The lexer is modeled after PHP's own lexer and tested against token_get_all() for a wide range of inputs -- some unimportant distinctions (LNUMBER/DNUMBER, COMMENT/DOC_COMMENT) and certain error behavior notwithstanding.

@nikic
Copy link
Contributor Author

nikic commented Nov 16, 2016

Any news here?

@plugowski
Copy link

Will be nice if that pull request will be merged into ajaxorg/ace :)

@nightwing
Copy link
Member

Thanks for awesome work! Merging.

@nightwing nightwing merged commit 5b52446 into ajaxorg:master Nov 29, 2016
@nikic
Copy link
Contributor Author

nikic commented Nov 30, 2016

Thanks! The issues #2908, #2285 and #2232 can now be closed as well.

SjonHortensius added a commit to SjonHortensius/ace that referenced this pull request Oct 4, 2019
this repeats ajaxorg#3107, but for 7.4, instead of the kmyacc binary
I used https://github.com/ircmaxell/PHP-Yacc
SjonHortensius added a commit to SjonHortensius/3v4l_org that referenced this pull request Nov 27, 2019
SjonHortensius added a commit to SjonHortensius/ace that referenced this pull request May 29, 2020
this repeats ajaxorg#3107, but for 7.4, instead of the kmyacc binary
I used https://github.com/ircmaxell/PHP-Yacc
SjonHortensius added a commit to SjonHortensius/ace that referenced this pull request Jun 8, 2020
this repeats ajaxorg#3107, but for 7.4, instead of the kmyacc binary
I used https://github.com/ircmaxell/PHP-Yacc

here is the rebuildParser that I used:

```
<?php

const GRAMMAR_REPO  = 'https://raw.githubusercontent.com/nikic/PHP-Parser';
const PARSER_REPO  = 'https://raw.githubusercontent.com/niklasvh/php.js';

const GRAMMAR_FILE = GRAMMAR_REPO .'/master/grammar/php7.y';
const TOKENS_FILE  = GRAMMAR_REPO .'/master/grammar/tokens.y';
const TMP_FILE     = './tmp_parser.jsy';
const RESULT_FILE  = './tmp_parser.js';

file_put_contents('kmyacc.js.parser', file_get_contents(PARSER_REPO .'/master/grammar/kmyacc.js.parser'));

$tokens = file_get_contents(TOKENS_FILE);
$grammarCode = file_get_contents(GRAMMAR_FILE);
$grammarCode = str_replace('%tokens', $tokens, $grammarCode);

file_put_contents(TMP_FILE, $grammarCode);

echo 'Building parser. Output: "',
     trim(shell_exec('./PHP-Yacc/bin/phpyacc -m kmyacc.js.parser ' . TMP_FILE . ' 2>&1')),
     '"', "\n";

preg_match_all('/T_\w+/', $tokens, $matches);
foreach ($matches[0] as $match)
    echo '"' . $match . '",';

echo "\n". str_repeat('-', 45). "\n";
foreach ($matches[0] as $match)
        if (NULL != constant($match))
            echo 'PHP.Constants.' . $match . ' = ' . constant($match) . "\n";
```
SjonHortensius added a commit to SjonHortensius/ace that referenced this pull request Jun 8, 2020
this repeats ajaxorg#3107, but for 7.4, instead of the kmyacc binary
I used https://github.com/ircmaxell/PHP-Yacc

here is the rebuildParser that I used:

```
<?php

const GRAMMAR_REPO  = 'https://raw.githubusercontent.com/nikic/PHP-Parser';
const PARSER_REPO  = 'https://raw.githubusercontent.com/niklasvh/php.js';

const GRAMMAR_FILE = GRAMMAR_REPO .'/master/grammar/php7.y';
const TOKENS_FILE  = GRAMMAR_REPO .'/master/grammar/tokens.y';
const TMP_FILE     = './tmp_parser.jsy';
const RESULT_FILE  = './tmp_parser.js';

file_put_contents('kmyacc.js.parser', file_get_contents(PARSER_REPO .'/master/grammar/kmyacc.js.parser'));

$tokens = file_get_contents(TOKENS_FILE);
$grammarCode = file_get_contents(GRAMMAR_FILE);
$grammarCode = str_replace('%tokens', $tokens, $grammarCode);

file_put_contents(TMP_FILE, $grammarCode);

echo 'Building parser. Output: "',
     trim(shell_exec('./PHP-Yacc/bin/phpyacc -m kmyacc.js.parser ' . TMP_FILE . ' 2>&1')),
     '"', "\n";

preg_match_all('/T_\w+/', $tokens, $matches);
foreach ($matches[0] as $match)
    echo '"' . $match . '",';

echo "\n". str_repeat('-', 45). "\n";
foreach ($matches[0] as $match)
        if (NULL != constant($match))
            echo 'PHP.Constants.' . $match . ' = ' . constant($match) . "\n";
```
SjonHortensius added a commit to SjonHortensius/3v4l_org that referenced this pull request Oct 26, 2022
SjonHortensius added a commit to SjonHortensius/3v4l_org that referenced this pull request Dec 1, 2022
SjonHortensius added a commit to SjonHortensius/3v4l_org that referenced this pull request Dec 1, 2022
SjonHortensius added a commit to SjonHortensius/3v4l_org that referenced this pull request Dec 2, 2022
SjonHortensius added a commit to SjonHortensius/3v4l_org that referenced this pull request Jan 25, 2023
SjonHortensius added a commit to SjonHortensius/3v4l_org that referenced this pull request Feb 1, 2023
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.

5 participants