| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | fn case_match_fixture(pattern_source string, input_source string, literal_names []string) (SyntaxCaseMatcher, i32, SyntaxCaseCaptures) { |
| 7 | mut code := &Code{ doc: reader.read_all(pattern_source + '\n' + input_source) or { panic(err) } } |
| 8 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 9 | tree, patterns := builder.freeze_syntax([code.doc.forms[0]]) or { panic(err) } |
| 10 | mut literals := map[i32]SyntaxBinding{} |
| 11 | for i, identifier in tree.identifiers { |
| 12 | if identifier.raw_name in literal_names { literals[i32(i)] = identifier.binding } |
| 13 | } |
| 14 | pattern := tree.compile_case_pattern(patterns[0], literals) or { panic(err) } |
| 15 | input, roots := builder.freeze_syntax_input([code.doc.forms[1]]) or { panic(err) } |
| 16 | data := &SyntaxObjectData{ tree: input } |
| 17 | return SyntaxCaseMatcher{ tree: tree, pattern: &pattern, literals: literals, input: data }, roots[0], SyntaxCaseCaptures{ input: data, pattern: &pattern } |
| 18 | } |
| 19 | |
| 20 | fn test_syntax_case_patterns_match_the_entire_root_including_head() { |
| 21 | for sample in [ |
| 22 | ['x', '42'], |
| 23 | ['x', '(one two)'], |
| 24 | ['_', '#t'], |
| 25 | ['()', '()'], |
| 26 | ['#(x y)', '#(1 2)'], |
| 27 | ['1', '#e1.0'], |
| 28 | ['"λ😀"', '"λ😀"'], |
| 29 | ['(head tail)', '(1 2)'], |
| 30 | ['(head . tail)', '(1 2 . end)'], |
| 31 | ['(x ... last)', '(1 2 3)'], |
| 32 | ['#(first x ... last)', '#(1 2)'], |
| 33 | ['(literal x)', '(literal 42)'], |
| 34 | ] { |
| 35 | matcher, root, mut captures := case_match_fixture(sample[0], sample[1], ['literal']) |
| 36 | assert matcher.matches(mut captures, root) or { panic(err) }, '${sample}' |
| 37 | } |
| 38 | for sample in [ |
| 39 | ['()', '(1)'], |
| 40 | ['#()', '()'], |
| 41 | ['1', '1.0'], |
| 42 | ['#t', '#f'], |
| 43 | ['(literal x)', '(other 42)'], |
| 44 | ['(literal x)', '(42 42)'], |
| 45 | ['(x ... last)', '()'], |
| 46 | ['#(first x ... last)', '#(1)'], |
| 47 | ['"a"', '"b"'], |
| 48 | ['#\\space', '#\\newline'], |
| 49 | ] { |
| 50 | matcher, root, mut captures := case_match_fixture(sample[0], sample[1], ['literal']) |
| 51 | assert !(matcher.matches(mut captures, root) or { panic(err) }), '${sample}' |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn test_syntax_case_captures_include_head_and_share_the_input_document() { |
| 56 | matcher, root, mut captures := case_match_fixture('(head . tail)', '(1 2 . 3)', []) |
| 57 | assert matcher.matches(mut captures, root) or { panic(err) } |
| 58 | assert matcher.pattern.variables.len == 2 |
| 59 | for i, variable in matcher.pattern.variables { |
| 60 | name := matcher.tree.identifiers[variable.identifier].raw_name |
| 61 | value := captures.value(i, []) or { panic(err) } |
| 62 | object := syntax_object_ref(value) or { panic(err) } |
| 63 | assert voidptr(object.data) == voidptr(matcher.input) |
| 64 | if name == 'head' { |
| 65 | assert matcher.input.tree.values[matcher.input.tree.nodes[object.index].car].as_integer() or { panic(err) } == 1 |
| 66 | } else { |
| 67 | assert name == 'tail' && matcher.input.tree.nodes[object.index].kind == .pair |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn test_syntax_case_nested_captures_keep_ragged_dimensions() { |
| 73 | matcher, root, mut captures := case_match_fixture('((x ...) ...)', '((1 2) () (3))', []) |
| 74 | assert matcher.matches(mut captures, root) or { panic(err) } |
| 75 | assert matcher.pattern.variables.len == 1 && matcher.pattern.variables[0].depth == 2 |
| 76 | assert captures.coordinates.items[syntax_capture_key(0, 0)] == 3 |
| 77 | for row, expected in [[1, 2], []int{}, [3]] { |
| 78 | outer := captures.coordinates.child_path(0, row) or { panic(err) } |
| 79 | assert captures.coordinates.items[syntax_capture_key(0, outer)] == expected.len |
| 80 | for col, integer in expected { |
| 81 | inner := captures.coordinates.child_path(outer, col) or { panic(err) } |
| 82 | object := syntax_object_ref(captures.value(0, [row, col]) or { panic(err) }) or { panic(err) } |
| 83 | assert matcher.input.tree.values[matcher.input.tree.nodes[object.index].car].as_integer() or { panic(err) } == integer |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn test_syntax_case_root_validation_keeps_ranks_duplicates_and_ellipsis_checks() { |
| 89 | for source in ['(x x)', '#(x x)', '...', '(x ... y ...)', '#(x ... y ...)', '(x (x ...))'] { |
| 90 | mut code := &Code{ doc: reader.read_all(source) or { panic(err) } } |
| 91 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 92 | tree, roots := builder.freeze_syntax(code.doc.forms) or { panic(err) } |
| 93 | if _ := tree.compile_case_pattern(roots[0], map[i32]SyntaxBinding{}) { |
| 94 | assert false, source |
| 95 | } else { |
| 96 | assert err is EvalError |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | fn test_syntax_case_literal_wildcard_precedence_and_zero_storage_repeats() { |
| 102 | for pair in [['(literal ...)', '(' + 'literal '.repeat(20000) + ')'], |
| 103 | ['#(literal ...)', '#(' + 'literal '.repeat(20000) + ')'], |
| 104 | ['(_ ...)', '(' + '42 '.repeat(20000) + ')']] { |
| 105 | matcher, root, mut captures := case_match_fixture(pair[0], pair[1], ['literal']) |
| 106 | mut region := arena.new() |
| 107 | region.push() |
| 108 | before := region.used() |
| 109 | assert matcher.matches(mut captures, root) or { panic(err) } |
| 110 | assert captures.coordinates.items.len == 0 && captures.coordinates.paths.len == 0 |
| 111 | assert region.used() == before |
| 112 | region.pop() |
| 113 | region.free() |
| 114 | } |
| 115 | matcher, root, mut captures := case_match_fixture('_', 'anything', ['_']) |
| 116 | assert !(matcher.matches(mut captures, root) or { panic(err) }) |
| 117 | } |
| 118 | |
| 119 | fn test_syntax_case_cyclic_subforms_can_be_captured_but_repetition_cannot_loop() { |
| 120 | for source in ['#1=(a . #1#)', '#1=#(#1#)', '#1=(a b c . #1#)'] { |
| 121 | matcher, root, mut captures := case_match_fixture('x', source, []) |
| 122 | assert matcher.matches(mut captures, root) or { panic(err) } |
| 123 | value := captures.value(0, []) or { panic(err) } |
| 124 | assert value.syntax_index == root |
| 125 | } |
| 126 | for pattern in ['(_ ...)', '(_ ... last)', '(first _ ...)'] { |
| 127 | for source in ['#1=(a . #1#)', '(p q . #1=(a b c . #1#))'] { |
| 128 | matcher, root, mut captures := case_match_fixture(pattern, source, []) |
| 129 | if _ := matcher.matches(mut captures, root) { |
| 130 | assert false, '${pattern}: ${source}' |
| 131 | } else { |
| 132 | assert err.msg().contains('cyclic syntax-case repetition') |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fn test_syntax_case_capture_reads_validate_rank_dimensions_and_variable() { |
| 139 | matcher, root, mut captures := case_match_fixture('(x ...)', '(1 2)', []) |
| 140 | assert matcher.matches(mut captures, root) or { panic(err) } |
| 141 | if _ := captures.value(0, []) { |
| 142 | assert false |
| 143 | } else { |
| 144 | assert err.msg().contains('rank mismatch') |
| 145 | } |
| 146 | for indices in [[-1], [2], [99]] { |
| 147 | if _ := captures.value(0, indices) { |
| 148 | assert false |
| 149 | } else { |
| 150 | assert err.msg().contains('index out of range') |
| 151 | } |
| 152 | } |
| 153 | if _ := captures.value(1, [0]) { |
| 154 | assert false |
| 155 | } else { |
| 156 | assert err.msg().contains('invalid syntax-case capture variable') |
| 157 | } |
| 158 | // Shared coordinate paths can exist for a wider sibling repetition. Bounds |
| 159 | // must come from this variable's dimensions, rather than path presence alone. |
| 160 | nested, input, mut ragged := case_match_fixture('((x ...) ...)', '((1 2) ())', []) |
| 161 | assert nested.matches(mut ragged, input) or { panic(err) } |
| 162 | if _ := ragged.value(0, [1, 0]) { |
| 163 | assert false |
| 164 | } else { |
| 165 | assert err.msg().contains('index out of range') |
| 166 | } |
| 167 | object := syntax_object_ref(captures.value(0, [1]) or { panic(err) }) or { panic(err) } |
| 168 | assert matcher.input.tree.values[matcher.input.tree.nodes[object.index].car].as_integer() or { panic(err) } == 2 |
| 169 | } |
| 170 | |