-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 PHP parser based on PEG.js grammar #1152
Merged
+2,247
−77
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
753aab9
Introduce `php-pegjs`
nylen 2533fc8
Change php-pegjs to phpegjs (forked version)
nylen 01e661f
Add parser output to full-content test fixtures
nylen b6337f7
Run tests against PHP parser during Travis CI builds
nylen c88b9f4
Update parser fixture files after rebase
nylen 21c2ac3
Decode block JSON attributes as PHP arrays, not StdClass objects
nylen 15b9966
Remove error logging and clarify variable name
nylen e29c413
Upgrade `phpegjs` to 1.0.0-beta2
nylen 8eb22a7
Use a different method of skipping the PHP parser tests
nylen 8f8d649
Make phpcs happy again
nylen dcb968a
Upgrade `phpegjs` to 1.0.0-beta3
nylen dd0f205
Do not use namespaces in generated PHP parser
nylen eff5ee4
Add generated PHP parser to repository
nylen 4c4fe43
During Travis build, make sure PHP parser is up to date
nylen b950437
Fix a few test fixtures that needed to change after rebase
nylen ef05ef1
Modify the PHP parser (this should fail the build)
nylen 75d418f
Revert "Modify the PHP parser (this should fail the build)"
nylen f095297
Use PHP PEG parser in plugin
nylen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Run tests against PHP parser during Travis CI builds
Or using the following commands (TODO - add a npm script for this?): ``` bin/create-php-parser.js RUN_PARSER_TESTS=1 phpunit ```
- Loading branch information
commit b6337f702948a6247feb2b11a3d49d01163263fa
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* PEG.js parser (PHP code target) tests | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
class Parsing_Test extends WP_UnitTestCase { | ||
protected static $fixtures_dir; | ||
|
||
function parsing_test_filenames() { | ||
if ( ! getenv( 'RUN_PARSER_TESTS' ) ) { | ||
// Use the RUN_PARSER_TESTS environment variable to run these tests. | ||
return array(); | ||
} | ||
self::$fixtures_dir = dirname( dirname( __FILE__ ) ) . '/blocks/test/fixtures'; | ||
|
||
require_once dirname( dirname( __FILE__ ) ) . '/lib/parser.php'; | ||
|
||
$fixture_filenames = glob( self::$fixtures_dir . '/*.{json,html}', GLOB_BRACE ); | ||
$fixture_filenames = array_values( array_unique( array_map( | ||
array( $this, 'clean_fixture_filename' ), | ||
$fixture_filenames | ||
) ) ); | ||
|
||
return array_map( | ||
array( $this, 'pass_parser_fixture_filenames' ), | ||
$fixture_filenames | ||
); | ||
} | ||
|
||
function clean_fixture_filename( $filename ) { | ||
$filename = basename( $filename ); | ||
$filename = preg_replace( '/\..+$/', '', $filename ); | ||
return $filename; | ||
} | ||
|
||
function pass_parser_fixture_filenames( $filename ) { | ||
return array( | ||
"$filename.html", | ||
"$filename.parsed.json", | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider parsing_test_filenames | ||
*/ | ||
function test_parser_output( $html_filename, $parsed_json_filename ) { | ||
$html_filename = self::$fixtures_dir . '/' . $html_filename; | ||
$parsed_json_filename = self::$fixtures_dir . '/' . $parsed_json_filename; | ||
|
||
foreach ( array( $html_filename, $parsed_json_filename ) as $filename ) { | ||
if ( ! file_exists( $filename ) ) { | ||
throw new Exception( "Missing fixture file: '$filename'" ); | ||
} | ||
} | ||
|
||
$html = file_get_contents( $html_filename ); | ||
$parsed = json_decode( file_get_contents( $parsed_json_filename ), true ); | ||
|
||
$parser = new PhpPegJs\Parser; | ||
$result = $parser->parse( $html ); | ||
|
||
error_log( json_encode( compact( 'html', 'result' ) ) ); | ||
|
||
$this->assertEquals( $parsed, $result ); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be added to the NPM
build
anddev
scripts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think not yet - instead, we need to make sure we run our existing parsing tests against the PHP parser as well.