1 | 'use strict'
|
---|
2 |
|
---|
3 | module.exports = icuMessageFormat
|
---|
4 | icuMessageFormat.displayName = 'icuMessageFormat'
|
---|
5 | icuMessageFormat.aliases = []
|
---|
6 | function icuMessageFormat(Prism) {
|
---|
7 | // https://unicode-org.github.io/icu/userguide/format_parse/messages/
|
---|
8 | // https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/MessageFormat.html
|
---|
9 | ;(function (Prism) {
|
---|
10 | /**
|
---|
11 | * @param {string} source
|
---|
12 | * @param {number} level
|
---|
13 | * @returns {string}
|
---|
14 | */
|
---|
15 | function nested(source, level) {
|
---|
16 | if (level <= 0) {
|
---|
17 | return /[]/.source
|
---|
18 | } else {
|
---|
19 | return source.replace(/<SELF>/g, function () {
|
---|
20 | return nested(source, level - 1)
|
---|
21 | })
|
---|
22 | }
|
---|
23 | }
|
---|
24 | var stringPattern = /'[{}:=,](?:[^']|'')*'(?!')/
|
---|
25 | var escape = {
|
---|
26 | pattern: /''/,
|
---|
27 | greedy: true,
|
---|
28 | alias: 'operator'
|
---|
29 | }
|
---|
30 | var string = {
|
---|
31 | pattern: stringPattern,
|
---|
32 | greedy: true,
|
---|
33 | inside: {
|
---|
34 | escape: escape
|
---|
35 | }
|
---|
36 | }
|
---|
37 | var argumentSource = nested(
|
---|
38 | /\{(?:[^{}']|'(?![{},'])|''|<STR>|<SELF>)*\}/.source.replace(
|
---|
39 | /<STR>/g,
|
---|
40 | function () {
|
---|
41 | return stringPattern.source
|
---|
42 | }
|
---|
43 | ),
|
---|
44 | 8
|
---|
45 | )
|
---|
46 | var nestedMessage = {
|
---|
47 | pattern: RegExp(argumentSource),
|
---|
48 | inside: {
|
---|
49 | message: {
|
---|
50 | pattern: /^(\{)[\s\S]+(?=\}$)/,
|
---|
51 | lookbehind: true,
|
---|
52 | inside: null // see below
|
---|
53 | },
|
---|
54 | 'message-delimiter': {
|
---|
55 | pattern: /./,
|
---|
56 | alias: 'punctuation'
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 | Prism.languages['icu-message-format'] = {
|
---|
61 | argument: {
|
---|
62 | pattern: RegExp(argumentSource),
|
---|
63 | greedy: true,
|
---|
64 | inside: {
|
---|
65 | content: {
|
---|
66 | pattern: /^(\{)[\s\S]+(?=\}$)/,
|
---|
67 | lookbehind: true,
|
---|
68 | inside: {
|
---|
69 | 'argument-name': {
|
---|
70 | pattern: /^(\s*)[^{}:=,\s]+/,
|
---|
71 | lookbehind: true
|
---|
72 | },
|
---|
73 | 'choice-style': {
|
---|
74 | // https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1ChoiceFormat.html#details
|
---|
75 | pattern: /^(\s*,\s*choice\s*,\s*)\S(?:[\s\S]*\S)?/,
|
---|
76 | lookbehind: true,
|
---|
77 | inside: {
|
---|
78 | punctuation: /\|/,
|
---|
79 | range: {
|
---|
80 | pattern: /^(\s*)[+-]?(?:\d+(?:\.\d*)?|\u221e)\s*[<#\u2264]/,
|
---|
81 | lookbehind: true,
|
---|
82 | inside: {
|
---|
83 | operator: /[<#\u2264]/,
|
---|
84 | number: /\S+/
|
---|
85 | }
|
---|
86 | },
|
---|
87 | rest: null // see below
|
---|
88 | }
|
---|
89 | },
|
---|
90 | 'plural-style': {
|
---|
91 | // https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/PluralFormat.html#:~:text=Patterns%20and%20Their%20Interpretation
|
---|
92 | pattern:
|
---|
93 | /^(\s*,\s*(?:plural|selectordinal)\s*,\s*)\S(?:[\s\S]*\S)?/,
|
---|
94 | lookbehind: true,
|
---|
95 | inside: {
|
---|
96 | offset: /^offset:\s*\d+/,
|
---|
97 | 'nested-message': nestedMessage,
|
---|
98 | selector: {
|
---|
99 | pattern: /=\d+|[^{}:=,\s]+/,
|
---|
100 | inside: {
|
---|
101 | keyword: /^(?:few|many|one|other|two|zero)$/
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | },
|
---|
106 | 'select-style': {
|
---|
107 | // https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/SelectFormat.html#:~:text=Patterns%20and%20Their%20Interpretation
|
---|
108 | pattern: /^(\s*,\s*select\s*,\s*)\S(?:[\s\S]*\S)?/,
|
---|
109 | lookbehind: true,
|
---|
110 | inside: {
|
---|
111 | 'nested-message': nestedMessage,
|
---|
112 | selector: {
|
---|
113 | pattern: /[^{}:=,\s]+/,
|
---|
114 | inside: {
|
---|
115 | keyword: /^other$/
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | },
|
---|
120 | keyword: /\b(?:choice|plural|select|selectordinal)\b/,
|
---|
121 | 'arg-type': {
|
---|
122 | pattern: /\b(?:date|duration|number|ordinal|spellout|time)\b/,
|
---|
123 | alias: 'keyword'
|
---|
124 | },
|
---|
125 | 'arg-skeleton': {
|
---|
126 | pattern: /(,\s*)::[^{}:=,\s]+/,
|
---|
127 | lookbehind: true
|
---|
128 | },
|
---|
129 | 'arg-style': {
|
---|
130 | pattern:
|
---|
131 | /(,\s*)(?:currency|full|integer|long|medium|percent|short)(?=\s*$)/,
|
---|
132 | lookbehind: true
|
---|
133 | },
|
---|
134 | 'arg-style-text': {
|
---|
135 | pattern: RegExp(
|
---|
136 | /(^\s*,\s*(?=\S))/.source +
|
---|
137 | nested(/(?:[^{}']|'[^']*'|\{(?:<SELF>)?\})+/.source, 8) +
|
---|
138 | '$'
|
---|
139 | ),
|
---|
140 | lookbehind: true,
|
---|
141 | alias: 'string'
|
---|
142 | },
|
---|
143 | punctuation: /,/
|
---|
144 | }
|
---|
145 | },
|
---|
146 | 'argument-delimiter': {
|
---|
147 | pattern: /./,
|
---|
148 | alias: 'operator'
|
---|
149 | }
|
---|
150 | }
|
---|
151 | },
|
---|
152 | escape: escape,
|
---|
153 | string: string
|
---|
154 | }
|
---|
155 | nestedMessage.inside.message.inside = Prism.languages['icu-message-format']
|
---|
156 | Prism.languages['icu-message-format'].argument.inside.content.inside[
|
---|
157 | 'choice-style'
|
---|
158 | ].inside.rest = Prism.languages['icu-message-format']
|
---|
159 | })(Prism)
|
---|
160 | }
|
---|