Skip to content

Commit 4f47e34

Browse files
feat(mf2)!: Remove all reserved & private syntax (unicode-org/message-format-wg#883)
1 parent c293b00 commit 4f47e34

32 files changed

+166
-676
lines changed

packages/mf2-fluent/src/fluent-to-message.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Fluent from '@fluent/syntax';
22
import deepEqual from 'fast-deep-equal';
33
import {
44
Expression,
5-
FunctionAnnotation,
5+
FunctionRef,
66
InputDeclaration,
77
Literal,
88
LocalDeclaration,
@@ -64,7 +64,7 @@ function asSelectorDeclaration(
6464
value: {
6565
type: 'expression',
6666
arg: asValue(selector),
67-
annotation: { type: 'function', name: 'string' }
67+
functionRef: { type: 'function', name: 'string' }
6868
}
6969
};
7070
case 'VariableReference': {
@@ -86,7 +86,7 @@ function asSelectorDeclaration(
8686
value: {
8787
type: 'expression',
8888
arg: asValue(selector),
89-
annotation: { type: 'function', name }
89+
functionRef: { type: 'function', name }
9090
}
9191
};
9292
}
@@ -122,14 +122,14 @@ function asExpression(exp: Fluent.Expression): Expression {
122122
return {
123123
type: 'expression',
124124
arg: asValue(exp),
125-
annotation: { type: 'function', name: 'number' }
125+
functionRef: { type: 'function', name: 'number' }
126126
};
127127
case 'StringLiteral':
128128
case 'VariableReference': {
129129
return { type: 'expression', arg: asValue(exp) };
130130
}
131131
case 'FunctionReference': {
132-
const annotation: FunctionAnnotation = {
132+
const annotation: FunctionRef = {
133133
type: 'function',
134134
name: exp.id.name.toLowerCase()
135135
};
@@ -150,8 +150,8 @@ function asExpression(exp: Fluent.Expression): Expression {
150150
}
151151
}
152152
return args.length > 0
153-
? { type: 'expression', arg: args[0], annotation }
154-
: { type: 'expression', annotation };
153+
? { type: 'expression', arg: args[0], functionRef: annotation }
154+
: { type: 'expression', functionRef: annotation };
155155
}
156156
case 'MessageReference': {
157157
const msgId = exp.attribute
@@ -160,11 +160,11 @@ function asExpression(exp: Fluent.Expression): Expression {
160160
return {
161161
type: 'expression',
162162
arg: { type: 'literal', value: msgId },
163-
annotation: { type: 'function', name: 'message' }
163+
functionRef: { type: 'function', name: 'message' }
164164
};
165165
}
166166
case 'TermReference': {
167-
const annotation: FunctionAnnotation = {
167+
const annotation: FunctionRef = {
168168
type: 'function',
169169
name: 'message'
170170
};
@@ -185,7 +185,7 @@ function asExpression(exp: Fluent.Expression): Expression {
185185
return {
186186
type: 'expression',
187187
arg: { type: 'literal', value: msgId },
188-
annotation
188+
functionRef: annotation
189189
};
190190
}
191191

packages/mf2-fluent/src/fluent.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ describe('messagetoFluent', () => {
779779
value: {
780780
type: 'expression',
781781
arg: { type: 'variable', name: 'num' },
782-
annotation: { type: 'function', name: 'number' }
782+
functionRef: { type: 'function', name: 'number' }
783783
}
784784
}
785785
],
@@ -847,12 +847,12 @@ describe('messagetoFluent', () => {
847847
{
848848
type: 'expression',
849849
arg: { type: 'literal', value: 'msg' },
850-
annotation: { type: 'function', name: 'message' }
850+
functionRef: { type: 'function', name: 'message' }
851851
},
852852
{
853853
type: 'expression',
854854
arg: { type: 'variable', name: 'local' },
855-
annotation: { type: 'function', name: 'message' }
855+
functionRef: { type: 'function', name: 'message' }
856856
}
857857
]
858858
};
@@ -889,7 +889,7 @@ describe('messagetoFluent', () => {
889889
{
890890
type: 'expression',
891891
arg: { type: 'variable', name: 'input' },
892-
annotation: { type: 'function', name: 'message' }
892+
functionRef: { type: 'function', name: 'message' }
893893
}
894894
]
895895
};

packages/mf2-fluent/src/message-to-fluent.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
CatchallKey,
44
Declaration,
55
Expression,
6-
FunctionAnnotation,
6+
FunctionRef,
77
Literal,
88
Message,
99
Pattern,
@@ -161,7 +161,7 @@ function patternToFluent(ctx: MsgContext, pattern: Pattern) {
161161
function functionRefToFluent(
162162
ctx: MsgContext,
163163
arg: Fluent.InlineExpression | null,
164-
{ name, options }: FunctionAnnotation
164+
{ name, options }: FunctionRef
165165
): Fluent.InlineExpression {
166166
const args = new Fluent.CallArguments();
167167
if (arg) args.positional[0] = arg;
@@ -236,18 +236,10 @@ function literalToFluent({ value }: Literal) {
236236

237237
function expressionToFluent(
238238
ctx: MsgContext,
239-
{ arg, annotation }: Expression
239+
{ arg, functionRef }: Expression
240240
): Fluent.InlineExpression {
241241
const fluentArg = arg ? valueToFluent(ctx, arg) : null;
242-
if (annotation) {
243-
if (annotation.type === 'function') {
244-
return functionRefToFluent(ctx, fluentArg, annotation);
245-
} else {
246-
throw new Error(
247-
`Conversion of ${annotation.type} annotation to Fluent is not supported`
248-
);
249-
}
250-
}
242+
if (functionRef) return functionRefToFluent(ctx, fluentArg, functionRef);
251243
if (fluentArg) return fluentArg;
252244
throw new Error('Invalid empty expression');
253245
}

packages/mf2-icu-mf1/src/mf1-to-message-data.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as AST from '@messageformat/parser';
22
import type {
33
Expression,
4-
FunctionAnnotation,
4+
FunctionRef,
55
InputDeclaration,
66
Message,
77
Options,
@@ -65,7 +65,7 @@ function tokenToPart(
6565
arg: { type: 'variable', name: token.arg }
6666
};
6767
case 'function': {
68-
const annotation: FunctionAnnotation = {
68+
const functionRef: FunctionRef = {
6969
type: 'function',
7070
name: token.key
7171
};
@@ -75,12 +75,12 @@ function tokenToPart(
7575
if (pt.type === 'content') value += pt.value;
7676
else throw new Error(`Unsupported param type: ${pt.type}`);
7777
}
78-
annotation.options = new Map([['param', { type: 'literal', value }]]);
78+
functionRef.options = new Map([['param', { type: 'literal', value }]]);
7979
}
8080
return {
8181
type: 'expression',
8282
arg: { type: 'variable', name: token.arg },
83-
annotation
83+
functionRef
8484
};
8585
}
8686
case 'octothorpe':
@@ -98,9 +98,9 @@ function argToInputDeclaration({
9898
pluralOffset,
9999
type
100100
}: SelectArg): InputDeclaration {
101-
let annotation: FunctionAnnotation;
101+
let functionRef: FunctionRef;
102102
if (type === 'select') {
103-
annotation = { type: 'function', name: 'string' };
103+
functionRef = { type: 'function', name: 'string' };
104104
} else {
105105
const options: Options = new Map();
106106
if (pluralOffset) {
@@ -113,16 +113,16 @@ function argToInputDeclaration({
113113
options.set('type', { type: 'literal', value: 'ordinal' });
114114
}
115115

116-
annotation = { type: 'function', name: 'number' };
117-
if (options.size) annotation.options = options;
116+
functionRef = { type: 'function', name: 'number' };
117+
if (options.size) functionRef.options = options;
118118
}
119119
return {
120120
type: 'input',
121121
name: selName,
122122
value: {
123123
type: 'expression',
124124
arg: { type: 'variable', name: selName },
125-
annotation
125+
functionRef
126126
}
127127
};
128128
}

packages/mf2-messageformat/src/cst/declarations.ts

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parseNameValue } from './names.js';
2-
import { parseExpression, parseReservedBody } from './expression.js';
2+
import { parseExpression } from './expression.js';
33
import type { ParseContext } from './parse-cst.js';
44
import type * as CST from './types.js';
55
import { whitespaces } from './util.js';
@@ -29,7 +29,7 @@ export function parseDeclarations(
2929
decl = parseLocalDeclaration(ctx, pos);
3030
break;
3131
default:
32-
decl = parseReservedStatement(ctx, pos, '.' + keyword);
32+
decl = parseDeclarationJunk(ctx, pos);
3333
}
3434
declarations.push(decl);
3535
pos = decl.end;
@@ -110,47 +110,19 @@ function parseLocalDeclaration(
110110
};
111111
}
112112

113-
function parseReservedStatement(
114-
ctx: ParseContext,
115-
start: number,
116-
keyword: string
117-
): CST.ReservedStatement {
118-
let pos = start + keyword.length;
119-
pos += whitespaces(ctx.source, pos);
120-
121-
const body = parseReservedBody(ctx, pos);
122-
let end = body.end;
123-
pos = end + whitespaces(ctx.source, end);
124-
125-
const values: CST.Expression[] = [];
126-
while (ctx.source[pos] === '{') {
127-
if (ctx.source.startsWith('{{', pos)) break;
128-
const value = parseExpression(ctx, pos);
129-
values.push(value);
130-
end = value.end;
131-
pos = end + whitespaces(ctx.source, end);
132-
}
133-
if (values.length === 0) ctx.onError('missing-syntax', end, '{');
134-
135-
return {
136-
type: 'reserved-statement',
137-
start,
138-
end,
139-
keyword: { start, end: keyword.length, value: keyword },
140-
body,
141-
values
142-
};
143-
}
144-
145113
function parseDeclarationValue(
146114
ctx: ParseContext,
147115
start: number
148116
): CST.Expression | CST.Junk {
149-
const { source } = ctx;
150-
if (source[start] === '{') return parseExpression(ctx, start);
117+
return ctx.source[start] === '{'
118+
? parseExpression(ctx, start)
119+
: parseDeclarationJunk(ctx, start);
120+
}
151121

152-
const junkEndOffset = source.substring(start).search(/\.[a-z]|{{/);
153-
const end = junkEndOffset === -1 ? source.length : start + junkEndOffset;
122+
function parseDeclarationJunk(ctx: ParseContext, start: number): CST.Junk {
123+
const { source } = ctx;
124+
const junkEndOffset = source.substring(start + 1).search(/\s*(\.[a-z]|{{)/);
125+
const end = junkEndOffset === -1 ? source.length : start + 1 + junkEndOffset;
154126
ctx.onError('missing-syntax', start, '{');
155127
return { type: 'junk', start, end, source: source.substring(start, end) };
156128
}

0 commit comments

Comments
 (0)