Best Gherkin-php code snippet using AstNode.getTokenMatch
GherkinDocumentBuilder.php
Source:GherkinDocumentBuilder.php
...116 private function getTableRows(AstNode $node): array117 {118 $rows = array_map(119 fn ($token) => new TableRow($this->getLocation($token, 0), $this->getCells($token), $this->idGenerator->newId()),120 $node->getTokenMatches(TokenType::TableRow),121 );122 $this->ensureCellCount($rows);123 return $rows;124 }125 /** @param list<TableRow> $rows */126 private function ensureCellCount(array $rows): void127 {128 if (!count($rows)) {129 return;130 }131 $cellCount = count($rows[0]->cells);132 foreach ($rows as $row) {133 if (count($row->cells) !== $cellCount) {134 $location = new Location($row->location->line, $row->location->column ?? 0);135 throw new AstBuilderException('inconsistent cell count within the table', $location);136 }137 }138 }139 /**140 * @return list<TableCell>141 */142 private function getCells(TokenMatch $token): array143 {144 return array_map(145 fn ($cellItem) => new TableCell($this->getLocation($token, $cellItem->column), $cellItem->text),146 $token->items,147 );148 }149 /**150 * @return list<Tag>151 */152 private function getTags(AstNode $node): array153 {154 $tagsNode = $node->getSingle(AstNode::class, RuleType::Tags, new AstNode(RuleType::None));155 $tokens = $tagsNode->getTokenMatches(TokenType::TagLine);156 $tags = [];157 foreach ($tokens as $token) {158 foreach ($token->items as $tagItem) {159 $tags[] = new Tag(160 location: $this->getLocation($token, $tagItem->column),161 name: $tagItem->text,162 id: $this->idGenerator->newId(),163 );164 }165 }166 return $tags;167 }168 /**169 * @param array<TokenMatch> $lineTokens170 */171 private function joinMatchedTextWithLinebreaks(array $lineTokens): string172 {173 return join("\n", array_map(fn ($t) => $t->text, $lineTokens));174 }175 private function transformStepNode(AstNode $node): Step176 {177 $stepLine = $node->getTokenMatch(TokenType::StepLine);178 return new Step(179 location: $this->getLocation($stepLine, 0),180 keyword: $stepLine->keyword,181 keywordType: $stepLine->keywordType,182 text: $stepLine->text,183 docString: $node->getSingle(DocString::class, RuleType::DocString),184 dataTable: $node->getSingle(DataTable::class, RuleType::DataTable),185 id: $this->idGenerator->newId(),186 );187 }188 private function transformDocStringNode(AstNode $node): DocString189 {190 $separatorToken = $node->getTokenMatches(TokenType::DocStringSeparator)[0];191 $mediaType = $separatorToken->text;192 $lineTokens = $node->getTokenMatches(TokenType::Other);193 $content = $this->joinMatchedTextWithLinebreaks($lineTokens);194 return new DocString(195 location: $this->getLocation($separatorToken, 0),196 mediaType: $mediaType ?: null, // special case turns '' into null197 content: $content,198 delimiter: $separatorToken->keyword,199 );200 }201 private function transformScenarioDefinitionNode(AstNode $node): ?Scenario202 {203 $scenarioNode = $node->getSingle(AstNode::class, RuleType::Scenario);204 if (null === $scenarioNode) {205 return null;206 }207 $scenarioLine = $scenarioNode->getTokenMatch(TokenType::ScenarioLine);208 return new Scenario(209 location: $this->getLocation($scenarioLine, 0),210 tags: $this->getTags($node),211 keyword: $scenarioLine->keyword,212 name: $scenarioLine->text,213 description: $this->getDescription($scenarioNode),214 steps: $this->getSteps($scenarioNode),215 examples: $scenarioNode->getItems(Examples::class, RuleType::ExamplesDefinition),216 id: $this->idGenerator->newId(),217 );218 }219 private function transformExamplesDefinitionNode(AstNode $node): ?Examples220 {221 $examplesNode = $node->getSingle(AstNode::class, RuleType::Examples);222 if (null === $examplesNode) {223 return null;224 }225 $examplesLine = $examplesNode->getTokenMatch(TokenType::ExamplesLine);226 /** @var list<TableRow>|null $rows */227 $rows = $examplesNode->getSingleUntyped(RuleType::ExamplesTable);228 $tableHeader = is_array($rows) && count($rows) ? $rows[0] : null;229 $tableBody = (is_array($rows) && count($rows) > 0) ? array_slice($rows, 1) : [];230 return new Examples(231 location: $this->getLocation($examplesLine, 0),232 tags: $this->getTags($node),233 keyword: $examplesLine->keyword,234 name: $examplesLine->text,235 description: $this->getDescription($examplesNode),236 tableHeader: $tableHeader,237 tableBody: $tableBody,238 id: $this->idGenerator->newId(),239 );240 }241 private function transformDataTableNode(AstNode $node): DataTable242 {243 $rows = $this->getTableRows($node);244 return new DataTable($rows[0]->location, $rows);245 }246 /** @return list<TableRow> */247 private function transformExamplesTableNode(AstNode $node): array248 {249 return $this->getTableRows($node);250 }251 private function transformBackgroundNode(AstNode $node): Background252 {253 $backgroundLine = $node->getTokenMatch(TokenType::BackgroundLine);254 return new Background(255 location: $this->getLocation($backgroundLine, 0),256 keyword: $backgroundLine->keyword,257 name: $backgroundLine->text,258 description: $this->getDescription($node),259 steps: $this->getSteps($node),260 id: $this->idGenerator->newId(),261 );262 }263 private function transformDescriptionNode(AstNode $node): string264 {265 $lineTokens = $node->getTokenMatches(TokenType::Other);266 $lineText = preg_replace(267 '/(\\n\\s*)*$/u',268 '',269 $this->joinMatchedTextWithLinebreaks($lineTokens),270 );271 return $lineText;272 }273 private function transformFeatureNode(AstNode $node): ?Feature274 {275 $header = $node->getSingle(AstNode::class, RuleType::FeatureHeader, new AstNode(RuleType::FeatureHeader));276 if (!$header instanceof AstNode) {277 return null;278 }279 $tags = $this->getTags($header);280 $featureLine = $header->getTokenMatch(TokenType::FeatureLine);281 $children = [];282 $background = $node->getSingle(Background::class, RuleType::Background);283 if ($background instanceof Background) {284 $children[] = new FeatureChild(background: $background);285 }286 foreach ($node->getItems(Scenario::class, RuleType::ScenarioDefinition) as $scenario) {287 $children[] = new FeatureChild(scenario: $scenario);288 }289 foreach ($node->getItems(Rule::class, RuleType::Rule) as $rule) {290 $children[] = new FeatureChild($rule, null, null);291 }292 $language = $featureLine->gherkinDialect->getLanguage();293 return new Feature(294 location: $this->getLocation($featureLine, 0),295 tags: $tags,296 language: $language,297 keyword: $featureLine->keyword,298 name: $featureLine->text,299 description: $this->getDescription($header),300 children: $children,301 );302 }303 private function transformRuleNode(AstNode $node): Rule304 {305 $header = $node->getSingle(AstNode::class, RuleType::RuleHeader, new AstNode(RuleType::RuleHeader));306 $ruleLine = $header->getTokenMatch(TokenType::RuleLine);307 $children = [];308 $tags = $this->getTags($header);309 $background = $node->getSingle(Background::class, RuleType::Background);310 if ($background) {311 $children[] = new RuleChild(background: $background);312 }313 $scenarios = $node->getItems(Scenario::class, RuleType::ScenarioDefinition);314 foreach ($scenarios as $scenario) {315 $children[] = new RuleChild(scenario: $scenario);316 }317 return new Rule(318 location: $this->getLocation($ruleLine, 0),319 tags: $tags,320 keyword: $ruleLine->keyword,...
AstNodeTest.php
Source:AstNodeTest.php
...41 self::assertSame($obj1, $item);42 }43 public function testItGetsNoTokensWhenNoneAreAdded(): void44 {45 $tokens = $this->astNode->getTokenMatches(TokenType::Empty);46 self::assertSame([], $tokens);47 }48 public function testItGetsTokensWhenTheyAreAddedByRuletype(): void49 {50 $this->astNode->add(RuleType::_Empty, $token1 = $this->getTokenMatch());51 $this->astNode->add(RuleType::_Empty, $token2 = $this->getTokenMatch());52 $tokens = $this->astNode->getTokenMatches(TokenType::Empty);53 self::assertSame([$token1, $token2], $tokens);54 }55 public function testItThrowsWhenGettingATokenThatIsNotAdded(): void56 {57 $this->expectException(\LogicException::class);58 $this->astNode->getTokenMatch(TokenType::Empty);59 }60 public function testItGetsTheFirstTokenWhenSomeAreAdded(): void61 {62 $this->astNode->add(RuleType::_Empty, $token1 = $this->getTokenMatch());63 $this->astNode->add(RuleType::_Empty, $this->getTokenMatch());64 $token = $this->astNode->getTokenMatch(TokenType::Empty);65 self::assertSame($token1, $token);66 }67 private function getTokenMatch(): TokenMatch68 {69 return new TokenMatch(TokenType::Other, (new GherkinDialectProvider())->getDefaultDialect(), 100, 'keyword', KeywordType::UNKNOWN, 'text', [], new Location(1, 1));70 }71}...
getTokenMatch
Using AI Code Generation
1$ast = new \ast\Node(AST_ASSIGN, 0, [2 'var' => new \ast\Node(AST_VAR, 0, ['name' => 'x']),3 'expr' => new \ast\Node(AST_CONST, 0, ['name' => 'y']),4]);5$ast->getTokenMatch(0, 0);6$ast = new \ast\Node(AST_ASSIGN, 0, [7 'var' => new \ast\Node(AST_VAR, 0, ['name' => 'x']),8 'expr' => new \ast\Node(AST_CONST, 0, ['name' => 'y']),9]);10$ast->getTokenMatch(0, 0);11$ast = new \ast\Node(AST_ASSIGN, 0, [12 'var' => new \ast\Node(AST_VAR, 0, ['name' => 'x']),13 'expr' => new \ast\Node(AST_CONST, 0, ['name' => 'y']),14]);15$ast->getTokenMatch(0, 0);
getTokenMatch
Using AI Code Generation
1$node = $root->getChild(0);2$token = $node->getTokenMatch(0);3echo $token->getText();4$node = $root->getChild(0);5$token = $node->getTokenMatch(1);6echo $token->getText();7$node = $root->getChild(0);8$token = $node->getTokenMatch(2);9echo $token->getText();10$node = $root->getChild(0);11$token = $node->getTokenMatch(3);12echo $token->getText();13$node = $root->getChild(0);14$token = $node->getTokenMatch(4);15echo $token->getText();16$node = $root->getChild(0);17$token = $node->getTokenMatch(5);18echo $token->getText();19$node = $root->getChild(0);20$token = $node->getTokenMatch(6);21echo $token->getText();22$node = $root->getChild(0);23$token = $node->getTokenMatch(7);24echo $token->getText();25$node = $root->getChild(0);26$token = $node->getTokenMatch(8);27echo $token->getText();28$node = $root->getChild(0);29$token = $node->getTokenMatch(9);30echo $token->getText();31$node = $root->getChild(0);32$token = $node->getTokenMatch(10);33echo $token->getText();
getTokenMatch
Using AI Code Generation
1function myFunction($a, $b) {2 return $a + $b;3}4';5$ast = \ast\parse_code($code, 50);6$func = $ast->children[0];7$param = $func->children['params']->children[0];8echo $param->getTokenMatch() . "9";10Example Description AstNode::getChildren() Get the children of the node AstNode::getDocComment() Get the doc comment of the node AstNode::getEndLineno() Get the end line number of the node AstNode::getEndOffset() Get the end offset of the node AstNode::getFlags() Get the flags of the node AstNode::getKind() Get the kind of the node AstNode::getLine() Get the line number of the node AstNode::getMetadata() Get the metadata of the node AstNode::getName() Get the name of the node AstNode::getStartLineno() Get the start line number of the node AstNode::getStartOffset() Get the start offset of the node AstNode::getToken() Get the token of the node AstNode::getTokenMatch() Get the token match of the node AstNode::getChildren() Get the children of the node AstNode::getDocComment() Get the doc comment of the node AstNode::getEndLineno() Get the end line number of the node AstNode::getEndOffset() Get the end offset of the node AstNode::getFlags() Get the flags of the node AstNode::getKind() Get the kind of the node AstNode::getLine() Get the line number of the node AstNode::getMetadata() Get the metadata of the node AstNode::getName() Get the name of the node AstNode::getStartLineno() Get the start line number of the node AstNode::getStartOffset() Get the start offset of the node AstNode::getToken() Get the token of the node AstNode::getTokenMatch() Get the token match of the node11Example Description AstNode::getChildren() Get the children of the node AstNode::getDocComment() Get the doc comment of the node AstNode::getEndLineno() Get the end line number of the node AstNode::getEndOffset() Get the end offset of
getTokenMatch
Using AI Code Generation
1$astNode = new AstNode();2$token = $astNode->getTokenMatch($ast, 'T_STRING', 'echo');3print_r($token);4 (5 (6 (7 (8 (9 (10 (11 (12 (13 (14 (15 (16 (17 (18 [3] => ;19 (20$astNode = new AstNode();21$ast = $astNode->getAst(file_get_contents('1.php'));22print_r($ast);23 (
getTokenMatch
Using AI Code Generation
1require_once 'Token.php';2require_once 'TokenTree.php';3require_once 'TokenTreeBuilder.php';4require_once 'AstNode.php';5require_once 'AstNodeBuilder.php';6require_once 'AstNodeTree.php';7require_once 'AstNodeTreeBuilder.php';8require_once 'AstNodeTreeWalker.php';9require_once 'AstNodeTreeWalkerListener.php';10require_once 'AstNodeTreeWalkerListenerAdapter.php';
getTokenMatch
Using AI Code Generation
1$node = $ast->getFirstNode();2$token = $node->getTokenMatch(T_STRING, 'echo');3$token->getLine();4$node = $ast->getFirstNode();5$node->getFirstNode()->getLine();6$node = $ast->getFirstNode();7$node->getFirstNode()->getLine();8$node = $ast->getFirstNode();9$node->getFirstNode()->getLine();10$node = $ast->getFirstNode();11$node->getFirstNode()->getLine();12$node = $ast->getFirstNode();13$node->getFirstNode()->getLine();14$node = $ast->getFirstNode();15$node->getFirstNode()->getLine();16$node = $ast->getFirstNode();17$node->getFirstNode()->getLine();18$node = $ast->getFirstNode();19$node->getFirstNode()->getLine();20$node = $ast->getFirstNode();21$node->getFirstNode()->getLine();22$node = $ast->getFirstNode();23$node->getFirstNode()->getLine();24$node = $ast->getFirstNode();25$node->getFirstNode()->getLine();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with getTokenMatch on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!