function yay_parse(string $source, Directives $directives = null, BlueContext $blueContext = null) : string { if ($gc = gc_enabled()) { gc_disable(); } // important optimization! static $globalDirectives = null; if (null === $globalDirectives) { $globalDirectives = new ArrayObject(); } $directives = $directives ?: new Directives(); $blueContext = $blueContext ?: new BlueContext(); $cg = (object) ['ts' => TokenStream::fromSource($source), 'directives' => $directives, 'cycle' => new Cycle($source), 'globalDirectives' => $globalDirectives, 'blueContext' => $blueContext]; foreach ($cg->globalDirectives as $d) { $cg->directives->add($d); } traverse(midrule(function (TokenStream $ts) use($directives, $blueContext) { $token = $ts->current(); tail_call: if (null === $token) { return; } // skip when something looks like a new macro to be parsed if ('macro' === (string) $token) { return; } // here we do the 'magic' to match and expand userland macros $directives->apply($ts, $token, $blueContext); $token = $ts->next(); goto tail_call; }), consume(chain(token(T_STRING, 'macro')->as('declaration'), optional(repeat(rtoken('/^·\\w+$/')))->as('tags'), lookahead(token('{')), commit(chain(braces()->as('pattern'), operator('>>'), braces()->as('expansion')))->as('body'), optional(token(';'))), CONSUME_DO_TRIM)->onCommit(function (Ast $macroAst) use($cg) { $scope = Map::fromEmpty(); $tags = Map::fromValues(array_map('strval', $macroAst->{'tags'})); $pattern = new Pattern($macroAst->{'declaration'}->line(), $macroAst->{'body pattern'}, $tags, $scope); $expansion = new Expansion($macroAst->{'body expansion'}, $tags, $scope); $macro = new Macro($tags, $pattern, $expansion, $cg->cycle); $cg->directives->add($macro); // allocate the userland macro // allocate the userland macro globally if it's declared as global if ($macro->tags()->contains('·global')) { $cg->globalDirectives[] = $macro; } }))->parse($cg->ts); $expansion = (string) $cg->ts; if ($gc) { gc_enable(); } return $expansion; }
function postfix($equation) { unset($GLOBALS['stack']); $GLOBALS['stack'] = array(); $GLOBALS['output'] = ""; for ($i = 0; $i < strlen($equation); $i++) { //123+5*6 $char = $equation[$i]; switch ($char) { case '+': case '-': if ($i == 0 || $i > 0 && is_operator($equation[$i - 1])) { $GLOBALS['output'] = $char . $GLOBALS['output']; break; } $GLOBALS['output'] = $GLOBALS['output'] . ','; operator($char, 1); break; case '*': case '/': $GLOBALS['output'] = $GLOBALS['output'] . ','; operator($char, 2); break; case '(': array_push($GLOBALS['stack'], $char); break; case ')': paren($char); break; default: $GLOBALS['output'] = $GLOBALS['output'] . $char; break; } } $GLOBALS['output'] = $GLOBALS['output'] . ','; while (count($GLOBALS['stack']) > 0) { $GLOBALS['output'] = $GLOBALS['output'] . array_pop($GLOBALS['stack']) . ','; } if (substr($GLOBALS['output'], -1) == ",") { $GLOBALS['output'] = rtrim($GLOBALS['output'], ','); } return $GLOBALS['output']; }
function postfix($equation) { unset($this->stack); $this->stack = array(); $this->output = ""; for ($i = 0; $i < strlen($equation); $i++) { //123+5*6 $char = $equation[$i]; switch ($char) { case '+': case '-': $this->output = $this->output . ','; operator($char, 1); break; case '*': case '/': $this->output = $this->output . ','; operator($char, 2); break; case '(': array_push($this->stack, $char); break; case ')': paren($char); break; default: $this->output = $this->output . $char; break; } } $this->output = $this->output . ','; while (count($this->stack) > 0) { $this->output = $this->output . array_pop($this->stack) . ','; } //var_dump($this->output); return $this->output; }
function postfix($equation) { unset($GLOBALS['stack']); $GLOBALS['stack'] = array(); $GLOBALS['output'] = ""; for ($i = 0; $i < strlen($equation); $i++) { //123+5*6 $char = $equation[$i]; switch ($char) { case '+': case '-': $GLOBALS['output'] = $GLOBALS['output'] . ','; operator($char, 1); break; case '*': case '/': $GLOBALS['output'] = $GLOBALS['output'] . ','; operator($char, 2); break; case '(': array_push($GLOBALS['stack'], $char); break; case ')': paren($char); break; default: $GLOBALS['output'] = $GLOBALS['output'] . $char; break; } } $GLOBALS['output'] = $GLOBALS['output'] . ','; while (count($GLOBALS['stack']) > 0) { $GLOBALS['output'] = $GLOBALS['output'] . array_pop($GLOBALS['stack']) . ','; } //var_dump($GLOBALS['output']); return $GLOBALS['output']; }
private function mutate(TokenStream $ts, Ast $context, Cycle $cycle, Directives $directives, BlueContext $blueContext) : TokenStream { if ($this->constant) { return $ts; } static $states, $parser; $states = $states ?? new Stack(); $parser = $parser ?? traverse(token(Token::CLOAKED), consume(chain(rtoken('/^··\\w+$/')->as('expander'), either(parentheses(), braces())->as('args')))->onCommit(function (Ast $result) use($states) { $cg = $states->current(); $expander = $result->expander; if (\count($result->args) === 0) { $cg->this->fail(self::E_EMPTY_EXPANDER_SLICE, (string) $expander, $expander->line()); } $context = Map::fromKeysAndValues(['scope' => $cg->cycle->id(), 'directives' => $cg->directives, 'blueContext' => $cg->blueContext]); $expansion = TokenStream::fromSlice($result->args); $mutation = $cg->this->mutate(clone $expansion, $cg->context, $cg->cycle, $cg->directives, $cg->blueContext); $mutation = $cg->this->lookupExpander($expander)($mutation, $context); $cg->ts->inject($mutation); }), consume(chain(rtoken('/^·\\w+|···\\w+$/')->as('label'), operator('···'), optional(parentheses()->as('delimiters')), braces()->as('expansion')))->onCommit(function (Ast $result) use($states) { $cg = $states->current(); $context = $cg->this->lookupContext($result->label, $cg->context, self::E_UNDEFINED_EXPANSION); $expansion = TokenStream::fromSlice($result->expansion); $delimiters = $result->delimiters; // normalize single context if (array_values($context) !== $context) { $context = [$context]; } foreach (array_reverse($context) as $i => $subContext) { $mutation = $cg->this->mutate(clone $expansion, (new Ast(null, $subContext))->withParent($cg->context), $cg->cycle, $cg->directives, $cg->blueContext); if ($i !== 0) { foreach ($delimiters as $d) { $mutation->push($d); } } $cg->ts->inject($mutation); } }), consume(rtoken('/^(T_\\w+·\\w+|·\\w+|···\\w+)$/')->as('label'))->onCommit(function (Ast $result) use($states) { $cg = $states->current(); $context = $cg->this->lookupContext($result->label, $cg->context, self::E_UNDEFINED_EXPANSION); if ($context instanceof Token) { $cg->ts->inject(TokenStream::fromSequence($context)); } elseif (is_array($context) && \count($context)) { $tokens = []; array_walk_recursive($context, function (Token $token) use(&$tokens) { $tokens[] = $token; }); $cg->ts->inject(TokenStream::fromSlice($tokens)); } })); $cg = (object) ['ts' => $ts, 'context' => $context, 'directives' => $directives, 'cycle' => $cycle, 'this' => $this, 'blueContext' => $blueContext]; $states->push($cg); $parser->parse($cg->ts); $states->pop(); $cg->ts->reset(); if ($this->cloaked) { traverse(consume(token(Token::CLOAKED))->onCommit(function (Ast $result) use($cg) { $cg->ts->inject(TokenStream::fromSourceWithoutOpenTag((string) $result->token())); }))->parse($cg->ts); $cg->ts->reset(); } return $cg->ts; }
private function mutate(TokenStream $ts, Ast $context) : TokenStream { $cg = (object) ['ts' => clone $ts, 'context' => $context]; if ($this->unsafe && !$this->hasTag('·unsafe')) { hygienize($cg->ts, $this->cycle->id()); } if ($this->constant) { return $cg->ts; } traverse(either(token(Token::CLOAKED), consume(chain(rtoken('/^·\\w+$/')->as('expander'), parentheses()->as('args')))->onCommit(function (Ast $result) use($cg) { $expander = $this->lookupExpander($result->expander); $args = []; foreach ($result->args as $arg) { if ($arg instanceof Token) { $key = (string) $arg; if (preg_match('/^·\\w+|T_\\w+·\\w+|···\\w+$/', $key)) { $arg = $cg->context->{$key}; } } if (is_array($arg)) { array_push($args, ...$arg); } else { $args[] = $arg; } } $mutation = $expander(TokenStream::fromSlice($args), $this->cycle->id()); $cg->ts->inject($mutation); }), consume(chain(rtoken('/^·\\w+|···\\w+$/')->as('label'), operator('···'), braces()->as('expansion')))->onCommit(function (Ast $result) use($cg) { $index = (string) $result->label; $context = $cg->context->{$index}; if ($context === null) { $this->fail(self::E_EXPANSION, $index, $result->label->line(), json_encode(array_keys($cg->context->all()[0]), self::PRETTY_PRINT)); } $expansion = TokenStream::fromSlice($result->expansion); // normalize single context if (array_values($context) !== $context) { $context = [$context]; } foreach (array_reverse($context) as $i => $subContext) { $mutation = $this->mutate($expansion, (new Ast(null, $subContext))->withParent($cg->context)); $cg->ts->inject($mutation); } }), consume(rtoken('/^(T_\\w+·\\w+|·\\w+|···\\w+)$/'))->onCommit(function (Ast $result) use($cg) { $expansion = $cg->context->{(string) $result->token()}; if ($expansion instanceof Token) { $cg->ts->inject(TokenStream::fromSequence($expansion)); } elseif (is_array($expansion) && \count($expansion)) { $tokens = []; array_walk_recursive($expansion, function (Token $token) use(&$tokens) { $tokens[] = $token; }); $cg->ts->inject(TokenStream::fromSlice($tokens)); } }), any()))->parse($cg->ts); $cg->ts->reset(); if ($this->cloaked) { traverse(either(consume(token(Token::CLOAKED))->onCommit(function (Ast $result) use($cg) { $cg->ts->inject(TokenStream::fromSourceWithoutOpenTag((string) $result->token())); }), any()))->parse($cg->ts); $cg->ts->reset(); } return $cg->ts; }
function closed($tokens, &$next, &$i, &$message, &$boolean_tokens) { //comes from closed parenthesis or token $i++; $next = $tokens[$i]; if (preg_match("/[\\|&]{1}/", $next)) { //pointer is an operator operator($tokens, $next, $i, $message, $boolean_tokens); } elseif (preg_match("/\\){1}/", $next)) { //pointer is a closed closed($tokens, $next, $i, $message, $boolean_tokens); } elseif ($next == "\n") { //end of descent, no errors $message = false; } else { //error found $message = $tokens[$i] == "\n" ? "Error near end of boolean expression" : "Error in boolean expression at token " . ($i + 1) . " near " . $next; } }
function testOperatorOnEnd() { $ts = TokenStream::fromSource('<?php <~>'); $this->parseSuccess($ts, token(T_OPEN_TAG), "T_OPEN_TAG(<?php )"); $this->parseSuccess($ts, operator('<~>'), 'OPERATOR(<~>)'); }