-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseMessageFormat.ts
125 lines (107 loc) · 3.59 KB
/
parseMessageFormat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {parse, ParseOptions, Token} from '@messageformat/parser';
import {ContainerNode, IFunctionNode, ISelectCaseNode, ISelectNode, Node, NodeType} from './parser-types';
export interface IMessageFormatParserOptions extends ParseOptions {
/**
* Rewrites formatting function name.
*
* @param name The name of a function.
* @returns The new name of a function.
*/
renameFunction?(name: string): string;
/**
* Rewrites argument name.
*
* @param name The name of an argument.
* @returns The new name of an argument.
*/
renameArgument?(name: string): string;
}
/**
* Parses ICU MessageFormat as a tree of AST nodes.
*
* @param str The string to parse.
* @param options The ICU MessageFormat parsing options.
* @returns An array of root AST nodes.
*/
export function parseMessageFormat(str: string, options: IMessageFormatParserOptions = {}): Array<Node> {
return pushMessageFormatTokensAsNodes(parse(str, options), [], null, options);
}
function pushMessageFormatTokensAsNodes(tokens: Array<Token>, arr: Array<Node>, parent: ContainerNode | null, options: IMessageFormatParserOptions): Array<Node> {
for (let i = 0; i < tokens.length; ++i) {
arr.push(convertMessageFormatTokenToNode(tokens[i], parent, options));
}
return arr;
}
function convertMessageFormatTokenToNode(token: Token, parent: ContainerNode | null, options: IMessageFormatParserOptions): Node {
const {renameFunction, renameArgument} = options;
const start = token.ctx.offset;
const end = start + token.ctx.text.length;
switch (token.type) {
case 'argument':
return {
nodeType: NodeType.ARGUMENT,
name: renameArgument ? renameArgument(token.arg) : token.arg,
parent,
start,
end,
};
case 'content':
return {
nodeType: NodeType.TEXT,
value: token.value,
parent,
start,
end,
};
case 'function':
const node: IFunctionNode = {
nodeType: NodeType.FUNCTION,
name: renameFunction ? renameFunction(token.key) : token.key,
argumentName: renameArgument ? renameArgument(token.arg) : token.arg,
children: [],
parent,
start,
end,
};
if (token.param) {
pushMessageFormatTokensAsNodes(token.param, node.children, node, options);
}
return node;
case 'plural':
case 'select':
case 'selectordinal':
const selectNode: ISelectNode = {
nodeType: token.type === 'plural' ? NodeType.PLURAL : token.type === 'select' ? NodeType.SELECT : NodeType.SELECT_ORDINAL,
pluralOffset: token.pluralOffset,
argumentName: renameArgument ? renameArgument(token.arg) : token.arg,
children: [],
parent,
start,
end,
};
for (let i = 0; i < token.cases.length; ++i) {
const caseToken = token.cases[i];
const caseChildren: Array<Node> = [];
const caseNode: ISelectCaseNode = {
nodeType: NodeType.SELECT_CASE,
key: caseToken.key,
children: caseChildren,
parent: selectNode,
start: caseToken.ctx.offset,
end: 0,
};
selectNode.children.push(caseNode);
pushMessageFormatTokensAsNodes(caseToken.tokens, caseChildren, caseNode, options);
const caseEnd = caseNode.end = caseChildren.length === 0 ? caseNode.start + 1 : caseChildren[caseChildren.length - 1].end + 1;
selectNode.end = caseEnd + 1;
}
return selectNode;
case 'octothorpe':
return {
nodeType: NodeType.OCTOTHORPE,
parent,
start,
end,
};
}
}