From cca54feae7d972534b96b8aa249289b4fbf70b0b Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Sat, 14 Apr 2018 10:09:21 +0100 Subject: [PATCH] basic renderable parser output --- bin/create-php-parser.js | 1 + blocks/api/post.pegjs | 34 +++++ lib/parser.php | 262 ++++++++++++++++++++++++++++++++++++--- webpack.config.js | 2 +- 4 files changed, 283 insertions(+), 16 deletions(-) diff --git a/bin/create-php-parser.js b/bin/create-php-parser.js index f5cd5327fd0008..cd3e77db790686 100755 --- a/bin/create-php-parser.js +++ b/bin/create-php-parser.js @@ -12,6 +12,7 @@ const parser = pegjs.generate( { plugins: [ phpegjs ], phpegjs: { + allowedStartRules: [ 'Block_List', 'Rendered_Output' ], parserNamespace: null, parserGlobalNamePrefix: 'Gutenberg_PEG_', mbstringAllowed: false, diff --git a/blocks/api/post.pegjs b/blocks/api/post.pegjs index 576970d10c3d21..4344e679300eeb 100644 --- a/blocks/api/post.pegjs +++ b/blocks/api/post.pegjs @@ -66,6 +66,18 @@ if ( ! function_exists( 'peg_array_partition' ) ) { } } +if ( ! function_exists( 'peg_is_dynamic_block' ) ) { + function peg_is_dynamic_block( $block_name ) { + static $dynamic_blocks = null; + + if ( null === $dynamic_blocks ) { + $dynamic_blocks = get_dynamic_block_names(); + } + + return in_array( $block_name, $dynamic_blocks ); + } +} + if ( ! function_exists( 'peg_join_blocks' ) ) { function peg_join_blocks( $pre, $tokens, $post ) { $blocks = array(); @@ -168,6 +180,28 @@ Block_List return joinBlocks( pre, ts, post ); } +Rendered_Output + = ts:Rendered_Token* { return ts.join( '' ) /** **/ } + +Rendered_Token + = Dynamic_Block + / Block_Start { return '' /** **/ } + / Block_End { return '' /** **/ } + / Block_Void { return '' /** **/ } + / . + +Dynamic_Block + = s:Block_Void + & { return false /** **/ } + { return '' /** **/ } + / s:Dynamic_Block_Start c:(!Block_End .)+ e:Block_End + { return '' /** **/ } + +Dynamic_Block_Start + = s:Block_Start + & { return false /** **/ } + { return s /** **/ } + Token = Block_Void / Block_Balanced diff --git a/lib/parser.php b/lib/parser.php index f69f53396ba829..be187c18a84b61 100644 --- a/lib/parser.php +++ b/lib/parser.php @@ -259,8 +259,14 @@ private function peg_buildException($message, $expected, $pos) { private function peg_f0($pre, $t, $html) { return array( $t, $html ); } private function peg_f1($pre, $ts, $post) { return peg_join_blocks( $pre, $ts, $post ); } - private function peg_f2($blockName, $a) { return $a; } - private function peg_f3($blockName, $attrs) { + private function peg_f2($ts) { return implode( '', $ts ); } + private function peg_f3() { return ''; } + private function peg_f4($s) { return peg_is_dynamic_block( $s['blockName'] ); } + private function peg_f5($s) { return 'dynamic'; } + private function peg_f6($s, $c, $e) { return 'dynamic'; } + private function peg_f7($s) { return $s; } + private function peg_f8($blockName, $a) { return $a; } + private function peg_f9($blockName, $attrs) { return array( 'blockName' => $blockName, 'attrs' => $attrs, @@ -268,7 +274,7 @@ private function peg_f3($blockName, $attrs) { 'innerHTML' => '', ); } - private function peg_f4($s, $children, $e) { + private function peg_f10($s, $children, $e) { list( $innerHTML, $innerBlocks ) = peg_array_partition( $children, 'is_string' ); return array( @@ -278,19 +284,19 @@ private function peg_f4($s, $children, $e) { 'innerHTML' => implode( '', $innerHTML ), ); } - private function peg_f5($blockName, $attrs) { + private function peg_f11($blockName, $attrs) { return array( 'blockName' => $blockName, 'attrs' => $attrs, ); } - private function peg_f6($blockName) { + private function peg_f12($blockName) { return array( 'blockName' => $blockName, ); } - private function peg_f7($type) { return "core/$type"; } - private function peg_f8($attrs) { return json_decode( $attrs, true ); } + private function peg_f13($type) { return "core/$type"; } + private function peg_f14($attrs) { return json_decode( $attrs, true ); } private function peg_parseBlock_List() { @@ -601,6 +607,220 @@ private function peg_parseBlock_List() { return $s0; } + private function peg_parseRendered_Output() { + + $s0 = $this->peg_currPos; + $s1 = array(); + $s2 = $this->peg_parseRendered_Token(); + while ($s2 !== $this->peg_FAILED) { + $s1[] = $s2; + $s2 = $this->peg_parseRendered_Token(); + } + if ($s1 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f2($s1); + } + $s0 = $s1; + + return $s0; + } + + private function peg_parseRendered_Token() { + + $s0 = $this->peg_parseDynamic_Block(); + if ($s0 === $this->peg_FAILED) { + $s0 = $this->peg_currPos; + $s1 = $this->peg_parseBlock_Start(); + if ($s1 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f3(); + } + $s0 = $s1; + if ($s0 === $this->peg_FAILED) { + $s0 = $this->peg_currPos; + $s1 = $this->peg_parseBlock_End(); + if ($s1 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f3(); + } + $s0 = $s1; + if ($s0 === $this->peg_FAILED) { + $s0 = $this->peg_currPos; + $s1 = $this->peg_parseBlock_Void(); + if ($s1 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f3(); + } + $s0 = $s1; + if ($s0 === $this->peg_FAILED) { + if ($this->input_length > $this->peg_currPos) { + $s0 = $this->input_substr($this->peg_currPos, 1); + $this->peg_currPos++; + } else { + $s0 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { + $this->peg_fail($this->peg_c0); + } + } + } + } + } + } + + return $s0; + } + + private function peg_parseDynamic_Block() { + + $s0 = $this->peg_currPos; + $s1 = $this->peg_parseBlock_Void(); + if ($s1 !== $this->peg_FAILED) { + $this->peg_reportedPos = $this->peg_currPos; + $s2 = $this->peg_f4($s1); + if ($s2) { + $s2 = null; + } else { + $s2 = $this->peg_FAILED; + } + if ($s2 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f5($s1); + $s0 = $s1; + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + if ($s0 === $this->peg_FAILED) { + $s0 = $this->peg_currPos; + $s1 = $this->peg_parseDynamic_Block_Start(); + if ($s1 !== $this->peg_FAILED) { + $s2 = array(); + $s3 = $this->peg_currPos; + $s4 = $this->peg_currPos; + $this->peg_silentFails++; + $s5 = $this->peg_parseBlock_End(); + $this->peg_silentFails--; + if ($s5 === $this->peg_FAILED) { + $s4 = null; + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + if ($s4 !== $this->peg_FAILED) { + if ($this->input_length > $this->peg_currPos) { + $s5 = $this->input_substr($this->peg_currPos, 1); + $this->peg_currPos++; + } else { + $s5 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { + $this->peg_fail($this->peg_c0); + } + } + if ($s5 !== $this->peg_FAILED) { + $s4 = array($s4, $s5); + $s3 = $s4; + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + if ($s3 !== $this->peg_FAILED) { + while ($s3 !== $this->peg_FAILED) { + $s2[] = $s3; + $s3 = $this->peg_currPos; + $s4 = $this->peg_currPos; + $this->peg_silentFails++; + $s5 = $this->peg_parseBlock_End(); + $this->peg_silentFails--; + if ($s5 === $this->peg_FAILED) { + $s4 = null; + } else { + $this->peg_currPos = $s4; + $s4 = $this->peg_FAILED; + } + if ($s4 !== $this->peg_FAILED) { + if ($this->input_length > $this->peg_currPos) { + $s5 = $this->input_substr($this->peg_currPos, 1); + $this->peg_currPos++; + } else { + $s5 = $this->peg_FAILED; + if ($this->peg_silentFails === 0) { + $this->peg_fail($this->peg_c0); + } + } + if ($s5 !== $this->peg_FAILED) { + $s4 = array($s4, $s5); + $s3 = $s4; + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s3; + $s3 = $this->peg_FAILED; + } + } + } else { + $s2 = $this->peg_FAILED; + } + if ($s2 !== $this->peg_FAILED) { + $s3 = $this->peg_parseBlock_End(); + if ($s3 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f6($s1, $s2, $s3); + $s0 = $s1; + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } + + return $s0; + } + + private function peg_parseDynamic_Block_Start() { + + $s0 = $this->peg_currPos; + $s1 = $this->peg_parseBlock_Start(); + if ($s1 !== $this->peg_FAILED) { + $this->peg_reportedPos = $this->peg_currPos; + $s2 = $this->peg_f4($s1); + if ($s2) { + $s2 = null; + } else { + $s2 = $this->peg_FAILED; + } + if ($s2 !== $this->peg_FAILED) { + $this->peg_reportedPos = $s0; + $s1 = $this->peg_f7($s1); + $s0 = $s1; + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + } else { + $this->peg_currPos = $s0; + $s0 = $this->peg_FAILED; + } + + return $s0; + } + private function peg_parseToken() { $s0 = $this->peg_parseBlock_Void(); @@ -673,7 +893,7 @@ private function peg_parseBlock_Void() { } if ($s8 !== $this->peg_FAILED) { $this->peg_reportedPos = $s6; - $s7 = $this->peg_f2($s4, $s7); + $s7 = $this->peg_f8($s4, $s7); $s6 = $s7; } else { $this->peg_currPos = $s6; @@ -698,7 +918,7 @@ private function peg_parseBlock_Void() { } if ($s7 !== $this->peg_FAILED) { $this->peg_reportedPos = $s0; - $s1 = $this->peg_f3($s4, $s6); + $s1 = $this->peg_f9($s4, $s6); $s0 = $s1; } else { $this->peg_currPos = $s0; @@ -827,7 +1047,7 @@ private function peg_parseBlock_Balanced() { $s3 = $this->peg_parseBlock_End(); if ($s3 !== $this->peg_FAILED) { $this->peg_reportedPos = $s0; - $s1 = $this->peg_f4($s1, $s2, $s3); + $s1 = $this->peg_f10($s1, $s2, $s3); $s0 = $s1; } else { $this->peg_currPos = $s0; @@ -907,7 +1127,7 @@ private function peg_parseBlock_Start() { } if ($s8 !== $this->peg_FAILED) { $this->peg_reportedPos = $s6; - $s7 = $this->peg_f2($s4, $s7); + $s7 = $this->peg_f8($s4, $s7); $s6 = $s7; } else { $this->peg_currPos = $s6; @@ -932,7 +1152,7 @@ private function peg_parseBlock_Start() { } if ($s7 !== $this->peg_FAILED) { $this->peg_reportedPos = $s0; - $s1 = $this->peg_f5($s4, $s6); + $s1 = $this->peg_f11($s4, $s6); $s0 = $s1; } else { $this->peg_currPos = $s0; @@ -1024,7 +1244,7 @@ private function peg_parseBlock_End() { } if ($s6 !== $this->peg_FAILED) { $this->peg_reportedPos = $s0; - $s1 = $this->peg_f6($s4); + $s1 = $this->peg_f12($s4); $s0 = $s1; } else { $this->peg_currPos = $s0; @@ -1117,7 +1337,7 @@ private function peg_parseCore_Block_Name() { } if ($s1 !== $this->peg_FAILED) { $this->peg_reportedPos = $s0; - $s1 = $this->peg_f7($s1); + $s1 = $this->peg_f13($s1); } $s0 = $s1; @@ -1431,7 +1651,7 @@ private function peg_parseBlock_Attributes() { } if ($s1 !== $this->peg_FAILED) { $this->peg_reportedPos = $s0; - $s1 = $this->peg_f8($s1); + $s1 = $this->peg_f14($s1); } $s0 = $s1; @@ -1588,6 +1808,18 @@ function peg_array_partition( $array, $predicate ) { } } + if ( ! function_exists( 'peg_is_dynamic_block' ) ) { + function peg_is_dynamic_block( $block_name ) { + static $dynamic_blocks = null; + + if ( null === $dynamic_blocks ) { + $dynamic_blocks = get_dynamic_block_names(); + } + + return in_array( $block_nam, $dynamic_blocks ); + } + } + if ( ! function_exists( 'peg_join_blocks' ) ) { function peg_join_blocks( $pre, $tokens, $post ) { $blocks = array(); diff --git a/webpack.config.js b/webpack.config.js index bf9b00b68afc2e..05df5d8abe5f03 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -144,7 +144,7 @@ const config = { rules: [ { test: /\.pegjs/, - use: 'pegjs-loader', + use: 'pegjs-loader?allowedStartRules[]=Block_List,allowedStartRules[]=Rendered_Output', }, { test: /\.js$/,