{"library":"phpegjs","title":"PHP PEG.js Plugin","description":"`phpegjs` is a plugin for the PEG.js parser generator that enables the generation of parsers in PHP. Currently in beta version 1.0.0-beta7, it extends PEG.js to output PHP source code based on a given grammar, allowing developers to define complex parsing logic using PEG.js's syntax and then deploy the resulting parser in a PHP environment. This library acts as a crucial bridge for projects requiring robust parsing capabilities in PHP without manually writing lexical analyzers and parsers. It maintains compatibility with various PHP versions, offering options for namespace management and `mbstring` extension usage. The project is a fork of `php-pegjs` and focuses on providing a stable PHP target for PEG.js grammars, making it a key tool for language processing, DSL implementation, or complex data format parsing within PHP applications. Its release cadence is tied to its beta status, with updates reflecting progress towards a stable 1.0 release.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install phpegjs"],"cli":null},"imports":["const pegjs = require('pegjs');","const phpegjs = require('phpegjs');","pegjs.buildParser(grammar, { plugins: [phpegjs], phpegjs: { /* plugin options */ } });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const pegjs = require(\"pegjs\");\nconst phpegjs = require(\"phpegjs\");\n\nconst grammar = `\nstart = expression\n\nexpression\n  = left:term op:(\"+\"|\"-\") right:expression { return { type: \"binary\", op, left, right }; }\n  / term\n\nterm\n  = left:factor op:(\"*\"|\"/\") right:term { return { type: \"binary\", op, left, right }; }\n  / factor\n\nfactor\n  = number\n  / \"(\" expression:expression \")\" { return expression; }\n\nnumber \"number\"\n  = [0-9]+ { return parseInt(text(), 10); }\n`;\n\ntry {\n    const phpParserSource = pegjs.buildParser(grammar, {\n        plugins: [phpegjs],\n        cache: true, // Recommended for larger grammars\n        phpegjs: {\n            parserNamespace: 'MyNamespace',\n            parserClassName: 'SimpleCalculator'\n        }\n    });\n\n    console.log(\"Generated PHP Parser Source (first 200 chars):\\n\", phpParserSource.substring(0, 200) + '...');\n\n    // In a real application, you would save phpParserSource to a file,\n    // e.g., './src/MyNamespace/SimpleCalculator.php'\n\n    // Example PHP usage (conceptual):\n    // -----------------------------------------------------\n    // File: my_parser_app.php\n    // <?php\n    // require_once __DIR__ . '/src/MyNamespace/SimpleCalculator.php';\n\n    // try {\n    //     $parser = new MyNamespace\\\\SimpleCalculator();\n    //     $input = \"1 + 2 * (3 - 1)\";\n    //     $result = $parser->parse($input);\n    //     echo \"Input: \" . $input . \"\\n\";\n    //     echo \"Result (AST): \" . json_encode($result, JSON_PRETTY_PRINT) . \"\\n\";\n    // } catch (MyNamespace\\\\SyntaxError $ex) {\n    //     echo \"Syntax error: \" . $ex->getMessage() . \n    //          ' at line ' . $ex->grammarLine . \n    //          ' column ' . $ex->grammarColumn . \n    //          ' offset ' . $ex->grammarOffset . \"\\n\";\n    // }\n    // ?>\n    // -----------------------------------------------------\n\n} catch (e) {\n    console.error(\"Error building parser:\", e.message);\n}","lang":"javascript","description":"Demonstrates how to use `phpegjs` with PEG.js to define a simple arithmetic grammar, generate the corresponding PHP parser code, and provides a conceptual example of how to instantiate and use the generated parser in a PHP application.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}