private function compile(array $tokens) { $cg = (object) ['ts' => TokenStream::fromSlice($tokens), 'parsers' => []]; traverse(rtoken('/^(T_\\w+)·(\\w+)$/')->onCommit(function (Ast $result) use($cg) { $token = $result->token(); $id = $this->lookupCapture($token); $type = $this->lookupTokenType($token); $cg->parsers[] = token($type)->as($id); }), ($parser = chain(rtoken('/^·\\w+$/')->as('type'), token('('), optional(ls(either(future($parser)->as('parser'), chain(token(T_FUNCTION), parentheses()->as('args'), braces()->as('body'))->as('function'), string()->as('string'), rtoken('/^T_\\w+·\\w+$/')->as('token'), rtoken('/^T_\\w+$/')->as('constant'), rtoken('/^·this$/')->as('this'), label()->as('label'))->as('parser'), token(',')))->as('args'), commit(token(')')), optional(rtoken('/^·\\w+$/')->as('label'), null)))->onCommit(function (Ast $result) use($cg) { $cg->parsers[] = $this->compileParser($result->type, $result->args, $result->label); }), $this->layer('{', '}', braces(), $cg), $this->layer('[', ']', brackets(), $cg), $this->layer('(', ')', parentheses(), $cg), rtoken('/^···(\\w+)$/')->onCommit(function (Ast $result) use($cg) { $id = $this->lookupCapture($result->token()); $cg->parsers[] = layer()->as($id); }), token(T_STRING, '·')->onCommit(function (Ast $result) use($cg) { $offset = \count($cg->parsers); if (0 !== $this->dominance || 0 === $offset) { $this->fail(self::E_BAD_DOMINANCE, $offset, $result->token()->line()); } $this->dominance = $offset; }), rtoken('/·/')->onCommit(function (Ast $result) { $token = $result->token(); $this->fail(self::E_BAD_CAPTURE, $token, $token->line()); }), any()->onCommit(function (Ast $result) use($cg) { $cg->parsers[] = token($result->token()); }))->parse($cg->ts); // check if macro dominance '·' is last token if ($this->dominance === \count($cg->parsers)) { $this->fail(self::E_BAD_DOMINANCE, $this->dominance, $cg->ts->last()->line()); } $this->specificity = \count($cg->parsers); if ($this->specificity > 1) { if (0 === $this->dominance) { $pattern = chain(...$cg->parsers); } else { /* dominat macros are partially wrapped in commit()s and dominance is the offset used as the 'event horizon' point... once the entry point is matched, there is no way back and a parser error arises */ $prefix = array_slice($cg->parsers, 0, $this->dominance); $suffix = array_slice($cg->parsers, $this->dominance); $pattern = chain(...array_merge($prefix, array_map(commit::class, $suffix))); } } else { /* micro optimization to save one function call for every token on the subject token stream whenever the macro pattern consists of a single parser */ $pattern = $cg->parsers[0]; } return $pattern; }
private function compilePattern(int $line, array $tokens) : Parser { if (!$tokens) { $this->fail(self::E_EMPTY_PATTERN, $line); } $ts = TokenStream::fromSlice($tokens); traverse(either(consume(chain(token(T_NS_SEPARATOR), token(T_NS_SEPARATOR), parentheses()->as('cloaked')))->onCommit(function (Ast $result) use($ts) { $ts->inject(TokenStream::fromSequence(...$result->cloaked)); $ts->skip(...TokenStream::SKIPPABLE); }), rtoken('/^(T_\\w+)·(\\w+)$/')->onCommit(function (Ast $result) { $token = $result->token(); $id = $this->lookupCapture($token); $type = $this->lookupTokenType($token); $this->parsers[] = token($type)->as($id); }), ($parser = chain(rtoken('/^·\\w+$/')->as('parser_type'), token('('), optional(ls(either(future($parser)->as('parser'), string()->as('string'), rtoken('/^T_\\w+·\\w+$/')->as('token'), rtoken('/^T_\\w+$/')->as('constant'), word()->as('word')), token(',')))->as('args'), commit(token(')')), optional(rtoken('/^·\\w+$/')->as('label'))))->onCommit(function (Ast $result) { $this->parsers[] = $this->compileParser($result->array()); }), $this->layer('{', '}', braces()), $this->layer('[', ']', brackets()), $this->layer('(', ')', parentheses()), rtoken('/^···(\\w+)$/')->onCommit(function (Ast $result) { $id = $this->lookupCapture($result->token()); $this->parsers[] = layer()->as($id); }), token(T_STRING, '·')->onCommit(function (Ast $result) use($ts) { $offset = \count($this->parsers); if (0 !== $this->dominance || 0 === $offset) { $this->fail(self::E_BAD_DOMINANCE, $offset, $result->token()->line()); } $this->dominance = $offset; }), rtoken('/·/')->onCommit(function (Ast $result) { $token = $result->token(); $this->fail(self::E_BAD_CAPTURE, $token, $token->line()); }), any()->onCommit(function (Ast $result) { $this->parsers[] = token($result->token()); })))->parse($ts); // check if macro dominance '·' is last token if ($this->dominance === \count($this->parsers)) { $this->fail(self::E_BAD_DOMINANCE, $this->dominance, $ts->last()->line()); } $this->specificity = \count($this->parsers); if ($this->specificity > 1) { if (0 === $this->dominance) { $pattern = chain(...$this->parsers); } else { /* dominat macros are partially wrapped in commit()s and dominance is the offset used as the 'event horizon' point... once the entry point is matched, there is no way back and a parser error arises */ $prefix = array_slice($this->parsers, 0, $this->dominance); $suffix = array_slice($this->parsers, $this->dominance); $pattern = chain(...array_merge($prefix, array_map(commit::class, $suffix))); } } else { /* micro optimization to save one function call for every token on the subject token stream whenever the macro pattern consists of a single parser */ $pattern = $this->parsers[0]; } return $pattern; }