Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/tools/build/v2/util/indirect.jam @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1# Copyright David Abrahams 2003. Permission to copy, use,
2# modify, sell and distribute this software is granted provided this
3# copyright notice appears in all copies. This software is provided
4# "as is" without express or implied warranty, and with no claim as
5# to its suitability for any purpose.
6import modules ;
7import numbers ;
8
9# The pattern that indirect rules must match: module$rule
10.pattern = ^([^%]*)%([^%]+)$ ;
11 
12#
13# Type checking rules.
14#
15local 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.
27rule 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.
40rule 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.
60rule 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
71rule get-rule ( [indirect-rule] x )
72{
73    local m = [ MATCH $(.pattern) : $(x) ] ;
74    return $(m[2]) ;
75}
76
77# Invoke the given indirect-rule.
78rule 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
86rule __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}
Note: See TracBrowser for help on using the repository browser.