phptoast

2019-08-14 admin

phptoast是什么

什么是phptoast,PHP to AST parser

phptoast使用教程帮助文档

PHP-To-AST

Build Status

PHP-to-AST parser.

Defining syntax extensions

Sometimes it can be handy to extend PHP’s syntax with custom constructs. You can modify and extend PHPToAST’s built-in grammar with the options argument passed to .create(...).

For example:

var phpParser = require('phptoast').create(null, {
    rules: {
        'N_CUSTOM_TRAP_IT': {
            components: [/trap_it/, /@/, {name: 'arg', rule: 'N_EXPRESSION'}, /;/]
        },
        'N_NAMESPACE_SCOPED_STATEMENT': {
            components: {oneOf: ['N_CUSTOM_TRAP_IT', 'N_NAMESPACE_SCOPED_STATEMENT']}
        }
    }
});

console.log(JSON.stringify(phpParser.parse('<?php firstFunc(); trap_it @ 21; secondFunc();'), null, 4));
/**
 * Gives:
 * {
 *    "name": "N_PROGRAM",
 *    "statements": [
 *        {
 *            "name": "N_EXPRESSION_STATEMENT",
 *            "expression": {
 *                "name": "N_FUNCTION_CALL",
 *                "func": {
 *                    "name": "N_STRING",
 *                    "string": "firstFunc"
 *                },
 *                "args": []
 *            }
 *        },
 *        {
 *            "name": "N_CUSTOM_TRAP_IT",
 *            "arg": {
 *                "name": "N_INTEGER",
 *                "number": "21"
 *            }
 *        },
 *        {
 *            "name": "N_EXPRESSION_STATEMENT",
 *            "expression": {
 *                "name": "N_FUNCTION_CALL",
 *                "func": {
 *                    "name": "N_STRING",
 *                    "string": "secondFunc"
 *                },
 *                "args": []
 *            }
 *        }
 *    ]
 * }
 */

Keeping up to date

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-72473.html

文章标题:phptoast

回到顶部