| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | fn test_case_empty_raw_input_matches_repeated_and_dotted_patterns_like_wrapped_input() { |
| 7 | for source in ['(x ...)', '((x ...) ...)', '(_ ...)', '(x ... . tail)', '(x ... . ())', |
| 8 | '(x ... . _)'] { |
| 9 | matcher := empty_case_spec(source) |
| 10 | mut raw := SyntaxCaseCaptures{ pattern: matcher.pattern } |
| 11 | assert matcher.matches_value(nil_value(), mut raw) or { panic(err) }, source |
| 12 | mut wrapped := SyntaxCaseCaptures{ pattern: matcher.pattern } |
| 13 | assert matcher.matches_value(empty_case_wrapped(), mut wrapped) or { panic(err) }, source |
| 14 | for variable, spec in matcher.pattern.variables { |
| 15 | if spec.depth > 0 { |
| 16 | assert raw.coordinates.items[syntax_capture_key(variable, 0)] == 0 |
| 17 | assert wrapped.coordinates.items[syntax_capture_key(variable, 0)] == 0 |
| 18 | } else { |
| 19 | assert raw.value(variable, []) or { panic(err) }.tag == .nil |
| 20 | assert syntax_to_datum(wrapped.value(variable, []) or { panic(err) }) or { panic(err) }.tag == .nil |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | for source in ['(first x ...)', '(x ... last)', '(x)', '#(x ...)', '(x ... . 42)'] { |
| 25 | matcher := empty_case_spec(source) |
| 26 | mut frame := SyntaxCaseCaptures{ pattern: matcher.pattern } |
| 27 | assert !(matcher.matches_value(nil_value(), mut frame) or { panic(err) }), source |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn test_case_empty_zero_variable_predicate_retains_no_temporary_storage() { |
| 32 | matcher := empty_case_spec('(_ ...)') |
| 33 | mut captures := SyntaxCaseCaptures{ pattern: matcher.pattern } |
| 34 | mut region := arena.new(chunk_size: 1048576) |
| 35 | region.push() |
| 36 | assert matcher.matches_value(nil_value(), mut captures) or { panic(err) } |
| 37 | assert captures.coordinates.items.len == 0 && captures.coordinates.paths.len == 0 && isnil(captures.values) |
| 38 | assert region.used() == 0 |
| 39 | region.pop() |
| 40 | region.free() |
| 41 | } |
| 42 | |
| 43 | fn empty_case_spec(source string) SyntaxCaseMatcher { |
| 44 | code := &Code{ doc: reader.read_all(source) or { panic(err) } } |
| 45 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 46 | tree, roots := builder.freeze_syntax(code.doc.forms) or { panic(err) } |
| 47 | pattern := tree.compile_case_pattern(roots[0], map[i32]SyntaxBinding{}) or { panic(err) } |
| 48 | return SyntaxCaseMatcher{ tree: tree, pattern: &pattern } |
| 49 | } |
| 50 | |
| 51 | fn empty_case_wrapped() Value { |
| 52 | tree := &SyntaxTree{ nodes: [SyntaxTreeNode{ kind: .empty_list }] } |
| 53 | return wrapped_syntax(&SyntaxObjectData{ tree: tree }, 0) or { panic(err) } |
| 54 | } |
| 55 | |