如何编写php解析器_一个用PHP编写的PHP解析器

This is a new major version of the PHP-Parser library. The biggest new feature in this release is an experimental pretty-printing mode, which preserves formatting for parts of the code which have not been modified. As such, it should now be much easier to use PHP-Parser for automated code refactorings. See the pretty printer documentation for information on how to use this functionality

The following changelog lists all significant changes relative to PHP-Parser 3.1. For more information on backwards incompatible changes, please see the upgrading guide.

Added

Added experimental support for format-preserving pretty-printing. In this mode formatting will be preserved for parts of the code which have not been modified.

Added replaceNodes option to NameResolver, defaulting to true. If this option is disabled, resolved names will be added as resolvedName attributes, instead of replacing the original names.

Added NodeFinder class, which can be used to find nodes based on a callback or class name. This is a utility to avoid custom node visitor implementations for simple search operations.

Added ClassMethod::isMagic() method.

Added BuilderFactory methods: val() method for creating an AST for a simple value, concat() for creating concatenation trees, args() for preparing function arguments.

Added NameContext class, which encapsulates the NameResolver logic independently of the actual AST traversal. This facilitates use in other context, such as class names in doc comments. Additionally it provides an API for getting the shortest representation of a name.

Added Node::setAttributes() method.

Added JsonDecoder. This allows conversion JSON back into an AST.

Added Name methods toLowerString() and isSpecialClassName().

Added Identifier and VarLikeIdentifier nodes, which are used in place of simple strings in many places.

Added getComments(), getStartLine(), getEndLine(), getStartTokenPos(), getEndTokenPos(), getStartFilePos() and getEndFilePos() methods to Node. These provide a more obvious access point for the already existing attributes of the same name.

Added ConstExprEvaluator to evaluate constant expressions to PHP values.

Added Expr\BinaryOp::getOperatorSigil(), returning + for Expr\BinaryOp\Plus, etc.

Added start token offsets to comments.

Changed

Many subnodes that previously held simple strings now use Identifier (or VarLikeIdentifier) nodes. Please see the UPGRADE-4.0 file for an exhaustive list of affected nodes and some notes on possible impact.

Expression statements (expr;) are now represented using a Stmt\Expression node. Previously these statements were directly represented as their constituent expression.

The name subnode of Param has been renamed to var and now contains a Variable rather than a plain string.

The name subnode of StaticVar has been renamed to var and now contains a Variable rather than a plain string.

The var subnode of ClosureUse now contains a Variable rather than a plain string.

The var subnode of Catch now contains a Variable rather than a plain string.

The alias subnode of UseUse is now null if no explicit alias is given. As such, use Foo\Bar and use Foo\Bar as Bar are now represented differently. The getAlias() method can be used to get the effective alias, even if it is not explicitly given.

Removed

Support for running on PHP 5 and HHVM has been removed. You can however still parse code of old PHP versions (such as PHP 5.2), while running on PHP 7.

Removed type subnode on Class, ClassMethod and Property nodes. Use flags instead.

The ClassConst::isStatic() method has been removed. Constants cannot have a static modifier.

The NodeTraverser no longer accepts false as a return value from a leaveNode() method. NodeTraverser::REMOVE_NODE should be returned instead.

The Node::setLine() method has been removed. If you really need to, you can use setAttribute() instead.

The misspelled Class_::VISIBILITY_MODIFER_MASK constant has been dropped in favor of Class_::VISIBILITY_MODIFIER_MASK.

The XML serializer has been removed. As such, the classes Serializer\XML, and Unserializer\XML, as well as the interfaces Serializer and Unserializer no longer exist.

The BuilderAbstract class has been removed. It's functionality is moved into BuilderHelpers. However, this is an internal class and should not be used directly.

The Autoloader class has been removed. It is now required to use the Composer autoloader.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个早期的 PHP 解析,相当于实现了 PHPPHP 脚本的解析。示例代码:<?php // Autoload required classes require "vendor/autoload.php"; // Instantiate new parser instance $parser = new PhpParser\Parser(); // Return and print an AST from string contents $astNode = $parser->parseSourceFile('<?php /* comment */ echo "hi!"'); var_dump($astNode); // Gets and prints errors from AST Node. The parser handles errors gracefully, // so it can be used in IDE usage scenarios (where code is often incomplete). $errors = PhpParser\Utilities::getDiagnostics($astNode); var_dump(iterator_to_array($errors)); // Traverse all Node descendants of $astNode foreach ($astNode->getDescendantNodes() as $descendant) {     if ($descendant instanceof \PhpParser\Node\StringLiteral) {         // Print the Node text (without whitespace or comments)         var_dump($descendant->getText());         // All Nodes link back to their parents, so it's easy to navigate the tree.         $grandParent = $descendant->getParent()->getParent();         var_dump($grandParent->getNodeKindName());         // The AST is fully-representative, and round-trippable to the original source.         // This enables consumers to build reliable formatting and refactoring tools.         var_dump($grandParent->getLeadingCommentAndWhitespaceText());     }     // In addition to retrieving all children or descendants of a Node,     // Nodes expose properties specific to the Node type.     if ($descendant instanceof \PhpParser\Node\Expression\EchoExpression) {         $echoKeywordStartPosition = $descendant->echoKeyword->getStartPosition();         // To cut down on memory consumption, positions are represented as a single integer          // index into the document, but their line and character positions are easily retrieved.         $lineCharacterPosition = \PhpParser\Utilities::getLineCharacterPositionFromPosition(             $echoKeywordStartPosition         );         echo "line: $lineCharacterPosition->line, character: $lineCharacterPosition->character";     } } 标签:Tolerant

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值