@@ -19,6 +19,111 @@ the local variable takes precedence.
19
19
It is an error for a local variable definition to
20
20
refer to a local variable that's defined after it in the message.
21
21
22
+ ## Pattern Selection
23
+
24
+ When formatting a message with one or more selectors,
25
+ the Pattern of one of Variants must be selected for formatting.
26
+
27
+ An implementation may use any pattern selection method,
28
+ as long as its observable behaviour matches the results of the method defined here.
29
+
30
+ ### Setup
31
+
32
+ To select a Pattern,
33
+ first the resolved values _ res_ of the Selector must be determined:
34
+
35
+ 1 . Let _ res_ be an empty list of resolved values that support selection.
36
+ 2 . For each Expression _ exp_ of the message's Selector Expressions,
37
+ 1 . Let _ rv_ be the resolved value of _ exp_ .
38
+ 2 . If selection is supported for _ rv_ :
39
+ 1 . Push _ rv_ to the end of _ res_ .
40
+ 3 . Else:
41
+ 1 . Emit a Selection Error.
42
+ 2 . Let _ nomatch_ be a resolved value for which selection always fails.
43
+ 3 . Push _ nomatch_ to the end of _ res_ .
44
+
45
+ The shape of the resolved values must be determined by each implementation,
46
+ along with the manner of determining their support for selection.
47
+
48
+ ### Variant Tests
49
+
50
+ Using _ res_ ,
51
+ the Variants are iterated in source order and the following test is performed:
52
+
53
+ 1 . For each index _ i_ in _ res_ :
54
+ 1 . Let _ key_ be the VariantKey at position _ i_ .
55
+ 2 . If _ key_ is not equal to the catch-all key ` '*' ` :
56
+ 1 . Let _ sel_ be the entry in _ res_ at position _ i_ .
57
+ 2 . Let _ pass_ be the boolean result of testing _ key_ against _ sel_ .
58
+ 3 . If _ pass_ is False,
59
+ 1 . Return False to indicate test failure.
60
+ 2 . Return True to indicate test success.
61
+
62
+ The manner of testing _ key_ against _ sel_ must be defined by each implementation.
63
+
64
+ Once a Variant is found for which the test succeeds,
65
+ its Pattern is used for formatting the message.
66
+ As each valid message must have a Variant with keys all equal to the catch-all key,
67
+ this selection will always succeed.
68
+ Variants after one with all catch-all keys will never be selected.
69
+
70
+ ### Examples
71
+
72
+ Presuming a minimal implementation which only supports string values
73
+ and tests keys by using string comparison,
74
+ and a formatting context in which
75
+ the variable reference ` $foo ` resolves to the string ` 'foo' ` and
76
+ the variable reference ` $bar ` resolves to the string ` 'bar' ` ,
77
+ pattern selection proceeds as follows for this message:
78
+
79
+ ```
80
+ match {$foo} {$bar}
81
+ when bar bar {All bar}
82
+ when foo foo {All foo}
83
+ when * * {Otherwise}
84
+ ```
85
+
86
+ 1 . During setup the values of the Selector Expressions
87
+ are resolved as ` 'foo' ` and ` 'bar' ` .<br >
88
+ As these are both strings, the list _ res_ is determined to be ` [ 'foo', 'bar' ] ` .
89
+
90
+ 2 . Testing the first variant ` bar bar ` :<br >
91
+ The first key ` bar ` is compared to the first resolved value ` foo ` ,
92
+ and the test fails.
93
+
94
+ 3 . Testing the second variant ` foo foo ` :<br >
95
+ The first key ` foo ` is compared to the first resolved value ` foo ` ,
96
+ and as this succeeds the test continues.<br >
97
+ The second key ` foo ` is compared to the second resolved value ` bar ` ,
98
+ and the test fails.
99
+
100
+ 4 . Testing the third variant ` * * ` :<br >
101
+ The first key ` * ` is a catch-all key, so the test continues.<br >
102
+ The second key ` * ` is a catch-all key, so the test continues and succeeds.
103
+
104
+ 5 . The Pattern ` {Otherwise} ` of the third variant is selected.
105
+
106
+ Alternatively, with the same implementation and formatting context,
107
+ pattern selection would proceed as follows for this message:
108
+
109
+ ```
110
+ match {$foo} {$bar}
111
+ when foo * {Foo and some}
112
+ when foo bar {Foo and bar}
113
+ when * * {Otherwise}
114
+ ```
115
+
116
+ 1 . During setup the values of the Selector Expressions
117
+ are resolved as ` 'foo' ` and ` 'bar' ` .<br >
118
+ As these are both strings, the list _ res_ is determined to be ` [ 'foo', 'bar' ] ` .
119
+
120
+ 2 . Testing the first variant ` foo * ` :<br >
121
+ The first key ` foo ` is compared to the first resolved selector value ` foo ` ,
122
+ and as this succeeds the test continues.<br >
123
+ The second key ` * ` is a catch-all key, so the test continues and succeeds.
124
+
125
+ 3 . The Pattern ` {Foo and some} ` of the first variant is selected.
126
+
22
127
## Error Handling
23
128
24
129
During the formatting of a message,
0 commit comments