-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathparser-v8.ts
333 lines (320 loc) · 8.79 KB
/
parser-v8.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* A simplified version of the message parser that handles messages like vue-i18n v8.
* This parser probably has poor performance.
*/
import type {
CompileError,
MessageNode,
NamedNode,
PluralNode,
TextNode,
ResourceNode,
SourceLocation,
ListNode,
LinkedNode,
LinkedModifierNode,
LinkedKeyNode
} from '@intlify/message-compiler'
import { sortedLastIndex } from 'lodash'
import { NodeTypes } from './utils'
import type { ModuloNamedNode } from './parser-v9'
export function parse(code: string): {
ast: ResourceNode
errors: CompileError[]
} {
const errors: CompileError[] = []
const ast = parseAST(code, errors)
return {
ast,
errors
}
}
class CodeContext {
public code: string
public buff: string
public offset: number
private lineStartIndices: number[]
private lines: string[]
constructor(code: string) {
this.code = code
this.buff = code
this.offset = 0
this.lines = []
this.lineStartIndices = [0]
const lineEndingPattern = /\r\n|[\r\n\u2028\u2029]/gu
let match
while ((match = lineEndingPattern.exec(this.code))) {
this.lines.push(
this.code.slice(
this.lineStartIndices[this.lineStartIndices.length - 1],
match.index
)
)
this.lineStartIndices.push(match.index + match[0].length)
}
this.lines.push(
this.code.slice(this.lineStartIndices[this.lineStartIndices.length - 1])
)
}
setOffset(offset: number) {
this.offset = offset
this.buff = this.code.slice(offset)
}
getLocFromIndex(index: number) {
if (index === this.code.length) {
return {
line: this.lines.length,
column: this.lines[this.lines.length - 1].length + 1
}
}
const lineNumber = sortedLastIndex(this.lineStartIndices, index)
return {
line: lineNumber,
column: index - this.lineStartIndices[lineNumber - 1] + 1
}
}
getNodeLoc(start: number, end: number) {
const startLoc = this.getLocFromIndex(start)
const endLoc = this.getLocFromIndex(end)
return {
start,
end,
loc: {
start: { ...startLoc, offset: start },
end: { ...endLoc, offset: end }
}
}
}
setEndLoc(
node: {
end?: number
loc?: SourceLocation
},
end: number
) {
const endLoc = this.getLocFromIndex(end)
node.end = end
node.loc!.end = { ...endLoc, offset: end }
}
createCompileError(message: string, offset: number) {
const loc = this.getLocFromIndex(offset)
const error: CompileError = new SyntaxError(message) as never
error.code = 42
error.location = {
start: { ...loc, offset },
end: { ...loc, offset }
}
error.domain = 'parser'
return error
}
}
function parseAST(code: string, errors: CompileError[]): ResourceNode {
const ctx = new CodeContext(code)
const regexp = /%?\{|@[\.:]|\s*\|\s*/u
let re
const node: ResourceNode = {
type: NodeTypes.Resource,
body: undefined as never,
...ctx.getNodeLoc(0, code.length)
}
let messageNode: MessageNode = {
type: NodeTypes.Message,
items: [],
...ctx.getNodeLoc(0, code.length)
}
let body: MessageNode | PluralNode = messageNode
while ((re = regexp.exec(ctx.buff))) {
const key = re[0]
const startOffset = ctx.offset + re.index
const endOffset = startOffset + key.length
if (ctx.offset < startOffset) {
const textNode: TextNode = {
type: NodeTypes.Text,
value: ctx.code.slice(ctx.offset, startOffset),
...ctx.getNodeLoc(ctx.offset, startOffset)
}
messageNode.items.push(textNode)
}
if (key.trim() === '|') {
ctx.setEndLoc(messageNode, startOffset)
if (body.type === NodeTypes.Message) {
const pluralNode: PluralNode = {
type: NodeTypes.Plural,
cases: [body],
start: body.start,
end: body.end,
loc: {
start: { ...body.loc!.start },
end: { ...body.loc!.end }
}
}
body = pluralNode
}
messageNode = {
type: NodeTypes.Message,
items: [],
...ctx.getNodeLoc(endOffset, endOffset)
}
body.cases.push(messageNode)
ctx.setOffset(endOffset)
continue
}
if (key === '{' || key === '%{') {
const endIndex = ctx.code.indexOf('}', endOffset)
let keyValue: string
if (endIndex > -1) {
keyValue = ctx.code.slice(endOffset, endIndex)
} else {
errors.push(
ctx.createCompileError('Unterminated closing brace', endOffset)
)
keyValue = ctx.code.slice(endOffset)
}
const placeholderEndOffset = endOffset + keyValue.length + 1
let node: NamedNode | ListNode | null = null
const trimmedKeyValue = keyValue.trim()
if (trimmedKeyValue) {
if (trimmedKeyValue !== keyValue) {
errors.push(
ctx.createCompileError(
'Unexpected space before or after the placeholder key',
endOffset
)
)
}
if (/^-?\d+$/u.test(trimmedKeyValue)) {
const num = Number(trimmedKeyValue)
const listNode: ListNode = {
type: NodeTypes.List,
index: num,
...ctx.getNodeLoc(endOffset - 1, placeholderEndOffset)
}
if (num < 0) {
errors.push(
ctx.createCompileError(
'Unexpected minus placeholder index',
endOffset
)
)
}
node = listNode
}
if (!node) {
const namedNode: ModuloNamedNode = {
type: NodeTypes.Named,
key: trimmedKeyValue,
...ctx.getNodeLoc(endOffset - 1, placeholderEndOffset)
}
if (key === '%{') {
namedNode.modulo = true
}
if (!/^[a-zA-Z][a-zA-Z0-9_$]*$/.test(namedNode.key)) {
errors.push(
ctx.createCompileError('Unexpected placeholder key', endOffset)
)
}
node = namedNode
}
messageNode.items.push(node)
} else {
errors.push(
ctx.createCompileError('Empty placeholder', placeholderEndOffset - 1)
)
}
ctx.setOffset(placeholderEndOffset)
continue
}
if (key[0] === '@') {
ctx.setOffset(endOffset)
messageNode.items.push(parseLiked(ctx, errors))
continue
}
}
if (ctx.buff) {
const textNode: TextNode = {
type: NodeTypes.Text,
value: ctx.buff,
...ctx.getNodeLoc(ctx.offset, code.length)
}
messageNode.items.push(textNode)
}
ctx.setEndLoc(messageNode, code.length)
ctx.setEndLoc(body, code.length)
node.body = body
return node
}
function parseLiked(ctx: CodeContext, errors: CompileError[]) {
const linked: LinkedNode = {
type: NodeTypes.Linked,
key: undefined as never,
...ctx.getNodeLoc(ctx.offset - 2, ctx.offset)
}
const mark = ctx.code[ctx.offset - 1]
if (mark === '.') {
const modifierValue = /^[a-z]*/u.exec(ctx.buff)![0]
const modifierEndOffset = ctx.offset + modifierValue.length
const modifier: LinkedModifierNode = {
type: NodeTypes.LinkedModifier,
value: modifierValue,
...ctx.getNodeLoc(ctx.offset - 1, modifierEndOffset)
}
// empty modifier...
if (!modifier.value) {
errors.push(
ctx.createCompileError(
'Expected linked modifier value',
modifier.loc!.start.offset
)
)
}
ctx.setOffset(modifierEndOffset)
linked.modifier = modifier
if (ctx.code[ctx.offset] !== ':') {
// empty key...
errors.push(
ctx.createCompileError('Expected linked key value', ctx.offset)
)
const key: LinkedKeyNode = {
type: NodeTypes.LinkedKey,
value: '',
...ctx.getNodeLoc(ctx.offset, ctx.offset)
}
linked.key = key
ctx.setEndLoc(linked, ctx.offset)
return linked
}
ctx.setOffset(ctx.offset + 1)
}
let paren = false
if (ctx.buff[0] === '(') {
ctx.setOffset(ctx.offset + 1)
paren = true
}
// see https://github.com/kazupon/vue-i18n/blob/96a676cca51b592f3f8718b149ef26b3c8e70a64/src/index.js#L28
const keyValue = /^[\w\-_|.]*/u.exec(ctx.buff)![0]
const keyEndOffset = ctx.offset + keyValue.length
const key: LinkedKeyNode = {
type: NodeTypes.LinkedKey,
value: keyValue,
...ctx.getNodeLoc(ctx.offset, keyEndOffset)
}
// empty key...
if (!key.value) {
errors.push(
ctx.createCompileError('Expected linked key value', key.loc!.start.offset)
)
}
linked.key = key
ctx.setOffset(keyEndOffset)
if (paren) {
if (ctx.buff[0] === ')') {
ctx.setOffset(ctx.offset + 1)
} else {
errors.push(
ctx.createCompileError('Unterminated closing paren', ctx.offset)
)
}
}
ctx.setEndLoc(linked, ctx.offset)
return linked
}