comparison vendor/nikic/php-parser/doc/component/Lexer.markdown @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
25 25
26 The attributes used in this example match the default behavior of the lexer. The following attributes are supported: 26 The attributes used in this example match the default behavior of the lexer. The following attributes are supported:
27 27
28 * `comments`: Array of `PhpParser\Comment` or `PhpParser\Comment\Doc` instances, representing all comments that occurred 28 * `comments`: Array of `PhpParser\Comment` or `PhpParser\Comment\Doc` instances, representing all comments that occurred
29 between the previous non-discarded token and the current one. Use of this attribute is required for the 29 between the previous non-discarded token and the current one. Use of this attribute is required for the
30 `$node->getDocComment()` method to work. The attribute is also needed if you wish the pretty printer to retain 30 `$node->getComments()` and `$node->getDocComment()` methods to work. The attribute is also needed if you wish the pretty
31 comments present in the original code. 31 printer to retain comments present in the original code.
32 * `startLine`: Line in which the node starts. This attribute is required for the `$node->getLine()` to work. It is also 32 * `startLine`: Line in which the node starts. This attribute is required for the `$node->getLine()` to work. It is also
33 required if syntax errors should contain line number information. 33 required if syntax errors should contain line number information.
34 * `endLine`: Line in which the node ends. 34 * `endLine`: Line in which the node ends. Required for `$node->getEndLine()`.
35 * `startTokenPos`: Offset into the token array of the first token in the node. 35 * `startTokenPos`: Offset into the token array of the first token in the node. Required for `$node->getStartTokenPos()`.
36 * `endTokenPos`: Offset into the token array of the last token in the node. 36 * `endTokenPos`: Offset into the token array of the last token in the node. Required for `$node->getEndTokenPos()`.
37 * `startFilePos`: Offset into the code string of the first character that is part of the node. 37 * `startFilePos`: Offset into the code string of the first character that is part of the node. Required for `$node->getStartFilePos()`.
38 * `endFilePos`: Offset into the code string of the last character that is part of the node. 38 * `endFilePos`: Offset into the code string of the last character that is part of the node. Required for `$node->getEndFilePos()`.
39 39
40 ### Using token positions 40 ### Using token positions
41
42 > **Note:** The example in this section is outdated in that this information is directly available in the AST: While
43 > `$property->isPublic()` does not distinguish between `public` and `var`, directly checking `$property->flags` for
44 > the `$property->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0` allows making this distinction without resorting to
45 > tokens. However the general idea behind the example still applies in other cases.
41 46
42 The token offset information is useful if you wish to examine the exact formatting used for a node. For example the AST 47 The token offset information is useful if you wish to examine the exact formatting used for a node. For example the AST
43 does not distinguish whether a property was declared using `public` or using `var`, but you can retrieve this 48 does not distinguish whether a property was declared using `public` or using `var`, but you can retrieve this
44 information based on the token position: 49 information based on the token position:
45 50
70 $lexer = new PhpParser\Lexer(array( 75 $lexer = new PhpParser\Lexer(array(
71 'usedAttributes' => array( 76 'usedAttributes' => array(
72 'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos' 77 'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos'
73 ) 78 )
74 )); 79 ));
75 $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer); 80 $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::ONLY_PHP7, $lexer);
76 81
77 $visitor = new MyNodeVisitor(); 82 $visitor = new MyNodeVisitor();
78 $traverser = new PhpParser\NodeTraverser(); 83 $traverser = new PhpParser\NodeTraverser();
79 $traverser->addVisitor($visitor); 84 $traverser->addVisitor($visitor);
80 85
93 Lexer extension 98 Lexer extension
94 --------------- 99 ---------------
95 100
96 A lexer has to define the following public interface: 101 A lexer has to define the following public interface:
97 102
98 void startLexing(string $code, ErrorHandler $errorHandler = null); 103 ```php
99 array getTokens(); 104 function startLexing(string $code, ErrorHandler $errorHandler = null): void;
100 string handleHaltCompiler(); 105 function getTokens(): array;
101 int getNextToken(string &$value = null, array &$startAttributes = null, array &$endAttributes = null); 106 function handleHaltCompiler(): string;
107 function getNextToken(string &$value = null, array &$startAttributes = null, array &$endAttributes = null): int;
108 ```
102 109
103 The `startLexing()` method is invoked with the source code that is to be lexed (including the opening tag) whenever the 110 The `startLexing()` method is invoked whenever the `parse()` method of the parser is called and is passed the source
104 `parse()` method of the parser is called. It can be used to reset state or preprocess the source code or tokens. The 111 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
105 passes `ErrorHandler` should be used to report lexing errors. 112 passed `ErrorHandler` should be used to report lexing errors.
106 113
107 The `getTokens()` method returns the current token array, in the usual `token_get_all()` format. This method is not 114 The `getTokens()` method returns the current token array, in the usual `token_get_all()` format. This method is not
108 used by the parser (which uses `getNextToken()`), but is useful in combination with the token position attributes. 115 used by the parser (which uses `getNextToken()`), but is useful in combination with the token position attributes.
109 116
110 The `handleHaltCompiler()` method is called whenever a `T_HALT_COMPILER` token is encountered. It has to return the 117 The `handleHaltCompiler()` method is called whenever a `T_HALT_COMPILER` token is encountered. It has to return the