1 | # Copyright 2003 Dave Abrahams |
---|
2 | # Copyright 2003 Vladimir Prus |
---|
3 | # Distributed under the Boost Software License, Version 1.0. |
---|
4 | # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | import modules ; |
---|
7 | import numbers ; |
---|
8 | |
---|
9 | # The pattern that indirect rules must match: module$rule |
---|
10 | .pattern = ^([^%]*)%([^%]+)$ ; |
---|
11 | |
---|
12 | # |
---|
13 | # Type checking rules. |
---|
14 | # |
---|
15 | local rule indirect-rule ( x ) |
---|
16 | { |
---|
17 | if ! [ MATCH $(.pattern) : $(x) ] |
---|
18 | { |
---|
19 | return "expected a string of the form module$rule, but got \""$(x)"\" for argument" ; |
---|
20 | } |
---|
21 | } |
---|
22 | |
---|
23 | # make an indirect rule which calls the given rule; if context is |
---|
24 | # supplied it is expected to be the module in which to invoke the rule |
---|
25 | # by the 'call' rule below. Otherwise, the rule will be invoked in |
---|
26 | # the module of this rule's caller. |
---|
27 | rule make ( rulename bound-args * : context ? ) |
---|
28 | { |
---|
29 | context ?= [ CALLER_MODULE ] ; |
---|
30 | context ?= "" ; |
---|
31 | return $(context)%$(rulename) $(bound-args) ; |
---|
32 | } |
---|
33 | |
---|
34 | # make an indirect rule which calls the given rule. rulename may be a |
---|
35 | # qualified rule; if so it is returned unchanged. Otherwise, if |
---|
36 | # frames is not supplied, the result will be invoked (by 'call', |
---|
37 | # below) in the module of the caller. Otherwise, frames > 1 |
---|
38 | # specifies additional call frames to back up in order to find the |
---|
39 | # module context. |
---|
40 | rule make-qualified ( rulename bound-args * : frames ? ) |
---|
41 | { |
---|
42 | if [ MATCH $(.pattern) : $(rulename) ] |
---|
43 | { |
---|
44 | return $(rulename) $(bound-args) ; |
---|
45 | } |
---|
46 | else |
---|
47 | { |
---|
48 | frames ?= 1 ; |
---|
49 | # Take the first dot-separated element as module name. |
---|
50 | # This disallows module names with dots, but allows rule names |
---|
51 | # with dots. |
---|
52 | local module-context = [ MATCH ^([^.]*)\\..* : $(rulename) ] ; |
---|
53 | module-context ?= [ CALLER_MODULE $(frames) ] ; |
---|
54 | return [ make $(rulename) $(bound-args) : $(module-context) ] ; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | # return the module name in which the given indirect rule will be |
---|
59 | # invoked. |
---|
60 | rule get-module ( [indirect-rule] x ) |
---|
61 | { |
---|
62 | local m = [ MATCH $(.pattern) : $(x) ] ; |
---|
63 | if ! $(m[1]) |
---|
64 | { |
---|
65 | m = ; |
---|
66 | } |
---|
67 | return $(m[1]) ; |
---|
68 | } |
---|
69 | |
---|
70 | # return the rulename that will be called when x is invoked |
---|
71 | rule get-rule ( [indirect-rule] x ) |
---|
72 | { |
---|
73 | local m = [ MATCH $(.pattern) : $(x) ] ; |
---|
74 | return $(m[2]) ; |
---|
75 | } |
---|
76 | |
---|
77 | # Invoke the given indirect-rule. |
---|
78 | rule call ( [indirect-rule] r args * : * ) |
---|
79 | { |
---|
80 | return [ |
---|
81 | modules.call-in [ get-module $(r) ] |
---|
82 | : [ get-rule $(r) ] $(args) : $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) |
---|
83 | ] ; |
---|
84 | } |
---|
85 | |
---|
86 | rule __test__ |
---|
87 | { |
---|
88 | import assert ; |
---|
89 | |
---|
90 | rule foo-barr! ( x ) |
---|
91 | { |
---|
92 | assert.equal $(x) : x ; |
---|
93 | } |
---|
94 | |
---|
95 | assert.equal [ get-rule [ make foo-barr! ] ] : foo-barr! ; |
---|
96 | assert.equal [ get-module [ make foo-barr! ] ] : [ CALLER_MODULE ] ; |
---|
97 | |
---|
98 | call [ make foo-barr! ] x ; |
---|
99 | call [ make foo-barr! x ] ; |
---|
100 | |
---|
101 | |
---|
102 | call [ make foo-barr! : [ CALLER_MODULE ] ] x ; |
---|
103 | } |
---|