-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaprules.scm
130 lines (116 loc) · 4.44 KB
/
maprules.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
;;; -*- Mode: Scheme; Character-encoding: utf-8; -*-
;;; Copyright (C) 2005-2020 beingmeta, inc. All rights reserved.
;;; Copyright (C) 2020-2022 Kenneth Haase ([email protected])
;;; Managing custom maps into BRICO
(in-module 'brico/maprules)
(use-module 'reflection)
(module-export! '{custom-map-name custom-map-language custom-map-handler})
(module-export! '{custom-get conform-maprule})
;;; A custom entry is a vector of the form:
;;; #(name lang table)
;;; and rulesets are used to manage the replacement of entries
;;; with the same name.
(define custom-map-name first)
(define custom-map-language second)
(define custom-map-handler third)
(define (custom-get-list key language custom)
(tryif (pair? custom)
(let* ((entry (car custom))
(handler (custom-map-handler entry)))
(try
(if (custom-map-language entry)
(tryif (eq? language (custom-map-language entry))
(if (table? handler) (get handler key)
(tryif (applicable? handler)
(handler key))))
(if (table? handler)
(get handler (cons language key))
(tryif (applicable? handler)
(handler key language))))
(custom-get-list key language (cdr custom))))))
(defambda (custom-get key language custom)
(if (or (fail? custom) (not custom) (null? custom))
(fail)
(if (pair? custom)
(custom-get-list key language custom)
(choice (get (custom-map-handler
(pick custom custom-map-language language))
key)
(get (custom-map-handler
(pick custom custom-map-language #f))
(cons language key))))))
;; This is used by config functions and massages a variety
;; of specifications into a custom-map
(define (conform-maprule value)
(if (vector? value) value
(let* ((items (if (pair? value) (choice (car value) (cdr value))
value))
(name (try (pick items symbol?) #f))
(language (try (pick items oid?) #f))
(map (try (difference items (choice name language)) #f)))
(cond ((not (or (table? map) (applicable? map)))
(error 'config "Invalid map: " map))
((and language (applicable? map)
(not (= (procedure-min-arity map) 1)))
(error 'config "map function requires too many arguments: "
map))
((and (not language) (applicable? map)
(> (procedure-arity map) 1)
(not (= (procedure-min-arity map) 2)))
(error 'config "map function has wrong number of arguments: "
map))
(else (vector name language map))))))
;;; A custom entry is a vector of the form:
;;; #(name lang table)
;;; and rulesets are used to manage the replacement of entries
;;; with the same name.
(define custom-map-name first)
(define custom-map-language second)
(define custom-map-handler third)
(define (custom-get-list key language custom)
(tryif (pair? custom)
(let* ((entry (car custom))
(handler (custom-map-handler entry)))
(try
(if (custom-map-language entry)
(tryif (eq? language (custom-map-language entry))
(if (table? handler) (get handler key)
(tryif (applicable? handler)
(handler key))))
(if (table? handler)
(get handler (cons language key))
(tryif (applicable? handler)
(handler key language))))
(custom-get-list key language (cdr custom))))))
(defambda (custom-get key language custom)
(if (or (fail? custom) (not custom) (null? custom))
(fail)
(if (pair? custom)
(custom-get-list key language custom)
(choice (get (custom-map-handler
(pick custom custom-map-language language))
key)
(get (custom-map-handler
(pick custom custom-map-language #f))
(cons language key))))))
;; This is used by config functions and massages a variety
;; of specifications into a custom-map
(define (conform-maprule value)
(if (vector? value) value
(let* ((items (if (pair? value) (choice (car value) (cdr value))
value))
(name (try (pick items symbol?) #f))
(language (try (pick items oid?) #f))
(map (try (difference items (choice name language)) #f)))
(cond ((not (or (table? map) (applicable? map)))
(error 'config "Invalid map: " map))
((and language (applicable? map)
(not (= (procedure-min-arity map) 1)))
(error 'config "map function requires too many arguments: "
map))
((and (not language) (applicable? map)
(> (procedure-arity map) 1)
(not (= (procedure-min-arity map) 2)))
(error 'config "map function has wrong number of arguments: "
map))
(else (vector name language map))))))