719
Le
lextide / files
lextide / src / languages / bash.ts
15.3 KB
1import type { InternalRule } from "../core/grammar"; 2import { defineGrammar } from "../core/grammar"; 3import type { LanguageFn } from "../types"; 4 5import { 6 consumeUntil, 7 createIdentifierMatcher, 8 createNumberRule, 9 scanIdentifier, 10} from "./helpers"; 11 12const BASH_KEYWORDS = new Set([ 13 "case", 14 "coproc", 15 "do", 16 "done", 17 "elif", 18 "else", 19 "esac", 20 "fi", 21 "for", 22 "function", 23 "if", 24 "in", 25 "select", 26 "then", 27 "time", 28 "until", 29 "while", 30]); 31 32const BASH_LITERALS = new Set(["false", "true"]); 33 34const BASH_BUILT_INS = new Set([ 35 "alias", 36 "bind", 37 "break", 38 "builtin", 39 "caller", 40 "cd", 41 "command", 42 "continue", 43 "declare", 44 "echo", 45 "enable", 46 "eval", 47 "exec", 48 "exit", 49 "export", 50 "getopts", 51 "hash", 52 "help", 53 "let", 54 "local", 55 "logout", 56 "mapfile", 57 "printf", 58 "pwd", 59 "read", 60 "readarray", 61 "readonly", 62 "return", 63 "set", 64 "shift", 65 "shopt", 66 "source", 67 "test", 68 "times", 69 "trap", 70 "type", 71 "typeset", 72 "ulimit", 73 "umask", 74 "unalias", 75 "unset", 76]); 77 78const BASH_ENVIRONMENT_COMMANDS = new Set([ 79 "awk", 80 "basename", 81 "bun", 82 "bunx", 83 "cat", 84 "chmod", 85 "chown", 86 "cp", 87 "curl", 88 "cut", 89 "date", 90 "diff", 91 "dirname", 92 "du", 93 "env", 94 "find", 95 "git", 96 "grep", 97 "head", 98 "jq", 99 "kill", 100 "less", 101 "ln", 102 "ls", 103 "make", 104 "mkdir", 105 "mv", 106 "node", 107 "nohup", 108 "perl", 109 "printenv", 110 "ps", 111 "python", 112 "readlink", 113 "rg", 114 "rm", 115 "rsync", 116 "scp", 117 "sed", 118 "sh", 119 "sleep", 120 "sort", 121 "ssh", 122 "sudo", 123 "tail", 124 "tar", 125 "tee", 126 "touch", 127 "tr", 128 "uniq", 129 "wc", 130 "wget", 131 "whoami", 132 "xargs", 133 "yes", 134]); 135 136const BASH_COMMANDS = new Set([ 137 ...BASH_BUILT_INS, 138 ...BASH_ENVIRONMENT_COMMANDS, 139]); 140const BASH_ARITHMETIC_NUMBER = 141 /\d+#[0-9A-Za-z]+|(?:0[xX][0-9a-fA-F]+|\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)/; 142const BASH_IDENTIFIER_RULE = { 143 match: createIdentifierMatcher({ 144 keywordRelevance: 2, 145 keywords: BASH_KEYWORDS, 146 literalRelevance: 1, 147 literals: BASH_LITERALS, 148 }), 149}; 150 151export const bash: LanguageFn = () => 152 defineGrammar({ 153 aliases: ["sh", "shell", "zsh", "bash"], 154 name: "bash", 155 relevance: 0, 156 startState: "root", 157 states: { 158 doubleString: { 159 defaultScope: "string", 160 rules: [ 161 { match: /\\./, scope: "escape" }, 162 { match: bashArithmeticMatcher, relevance: 2 }, 163 { match: bashCommandSubstitutionMatcher, relevance: 2 }, 164 { match: bashVariableMatcher, scope: "variable", relevance: 2 }, 165 { match: /"/, pop: true }, 166 { match: consumeUntil(["\\", '"', "$"]) }, 167 ], 168 }, 169 root: { 170 rules: createBashInlineRules(true), 171 }, 172 singleString: { 173 defaultScope: "string", 174 rules: [{ match: /'/, pop: true }, { match: consumeUntil(["'"]) }], 175 }, 176 }, 177 }); 178 179function bashArrayAssignmentMatcher( 180 input: string, 181 position: number, 182): number | null { 183 const nameEnd = scanIdentifier(input, position, false); 184 185 if (nameEnd === null) { 186 return null; 187 } 188 189 let cursor = nameEnd; 190 191 while (cursor < input.length && /\s/.test(input[cursor])) { 192 cursor += 1; 193 } 194 195 if (input[cursor] === "+" && input[cursor + 1] === "=") { 196 return nameEnd - position; 197 } 198 199 if (input[cursor] === "=") { 200 return nameEnd - position; 201 } 202 203 if (input[cursor] !== "[") { 204 return null; 205 } 206 207 const close = input.indexOf("]", cursor + 1); 208 209 if (close === -1) { 210 return null; 211 } 212 213 cursor = close + 1; 214 215 while (cursor < input.length && /\s/.test(input[cursor])) { 216 cursor += 1; 217 } 218 219 if (input[cursor] === "+" && input[cursor + 1] === "=") { 220 return nameEnd - position; 221 } 222 223 return input[cursor] === "=" ? nameEnd - position : null; 224} 225 226function bashArithmeticMatcher( 227 input: string, 228 position: number, 229): { children: InternalRule[]; length: number; scope: string } | null { 230 if (!input.startsWith("$((", position)) { 231 return null; 232 } 233 234 const end = findArithmeticEnd(input, position); 235 236 if (end === null) { 237 return null; 238 } 239 240 return { 241 children: createWrappedChildren( 242 3, 243 end - position - 2, 244 createArithmeticChildren(), 245 ), 246 length: end - position, 247 scope: "subst", 248 }; 249} 250 251function bashCommandMatcher(input: string, position: number): number | null { 252 if (!isCommandStart(input, position)) { 253 return null; 254 } 255 256 const end = scanIdentifier(input, position, false); 257 258 if (end === null) { 259 return null; 260 } 261 262 const value = input.slice(position, end); 263 264 if (!BASH_COMMANDS.has(value) || looksLikeAssignment(input, end)) { 265 return null; 266 } 267 268 return end - position; 269} 270 271function bashCommandSubstitutionMatcher( 272 input: string, 273 position: number, 274): { children: InternalRule[]; length: number; scope: string } | null { 275 if (!input.startsWith("$(", position) || input.startsWith("$((", position)) { 276 return null; 277 } 278 279 const end = findCommandSubstitutionEnd(input, position); 280 281 if (end === null) { 282 return null; 283 } 284 285 return { 286 children: createWrappedChildren( 287 2, 288 end - position - 1, 289 createBashInlineRules(true), 290 ), 291 length: end - position, 292 scope: "subst", 293 }; 294} 295 296function bashCommentMatcher(input: string, position: number): number | null { 297 if (input[position] !== "#") { 298 return null; 299 } 300 301 const previous = position === 0 ? "\n" : input[position - 1]; 302 303 if (position !== 0 && previous !== "\n" && !/\s/.test(previous)) { 304 return null; 305 } 306 307 let cursor = position; 308 309 while (cursor < input.length && input[cursor] !== "\n") { 310 cursor += 1; 311 } 312 313 return cursor - position; 314} 315 316function bashFunctionMatcher(input: string, position: number): number | null { 317 const nameEnd = scanIdentifier(input, position, false); 318 319 if (nameEnd === null) { 320 return null; 321 } 322 323 if (!/^\s*\(\s*\)\s*\{/.test(input.slice(nameEnd))) { 324 return null; 325 } 326 327 return nameEnd - position; 328} 329 330function bashOptionMatcher(input: string, position: number): number | null { 331 if (input[position] !== "-" || !isOptionStart(input, position)) { 332 return null; 333 } 334 335 if (input[position + 1] === "-") { 336 return readLongOption(input, position); 337 } 338 339 return readShortOption(input, position); 340} 341 342function bashOperatorMatcher(input: string, position: number): number | null { 343 if (input.startsWith("&&", position) || input.startsWith("||", position)) { 344 return 2; 345 } 346 347 return input[position] === "|" ? 1 : null; 348} 349 350function bashHereDocMatcher( 351 input: string, 352 position: number, 353): { children: InternalRule[]; length: number } | null { 354 if (!input.startsWith("<<", position)) { 355 return null; 356 } 357 358 let cursor = position + 2; 359 let stripTabs = false; 360 361 if (input[cursor] === "-") { 362 stripTabs = true; 363 cursor += 1; 364 } 365 366 while (cursor < input.length && /[ \t]/.test(input[cursor])) { 367 cursor += 1; 368 } 369 370 const delimiterResult = readHereDocDelimiter(input, cursor); 371 372 if (!delimiterResult) { 373 return null; 374 } 375 376 const markerLength = delimiterResult.end - position; 377 const lineEnd = input.indexOf("\n", delimiterResult.end); 378 379 if (lineEnd === -1) { 380 return { 381 children: createHereDocChildren(markerLength, markerLength), 382 length: markerLength, 383 }; 384 } 385 386 const bodyStart = lineEnd - position; 387 const closingEnd = findHereDocEnd( 388 input, 389 lineEnd + 1, 390 delimiterResult.value, 391 stripTabs, 392 ); 393 394 return { 395 children: createHereDocChildren(markerLength, bodyStart), 396 length: (closingEnd ?? input.length) - position, 397 }; 398} 399 400function bashVariableMatcher(input: string, position: number): number | null { 401 if (input[position] !== "$") { 402 return null; 403 } 404 405 if (input[position + 1] === "{") { 406 const end = input.indexOf("}", position + 2); 407 408 if (end === -1) { 409 return input.length - position; 410 } 411 412 return end - position + 1; 413 } 414 415 const identifierEnd = scanIdentifier(input, position + 1, false); 416 417 if (identifierEnd !== null) { 418 return identifierEnd - position; 419 } 420 421 return input[position + 1] ? 2 : 1; 422} 423 424function createArithmeticChildren(): InternalRule[] { 425 return [ 426 { match: bashCommandSubstitutionMatcher, relevance: 2 }, 427 { match: bashVariableMatcher, scope: "variable", relevance: 2 }, 428 { match: BASH_ARITHMETIC_NUMBER, scope: "number", relevance: 1 }, 429 BASH_IDENTIFIER_RULE, 430 ]; 431} 432 433function createBashInlineRules(includeComments: boolean): InternalRule[] { 434 const rules: InternalRule[] = [ 435 { match: /^#![^\n]*/, scope: "meta", relevance: 10 }, 436 { match: bashHereDocMatcher, relevance: 3 }, 437 ]; 438 439 if (includeComments) { 440 rules.push({ match: bashCommentMatcher, scope: "comment", relevance: 2 }); 441 } 442 443 rules.push( 444 { 445 match: /"/, 446 push: "doubleString", 447 pushScope: "string", 448 relevance: 1, 449 }, 450 { 451 match: /'/, 452 push: "singleString", 453 pushScope: "string", 454 relevance: 1, 455 }, 456 { match: bashArithmeticMatcher, relevance: 2 }, 457 { match: bashCommandSubstitutionMatcher, relevance: 2 }, 458 { match: bashVariableMatcher, scope: "variable", relevance: 2 }, 459 { match: bashOptionMatcher, scope: "attr", relevance: 1 }, 460 { match: bashOperatorMatcher, scope: "operator", relevance: 0 }, 461 { match: bashArrayAssignmentMatcher, scope: "variable", relevance: 1 }, 462 { match: bashFunctionMatcher, scope: "title.function", relevance: 2 }, 463 { match: bashCommandMatcher, scope: "built_in", relevance: 1 }, 464 createNumberRule(), 465 BASH_IDENTIFIER_RULE, 466 ); 467 468 return rules; 469} 470 471function createHereDocChildren( 472 markerLength: number, 473 bodyStart: number, 474): InternalRule[] { 475 const children: InternalRule[] = [ 476 { 477 match: (_input, position) => (position === 0 ? markerLength : null), 478 scope: "string", 479 }, 480 ]; 481 482 if (bodyStart > markerLength) { 483 children.push({ 484 children: createBashInlineRules(false), 485 match: (_input, position) => 486 position === markerLength ? bodyStart - markerLength : null, 487 }); 488 } 489 490 children.push({ 491 match: (_input, position) => 492 position === bodyStart ? _input.length - bodyStart : null, 493 scope: "string", 494 }); 495 496 return children; 497} 498 499function createWrappedChildren( 500 innerStart: number, 501 innerLength: number, 502 children: InternalRule[], 503): InternalRule[] { 504 if (innerLength <= 0) { 505 return []; 506 } 507 508 return [ 509 { 510 children, 511 match: (_input, position) => 512 position === innerStart ? innerLength : null, 513 }, 514 ]; 515} 516 517function findArithmeticEnd(input: string, position: number): number | null { 518 let cursor = position + 3; 519 520 while (cursor < input.length - 1) { 521 const advanced = advanceArithmeticScanner(input, cursor); 522 523 if (advanced === null) { 524 return null; 525 } 526 527 if (advanced !== undefined) { 528 cursor = advanced; 529 continue; 530 } 531 532 if (input.startsWith("))", cursor)) { 533 return cursor + 2; 534 } 535 536 cursor += 1; 537 } 538 539 return null; 540} 541 542function findCommandSubstitutionEnd( 543 input: string, 544 position: number, 545): number | null { 546 let cursor = position + 2; 547 let depth = 1; 548 549 while (cursor < input.length) { 550 const advanced = advanceCommandScanner(input, cursor); 551 552 if (advanced === null) { 553 return null; 554 } 555 556 if (advanced) { 557 cursor = advanced.cursor; 558 depth += advanced.depthDelta ?? 0; 559 continue; 560 } 561 562 if (input[cursor] === ")") { 563 depth -= 1; 564 565 if (depth === 0) { 566 return cursor + 1; 567 } 568 } 569 570 cursor += 1; 571 } 572 573 return null; 574} 575 576function advanceArithmeticScanner( 577 input: string, 578 cursor: number, 579): number | null | undefined { 580 const quoted = advanceQuotedScanner(input, cursor); 581 582 if (quoted !== undefined) { 583 return quoted; 584 } 585 586 if (!input.startsWith("$(", cursor)) { 587 return undefined; 588 } 589 590 return input.startsWith("$((", cursor) 591 ? findArithmeticEnd(input, cursor) 592 : findCommandSubstitutionEnd(input, cursor); 593} 594 595function advanceCommandScanner( 596 input: string, 597 cursor: number, 598): { cursor: number; depthDelta?: number } | null | undefined { 599 const quoted = advanceQuotedScanner(input, cursor); 600 601 if (quoted !== undefined) { 602 return quoted === null ? null : { cursor: quoted }; 603 } 604 605 if (input.startsWith("$((", cursor)) { 606 const end = findArithmeticEnd(input, cursor); 607 return end === null ? null : { cursor: end }; 608 } 609 610 if (input.startsWith("$(", cursor)) { 611 return { cursor: cursor + 2, depthDelta: 1 }; 612 } 613 614 if (input[cursor] === "\\") { 615 return { cursor: cursor + 2 }; 616 } 617 618 return undefined; 619} 620 621function advanceQuotedScanner( 622 input: string, 623 cursor: number, 624): number | null | undefined { 625 const character = input[cursor]; 626 627 if (character !== '"' && character !== "'") { 628 return undefined; 629 } 630 631 return skipQuotedString(input, cursor, character); 632} 633 634function findHereDocEnd( 635 input: string, 636 position: number, 637 delimiter: string, 638 stripTabs: boolean, 639): number | null { 640 let cursor = position; 641 642 while (cursor <= input.length) { 643 const nextNewline = input.indexOf("\n", cursor); 644 const lineEnd = nextNewline === -1 ? input.length : nextNewline; 645 const line = input.slice(cursor, lineEnd); 646 const compare = stripTabs ? line.replace(/^\t+/, "") : line; 647 648 if (compare === delimiter) { 649 return lineEnd; 650 } 651 652 if (nextNewline === -1) { 653 return null; 654 } 655 656 cursor = nextNewline + 1; 657 } 658 659 return null; 660} 661 662function isCommandStart(input: string, position: number): boolean { 663 let cursor = position - 1; 664 665 while (cursor >= 0 && /[ \t]/.test(input[cursor])) { 666 cursor -= 1; 667 } 668 669 if (cursor < 0 || input[cursor] === "\n") { 670 return true; 671 } 672 673 if (/[;&|(){}]/.test(input[cursor])) { 674 return true; 675 } 676 677 const end = cursor + 1; 678 let wordStart = cursor; 679 680 while (wordStart >= 0 && /[A-Za-z_]/.test(input[wordStart])) { 681 wordStart -= 1; 682 } 683 684 const word = input.slice(wordStart + 1, end); 685 686 return [ 687 "builtin", 688 "command", 689 "do", 690 "elif", 691 "else", 692 "env", 693 "then", 694 "time", 695 ].includes(word); 696} 697 698function isOptionBoundaryCharacter(character: string | undefined): boolean { 699 return character === undefined || /[\s[\]{}()<>;&|]/.test(character); 700} 701 702function isOptionStart(input: string, position: number): boolean { 703 return isOptionBoundaryCharacter(input[position - 1]); 704} 705 706function isOptionValueCharacter(character: string | undefined): boolean { 707 return character !== undefined && !/[\s[\]{}()<>;&|]/.test(character); 708} 709 710function looksLikeAssignment(input: string, position: number): boolean { 711 let cursor = position; 712 713 while (cursor < input.length && /[ \t]/.test(input[cursor])) { 714 cursor += 1; 715 } 716 717 if ( 718 input[cursor] === "=" || 719 (input[cursor] === "+" && input[cursor + 1] === "=") 720 ) { 721 return true; 722 } 723 724 if (input[cursor] !== "[") { 725 return false; 726 } 727 728 const close = input.indexOf("]", cursor + 1); 729 730 if (close === -1) { 731 return false; 732 } 733 734 cursor = close + 1; 735 736 while (cursor < input.length && /[ \t]/.test(input[cursor])) { 737 cursor += 1; 738 } 739 740 return ( 741 input[cursor] === "=" || 742 (input[cursor] === "+" && input[cursor + 1] === "=") 743 ); 744} 745 746function readLongOption(input: string, position: number): number | null { 747 let cursor = position + 2; 748 749 if (!/[A-Za-z]/.test(input[cursor] ?? "")) { 750 return null; 751 } 752 753 while (/[A-Za-z0-9-]/.test(input[cursor] ?? "")) { 754 cursor += 1; 755 } 756 757 if (input[cursor] === "=") { 758 cursor += 1; 759 760 while (isOptionValueCharacter(input[cursor])) { 761 cursor += 1; 762 } 763 } 764 765 return cursor - position; 766} 767 768function readShortOption(input: string, position: number): number | null { 769 let cursor = position + 1; 770 771 if (!/[A-Za-z]/.test(input[cursor] ?? "")) { 772 return null; 773 } 774 775 while (/[A-Za-z]/.test(input[cursor] ?? "")) { 776 cursor += 1; 777 } 778 779 return cursor - position; 780} 781 782function readHereDocDelimiter( 783 input: string, 784 position: number, 785): { end: number; value: string } | null { 786 const quote = input[position]; 787 788 if (quote === '"' || quote === "'") { 789 const end = input.indexOf(quote, position + 1); 790 791 if (end === -1 || input.slice(position + 1, end).length === 0) { 792 return null; 793 } 794 795 return { 796 end: end + 1, 797 value: input.slice(position + 1, end), 798 }; 799 } 800 801 const end = scanIdentifier(input, position, false); 802 803 if (end === null) { 804 return null; 805 } 806 807 return { 808 end, 809 value: input.slice(position, end), 810 }; 811} 812 813function skipQuotedString( 814 input: string, 815 position: number, 816 quote: string, 817): number | null { 818 let cursor = position + 1; 819 820 while (cursor < input.length) { 821 if (input[cursor] === "\\") { 822 cursor += 2; 823 continue; 824 } 825 826 if (input[cursor] === quote) { 827 return cursor + 1; 828 } 829 830 cursor += 1; 831 } 832 833 return null; 834}