Skip to content

Commit b6db226

Browse files
committed
Add Pattern Selection
1 parent 4d8c753 commit b6db226

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

spec/formatting.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,111 @@ the local variable takes precedence.
1919
It is an error for a local variable definition to
2020
refer to a local variable that's defined after it in the message.
2121

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+
22127
## Error Handling
23128

24129
During the formatting of a message,

0 commit comments

Comments
 (0)