-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageformat-mode.js
208 lines (195 loc) · 5.8 KB
/
messageformat-mode.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
CodeMirror.defineMode("messageformat.js", function(config, parserConfig) {
var identifierRegexp = /^[0-9a-zA-Z$_][^ \t\n\r,.+={}]*/;
function literalModeToken(stream, state) {
if(stream.eat('#')) {
return state.stack.length ? "keyword" : "string";
}
if(stream.match(/^\\[{}#\\]/)) {
return "atom";
}
var unicodeMatch = stream.match(/^\\u([0-9a-fA-F]{0,4})/);
if(unicodeMatch) {
return unicodeMatch[1].length == 4 ? "atom" : "error";
}
if(stream.eat("{")) {
state.stack.push({type: "msgVariable"});
return "bracket";
}
if(stream.eat("}")) {
if(state.stack.length) {
state.stack.pop();
return "bracket";
} else {
return "error";
}
}
if(stream.match(/^[ \t]{2,}|[ \t]$/) || stream.eat("\t")) {
return "suspicious-space";
}
var inString = true;
while(inString) {
var spaceEaten = stream.eat(" ");
inString = stream.eatWhile(/[^ \t\\{}#]+/);
if(!inString && spaceEaten) {
stream.backUp(1);
}
}
if(stream.current() !== "") return "string";
stream.next();
return "error";
}
function illegalBlockToken(stream, state) {
if(stream.eat("{")) {
state.stack.push({type: "illegalBlock"});
return "error bracket";
}
if(stream.eat("}")) {
state.stack.pop();
return "error bracket";
}
stream.eatWhile(/[^{}]/);
return "error";
}
function msgFormatToken(stream, state) {
if(stream.eatWhile(/[ \t\n\r]/)) {
return null;
}
var ownState = state.stack[state.stack.length - 1];
if(!ownState.hasOwnProperty("varName")) {
var varNameMatch = stream.match(identifierRegexp);
if(varNameMatch) {
ownState.varName = varNameMatch[0];
return "def";
}
} else if(!ownState.hasOwnProperty("elementFormat")) {
if(!ownState.expectingElementFormat) {
if(stream.eat(",")) {
ownState.expectingElementFormat = true;
return null;
}
if(stream.eat("}")) {
state.stack.pop();
return "bracket";
}
} else {
var elementFormatMatch = stream.match(identifierRegexp);
if(elementFormatMatch) {
ownState.elementFormat = elementFormatMatch[0];
delete ownState.expectingElementFormat;
return "keyword";
}
}
} else if(ownState.elementFormat == "select") {
var parsedSelectToken = selectToken(stream, state);
if(parsedSelectToken !== undefined) return parsedSelectToken;
} else if(ownState.elementFormat == "plural" || ownState.elementFormat == "selectordinal") {
var parsedPluralToken = pluralToken(stream, state);
if(parsedPluralToken !== undefined) return parsedPluralToken;
} else {
var parsedArgumentToken = argumentToken(stream, state);
if(parsedArgumentToken !== undefined) return parsedArgumentToken;
}
if(stream.eat("{")) {
state.stack.push({type: "illegalBlock"});
return "error bracket";
}
if(stream.eat("}")) {
state.stack.pop();
return "error bracket";
}
stream.next();
return "error";
}
function createChoicesTokenizer(keyMatcher) {
return function(stream, state) {
var ownState = state.stack[state.stack.length - 1];
if(!ownState.isAcceptingChoices) {
if(stream.eat(",")) {
ownState.isAcceptingChoices = true;
ownState.currentKey = null;
ownState.keys = [];
return null;
}
} else {
if(ownState.currentKey === null) {
var keyToken = keyMatcher(stream, state);
if(keyToken !== undefined) {
ownState.currentKey = stream.current();
return keyToken;
}
if(ownState.keys.length > 0 && stream.eat("}")) {
state.stack.pop();
return "bracket";
}
} else {
if(stream.eat("{")) {
ownState.keys.push(ownState.currentKey);
ownState.currentKey = null;
state.stack.push({type: "literal"});
return "bracket";
}
}
}
};
}
var selectToken = createChoicesTokenizer(function(stream, state) {
if(stream.match(identifierRegexp)) {
return "keyword";
}
});
var pluralToken = createChoicesTokenizer(function(stream, state) {
if(stream.match(identifierRegexp)) {
return "keyword";
}
if(stream.match(/^=[0-9]+/)) {
return "number";
}
});
function argumentToken(stream, state) {
var ownState = state.stack[state.stack.length - 1];
if(!ownState.isAcceptingArgument) {
if(stream.eat(",")) {
ownState.isAcceptingArgument = true;
return null;
}
if(stream.eat("}")) {
state.stack.pop();
return "bracket";
}
} else {
if(stream.match(identifierRegexp)) {
ownState.isAcceptingArgument = false;
return "keyword";
}
}
}
return {
startState: function() {
return {
stack: []
};
},
token: function(stream, state) {
var stack = state.stack;
var currentState = stack.length > 0 ? stack[stack.length-1].type : "literal";
if(currentState === "literal") {
return literalModeToken(stream, state);
} else if(currentState === "msgVariable") {
return msgFormatToken(stream, state);
} else if(currentState === "illegalBlock") {
return illegalBlockToken(stream, state);
}
},
fold: "brace"
};
});
});