com php-langspec: Add grammar regeneration tool: tools/grammar.php tools/util.php
[email protected] (Nikita Popov) Sat, 12 Nov 2016 12:26:17 +0000
| Newsgroups | php.standards.cvs |
|---|---|
| Message-ID | <[email protected]> |
Commit: c9f5fd0cdd19a1ebc1c0571662e85e0e2385b1c0=0AAuthor: Nikita Pop= ov <[email protected]> Sat, 12 Nov 2016 12:48:53 +0100=0AParents: 7cd= 2faa0f81851a7f8891834bb4dc069a1e4f277=0ABranches: master=0A=0ALink: = http://git.php.net/?p=3Dphp-langspec.git;a=3Dcommitdiff;h=3Dc9f5fd0cdd19a1e= bc1c0571662e85e0e2385b1c0=0A=0ALog:=0AAdd grammar regeneration tool=0A=0ACh= anged paths:=0A A tools/grammar.php=0A M tools/util.php=0A=0A=0ADiff:= =0Adiff --git a/tools/grammar.php b/tools/grammar.php=0Anew file mode 10064= 4=0Aindex 0000000..81e8c48=0A--- /dev/null=0A+++ b/tools/grammar.php=0A@@ -= 0,0 +1,68 @@=0A+<?php error_reporting(E_ALL);=0A+=0A+require __DIR__ . '/ut= il.php';=0A+=0A+$dir =3D __DIR__ . '/../spec/';=0A+$grammarFile =3D $dir . = '19-grammar.md';=0A+=0A+$output =3D <<<'END'=0A+#Grammar=0A+=0A+##General= =0A+=0A+The grammar notation is described in [Grammars section](09-lexical-= structure.md#grammars).=0A+=0A+##Lexical Grammar=0A+=0A+=0A+END;=0A+=0A+$le= xical =3D file_get_contents($dir . '09-lexical-structure.md');=0A+$lexical = =3D strstr($lexical, '##Lexical analysis');=0A+$output .=3D extract_grammar= ($lexical);=0A+=0A+$output .=3D "\n\n##Syntactic Grammar";=0A+=0A+foreach (= spec_files() as $fileName =3D> $path) {=0A+ if ($fileName =3D=3D=3D '05-= types.md' || $fileName =3D=3D=3D '09-lexical-structure.md') {=0A+ co= ntinue;=0A+ }=0A+=0A+ $code =3D file_get_contents($path);=0A+ $gra= mmar =3D extract_grammar($code);=0A+ if (null =3D=3D=3D $grammar) {=0A+ = continue;=0A+ }=0A+=0A+ $heading =3D extract_heading($code);= =0A+ $output .=3D "\n\n###$heading\n\n" . $grammar;=0A+}=0A+=0A+$output = .=3D "\n";=0A+=0A+file_put_contents($grammarFile, $output);=0A+=0A+function= extract_heading($code) {=0A+ if (!preg_match('/#\s*(.*)/', $code, $matc= hes)) {=0A+ throw new Exception('No heading found');=0A+ }=0A+=0A= + return $matches[1];=0A+}=0A+=0A+function extract_grammar($code) {=0A+ = if (!preg_match_all('(<pre>(.*?)</pre>)s', $code, $matches)) {=0A+ = return null;=0A+ }=0A+=0A+ $parts =3D [];=0A+ foreach ($matches[= 1] as $match) {=0A+ if (!preg_match('/^\s*<i>.*:.*<\/i>/', $match)) = {=0A+ continue;=0A+ }=0A+ $parts[] =3D ' ' . trim= ($match);=0A+ }=0A+=0A+ $rawGrammar =3D implode("\n\n", $parts);=0A+ = return "<pre>\n$rawGrammar\n</pre>";=0A+}=0Adiff --git a/tools/util.php = b/tools/util.php=0Aindex 52a8a99..2a5d426 100644=0A--- a/tools/util.php=0A+= ++ b/tools/util.php=0A@@ -9,7 +9,9 @@ function spec_files() {=0A if= (pathinfo($file, PATHINFO_EXTENSION) !=3D 'md') {=0A continue;= =0A }=0A- if ($file =3D=3D '00-specification-for-php.md' || = $file =3D=3D 'php-spec-draft.md') {=0A+ if ($file =3D=3D '00-specifi= cation-for-php.md'=0A+ || $file =3D=3D '19-grammar.md'=0A+ = || $file =3D=3D 'php-spec-draft.md') {=0A continu= e;=0A }=0A