Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / nikic / php-parser / doc / component / Lexer.markdown
index b22942dd1673e96924d6ca20f14e1d8491b494b2..be26e381e5d7685448fada9bdc3aef3e48665fbf 100644 (file)
@@ -27,18 +27,23 @@ The attributes used in this example match the default behavior of the lexer. The
 
  * `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:
@@ -72,7 +77,7 @@ $lexer = new PhpParser\Lexer(array(
         '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();
@@ -95,14 +100,16 @@ Lexer extension
 
 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.