Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | 3x 3x 3x 3x 3x 5x 5x 7x 7x 7x 7x 5x 3x 2x 2x 1x 1x 1x 1x 3x 286x 286x 3x 286x 286x 286x 286x 285x 285x 3x 918x 411x 3x 1395x 36x 15x 10x 5x 21x 547x 9x 77x 13x 204x 3x 388x 3x 547x 547x 547x 546x 2x 2x 2x 544x 353x 546x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 544x 546x 3x 9x 9x 9x 9x 9x 9x 9x 3x 77x 77x 77x 77x 77x 77x 50x 27x 4x 77x 77x 24x 21x 21x 21x 21x 20x 20x 20x 20x 3x 2x 1x 20x 20x 20x 20x 3x 18x 18x 18x 17x 3x 18x 18x 30x 29x 16x 16x 13x 17x 3x 18x 18x 18x 18x 3x 18x 18x 31x 31x 31x 21x 21x 31x 18x 18x 13x 18x 3x 204x 204x 204x 5x 5x 5x 7x 7x 5x 199x 199x 199x 3x 13x 13x 13x 13x 13x 13x 13x 13x 3x 50x 50x 50x 50x 50x 222x 43x 43x 31x 31x 31x 31x 2x 48x 98x 50x 50x 50x 3x 4x 4x 4x 4x 4x 4x 4x 13x 3x 3x 3x 3x 4x 1x 4x 6x 3x 3x 4x 4x 4x 3x | import { Tokenizer } from './tokenizer' import { Token, nameTok, colonTok, dotTok, starTok, bangTok, bracketLTok, bracketRTok, braceLTok, braceRTok, bracketDLTok, parenLTok, parenRTok, commaTok, expandTok, eofTok, } from './tokens' import { bracketArrayContext, destructorContext } from './contexts' import { IdentifierNode, ExpandOperatorNode, WildcardOperatorNode, RangeExpressionNode, GroupExpressionNode, DotOperatorNode, IgnoreExpressionNode, DestructorExpressionNode, ObjectPatternNode, ObjectPatternPropertyNode, ArrayPatternNode, Node, Segments, } from './types' import { parseDestructorRules, setDestructor } from './destructor' import { isNumberLike } from './utils' import Path from './index' const createTreeBySegments = (Isegments: Segments = [], afterNode?: Node) => { const segLen = segments.length const build = (start = 0) => { const after = start < segLen - 1 ? build(start + 1) : afterNode const dot = after && { type: 'DotOperator', after, } return { type: 'Identifier', value: segments[start], after: dot, } } return build() } const calculate = ( a: string | number, b: string | number, operator: string ) => { Eif (isNumberLike(a) && isNumberLike(b)) { if (operator === '+') return String(Number(a) + Number(b)) Iif (operator === '-') return String(Number(a) - Number(b)) Iif (operator === '*') return String(Number(a) * Number(b)) Iif (operator === '/') return String(Number(a) / Number(b)) } else { if (operator === '+') return String(a) + String(b) if (operator === '-') return 'NaN' if (operator === '*') return 'NaN' if (operator === '/') return 'NaN' } return String(Number(a)) } export class Parser extends Tokenizer { public isMatchPattern: boolean public isWildMatchPattern: boolean public haveExcludePattern: boolean public base: Path public relative: string | number public data: { segments: Segments tree?: Node } constructor(input: string, base?: Path) { super(input) this.base = base } parse() { let node: Node this.data = { segments: [], } Eif (!this.eat(eofTok)) { this.next() node = this.parseAtom(this.state.type) } this.data.tree = node return node } append(parent: Node, node: Node) { if (parent && node) { parent.after = node } } parseAtom(type: Token): Node { switch (type) { case braceLTok: case bracketLTok: if (this.includesContext(destructorContext)) { if (type === braceLTok) { return this.parseObjectPattern() } else { return this.parseArrayPattern() } } return this.parseDestructorExpression() case nameTok: return this.parseIdentifier() case expandTok: return this.parseExpandOperator() case starTok: return this.parseWildcardOperator() case bracketDLTok: return this.parseIgnoreExpression() case dotTok: return this.parseDotOperator() } } pushSegments(key: string | number) { this.data.segments.push(key) } parseIdentifier() { const node: IdentifierNode = { type: 'Identifier', value: this.state.value, } const hasNotInDestructor = !this.includesContext(destructorContext) && !this.isMatchPattern && !this.isWildMatchPattern this.next() if (this.includesContext(bracketArrayContext)) { Iif (this.state.type !== bracketRTok) { throw this.unexpect() } else { this.state.context.pop() this.next() } } else if (hasNotInDestructor) { this.pushSegments(node.value) } if (this.state.type === bracketLTok) { this.next() Iif (this.state.type !== nameTok) { throw this.unexpect() } this.state.context.push(bracketArrayContext) let isNumberKey = false Eif (/^\d+$/.test(this.state.value)) { isNumberKey = true } const value = this.state.value this.pushSegments(isNumberKey ? Number(value) : value) const after = this.parseAtom(this.state.type) as IdentifierNode Eif (isNumberKey) { after.arrayIndex = true } this.append(node, after) } else { this.append(node, this.parseAtom(this.state.type)) } return node } parseExpandOperator() { const node: ExpandOperatorNode = { type: 'ExpandOperator', } this.isMatchPattern = true this.isWildMatchPattern = true this.data.segments = [] this.next() this.append(node, this.parseAtom(this.state.type)) return node } parseWildcardOperator(): WildcardOperatorNode { const node: WildcardOperatorNode = { type: 'WildcardOperator', } this.isMatchPattern = true this.isWildMatchPattern = true this.data.segments = [] this.next() if (this.state.type === parenLTok) { node.filter = this.parseGroupExpression(node) } else if (this.state.type === bracketLTok) { node.filter = this.parseRangeExpression(node) } this.append(node, this.parseAtom(this.state.type)) return node } parseDestructorExpression(): DestructorExpressionNode { const node: DestructorExpressionNode = { type: 'DestructorExpression', } this.state.context.push(destructorContext) const startPos = this.state.pos - 1 node.value = this.state.type === braceLTok ? this.parseObjectPattern() : this.parseArrayPattern() const endPos = this.state.pos this.state.context.pop() this.next() node.source = this.input .substring(startPos, endPos) .replace( /\[\s*([\+\-\*\/])?\s*([^,\]\s]*)\s*\]/, (match, operator, target) => { if (this.relative) return calculate(target || 1, this.relative, operator) return match } ) .replace(/\s*\.\s*/g, '') .replace(/\s*/g, '') setDestructor(node.source, parseDestructorRules(node)) this.pushSegments(node.source) this.append(node, this.parseAtom(this.state.type)) return node } parseArrayPattern(): ArrayPatternNode { const node: ArrayPatternNode = { type: 'ArrayPattern', elements: [], } this.next() node.elements = this.parseArrayPatternElements() return node } parseArrayPatternElements() { const nodes = [] while (this.state.type !== bracketRTok && this.state.type !== eofTok) { nodes.push(this.parseAtom(this.state.type)) if (this.state.type === bracketRTok) { this.next() break } this.next() } return nodes } parseObjectPattern(): ObjectPatternNode { const node: ObjectPatternNode = { type: 'ObjectPattern', properties: [], } this.next() node.properties = this.parseObjectProperties() return node } parseObjectProperties(): ObjectPatternPropertyNode[] { const nodes = [] while (this.state.type !== braceRTok && this.state.type !== eofTok) { const node: ObjectPatternPropertyNode = { type: 'ObjectPatternProperty', key: this.parseAtom(this.state.type) as IdentifierNode, } nodes.push(node) if (this.state.type === colonTok) { this.next() node.value = this.parseAtom(this.state.type) as | IdentifierNode | ObjectPatternNode[] | ArrayPatternNode[] } if (this.state.type === braceRTok) { this.next() break } this.next() } return nodes } parseDotOperator(): Node { const node: DotOperatorNode = { type: 'DotOperator', } const prevToken = this.type_ if (!prevToken && this.base) { Iif (this.base.isMatchPattern) { throw new Error('Base path must be an absolute path.') } this.data.segments = this.base.toArray() while (this.state.type === dotTok) { this.relative = this.data.segments.pop() this.next() } return createTreeBySegments( this.data.segments.slice(), this.parseAtom(this.state.type) ) } else { this.next() } this.append(node, this.parseAtom(this.state.type)) return node } parseIgnoreExpression() { this.next() const value = String(this.state.value).replace(/\s*/g, '') const node: IgnoreExpressionNode = { type: 'IgnoreExpression', value: value, } this.pushSegments(value) this.next() this.append(node, this.parseAtom(this.state.type)) this.next() return node } parseGroupExpression(parent: Node) { const node: GroupExpressionNode = { type: 'GroupExpression', value: [], } this.isMatchPattern = true this.data.segments = [] this.next() loop: while (true) { switch (this.state.type) { case commaTok: this.next() break case bangTok: node.isExclude = true this.haveExcludePattern = true this.next() break case eofTok: break loop case parenRTok: break loop default: node.value.push(this.parseAtom(this.state.type)) } } this.next() this.append(parent, this.parseAtom(this.state.type)) return node } parseRangeExpression(parent: Node) { const node: RangeExpressionNode = { type: 'RangeExpression', } this.next() this.isMatchPattern = true this.data.segments = [] let start = false, hasColon = false loop: while (true) { switch (this.state.type) { case colonTok: hasColon = true start = true this.next() break case bracketRTok: if (!hasColon && !node.end) { node.end = node.start } break loop case commaTok: throw this.unexpect() case eofTok: break loop default: if (!start) { node.start = this.parseAtom(this.state.type) as IdentifierNode } else { node.end = this.parseAtom(this.state.type) as IdentifierNode } } } this.next() this.append(parent, this.parseAtom(this.state.type)) return node } } |