| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | // Private matcher regressions; authored read-only against frozen source12. |
| 6 | // Expected semantic results are independent of its coordinate implementation. |
| 7 | fn review_case_fixture(pattern_source string, input_source string, literal_names []string) (SyntaxCaseMatcher, i32, SyntaxCaseCaptures) { |
| 8 | mut code := &Code{ doc: reader.read_all(pattern_source + '\n' + input_source) or { panic(err.msg()) } } |
| 9 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 10 | tree, roots := builder.freeze_syntax([code.doc.forms[0]]) or { panic(err.msg()) } |
| 11 | mut literals := map[i32]SyntaxBinding{} |
| 12 | for i, info in tree.identifiers { |
| 13 | if info.raw_name in literal_names { literals[i32(i)] = info.binding } |
| 14 | } |
| 15 | pattern := tree.compile_case_pattern(roots[0], literals) or { panic(err.msg()) } |
| 16 | input, inputs := builder.freeze_syntax_input([code.doc.forms[1]]) or { panic(err.msg()) } |
| 17 | data := &SyntaxObjectData{ tree: input } |
| 18 | return SyntaxCaseMatcher{ tree: tree, pattern: &pattern, literals: literals, input: data }, inputs[0], SyntaxCaseCaptures{ input: data, pattern: &pattern } |
| 19 | } |
| 20 | |
| 21 | fn review_case_variable(matcher SyntaxCaseMatcher, name string) int { |
| 22 | for i, variable in matcher.pattern.variables { |
| 23 | if matcher.tree.identifiers[variable.identifier].raw_name == name { return i } |
| 24 | } |
| 25 | panic('missing pattern variable ' + name) |
| 26 | } |
| 27 | |
| 28 | fn review_case_integer(captures &SyntaxCaseCaptures, variable int, indices []int) i64 { |
| 29 | value := captures.value(variable, indices) or { panic(err.msg()) } |
| 30 | object := syntax_object_ref(value) or { panic(err.msg()) } |
| 31 | return object.data.tree.values[object.data.tree.nodes[object.index].car].as_integer() or { panic(err.msg()) } |
| 32 | } |
| 33 | |
| 34 | fn review_case_literal_match(expected SyntaxBinding, actual SyntaxBinding, owner &Owner, input_owner &Owner) bool { |
| 35 | tree := &SyntaxTree{ nodes: [SyntaxTreeNode{ kind: .symbol, car: 0 }], identifiers: [SyntaxIdentifier{ name: expected.name, raw_name: expected.name, binding: expected }] } |
| 36 | literals := { |
| 37 | i32(0): expected |
| 38 | } |
| 39 | pattern := tree.compile_case_pattern(0, literals) or { panic(err.msg()) } |
| 40 | input := &SyntaxObjectData{ owner: input_owner, tree: &SyntaxTree{ nodes: [SyntaxTreeNode{ kind: .symbol, car: 0 }], identifiers: [SyntaxIdentifier{ name: actual.name, raw_name: actual.name, binding: actual }] } } |
| 41 | matcher := SyntaxCaseMatcher{ tree: tree, pattern: &pattern, literals: literals, owner: owner, input: input } |
| 42 | mut captures := SyntaxCaseCaptures{ input: input, pattern: &pattern } |
| 43 | return matcher.matches(mut captures, 0) or { panic(err.msg()) } |
| 44 | } |
| 45 | |
| 46 | fn test_independent_case_literal_head_is_significant_and_alias_binding_can_match() { |
| 47 | matcher, root, mut captures := review_case_fixture('(lit x)', '(other 42)', ['lit']) |
| 48 | assert !(matcher.matches(mut captures, root) or { panic(err.msg()) }) |
| 49 | owner := &Owner{} |
| 50 | assert review_case_literal_match(SyntaxBinding{ kind: .global, name: 'lit', global_slot: 12 }, SyntaxBinding{ kind: .global, name: 'renamed-lit', global_slot: 12 }, owner, owner) |
| 51 | assert !review_case_literal_match(SyntaxBinding{ kind: .global, name: 'lit', global_slot: 12 }, SyntaxBinding{ kind: .global, name: 'lit', global_slot: 13 }, owner, owner) |
| 52 | } |
| 53 | |
| 54 | fn test_independent_case_nested_vector_list_suffixes_keep_ragged_coordinates() { |
| 55 | matcher, root, mut captures := review_case_fixture('#(head (x ... end) ... tail)', '#(9 (1 2 10) (20) (3 30) 8)', []) |
| 56 | assert matcher.matches(mut captures, root) or { panic(err.msg()) } |
| 57 | x := review_case_variable(matcher, 'x') |
| 58 | end := review_case_variable(matcher, 'end') |
| 59 | assert matcher.pattern.variables[x].depth == 2 |
| 60 | assert matcher.pattern.variables[end].depth == 1 |
| 61 | assert review_case_integer(captures, review_case_variable(matcher, 'head'), []) == 9 |
| 62 | assert review_case_integer(captures, review_case_variable(matcher, 'tail'), []) == 8 |
| 63 | assert captures.coordinates.items[syntax_capture_key(x, 0)] == 3 |
| 64 | for row, expected in [[1, 2], []int{}, [3]] { |
| 65 | path := captures.coordinates.child_path(0, row) or { panic(err.msg()) } |
| 66 | assert captures.coordinates.items[syntax_capture_key(x, path)] == expected.len |
| 67 | assert review_case_integer(captures, end, [row]) == [10, 20, 30][row] |
| 68 | for col, number in expected { |
| 69 | assert review_case_integer(captures, x, [row, col]) == number |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn test_independent_case_zero_outer_and_all_empty_inner_dimensions() { |
| 75 | for input_source in ['#()', '#(() ())'] { |
| 76 | matcher, root, mut captures := review_case_fixture('#((x ...) ...)', input_source, []) |
| 77 | assert matcher.matches(mut captures, root) or { panic(err.msg()) } |
| 78 | x := review_case_variable(matcher, 'x') |
| 79 | rows := if input_source == '#()' { 0 } else { 2 } |
| 80 | assert captures.coordinates.items[syntax_capture_key(x, 0)] == rows |
| 81 | for row in 0 .. rows { |
| 82 | path := captures.coordinates.child_path(0, row) or { panic(err.msg()) } |
| 83 | assert captures.coordinates.items[syntax_capture_key(x, path)] == 0 |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn test_independent_case_dotted_repetition_reserves_suffix_and_final_tail() { |
| 89 | matcher, root, mut captures := review_case_fixture('(first x ... last . rest)', '(1 2 3 4 . 5)', []) |
| 90 | assert matcher.matches(mut captures, root) or { panic(err.msg()) } |
| 91 | for name, number in { |
| 92 | 'first': 1 |
| 93 | 'last': 4 |
| 94 | 'rest': 5 |
| 95 | } { |
| 96 | assert review_case_integer(captures, review_case_variable(matcher, name), []) == number |
| 97 | } |
| 98 | x := review_case_variable(matcher, 'x') |
| 99 | assert captures.coordinates.items[syntax_capture_key(x, 0)] == 2 |
| 100 | for col, number in [2, 3] { |
| 101 | assert review_case_integer(captures, x, [col]) == number |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn test_independent_case_literal_bindings_respect_owners_unbound_names_and_transformers() { |
| 106 | a := &Owner{} |
| 107 | b := &Owner{} |
| 108 | bound := SyntaxBinding{ kind: .global, name: 'v', global_slot: 8 } |
| 109 | assert !review_case_literal_match(bound, bound, a, b) |
| 110 | unbound := SyntaxBinding{ kind: .global, name: 'u', unbound: true } |
| 111 | assert review_case_literal_match(unbound, unbound, a, b) |
| 112 | assert !review_case_literal_match(unbound, SyntaxBinding{ kind: .global, name: 'U', unbound: true }, a, a) |
| 113 | assert !review_case_literal_match(unbound, SyntaxBinding{ kind: .global, name: 'u', global_slot: 8 }, a, a) |
| 114 | transformer := &SyntaxTransformer{} |
| 115 | local := SyntaxBinding{ kind: .transformer, name: 'm', transformer: transformer } |
| 116 | assert review_case_literal_match(local, local, a, a) |
| 117 | assert !review_case_literal_match(local, local, a, b) |
| 118 | assert !review_case_literal_match(local, SyntaxBinding{ kind: .transformer, name: 'm', transformer: &SyntaxTransformer{} }, a, a) |
| 119 | } |
| 120 | |
| 121 | fn test_independent_case_literal_lexicals_distinguish_shadowing_and_accept_detached_identity() { |
| 122 | code := &Code{ doc: reader.read_all('') or { panic(err.msg()) } } |
| 123 | outer := &Function{ code: code } |
| 124 | inner := &Function{ code: code } |
| 125 | binding := SyntaxBinding{ kind: .lexical, name: 'lit', function: outer, slot: 0 } |
| 126 | frozen := freeze_syntax_binding(binding) or { panic(err.msg()) } |
| 127 | assert review_case_literal_match(frozen, binding, unsafe { nil }, unsafe { nil }) |
| 128 | assert !review_case_literal_match(frozen, SyntaxBinding{ kind: .lexical, name: 'lit', function: inner, slot: 0 }, unsafe { nil }, unsafe { nil }) |
| 129 | assert !review_case_literal_match(frozen, SyntaxBinding{ kind: .lexical, name: 'lit', function: outer, slot: 1 }, unsafe { nil }, unsafe { nil }) |
| 130 | } |
| 131 | |
| 132 | fn test_independent_case_shared_and_cyclic_subforms_are_capture_handles() { |
| 133 | matcher, root, mut captures := review_case_fixture('(head . rest)', '#1=(7 . #1#)', []) |
| 134 | assert matcher.matches(mut captures, root) or { panic(err.msg()) } |
| 135 | rest := captures.value(review_case_variable(matcher, 'rest'), []) or { panic(err.msg()) } |
| 136 | assert rest.syntax_index == root |
| 137 | assert voidptr(rest.pointer()) == voidptr(matcher.input) |
| 138 | vector_matcher, vector_root, mut vector_captures := review_case_fixture('#(x ... last)', '#1=#(1 #1# 2)', []) |
| 139 | assert vector_matcher.matches(mut vector_captures, vector_root) or { panic(err.msg()) } |
| 140 | x := review_case_variable(vector_matcher, 'x') |
| 141 | assert vector_captures.coordinates.items[syntax_capture_key(x, 0)] == 2 |
| 142 | cycle := vector_captures.value(x, [1]) or { panic(err.msg()) } |
| 143 | assert cycle.syntax_index == vector_root |
| 144 | assert voidptr(cycle.pointer()) == voidptr(vector_matcher.input) |
| 145 | assert review_case_integer(vector_captures, review_case_variable(vector_matcher, 'last'), []) == 2 |
| 146 | } |
| 147 | |
| 148 | fn test_independent_case_nested_repetition_detects_cycle_without_consuming_forever() { |
| 149 | for pattern in ['((_ ...) ...)', '((_ ... end) ...)'] { |
| 150 | matcher, root, mut captures := review_case_fixture(pattern, '(#1=(1 2 3 . #1#))', []) |
| 151 | if _ := matcher.matches(mut captures, root) { |
| 152 | assert false, pattern |
| 153 | } else { |
| 154 | assert err.msg().contains('cyclic syntax-case repetition') |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | fn test_independent_case_capture_document_guard_and_missing_capture_errors() { |
| 160 | matcher, root, mut captures := review_case_fixture('x', '(a b)', []) |
| 161 | foreign := &SyntaxObjectData{ tree: matcher.input.tree } |
| 162 | mut wrong := SyntaxCaseCaptures{ input: foreign, pattern: matcher.pattern } |
| 163 | if _ := matcher.matches(mut wrong, root) { |
| 164 | assert false |
| 165 | } else { |
| 166 | assert err.msg().contains('document mismatch') |
| 167 | } |
| 168 | assert matcher.matches(mut captures, root) or { panic(err.msg()) } |
| 169 | if _ := captures.value(1000, []) { |
| 170 | assert false |
| 171 | } else { |
| 172 | assert err.msg().contains('invalid syntax-case capture variable') |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // A syntax-case datum pattern compares in the sense of equal?, including |
| 177 | // bytevector contents. This guards the equal? correction after source11. |
| 178 | fn test_independent_case_bytevector_datum_patterns_use_content_equality() { |
| 179 | for source in ['#u8()', '#u8(0 1 127 255)'] { |
| 180 | matcher, root, mut captures := review_case_fixture(source, source, []) |
| 181 | assert matcher.matches(mut captures, root) or { panic(err.msg()) }, source |
| 182 | } |
| 183 | for input_source in ['#u8(0 2 127 255)', '#u8(0 1 127)', '#(0 1 127 255)'] { |
| 184 | matcher, root, mut captures := review_case_fixture('#u8(0 1 127 255)', input_source, []) |
| 185 | assert !(matcher.matches(mut captures, root) or { panic(err.msg()) }), input_source |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn test_independent_case_value_extraction_checks_variable_specific_dimensions_and_rank() { |
| 190 | matcher, root, mut captures := review_case_fixture('((x ...) (y ...))', '((1) (2 3 4))', []) |
| 191 | assert matcher.matches(mut captures, root) or { panic(err.msg()) } |
| 192 | x := review_case_variable(matcher, 'x') |
| 193 | y := review_case_variable(matcher, 'y') |
| 194 | assert review_case_integer(captures, x, [0]) == 1 |
| 195 | assert review_case_integer(captures, y, [2]) == 4 |
| 196 | // The path for index 2 exists for y, but must not become a valid x path. |
| 197 | for indices in [[1], [2], [-1]] { |
| 198 | if _ := captures.value(x, indices) { |
| 199 | assert false |
| 200 | } else { |
| 201 | assert err.msg().contains('index out of range') |
| 202 | } |
| 203 | } |
| 204 | for indices in [[]int{}, [0, 0]] { |
| 205 | if _ := captures.value(x, indices) { |
| 206 | assert false |
| 207 | } else { |
| 208 | assert err.msg().contains('rank mismatch') |
| 209 | } |
| 210 | } |
| 211 | // Matching guards the pattern identity as well as the shared input owner. |
| 212 | copied := *matcher.pattern |
| 213 | mut wrong_pattern := SyntaxCaseCaptures{ input: matcher.input, pattern: &copied } |
| 214 | if _ := matcher.matches(mut wrong_pattern, root) { |
| 215 | assert false |
| 216 | } else { |
| 217 | assert err.msg().contains('document mismatch') |
| 218 | } |
| 219 | } |
| 220 | |