>=', '++', '--' ); const BAN_KW = array( 'global', 'xor', 'echo', 'print', 'include', 'include_once', 'require', 'require_once' ); /** * Source. * * @var string */ private $src = ''; /** * Tokens without comments. * * @var array */ private $t = array(); /** * Parse a string. * * @param string $src Legacy string. * @return array{ok:bool, tree?:array, reason?:string, detail?:string, tokens:array} */ public function parse( $src ) { $this->src = (string) $src; $lex = Lexer::tokenize( $this->src ); if ( ! $lex['ok'] ) { return $this->whole( 'parse_error', $lex['error'], array() ); } $all = $lex['tokens']; $has_line = false; $this->t = array(); foreach ( $all as $tok ) { if ( 'comment_line' === $tok['type'] ) { $has_line = true; continue; } if ( 'comment_block' === $tok['type'] ) { continue; } $this->t[] = $tok; } $preamble = false; // Spec amendment 2026-11-10 (gate decision 3): a leading statement that is exactly // "global $post;" is stripped and the remainder parsed under the normal rules. Any // other global (another variable, several variables, or not the first statement) // still trips the whole-string trigger below. if ( count( $this->t ) >= 3 && 'kw' === $this->t[0]['type'] && 'global' === $this->t[0]['value'] && 'var' === $this->t[1]['type'] && '$post' === $this->t[1]['value'] && 'punct' === $this->t[2]['type'] && ';' === $this->t[2]['value'] ) { $this->t = array_slice( $this->t, 3 ); $preamble = true; } $t = $this->t; $n = count( $t ); if ( 0 === $n ) { return $this->whole( 'parse_error', 'empty', $all ); } foreach ( $t as $k => $tok ) { if ( 'kw' === $tok['type'] && in_array( $tok['value'], self::BAN_KW, true ) ) { return $this->whole( 'unsupported_syntax', 'keyword_' . $tok['value'], $all ); } if ( 'op' === $tok['type'] && in_array( $tok['value'], self::ASSIGN_OPS, true ) ) { return $this->whole( 'unsupported_syntax', 'assignment', $all ); } if ( 'op' === $tok['type'] && in_array( $tok['value'], array( '?', '??', '`', '?>', '{', '}' ), true ) ) { return $this->whole( 'unsupported_syntax', 'operator_' . $tok['value'], $all ); } if ( 'punct' === $tok['type'] && ';' === $tok['value'] && $k !== $n - 1 ) { return $this->whole( 'multiple_statements', 'separator', $all ); } if ( 'kw' === $tok['type'] && 'return' === $tok['value'] && 0 !== $k ) { return $this->whole( 'multiple_statements', 'return_not_first', $all ); } } $starts_return = 'kw' === $t[0]['type'] && 'return' === $t[0]['value']; $ends_semi = 'punct' === $t[ $n - 1 ]['type'] && ';' === $t[ $n - 1 ]['value']; if ( $preamble && ! $starts_return ) { // 5.10.4 wrapped such text as "return ( global $post; ... );", a syntax error. return $this->whole( 'parse_error', 'semicolon_inside_implicit_return', $all ); } // Widget Logic 5.10.4 fidelity: it added "return ( ... );" only when the text did not // contain the letters "return" anywhere, then ran the text as PHP. if ( ! $starts_return && false !== stripos( $this->src, 'return' ) ) { return $this->whole( 'unsupported_syntax', 'legacy_return_quirk', $all ); } if ( $starts_return && ! $ends_semi ) { return $this->whole( 'parse_error', 'return_without_semicolon', $all ); } if ( ! $starts_return && $ends_semi ) { return $this->whole( 'parse_error', 'semicolon_inside_implicit_return', $all ); } if ( ! $starts_return && $has_line ) { return $this->whole( 'parse_error', 'line_comment_inside_implicit_return', $all ); } $lo = $starts_return ? 1 : 0; $hi = $ends_semi ? $n - 1 : $n; if ( $lo >= $hi ) { return $this->whole( 'parse_error', 'empty', $all ); } if ( ! $this->balanced( $lo, $hi ) ) { return $this->whole( 'parse_error', 'unbalanced', $all ); } $tree = $this->expr( $lo, $hi ); if ( null === $tree ) { return $this->whole( 'parse_error', 'empty_operand', $all ); } return array( 'ok' => true, 'tree' => $tree, 'tokens' => $all, 'preamble' => $preamble, ); } /** * Whole-string failure. * * @param string $reason Reason. * @param string $detail Detail. * @param array $tokens Tokens. * @return array */ private function whole( $reason, $detail, array $tokens ) { return array( 'ok' => false, 'reason' => $reason, 'detail' => $detail, 'tokens' => $tokens, ); } /** * Check bracket balance in a range. * * @param int $lo Start. * @param int $hi End (exclusive). * @return bool */ private function balanced( $lo, $hi ) { $stack = array(); for ( $i = $lo; $i < $hi; $i++ ) { $v = $this->t[ $i ]['value']; if ( 'punct' !== $this->t[ $i ]['type'] ) { continue; } if ( '(' === $v || '[' === $v ) { $stack[] = $v; } elseif ( ')' === $v || ']' === $v ) { $open = array_pop( $stack ); if ( ( ')' === $v && '(' !== $open ) || ( ']' === $v && '[' !== $open ) ) { return false; } } } return empty( $stack ); } /** * Boolean operator level of token i, or 0. * * @param int $i Index. * @return int 1 or, 2 and, 3 ||, 4 && (xor never gets here: whole-string trigger, prompt 7.1). */ private function bool_level( $i ) { $tok = $this->t[ $i ]; if ( 'kw' === $tok['type'] && 'or' === $tok['value'] ) { return 1; } if ( 'kw' === $tok['type'] && 'and' === $tok['value'] ) { return 2; } if ( 'punct' === $tok['type'] && '||' === $tok['value'] ) { return 3; } if ( 'punct' === $tok['type'] && '&&' === $tok['value'] ) { return 4; } return 0; } /** * Parse an expression over a balanced range. * * @param int $lo Start. * @param int $hi End (exclusive). * @return array|null Null on an empty operand (syntax error). */ private function expr( $lo, $hi ) { $operands = array(); $ops = array(); $depth = 0; $start = $lo; for ( $i = $lo; $i < $hi; $i++ ) { $tok = $this->t[ $i ]; if ( 'punct' === $tok['type'] && ( '(' === $tok['value'] || '[' === $tok['value'] ) ) { ++$depth; } elseif ( 'punct' === $tok['type'] && ( ')' === $tok['value'] || ']' === $tok['value'] ) ) { --$depth; } elseif ( 0 === $depth ) { $level = $this->bool_level( $i ); if ( $level > 0 ) { if ( $start >= $i ) { return null; } $operands[] = array( $start, $i ); $ops[] = $level; $start = $i + 1; } } } if ( $start >= $hi ) { return null; } $operands[] = array( $start, $hi ); $nodes = array(); foreach ( $operands as $o ) { $node = $this->operand( $o[0], $o[1] ); if ( null === $node ) { return null; } $nodes[] = $node; } return $this->build( $nodes, $ops, 1 ); } /** * Build a tree by precedence (1 lowest). * * @param array $nodes Nodes. * @param array $ops Operator levels between nodes. * @param int $level Level to split at. * @return array */ private function build( array $nodes, array $ops, $level ) { if ( 1 === count( $nodes ) ) { return $nodes[0]; } if ( $level > 4 ) { return $nodes[0]; } if ( ! in_array( $level, $ops, true ) ) { return $this->build( $nodes, $ops, $level + 1 ); } $groups = array(); $gn = array( $nodes[0] ); $go = array(); $counter = count( $ops ); for ( $k = 0; $k < $counter; $k++ ) { if ( $ops[ $k ] === $level ) { $groups[] = array( $gn, $go ); $gn = array( $nodes[ $k + 1 ] ); $go = array(); } else { $go[] = $ops[ $k ]; $gn[] = $nodes[ $k + 1 ]; } } $groups[] = array( $gn, $go ); $type = ( 1 === $level || 3 === $level ) ? 'or' : 'and'; $items = array(); foreach ( $groups as $g ) { $child = $this->build( $g[0], $g[1], $level + 1 ); if ( isset( $child['t'] ) && $child['t'] === $type ) { foreach ( $child['items'] as $c ) { $items[] = $c; } } else { $items[] = $child; } } return array( 't' => $type, 'items' => $items, ); } /** * Raw source text of a token range. * * @param int $lo Start. * @param int $hi End (exclusive). * @return string */ private function raw( $lo, $hi ) { $s = $this->t[ $lo ]['start']; $e = $this->t[ $hi - 1 ]['end']; return substr( $this->src, $s, $e - $s ); } /** * Find the matching close bracket index for an open at $i. * * @param int $i Open index. * @param int $hi Range end. * @return int|null */ private function match_close( $i, $hi ) { $depth = 0; for ( $k = $i; $k < $hi; $k++ ) { $tok = $this->t[ $k ]; if ( 'punct' !== $tok['type'] ) { continue; } if ( '(' === $tok['value'] || '[' === $tok['value'] ) { ++$depth; } elseif ( ')' === $tok['value'] || ']' === $tok['value'] ) { --$depth; if ( 0 === $depth ) { return $k; } } } return null; } /** * Parse one operand. An operand is translatable only if it is exactly * `!`* primary. Otherwise it is a foreign operand (patterns aside). * * @param int $lo Start. * @param int $hi End (exclusive). * @return array|null */ private function operand( $lo, $hi ) { $nots = 0; $i = $lo; while ( $i < $hi && 'punct' === $this->t[ $i ]['type'] && '!' === $this->t[ $i ]['value'] ) { ++$nots; ++$i; } if ( $i >= $hi ) { return null; } $primary = $this->primary( $i, $hi ); if ( null === $primary ) { $pattern = 0 === $nots ? Patterns::match( $this->t, $lo, $hi ) : null; if ( null !== $pattern ) { $pattern['t'] = 'pattern'; $pattern['raw'] = $this->raw( $lo, $hi ); return $pattern; } $foreign = array( 't' => 'foreign', 'raw' => $this->raw( $lo, $hi ), 'why' => 'unsupported_syntax', ); // A lone call that only failed on its arguments, or an unknown function, keeps a better reason. if ( 'ident' === $this->t[ $i ]['type'] && $i + 1 < $hi && '(' === $this->t[ $i + 1 ]['value'] ) { $close = $this->match_close( $i + 1, $hi ); if ( $close === $hi - 1 ) { $foreign['why'] = 'bad_arguments'; $foreign['name'] = $this->t[ $i ]['value']; } } return $foreign; } $node = $primary; for ( $k = 0; $k < $nots; $k++ ) { $node = array( 't' => 'not', 'item' => $node, ); } return $node; } /** * Parse a primary covering exactly [lo, hi), or null. * * @param int $lo Start. * @param int $hi End (exclusive). * @return array|null */ private function primary( $lo, $hi ) { $tok = $this->t[ $lo ]; if ( 'punct' === $tok['type'] && '(' === $tok['value'] ) { $close = $this->match_close( $lo, $hi ); if ( $close !== $hi - 1 ) { return null; } return $this->expr( $lo + 1, $close ); } if ( 'kw' === $tok['type'] && ( 'true' === $tok['value'] || 'false' === $tok['value'] ) && $hi === $lo + 1 ) { return array( 't' => 'bool', 'value' => 'true' === $tok['value'], ); } // A lone scalar literal in a boolean position (for example a quoted // "is_woocommerce()") is decided by PHP truthiness, as 5.10.4 ran it. if ( $hi === $lo + 1 && ( 'int' === $tok['type'] || 'sq' === $tok['type'] || ( 'dq' === $tok['type'] && empty( $tok['foreign'] ) ) || ( 'kw' === $tok['type'] && 'null' === $tok['value'] ) ) ) { $v = 'kw' === $tok['type'] ? null : $tok['value']; return array( 't' => 'bool', 'value' => (bool) $v, 'literal' => true, ); } if ( 'ident' === $tok['type'] && $lo + 1 < $hi && 'punct' === $this->t[ $lo + 1 ]['type'] && '(' === $this->t[ $lo + 1 ]['value'] ) { $close = $this->match_close( $lo + 1, $hi ); if ( $close !== $hi - 1 ) { return null; } $args = $this->arg_list( $lo + 2, $close ); if ( null === $args ) { return null; } return array( 't' => 'call', 'name' => $tok['value'], 'args' => $args, 'raw' => $this->raw( $lo, $hi ), ); } return null; } /** * Parse a comma-separated argument list (trailing comma allowed). * * @param int $lo Start. * @param int $hi End (exclusive). * @return array|null List of values, or null when an argument is not a literal. */ private function arg_list( $lo, $hi ) { $args = array(); $depth = 0; $start = $lo; for ( $i = $lo; $i <= $hi; $i++ ) { $end = ( $i === $hi ); if ( ! $end ) { $tok = $this->t[ $i ]; if ( 'punct' === $tok['type'] && ( '(' === $tok['value'] || '[' === $tok['value'] ) ) { ++$depth; continue; } if ( 'punct' === $tok['type'] && ( ')' === $tok['value'] || ']' === $tok['value'] ) ) { --$depth; continue; } if ( ! ( 0 === $depth && 'punct' === $tok['type'] && ',' === $tok['value'] ) ) { continue; } } if ( $start === $i ) { if ( $end && $i > $lo && ',' === $this->t[ $i - 1 ]['value'] ) { break; // Trailing comma. } if ( $end && $i === $lo ) { break; // No arguments. } return null; } $value = $this->arg( $start, $i ); if ( ! $value['ok'] ) { return null; } $args[] = $value['value']; $start = $i + 1; } return $args; } /** * Parse one literal argument over [lo, hi). * * @param int $lo Start. * @param int $hi End (exclusive). * @return array{ok:bool, value?:mixed} */ private function arg( $lo, $hi ) { $tok = $this->t[ $lo ]; $one = ( $hi === $lo + 1 ); if ( $one && 'int' === $tok['type'] ) { return array( 'ok' => true, 'value' => $tok['value'], ); } if ( $hi === $lo + 2 && 'op' === $tok['type'] && '-' === $tok['value'] && 'int' === $this->t[ $lo + 1 ]['type'] ) { return array( 'ok' => true, 'value' => -$this->t[ $lo + 1 ]['value'], ); } if ( $one && 'sq' === $tok['type'] ) { return array( 'ok' => true, 'value' => $tok['value'], ); } if ( $one && 'dq' === $tok['type'] && empty( $tok['foreign'] ) ) { return array( 'ok' => true, 'value' => $tok['value'], ); } if ( $one && 'kw' === $tok['type'] && in_array( $tok['value'], array( 'true', 'false', 'null' ), true ) ) { $map = array( 'true' => true, 'false' => false, 'null' => null, ); return array( 'ok' => true, 'value' => $map[ $tok['value'] ], ); } $is_array_kw = 'kw' === $tok['type'] && 'array' === $tok['value'] && $lo + 1 < $hi && '(' === $this->t[ $lo + 1 ]['value']; $is_bracket = 'punct' === $tok['type'] && '[' === $tok['value']; if ( $is_array_kw || $is_bracket ) { $open = $is_array_kw ? $lo + 1 : $lo; $close = $this->match_close( $open, $hi ); if ( $close !== $hi - 1 ) { return array( 'ok' => false ); } for ( $k = $open + 1; $k < $close; $k++ ) { if ( 'punct' === $this->t[ $k ]['type'] && '=>' === $this->t[ $k ]['value'] ) { return array( 'ok' => false ); // Keyed arrays are not literal lists. } } $items = $this->arg_list( $open + 1, $close ); if ( null === $items ) { return array( 'ok' => false ); } return array( 'ok' => true, 'value' => $items, ); } return array( 'ok' => false ); } }