Clients may create instances of this class using {@link #newAST(int)}, but this class is not intended to be subclassed.
@see ASTParser @see ASTNode @since 2.0Clients may create instances of this class using {@link #newAST(int)}, but this class is not intended to be subclassed.
@see ASTParser @see ASTNode @since 2.0 @noinstantiate This class is not intended to be instantiated by clients.AST
instance serves as the common owner of any number of AST nodes, and as the factory for creating new AST nodes owned by that instance. Abstract syntax trees may be hand constructed by clients, using the newTYPE
factory methods to create new nodes, and the various setCHILD
methods (see {@link org.eclipse.php.internal.core.ast.nodes.ASTNode} and its subclasses)to connect them together.
Each AST node belongs to a unique AST instance, called the owning AST. The children of an AST node always have the same owner as their parent node. If a node from one AST is to be added to a different AST, the subtree must be cloned first to ensures that the added nodes have the correct owning AST.
There can be any number of AST nodes owned by a single AST instance that are unparented. Each of these nodes is the root of a separate little tree of nodes. The method ASTNode.getProgramRoot()
navigates from any node to the root of the tree that it is contained in. Ordinarily, an AST instance has one main tree (rooted at a Program
), with newly-created nodes appearing as additional roots until they are parented somewhere under the main tree. One can navigate from any node to its AST instance, but not conversely.
The class {@link ASTParser} parses a string containing a PHP source code andreturns an abstract syntax tree for it. The resulting nodes carry source ranges relating the node back to the original source characters.
Programs created by ASTParser
from a source document can be serialized after arbitrary modifications with minimal loss of original formatting. Here is an example:
Document doc = new Document("\n class X {} \n echo 'hello world';\n ?>"); ASTParser parser = ASTParser.newParser(AST.PHP5); parser.setSource(doc.get().toCharArray()); Program program = parser.createAST(null); program.recordModifications(); AST ast = program.getAST(); EchoStatement echo = ast.newEchoStatement(); echo.setExpression(ast.newScalar("hello world"); program.statements().add(echo); TextEdit edits = program.rewrite(document, null); UndoEdit undo = edits.apply(document);See also {@link ASTRewrite} for an alternative way to describe and serializechanges to a read-only AST.
Clients may create instances of this class using {@link #newAST(int)}, but this class is not intended to be subclassed.
@see ASTParser @see ASTNode @since 2.0
|
|