Debian Codegraph @

extensions/Math/maintenance/buildPHPparser.js

raw symbols SCIP
1 #!/usr/bin/env node
2 /**
3 * This script is used for generating Parser.php from parser.pegjs.
4 * This is used for creating new versions of the parser expression grammar
5 * in TexVC.
6 *
7 * Make sure to have the node dev dependencies from package.json installed.
8 * Run it with: "$node maintenance/buildPHPparser.js <optional params>"
9 *
10 * Parameters can be defined over CLI parameters or by changing the
11 * defaultPathXYZ constants in this file.
12 *
13 * @author Johannes Stegmüller
14 */
15
16 'use strict';
17
18 const { program } = require( 'commander' );
19 const peggy = require( 'peggy' );
20 const phpeggy = require( 'phpeggy' );
21 const fs = require( 'fs' );
22 const GENERATE_INTENT_PARSER = false;
23
24 let defaultPathInput = './src/WikiTexVC/parser.pegjs';
25 let defaultPathOutput = './src/WikiTexVC/Parser.php';
26 if ( GENERATE_INTENT_PARSER ) {
27 defaultPathInput = './src/WikiTexVC/parserintent.pegjs';
28 defaultPathOutput = './src/WikiTexVC/ParserIntent.php';
29 }
30
31 const PHP_INSERTION_LINE = 9; // indicates where the 'use_xyz' statements are inserted
32
33 program
34 .name( 'buildPHPparser' )
35 .option( '-i, --input <string>',
36 'path of input parser.pegjs file (*.pegjs)', defaultPathInput )
37 .option( '-o, --output <string>',
38 'path of generated output file (*.php)', defaultPathOutput )
39 .option( '-d, --debug',
40 'debug logging activated', false )
41 .description( 'Generates Parser.php as output from parser.pegjs as input. ' +
42 'This is used for for updating the parser expression grammar in WikiTexVC ' +
43 'which is located in src/WikiTexVC' )
44 .version( '0.1.0' );
45
46 program.parse();
47
48 const options = program.opts();
49 console.log( 'Running buildPHPparser.js with this configuration: \n' +
50 'input path:\t' + options.input + '\n' +
51 'output path:\t' + options.output );
52
53 // eslint-disable-next-line security/detect-non-literal-fs-filename
54 const parserPeg = fs.readFileSync( options.input, 'utf-8' );
55 let parser = peggy.generate( parserPeg, {
56 plugins: [ phpeggy ],
57 cache: true,
58 phpeggy: {
59 parserClassName: GENERATE_INTENT_PARSER ? 'ParserIntent' : 'Parser',
60 parserNamespace: 'MediaWiki\\Extension\\Math\\WikiTexVC'
61 }
62 } );
63
64 const useStatements =
65 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Box;\n' +
66 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Big;\n' +
67 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\ChemFun2u;\n' +
68 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\ChemWord;\n' +
69 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Declh;\n' +
70 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Dollar;\n' +
71 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\DQ;\n' +
72 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\FQ;\n' +
73 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Fun1;\n' +
74 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Fun1nb;\n' +
75 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Fun2;\n' +
76 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Fun2nb;\n' +
77 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Fun2sq;\n' +
78 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Fun4;\n' +
79 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Infix;\n' +
80 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Literal;\n' +
81 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Lr;\n' +
82 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\LengthSpec;\n' +
83 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Matrix;\n' +
84 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\Mhchem;\n' +
85 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\UQ;\n' +
86 'use MediaWiki\\Extension\\Math\\WikiTexVC\\Nodes\\TexArray;\n' +
87 'use MediaWiki\\Extension\\Math\\WikiTexVC\\TexUtil;\n' +
88 'use MediaWiki\\Extension\\Math\\WikiTexVC\\ParserUtil;';
89
90 function addUseStatements( p, lineStart = PHP_INSERTION_LINE ) {
91 // Adding the specified use statements
92 const splitParser = p.split( '\n' );
93 splitParser.splice( lineStart, 0, useStatements );
94 return splitParser.join( '\n' );
95 }
96
97 if ( !GENERATE_INTENT_PARSER ) {
98 parser = addUseStatements( parser );
99 }
100
101 /**
102 * Fixing phpeggy to denote regular expressions which
103 * are \x12 to \x{0012} so php can interpret them.
104 * can be removed when phpeggy is fixed, see:
105 * https://phabricator.wikimedia.org/T320964
106 */
107 const regexp = /\\x(\d\d)/g;
108 if ( options.debug ) {
109 const matches = parser.match( regexp );
110 for ( const match of matches ) {
111 console.log( `Found ${ match }.` );
112 }
113 }
114 parser = parser
115 .replace( regexp, '\\x{00$1}' )
116 // declare properties for the parser that were created dynamically before PHP 8.2
117 .replace( /class Parser \{/, 'class Parser {\n private $tu;\n private $options;' );
118
119 // eslint-disable-next-line security/detect-non-literal-fs-filename
120 fs.writeFileSync( options.output, parser );
121 console.log( 'Generated output file at: ' + options.output );