module scheme import arena import reader fn case_match_fixture(pattern_source string, input_source string, literal_names []string) (SyntaxCaseMatcher, i32, SyntaxCaseCaptures) { mut code := &Code{ doc: reader.read_all(pattern_source + '\n' + input_source) or { panic(err) } } mut builder := Builder{ code: code, function: &Function{ code: code } } tree, patterns := builder.freeze_syntax([code.doc.forms[0]]) or { panic(err) } mut literals := map[i32]SyntaxBinding{} for i, identifier in tree.identifiers { if identifier.raw_name in literal_names { literals[i32(i)] = identifier.binding } } pattern := tree.compile_case_pattern(patterns[0], literals) or { panic(err) } input, roots := builder.freeze_syntax_input([code.doc.forms[1]]) or { panic(err) } data := &SyntaxObjectData{ tree: input } return SyntaxCaseMatcher{ tree: tree, pattern: &pattern, literals: literals, input: data }, roots[0], SyntaxCaseCaptures{ input: data, pattern: &pattern } } fn test_syntax_case_patterns_match_the_entire_root_including_head() { for sample in [ ['x', '42'], ['x', '(one two)'], ['_', '#t'], ['()', '()'], ['#(x y)', '#(1 2)'], ['1', '#e1.0'], ['"λ😀"', '"λ😀"'], ['(head tail)', '(1 2)'], ['(head . tail)', '(1 2 . end)'], ['(x ... last)', '(1 2 3)'], ['#(first x ... last)', '#(1 2)'], ['(literal x)', '(literal 42)'], ] { matcher, root, mut captures := case_match_fixture(sample[0], sample[1], ['literal']) assert matcher.matches(mut captures, root) or { panic(err) }, '${sample}' } for sample in [ ['()', '(1)'], ['#()', '()'], ['1', '1.0'], ['#t', '#f'], ['(literal x)', '(other 42)'], ['(literal x)', '(42 42)'], ['(x ... last)', '()'], ['#(first x ... last)', '#(1)'], ['"a"', '"b"'], ['#\\space', '#\\newline'], ] { matcher, root, mut captures := case_match_fixture(sample[0], sample[1], ['literal']) assert !(matcher.matches(mut captures, root) or { panic(err) }), '${sample}' } } fn test_syntax_case_captures_include_head_and_share_the_input_document() { matcher, root, mut captures := case_match_fixture('(head . tail)', '(1 2 . 3)', []) assert matcher.matches(mut captures, root) or { panic(err) } assert matcher.pattern.variables.len == 2 for i, variable in matcher.pattern.variables { name := matcher.tree.identifiers[variable.identifier].raw_name value := captures.value(i, []) or { panic(err) } object := syntax_object_ref(value) or { panic(err) } assert voidptr(object.data) == voidptr(matcher.input) if name == 'head' { assert matcher.input.tree.values[matcher.input.tree.nodes[object.index].car].as_integer() or { panic(err) } == 1 } else { assert name == 'tail' && matcher.input.tree.nodes[object.index].kind == .pair } } } fn test_syntax_case_nested_captures_keep_ragged_dimensions() { matcher, root, mut captures := case_match_fixture('((x ...) ...)', '((1 2) () (3))', []) assert matcher.matches(mut captures, root) or { panic(err) } assert matcher.pattern.variables.len == 1 && matcher.pattern.variables[0].depth == 2 assert captures.coordinates.items[syntax_capture_key(0, 0)] == 3 for row, expected in [[1, 2], []int{}, [3]] { outer := captures.coordinates.child_path(0, row) or { panic(err) } assert captures.coordinates.items[syntax_capture_key(0, outer)] == expected.len for col, integer in expected { inner := captures.coordinates.child_path(outer, col) or { panic(err) } object := syntax_object_ref(captures.value(0, [row, col]) or { panic(err) }) or { panic(err) } assert matcher.input.tree.values[matcher.input.tree.nodes[object.index].car].as_integer() or { panic(err) } == integer } } } fn test_syntax_case_root_validation_keeps_ranks_duplicates_and_ellipsis_checks() { for source in ['(x x)', '#(x x)', '...', '(x ... y ...)', '#(x ... y ...)', '(x (x ...))'] { mut code := &Code{ doc: reader.read_all(source) or { panic(err) } } mut builder := Builder{ code: code, function: &Function{ code: code } } tree, roots := builder.freeze_syntax(code.doc.forms) or { panic(err) } if _ := tree.compile_case_pattern(roots[0], map[i32]SyntaxBinding{}) { assert false, source } else { assert err is EvalError } } } fn test_syntax_case_literal_wildcard_precedence_and_zero_storage_repeats() { for pair in [['(literal ...)', '(' + 'literal '.repeat(20000) + ')'], ['#(literal ...)', '#(' + 'literal '.repeat(20000) + ')'], ['(_ ...)', '(' + '42 '.repeat(20000) + ')']] { matcher, root, mut captures := case_match_fixture(pair[0], pair[1], ['literal']) mut region := arena.new() region.push() before := region.used() assert matcher.matches(mut captures, root) or { panic(err) } assert captures.coordinates.items.len == 0 && captures.coordinates.paths.len == 0 assert region.used() == before region.pop() region.free() } matcher, root, mut captures := case_match_fixture('_', 'anything', ['_']) assert !(matcher.matches(mut captures, root) or { panic(err) }) } fn test_syntax_case_cyclic_subforms_can_be_captured_but_repetition_cannot_loop() { for source in ['#1=(a . #1#)', '#1=#(#1#)', '#1=(a b c . #1#)'] { matcher, root, mut captures := case_match_fixture('x', source, []) assert matcher.matches(mut captures, root) or { panic(err) } value := captures.value(0, []) or { panic(err) } assert value.syntax_index == root } for pattern in ['(_ ...)', '(_ ... last)', '(first _ ...)'] { for source in ['#1=(a . #1#)', '(p q . #1=(a b c . #1#))'] { matcher, root, mut captures := case_match_fixture(pattern, source, []) if _ := matcher.matches(mut captures, root) { assert false, '${pattern}: ${source}' } else { assert err.msg().contains('cyclic syntax-case repetition') } } } } fn test_syntax_case_capture_reads_validate_rank_dimensions_and_variable() { matcher, root, mut captures := case_match_fixture('(x ...)', '(1 2)', []) assert matcher.matches(mut captures, root) or { panic(err) } if _ := captures.value(0, []) { assert false } else { assert err.msg().contains('rank mismatch') } for indices in [[-1], [2], [99]] { if _ := captures.value(0, indices) { assert false } else { assert err.msg().contains('index out of range') } } if _ := captures.value(1, [0]) { assert false } else { assert err.msg().contains('invalid syntax-case capture variable') } // Shared coordinate paths can exist for a wider sibling repetition. Bounds // must come from this variable's dimensions, rather than path presence alone. nested, input, mut ragged := case_match_fixture('((x ...) ...)', '((1 2) ())', []) assert nested.matches(mut ragged, input) or { panic(err) } if _ := ragged.value(0, [1, 0]) { assert false } else { assert err.msg().contains('index out of range') } object := syntax_object_ref(captures.value(0, [1]) or { panic(err) }) or { panic(err) } assert matcher.input.tree.values[matcher.input.tree.nodes[object.index].car].as_integer() or { panic(err) } == 2 }