* `comments`: Array of `PhpParser\Comment` or `PhpParser\Comment\Doc` instances, representing all comments that occurred
between the previous non-discarded token and the current one. Use of this attribute is required for the
- `$node->getDocComment()` method to work. The attribute is also needed if you wish the pretty printer to retain
- comments present in the original code.
+ `$node->getComments()` and `$node->getDocComment()` methods to work. The attribute is also needed if you wish the pretty
+ printer to retain comments present in the original code.
* `startLine`: Line in which the node starts. This attribute is required for the `$node->getLine()` to work. It is also
required if syntax errors should contain line number information.
- * `endLine`: Line in which the node ends.
- * `startTokenPos`: Offset into the token array of the first token in the node.
- * `endTokenPos`: Offset into the token array of the last token in the node.
- * `startFilePos`: Offset into the code string of the first character that is part of the node.
- * `endFilePos`: Offset into the code string of the last character that is part of the node.
+ * `endLine`: Line in which the node ends. Required for `$node->getEndLine()`.
+ * `startTokenPos`: Offset into the token array of the first token in the node. Required for `$node->getStartTokenPos()`.
+ * `endTokenPos`: Offset into the token array of the last token in the node. Required for `$node->getEndTokenPos()`.
+ * `startFilePos`: Offset into the code string of the first character that is part of the node. Required for `$node->getStartFilePos()`.
+ * `endFilePos`: Offset into the code string of the last character that is part of the node. Required for `$node->getEndFilePos()`.
### Using token positions
+> **Note:** The example in this section is outdated in that this information is directly available in the AST: While
+> `$property->isPublic()` does not distinguish between `public` and `var`, directly checking `$property->flags` for
+> the `$property->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0` allows making this distinction without resorting to
+> tokens. However the general idea behind the example still applies in other cases.
+
The token offset information is useful if you wish to examine the exact formatting used for a node. For example the AST
does not distinguish whether a property was declared using `public` or using `var`, but you can retrieve this
information based on the token position:
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'
)
));
-$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer);
+$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::ONLY_PHP7, $lexer);
$visitor = new MyNodeVisitor();
$traverser = new PhpParser\NodeTraverser();
A lexer has to define the following public interface:
- void startLexing(string $code, ErrorHandler $errorHandler = null);
- array getTokens();
- string handleHaltCompiler();
- int getNextToken(string &$value = null, array &$startAttributes = null, array &$endAttributes = null);
+```php
+function startLexing(string $code, ErrorHandler $errorHandler = null): void;
+function getTokens(): array;
+function handleHaltCompiler(): string;
+function getNextToken(string &$value = null, array &$startAttributes = null, array &$endAttributes = null): int;
+```
-The `startLexing()` method is invoked with the source code that is to be lexed (including the opening tag) whenever the
-`parse()` method of the parser is called. It can be used to reset state or preprocess the source code or tokens. The
-passes `ErrorHandler` should be used to report lexing errors.
+The `startLexing()` method is invoked whenever the `parse()` method of the parser is called and is passed the source
+code that is to be lexed (including the opening tag). It can be used to reset state or preprocess the source code or tokens. The
+passed `ErrorHandler` should be used to report lexing errors.
The `getTokens()` method returns the current token array, in the usual `token_get_all()` format. This method is not
used by the parser (which uses `getNextToken()`), but is useful in combination with the token position attributes.