From a5966aab0472b2380fef44c5a77731b668ff4f2d Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 17 Nov 2026 03:18:46 +0000 Subject: [PATCH] Add JEP-18 lexical scoping with let expressions Co-authored-by: Harry Vennik --- CHANGELOG.md | 1 + src/Lexer.php | 26 +- src/Parser.php | 56 ++++ src/TreeCompiler.php | 54 ++- src/TreeInterpreter.php | 36 ++ tests/CompilerRuntimeTest.php | 21 ++ tests/LexerTest.php | 21 ++ tests/ParserTest.php | 54 +++ tests/TreeCompilerTest.php | 28 ++ tests/TreeInterpreterTest.php | 21 ++ tests/compliance/letexpr.json | 604 ++++++++++++++++++++++++++++++++++ 11 files changed, 917 insertions(+), 5 deletions(-) create mode 100644 tests/compliance/letexpr.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 53bddb0..89d8cea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 2.10.0 - Upcoming * Added PHP 8.6 support. +* Added support for JEP-18 lexical scoping with let expressions. * Fixed the compiled runtime to evaluate sub-expressions when the left side is null. * Fixed the ordering operators to return null instead of false for non-numbers. * Fixed aggregate and sort functions to error on an invalid first or only element. diff --git a/src/Lexer.php b/src/Lexer.php index 9a9678c..5a140eb 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -10,6 +10,7 @@ class Lexer const T_STAR = 'star'; const T_COMMA = 'comma'; const T_COLON = 'colon'; + const T_ASSIGN = 'assign'; const T_CURRENT = 'current'; const T_EXPREF = 'expref'; const T_LPAREN = 'lparen'; @@ -20,6 +21,7 @@ class Lexer const T_RBRACKET = 'rbracket'; const T_FLATTEN = 'flatten'; const T_IDENTIFIER = 'identifier'; + const T_VARIABLE = 'variable'; const T_NUMBER = 'number'; const T_QUOTED_IDENTIFIER = 'quoted_identifier'; const T_UNKNOWN = 'unknown'; @@ -46,6 +48,7 @@ class Lexer const STATE_EQ = 11; const STATE_NOT = 12; const STATE_AND = 13; + const STATE_VARIABLE = 14; /** @var array We know what token we are consuming based on each char */ private static $transitionTable = [ @@ -56,6 +59,7 @@ class Lexer '[' => self::STATE_LBRACKET, '|' => self::STATE_PIPE, '&' => self::STATE_AND, + '$' => self::STATE_VARIABLE, '`' => self::STATE_JSON_LITERAL, '"' => self::STATE_QUOTED_STRING, "'" => self::STATE_STRING_LITERAL, @@ -235,6 +239,24 @@ public function tokenize($input) 'pos' => $start ]; + } elseif ($state === self::STATE_VARIABLE) { + + // Consume variable references + $start = key($chars); + $buffer = ''; + $current = next($chars); + if ($current !== false && !isset($this->numbers[$current])) { + while ($current !== false && isset($this->validIdentifier[$current])) { + $buffer .= $current; + $current = next($chars); + } + } + $tokens[] = [ + 'type' => $buffer === '' ? self::T_UNKNOWN : self::T_VARIABLE, + 'value' => $buffer === '' ? '$' : $buffer, + 'pos' => $start + ]; + } elseif ($state === self::STATE_WHITESPACE) { // Skip whitespace @@ -317,8 +339,8 @@ public function tokenize($input) } elseif ($state === self::STATE_EQ) { - // Consume equals - $tokens[] = $this->matchOr($chars, '=', '=', self::T_COMPARATOR, self::T_UNKNOWN); + // Consume "==" and "=" + $tokens[] = $this->matchOr($chars, '=', '=', self::T_COMPARATOR, self::T_ASSIGN); } elseif ($state == self::STATE_AND) { diff --git a/src/Parser.php b/src/Parser.php index f458b2a..b157a77 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -22,6 +22,7 @@ class Parser T::T_EOF => 0, T::T_QUOTED_IDENTIFIER => 0, T::T_IDENTIFIER => 0, + T::T_VARIABLE => 0, T::T_UNKNOWN => 0, T::T_LITERAL => 0, T::T_RBRACKET => 0, @@ -32,6 +33,7 @@ class Parser T::T_CURRENT => 0, T::T_EXPREF => 0, T::T_COLON => 0, + T::T_ASSIGN => 0, T::T_PIPE => 1, T::T_OR => 2, T::T_AND => 3, @@ -106,6 +108,10 @@ private function expr($rbp = 0) private function nud_identifier() { + if ($this->isLet()) { + return $this->parseLet(); + } + $token = $this->token; $this->next(); return ['type' => 'field', 'value' => $token['value']]; @@ -119,6 +125,13 @@ private function nud_quoted_identifier() return ['type' => 'field', 'value' => $token['value']]; } + private function nud_variable() + { + $token = $this->token; + $this->next(); + return ['type' => 'variable', 'value' => $token['value']]; + } + private function nud_current() { $this->next(); @@ -357,6 +370,8 @@ private function parseDot($bp) // Like the multi-select list above, a multi-select hash ends any // projection: tokens that follow apply to the projected list. return $this->nud_lbrace(); + } elseif ($this->isLet()) { + throw $this->syntax('A let expression is not allowed after a dot'); } return $this->expr($bp); @@ -376,6 +391,39 @@ private function parseKeyValuePair() ]; } + private function parseLet() + { + static $validVariable = [T::T_VARIABLE => true]; + static $validAssign = [T::T_ASSIGN => true]; + $bindings = []; + + do { + $this->next($validVariable); + $name = $this->token['value']; + $this->next($validAssign); + $this->next(); + $bindings[] = [ + 'type' => 'variable_binding', + 'value' => $name, + 'children' => [$this->expr()] + ]; + } while ($this->token['type'] == T::T_COMMA); + + if ($this->token['type'] !== T::T_IDENTIFIER || $this->token['value'] !== 'in') { + throw $this->syntax('Expected "in" after the let bindings'); + } + + $this->next(); + + return [ + 'type' => 'let', + 'children' => [ + ['type' => 'bindings', 'children' => $bindings], + $this->expr() + ] + ]; + } + private function parseWildcardObject(?array $left = null) { $this->next(); @@ -476,6 +524,14 @@ private function syntax($msg) return new SyntaxErrorException($msg, $this->token, $this->expression); } + private function isLet() + { + // "let" is only a keyword before a variable; elsewhere it is a field. + return $this->token['type'] === T::T_IDENTIFIER + && $this->token['value'] === 'let' + && $this->lookahead() === T::T_VARIABLE; + } + private function lookahead() { return (!isset($this->tokens[$this->tpos + 1])) diff --git a/src/TreeCompiler.php b/src/TreeCompiler.php index 666bfb4..7bd97ec 100644 --- a/src/TreeCompiler.php +++ b/src/TreeCompiler.php @@ -9,6 +9,7 @@ class TreeCompiler private $indentation; private $source; private $vars; + private $scope; /** * @param array $ast AST to compile. @@ -19,7 +20,7 @@ class TreeCompiler */ public function visit(array $ast, $fnName, $expr) { - $this->vars = []; + $this->vars = $this->scope = []; $this->source = $this->indentation = ''; $this->write("write('use JmesPath\\TreeInterpreter as Ti;') @@ -203,6 +204,29 @@ private function visit_pipe(array $node) ->dispatch($node['children'][1]); } + private function visit_let(array $node) + { + $value = $this->makeVar('beforeLet'); + $scope = $this->scope; + $bound = []; + $this->write('%s = $value;', $value); + + foreach ($node['children'][0]['children'] as $binding) { + $var = $this->makeVar('let'); + $this->dispatch($binding['children'][0]) + ->write('%s = $value;', $var) + ->write('$value = %s;', $value); + $bound[$binding['value']] = $var; + } + + // Bindings are only visible to the body, not to each other. + $this->scope = $bound + $scope; + $this->dispatch($node['children'][1]); + $this->scope = $scope; + + return $this; + } + private function visit_multi_select_list(array $node) { return $this->visit_multi_select_hash($node); @@ -274,12 +298,36 @@ private function visit_current(array $node) return $this->write('// Visiting current node (no-op)'); } + private function visit_variable(array $node) + { + if (isset($this->scope[$node['value']])) { + return $this->write('$value = %s;', $this->scope[$node['value']]); + } + + return $this->write( + 'throw new \\RuntimeException(%s);', + var_export('Undefined variable: $' . $node['value'], true) + ); + } + private function visit_expref(array $node) { $child = var_export($node['children'][0], true); - return $this->write('$value = function ($value) use ($interpreter) {') + $interpreter = '$interpreter'; + + if ($this->scope) { + $bindings = []; + foreach ($this->scope as $name => $var) { + $bindings[] = var_export($name, true) . ' => ' . $var; + } + $scoped = $this->makeVar('scoped'); + $this->write('%s = $interpreter->withScope([%s]);', $scoped, implode(', ', $bindings)); + $interpreter = $scoped; + } + + return $this->write('$value = function ($value) use (%s) {', $interpreter) ->indent() - ->write('return $interpreter->visit(%s, $value);', $child) + ->write('return %s->visit(%s, $value);', $interpreter, $child) ->outdent() ->write('};'); } diff --git a/src/TreeInterpreter.php b/src/TreeInterpreter.php index fe59155..6dd19c8 100644 --- a/src/TreeInterpreter.php +++ b/src/TreeInterpreter.php @@ -9,6 +9,9 @@ class TreeInterpreter /** @var callable */ private $fnDispatcher; + /** @var array Variables in scope, keyed by name */ + private $scope = []; + /** * @param callable|null $fnDispatcher Function dispatching function that accepts * a function name argument and an array of @@ -32,6 +35,22 @@ public function visit(array $node, $data) return $this->dispatch($node, $data); } + /** + * Returns a copy of the interpreter that has the given variables in scope. + * + * @param array $scope Variables to bind, keyed by name + * + * @return static + */ + public function withScope(array $scope) + { + // Bind on a copy so that expression references keep their variables. + $interpreter = clone $this; + $interpreter->scope = $scope + $this->scope; + + return $interpreter; + } + /** * Recursively traverses an AST using depth-first, pre-order traversal. * The evaluation logic for each node type is embedded into a large switch @@ -123,6 +142,12 @@ private function dispatch(array $node, $value) case 'current': return $value; + case 'variable': + if (!array_key_exists($node['value'], $this->scope)) { + throw new \RuntimeException('Undefined variable: $' . $node['value']); + } + return $this->scope[$node['value']]; + case 'or': $result = $this->dispatch($node['children'][0], $value); return Utils::isTruthy($result) @@ -146,6 +171,17 @@ private function dispatch(array $node, $value) $this->dispatch($node['children'][0], $value) ); + case 'let': + $bound = []; + foreach ($node['children'][0]['children'] as $binding) { + $bound[$binding['value']] = $this->dispatch( + $binding['children'][0], + $value + ); + } + + return $this->withScope($bound)->dispatch($node['children'][1], $value); + case 'multi_select_list': if ($value === null) { return null; diff --git a/tests/CompilerRuntimeTest.php b/tests/CompilerRuntimeTest.php index cba1206..f076aba 100644 --- a/tests/CompilerRuntimeTest.php +++ b/tests/CompilerRuntimeTest.php @@ -304,6 +304,27 @@ public function testRuntimesEvaluateSubexpressionsOfNull(): void } } + public function testRuntimesReportUndefinedVariables(): void + { + $dir = $this->createTempDir(); + + try { + foreach ([new AstRuntime(), new CompilerRuntime($dir)] as $runtime) { + $message = null; + + try { + $runtime('[let $foo = @ in $foo, $foo]', []); + } catch (\RuntimeException $e) { + $message = $e->getMessage(); + } + + $this->assertSame('Undefined variable: $foo', $message); + } + } finally { + $this->removeTempDir($dir); + } + } + public function testCompiledRuntimeSortsNumerically(): void { $dir = $this->createTempDir(); diff --git a/tests/LexerTest.php b/tests/LexerTest.php index 7d86328..f95016c 100644 --- a/tests/LexerTest.php +++ b/tests/LexerTest.php @@ -33,9 +33,16 @@ public static function inputProvider(): array [']', 'rbracket'], [':', 'colon'], [',', 'comma'], + ['=', 'assign'], + ['==', 'comparator'], ['||', 'or'], ['*', 'star'], ['foo', 'identifier'], + ['$foo', 'variable'], + ['$_', 'variable'], + ['$', 'unknown'], + ['$1', 'unknown'], + ['$ foo', 'unknown'], ['"foo"', 'quoted_identifier'], ['`true`', 'literal'], ['`false`', 'literal'], @@ -79,6 +86,20 @@ public function testTokenizesJsonNumbers(): void $this->assertEquals(120, $tokens[6]['value']); } + public function testTokenizesVariables(): void + { + $l = new Lexer(); + $tokens = $l->tokenize('let $foo_1 = $Bar'); + $this->assertSame('identifier', $tokens[0]['type']); + $this->assertSame('variable', $tokens[1]['type']); + $this->assertSame('foo_1', $tokens[1]['value']); + $this->assertSame(4, $tokens[1]['pos']); + $this->assertSame('assign', $tokens[2]['type']); + $this->assertSame('variable', $tokens[3]['type']); + $this->assertSame('Bar', $tokens[3]['value']); + $this->assertSame(13, $tokens[3]['pos']); + } + public function testCanWorkWithElidedJsonLiterals(): void { $l = new Lexer(); diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 663926c..9355a9e 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -44,6 +44,18 @@ public static function invalidExpressionProvider(): array ['`"not_a_function"`(@)', 'Invalid function name'], ["'not_a_function'(@)", 'Invalid function name'], ['@=', 'Did not reach the end of the token stream'], + ['$foo = bar', 'Did not reach the end of the token stream'], + ['$foo $bar', 'Did not reach the end of the token stream'], + ['$', 'Syntax error at character 0'], + ['$foo(@)', 'Invalid function name'], + ['foo.$bar', 'found variable "bar"'], + ['{$foo: bar}', 'found variable "foo"'], + ['foo.let $a = bar in $a', 'A let expression is not allowed after a dot'], + ['foo[*].let $a = bar in $a', 'A let expression is not allowed after a dot'], + ['let $a bar in $a', 'Expected one of the following: assign; found identifier "bar"'], + ['let $a = foo, bar in $a', 'Expected one of the following: variable; found identifier "bar"'], + ['let $a = foo', 'Expected "in" after the let bindings'], + ['let $a = foo in', 'Unexpected "eof" token'], ['`1` `2`', 'Did not reach the end of the token stream'], ['{a: @', 'Syntax error at character 5'], ['foo[-]', 'Syntax error at character 4'], @@ -92,11 +104,53 @@ public function testParsesIdentifierFunctionName(): void ); } + public function testParsesLetExpression(): void + { + $parser = new Parser(); + + $this->assertSame( + [ + 'type' => 'let', + 'children' => [ + [ + 'type' => 'bindings', + 'children' => [ + [ + 'type' => 'variable_binding', + 'value' => 'a', + 'children' => [ + ['type' => 'field', 'value' => 'foo'], + ], + ], + [ + 'type' => 'variable_binding', + 'value' => 'b', + 'children' => [ + ['type' => 'current'], + ], + ], + ], + ], + [ + 'type' => 'multi_select_list', + 'children' => [ + ['type' => 'variable', 'value' => 'a'], + ['type' => 'variable', 'value' => 'b'], + ], + ], + ], + ], + $parser->parse('let $a = foo, $b = @ in [$a, $b]') + ); + } + public static function invalidLedTokenProvider(): array { return [ ['avg([].size)+'], ['a = b'], + ['a[?b = c]'], + ['let $ = a in @'], ['@ `1`'], ['@``'], ['foo[]+'], diff --git a/tests/TreeCompilerTest.php b/tests/TreeCompilerTest.php index a8f72ce..1f531ba 100644 --- a/tests/TreeCompilerTest.php +++ b/tests/TreeCompilerTest.php @@ -43,6 +43,34 @@ public function testFromlessProjectionUsesCorrectGuard(): void ); } + public function testEscapesVariableNames(): void + { + $compiler = new TreeCompiler(); + $name = '\' . $shouldNotBeCode . \''; + $variable = ['type' => 'variable', 'value' => $name]; + $let = [ + 'type' => 'let', + 'children' => [ + [ + 'type' => 'bindings', + 'children' => [ + ['type' => 'variable_binding', 'value' => $name, 'children' => [['type' => 'current']]], + ], + ], + ['type' => 'expref', 'children' => [$variable]], + ], + ]; + + $this->assertStringContainsString( + 'throw new \\RuntimeException(\'Undefined variable: $\\\' . $shouldNotBeCode . \\\'\');', + $compiler->visit($variable, 'testing', 'example') + ); + $this->assertStringContainsString( + '$interpreter->withScope([\'\\\' . $shouldNotBeCode . \\\'\' => ', + $compiler->visit($let, 'testing', 'example') + ); + } + public function testEscapesFunctionName(): void { $compiler = new TreeCompiler(); diff --git a/tests/TreeInterpreterTest.php b/tests/TreeInterpreterTest.php index 9d24cfb..5675121 100644 --- a/tests/TreeInterpreterTest.php +++ b/tests/TreeInterpreterTest.php @@ -38,6 +38,27 @@ public function testFromlessProjectionTraversesArrays(): void $this->assertSame([1, 2, 3], $t->visit($this->fromlessProjection(), [1, 2, 3])); } + public function testBindsVariablesInScope(): void + { + $t = new TreeInterpreter(); + $node = ['type' => 'variable', 'value' => 'foo']; + + $this->assertSame('bar', $t->withScope(['foo' => 'bar'])->visit($node, [])); + $this->assertNull($t->withScope(['foo' => null])->visit($node, [])); + } + + public function testBindsVariablesOnACopy(): void + { + $t = new TreeInterpreter(); + $node = ['type' => 'variable', 'value' => 'foo']; + $t->withScope(['foo' => 'bar']); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Undefined variable: $foo'); + + $t->visit($node, []); + } + public function testWorksWithArrayObjectAsObject(): void { $runtime = new AstRuntime(); diff --git a/tests/compliance/letexpr.json b/tests/compliance/letexpr.json new file mode 100644 index 0000000..a3ddaf7 --- /dev/null +++ b/tests/compliance/letexpr.json @@ -0,0 +1,604 @@ +[{ + "comment": "Basic expressions", + "given": {"foo": {"bar": "baz"}}, + "cases": [ + { + "expression": "let $foo = foo in $foo", + "result": {"bar": "baz"} + }, + { + "expression": "let $foo = foo.bar in $foo", + "result": "baz" + }, + { + "expression": "let $foo = foo.bar in [$foo, $foo]", + "result": ["baz", "baz"] + }, + { + "comment": "Multiple assignments", + "expression": "let $foo = 'foo', $bar = 'bar' in [$foo, $bar]", + "result": ["foo", "bar"] + } + ] +}, { + "comment": "Nested expressions", + "given": {"a": "topval", "b": [{"a": "inner1"}, {"a": "inner2"}]}, + "cases": [ + { + "expression": "let $a = a in b[*].[a, $a, let $a = 'shadow' in $a]", + "result": [["inner1", "topval", "shadow"], ["inner2", "topval", "shadow"]] + }, + { + "comment": "Bindings only visible within expression clause", + "expression": "let $a = 'top-a' in let $a = 'in-a', $b = $a in $b", + "result": "top-a" + }, + { + "comment": "Outer binding is visible again after the inner expression", + "expression": "let $a = a in [let $a = 'shadow' in $a, $a]", + "result": ["shadow", "topval"] + }, + { + "expression": "[let $a = 'first' in $a, let $a = 'second' in $a]", + "result": ["first", "second"] + }, + { + "expression": "let $a = a in let $b = $a in [$a, $b]", + "result": ["topval", "topval"] + }, + { + "comment": "Let expression as a binding", + "expression": "let $a = let $b = a in $b in $a", + "result": "topval" + }, + { + "comment": "Current node is restored after a let expression in a binding", + "expression": "let $a = b[0] | let $b = a in $b in [$a, a]", + "result": ["inner1", "topval"] + } + ] +}, { + "comment": "Let as valid identifiers", + "given": {"let": {"let": "let-val", "in": "in-val"}}, + "cases": [ + { + "expression": "let $let = let in {let: let, in: $let}", + "result": { + "let": {"let": "let-val", "in": "in-val"}, + "in": {"let": "let-val", "in": "in-val"} + } + }, + { + "expression": "let $let = 'let' in { let: let, in: $let }", + "result": {"let": {"let": "let-val", "in": "in-val"}, "in": "let"} + }, + { + "expression": "let $let = 'let' in { let: 'let', in: $let }", + "result": {"let": "let", "in": "let"} + }, + { + "expression": "let", + "result": {"let": "let-val", "in": "in-val"} + }, + { + "expression": "let.let", + "result": "let-val" + }, + { + "expression": "let.in", + "result": "in-val" + }, + { + "expression": "let | in", + "result": "in-val" + }, + { + "expression": "[let.let, let.in]", + "result": ["let-val", "in-val"] + }, + { + "expression": "let $in = let.in in $in", + "result": "in-val" + }, + { + "expression": "let $a = let in $a.in", + "result": "in-val" + }, + { + "expression": "let in", + "error": "syntax" + }, + { + "expression": "let(@)", + "error": "unknown-function" + } + ] +}, { + "comment": "Projections stop", + "given": {"foo": [[0, 1], [2, 3], [4, 5]]}, + "cases": [ + { + "comment": "Projection is stopped when bound to variable", + "expression": "let $foo = foo[*] in $foo[0]", + "result": [0, 1] + }, + { + "expression": "let $foo = foo[*] in $foo[*][0]", + "result": [0, 2, 4] + } + ] +}, { + "comment": "Examples from Motivation section", + "given": [ + { + "home_state": "WA", + "states": [ + {"name": "WA", "cities": ["Seattle", "Bellevue", "Olympia"]}, + {"name": "CA", "cities": ["Los Angeles", "San Francisco"]}, + {"name": "NY", "cities": ["New York City", "Albany"]} + ] + }, + { + "home_state": "NY", + "states": [ + {"name": "WA", "cities": ["Seattle", "Bellevue", "Olympia"]}, + {"name": "CA", "cities": ["Los Angeles", "San Francisco"]}, + {"name": "NY", "cities": ["New York City", "Albany"]} + ] + } + ], + "cases": [ + { + "expression": "[*].[let $home_state = home_state in states[? name == $home_state].cities[]][]", + "result": [["Seattle", "Bellevue", "Olympia"], ["New York City", "Albany"]] + } + ] +}, { + "given": { + "imageDetails": [ + { + "repositoryName": "org/first-repo", + "imageTags": ["latest", "v1.0", "v1.2"], + "imageDigest": "sha256:abcd" + }, + { + "repositoryName": "org/second-repo", + "imageTags": ["v2.0", "v2.2"], + "imageDigest": "sha256:efgh" + } + ] + }, + "cases": [ + { + "expression": "imageDetails[].[\n let $repo = repositoryName,\n $digest = imageDigest\n in\n imageTags[].[@, $digest, $repo]\n][][]\n", + "result": [ + ["latest", "sha256:abcd", "org/first-repo"], + ["v1.0", "sha256:abcd", "org/first-repo"], + ["v1.2", "sha256:abcd", "org/first-repo"], + ["v2.0", "sha256:efgh", "org/second-repo"], + ["v2.2", "sha256:efgh", "org/second-repo"] + ] + } + ] +}, { + "comment": "Variables on the left of other expressions", + "given": { + "obj": {"x": 1, "y": 2}, + "list": [[1, 2], [3], [4, 5, 6]], + "items": [{"id": 2, "name": "b"}, {"id": 1, "name": "a"}] + }, + "cases": [ + { + "expression": "let $v = obj in $v.x", + "result": 1 + }, + { + "expression": "let $v = obj in $v.*", + "result": [1, 2] + }, + { + "expression": "let $v = list in $v[0]", + "result": [1, 2] + }, + { + "expression": "let $v = list in $v[-1]", + "result": [4, 5, 6] + }, + { + "expression": "let $v = list in $v[1:]", + "result": [[3], [4, 5, 6]] + }, + { + "expression": "let $v = list in $v[]", + "result": [1, 2, 3, 4, 5, 6] + }, + { + "expression": "let $v = list in $v[*][0]", + "result": [1, 3, 4] + }, + { + "expression": "let $v = items in $v[?id == `1`].name", + "result": ["a"] + }, + { + "expression": "let $v = items in $v | [0].name", + "result": "b" + }, + { + "expression": "let $v = items in length($v)", + "result": 2 + }, + { + "expression": "let $v = items[0].id, $w = items[1].id in $v > $w", + "result": true + }, + { + "expression": "let $v = missing in !$v", + "result": true + }, + { + "expression": "let $v = @ in items[*].[name, $v.obj.x]", + "result": [["b", 1], ["a", 1]] + } + ] +}, { + "comment": "Extent of the binding expressions", + "given": {"foo": "F", "bar": "B", "list": [1, 2, 3], "two": 2}, + "cases": [ + { + "expression": "let $a = list | [0] in $a", + "result": 1 + }, + { + "expression": "let $a = missing || foo, $b = foo && bar in [$a, $b]", + "result": ["F", "B"] + }, + { + "expression": "let $a = two == `2` in $a", + "result": true + } + ] +}, { + "comment": "Extent of the expression clause", + "given": {"foo": "F", "bar": "B", "list": [1, 2, 3], "two": 2}, + "cases": [ + { + "comment": "Variables are visible on the right of a pipe", + "expression": "let $a = foo in bar | $a", + "result": "F" + }, + { + "expression": "let $a = foo in bar | [@, $a]", + "result": ["B", "F"] + }, + { + "expression": "let $a = two in list[*] | [?@ == $a]", + "result": [2] + }, + { + "expression": "let $a = foo in $a || bar", + "result": "F" + }, + { + "expression": "let $a = missing in $a || bar", + "result": "B" + }, + { + "expression": "missing || let $a = foo in $a", + "result": "F" + }, + { + "expression": "foo && let $a = bar in $a", + "result": "B" + }, + { + "expression": "foo == let $a = foo in $a", + "result": true + }, + { + "expression": "!let $a = missing in $a", + "result": true + }, + { + "expression": "[let $a = foo in $a, bar]", + "result": ["F", "B"] + }, + { + "expression": "{a: let $a = foo in $a, b: bar}", + "result": {"a": "F", "b": "B"} + }, + { + "expression": "length(let $a = list in $a)", + "result": 3 + }, + { + "expression": "list[?let $a = @ in $a > `1`]", + "result": [2, 3] + }, + { + "expression": "(let $a = foo in $a)", + "result": "F" + }, + { + "expression": "(let $a = @ in $a).bar", + "result": "B" + } + ] +}, { + "comment": "Expression references", + "given": {"owner": "ann", "items": [{"id": 2, "name": "b"}, {"id": 1, "name": "a"}]}, + "cases": [ + { + "expression": "let $o = owner in map(&[name, $o], items)", + "result": [["b", "ann"], ["a", "ann"]] + }, + { + "expression": "let $o = owner in sort_by(items, &join('-', [$o, name]))[*].id", + "result": [1, 2] + }, + { + "comment": "Expression reference that does not use the variables in scope", + "expression": "let $o = owner in sort_by(items, &id)[*].name", + "result": ["a", "b"] + }, + { + "expression": "items[*].[let $i = @ in map(&[@, $i.name], `[1]`)]", + "result": [[[[1, "b"]]], [[[1, "a"]]]] + }, + { + "expression": "let $o = owner in let $o = 'bob' in map(&$o, items)", + "result": ["bob", "bob"] + }, + { + "comment": "Let expression inside an expression reference", + "expression": "map(&(let $n = name in [$n, id]), items)", + "result": [["b", 2], ["a", 1]] + }, + { + "comment": "Expression references keep the scope they were created in", + "expression": "map(let $o = owner in &[name, $o], items)", + "result": [["b", "ann"], ["a", "ann"]] + }, + { + "expression": "let $f = &$o in let $o = 'bob' in map($f, items)", + "error": "undefined-variable" + }, + { + "expression": "map(&map(f, `[0]`), items[*].{f: let $i = id in &$i})", + "result": [[2], [1]] + }, + { + "expression": "map(&$noexist, items)", + "error": "undefined-variable" + }, + { + "comment": "Undefined variables are only an error when evaluated", + "expression": "map(&$noexist, `[]`)", + "result": [] + } + ] +}, { + "comment": "Current node is null", + "given": null, + "cases": [ + { + "expression": "let $a = 'x' in $a", + "result": "x" + }, + { + "expression": "let $a = @ in $a", + "result": null + }, + { + "expression": "let $a = 'x' in @ || $a", + "result": "x" + }, + { + "expression": "let $a = 'x' in [$a]", + "result": null + } + ] +}, { + "given": {"foo": 1}, + "cases": [ + { + "expression": "missing | let $a = 'x' in $a", + "result": "x" + }, + { + "expression": "let $a = missing in $a", + "result": null + }, + { + "expression": "let $a = missing in [$a]", + "result": [null] + }, + { + "expression": "let $a = `null` in $a == `null`", + "result": true + } + ] +}, { + "comment": "Undefined variables", + "given": {"list": [1], "empty": []}, + "cases": [ + { + "expression": "$noexist", + "error": "undefined-variable" + }, + { + "comment": "Reference out of scope variable", + "expression": "[let $scope = 'foo' in [$scope], $scope]", + "error": "undefined-variable" + }, + { + "comment": "Bindings are not visible to their own expression", + "expression": "let $a = $a in $a", + "error": "undefined-variable" + }, + { + "comment": "Bindings are not visible to each other", + "expression": "let $a = 'x', $b = $a in $b", + "error": "undefined-variable" + }, + { + "comment": "Variable names are case sensitive", + "expression": "let $A = 'x' in $a", + "error": "undefined-variable" + }, + { + "expression": "list[*].[$noexist]", + "error": "undefined-variable" + }, + { + "comment": "Undefined variables are only an error when evaluated", + "expression": "empty[*].[$noexist]", + "result": [] + }, + { + "expression": "empty[?$noexist]", + "result": [] + }, + { + "expression": "'x' || $noexist", + "result": "x" + }, + { + "expression": "`false` && $noexist", + "result": false + } + ] +}, { + "comment": "Syntax errors", + "given": {"foo": {"let": 1}}, + "cases": [ + { + "comment": "Can't use var ref in RHS of subexpression", + "expression": "foo.$bar", + "error": "syntax" + }, + { + "expression": "foo.*.$bar", + "error": "syntax" + }, + { + "comment": "Can't use let expression in RHS of subexpression", + "expression": "foo.let $a = 'x' in $a", + "error": "syntax" + }, + { + "comment": "Only let starts a let expression", + "expression": "foo $a = 'x' in $a", + "error": "syntax" + }, + { + "expression": "foo $bar", + "error": "syntax" + }, + { + "expression": "$foo $bar", + "error": "syntax" + }, + { + "expression": "foo$bar", + "error": "syntax" + }, + { + "expression": "{$a: foo}", + "error": "syntax" + }, + { + "expression": "$a(@)", + "error": "syntax" + }, + { + "comment": "Missing in", + "expression": "let $a = foo", + "error": "syntax" + }, + { + "expression": "let $a = foo bar $a", + "error": "syntax" + }, + { + "expression": "let $a = foo \"in\" $a", + "error": "syntax" + }, + { + "expression": "let $a = foo ) $a", + "error": "syntax" + }, + { + "comment": "Missing expression", + "expression": "let $a = foo in", + "error": "syntax" + }, + { + "expression": "let $a = in $a", + "error": "syntax" + }, + { + "comment": "Invalid bindings", + "expression": "let $a = foo,", + "error": "syntax" + }, + { + "expression": "let $a = foo, in $a", + "error": "syntax" + }, + { + "expression": "let $a, $b = foo in $a", + "error": "syntax" + }, + { + "expression": "let $a foo in $a", + "error": "syntax" + }, + { + "expression": "let $a == foo in $a", + "error": "syntax" + }, + { + "expression": "let = foo in $a", + "error": "syntax" + }, + { + "comment": "Invalid variable names", + "expression": "$", + "error": "syntax" + }, + { + "expression": "$ a", + "error": "syntax" + }, + { + "expression": "$1", + "error": "syntax" + }, + { + "expression": "$$a", + "error": "syntax" + }, + { + "expression": "$\"a\"", + "error": "syntax" + }, + { + "expression": "let $ = foo in @", + "error": "syntax" + }, + { + "comment": "Assignment outside of a let expression", + "expression": "$a = foo", + "error": "syntax" + }, + { + "expression": "foo = bar", + "error": "syntax" + }, + { + "expression": "foo[?let = `1`]", + "error": "syntax" + } + ] +}]