-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Node.js
282 lines (255 loc) · 7.37 KB
/
Node.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
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
import Range from './Range'
export const Type = {
ALIAS: 'ALIAS',
BLOCK_FOLDED: 'BLOCK_FOLDED',
BLOCK_LITERAL: 'BLOCK_LITERAL',
COMMENT: 'COMMENT',
DIRECTIVE: 'DIRECTIVE',
DOCUMENT: 'DOCUMENT',
FLOW_MAP: 'FLOW_MAP',
FLOW_SEQ: 'FLOW_SEQ',
MAP: 'MAP',
MAP_KEY: 'MAP_KEY',
MAP_VALUE: 'MAP_VALUE',
PLAIN: 'PLAIN',
QUOTE_DOUBLE: 'QUOTE_DOUBLE',
QUOTE_SINGLE: 'QUOTE_SINGLE',
SEQ: 'SEQ',
SEQ_ITEM: 'SEQ_ITEM'
}
export const Char = {
ANCHOR: '&',
COMMENT: '#',
TAG: '!',
DIRECTIVES_END: '-',
DOCUMENT_END: '.'
}
/** Root class of all nodes */
export default class Node {
static addStringTerminator (src, offset, str) {
if (str[str.length - 1] === '\n') return str
const next = Node.endOfWhiteSpace(src, offset)
return next >= src.length || src[next] === '\n' ? str + '\n' : str
}
// ^(---|...)
static atDocumentBoundary (src, offset, sep) {
const prev = src[offset - 1]
if (prev && prev !== '\n') return false
const ch0 = src[offset]
if (!ch0) return true
if (sep) {
if (ch0 !== sep) return false
} else {
if (ch0 !== Char.DIRECTIVES_END && ch0 !== Char.DOCUMENT_END) return false
}
const ch1 = src[offset + 1]
const ch2 = src[offset + 2]
if (ch1 !== ch0 || ch2 !== ch0) return false
const ch3 = src[offset + 3]
return !ch3 || ch3 === '\n' || ch3 === '\t' || ch3 === ' '
}
static endOfIdentifier (src, offset) {
let ch = src[offset]
const isVerbatim = (ch === '<')
const notOk = isVerbatim
? ['\n', '\t', ' ', '>']
: ['\n', '\t', ' ', '[', ']', '{', '}', ',']
while (ch && notOk.indexOf(ch) === -1) ch = src[offset += 1]
if (isVerbatim && ch === '>') offset += 1
return offset
}
static endOfIndent (src, offset) {
let ch = src[offset]
while (ch === ' ') ch = src[offset += 1]
return offset
}
static endOfLine (src, offset) {
let ch = src[offset]
while (ch && ch !== '\n') ch = src[offset += 1]
return offset
}
static endOfWhiteSpace (src, offset) {
let ch = src[offset]
while (ch === '\t' || ch === ' ') ch = src[offset += 1]
return offset
}
/**
* End of indentation, or null if the line's indent level is not more
* than `indent`
*
* @param {string} src
* @param {number} indent
* @param {number} lineStart
* @returns {?number}
*/
static endOfBlockIndent (src, indent, lineStart) {
const inEnd = Node.endOfIndent(src, lineStart)
if (inEnd > lineStart + indent) {
return inEnd
} else {
const wsEnd = Node.endOfWhiteSpace(src, inEnd)
const ch = src[wsEnd]
if (!ch || ch === '\n') return wsEnd
}
return null
}
static atBlank (src, offset) {
const ch = src[offset]
return ch === '\n' || ch === '\t' || ch === ' '
}
static atCollectionItem (src, offset) {
const ch = src[offset]
return (ch === '?' || ch === ':' || ch === '-') && Node.atBlank(src, offset + 1)
}
static nextNodeIsIndented (ch, indentDiff, indicatorAsIndent) {
if (!ch || indentDiff < 0) return false
if (indentDiff > 0) return true
return indicatorAsIndent && (ch === '-')
}
// should be at line or string end, or at next non-whitespace char
static normalizeOffset (src, offset) {
const ch = src[offset]
return !ch ? offset
: ch !== '\n' && src[offset - 1] === '\n' ? offset - 1
: Node.endOfWhiteSpace(src, offset)
}
// fold single newline into space, multiple newlines to N - 1 newlines
// presumes src[offset] === '\n'
static foldNewline (src, offset, indent) {
let inCount = 0
let error = false
let fold = ''
let ch = src[offset + 1]
while (ch === ' ' || ch === '\t' || ch === '\n') {
switch (ch) {
case '\n':
inCount = 0
offset += 1
fold += '\n'
break
case '\t':
if (inCount <= indent) error = true
offset = Node.endOfWhiteSpace(src, offset + 2) - 1
break
case ' ':
inCount += 1
offset += 1
break
}
ch = src[offset + 1]
}
if (!fold) fold = ' '
if (ch && inCount <= indent) error = true
return { fold, offset, error }
}
constructor (type, props, context) {
this.context = context || null
this.error = null
this.range = null
this.valueRange = null
this.props = props || []
this.type = type
this.value = null
}
getPropValue (idx, key, skipKey) {
if (!this.context) return null
const { src } = this.context
const prop = this.props[idx]
return prop && (src[prop.start] === key) ? src.slice(prop.start + (skipKey ? 1 : 0), prop.end) : null
}
get anchor () {
for (let i = 0; i < this.props.length; ++i) {
const anchor = this.getPropValue(i, Char.ANCHOR, true)
if (anchor != null) return anchor
}
return null
}
get comment () {
const comments = []
for (let i = 0; i < this.props.length; ++i) {
const comment = this.getPropValue(i, Char.COMMENT, true)
if (comment != null) comments.push(comment)
}
return comments.length > 0 ? comments.join('\n') : null
}
commentHasRequiredWhitespace (start) {
const { src } = this.context
if (this.header && start === this.header.end) return false
if (this.valueRange) {
const { end } = this.valueRange
return start !== end || Node.atBlank(src, end - 1)
}
}
get hasComment () {
if (this.context) {
const { src } = this.context
for (let i = 0; i < this.props.length; ++i) {
if (src[this.props[i].start] === Char.COMMENT) return true
}
}
return false
}
get hasProps () {
if (this.context) {
const { src } = this.context
for (let i = 0; i < this.props.length; ++i) {
if (src[this.props[i].start] !== Char.COMMENT) return true
}
}
return false
}
get jsonLike () {
const jsonLikeTypes = [
Type.FLOW_MAP,
Type.FLOW_SEQ,
Type.QUOTE_DOUBLE,
Type.QUOTE_SINGLE
]
return jsonLikeTypes.indexOf(this.type) !== -1
}
get rawValue () {
if (!this.valueRange || !this.context) return null
const { start, end } = this.valueRange
return this.context.src.slice(start, end)
}
get tag () {
for (let i = 0; i < this.props.length; ++i) {
const tag = this.getPropValue(i, Char.TAG, false)
if (tag != null) {
if (tag[1] === '<') {
return { verbatim: tag.slice(2, -1) }
} else {
const [_, handle, suffix] = tag.match(/^(.*!)([^!]*)$/)
return { handle, suffix }
}
}
}
return null
}
get valueRangeContainsNewline () {
if (!this.valueRange || !this.context) return false
const { start, end } = this.valueRange
const { src } = this.context
for (let i = start; i < end; ++i) {
if (src[i] === '\n') return true
}
return false
}
parseComment (start) {
const { src } = this.context
if (src[start] === Char.COMMENT) {
const end = Node.endOfLine(src, start + 1)
const commentRange = new Range(start, end)
this.props.push(commentRange)
trace: commentRange, JSON.stringify(this.getPropValue(this.props.length - 1, Char.COMMENT, true))
return end
}
return start
}
toString () {
const { context: { src }, range, value } = this
if (value != null) return value
const str = src.slice(range.start, range.end)
return Node.addStringTerminator(src, range.end, str)
}
}