Class ShellParser
Source: shellParser.ts
A static utility class for parsing shell scripts into an Abstract Syntax Tree (AST). Handles tokenization, control flow parsing (if, while, for), and shell-specific expansions.
Properties
KEYWORD_IF
readonlystaticKEYWORD_IF:"if"="if"
KEYWORD_THEN
readonlystaticKEYWORD_THEN:"then"="then"
KEYWORD_ELIF
readonlystaticKEYWORD_ELIF:"elif"="elif"
KEYWORD_ELSE
readonlystaticKEYWORD_ELSE:"else"="else"
KEYWORD_FI
readonlystaticKEYWORD_FI:"fi"="fi"
KEYWORD_WHILE
readonlystaticKEYWORD_WHILE:"while"="while"
KEYWORD_FOR
readonlystaticKEYWORD_FOR:"for"="for"
KEYWORD_DO
readonlystaticKEYWORD_DO:"do"="do"
KEYWORD_DONE
readonlystaticKEYWORD_DONE:"done"="done"
KEYWORD_IN
readonlystaticKEYWORD_IN:"in"="in"
ARITHMETIC_PREFIX_TOKEN
readonlystaticARITHMETIC_PREFIX_TOKEN:"(("="(("
ARITHMETIC_SUFFIX_TOKEN
readonlystaticARITHMETIC_SUFFIX_TOKEN:"))"="))"
Methods
parseScript()
staticparseScript(script:string):Block
High-level method to transform a raw script string into a structured AST.
Parameters
script
string
The raw shell script string.
Returns
An array of AST nodes representing the script execution flow.
parseArgument()
staticparseArgument(input:string):Argument
Converts an input string into an ShellAST.Argument.
Parameters
input
string
Returns
expandBraces()
staticexpandBraces(input:string):string[]
Expands braces in a shell argument (e.g., "file{1..3}.txt" or "img.{jpg,png}"). Supports nested expansion and numeric sequences.
Parameters
input
string
The raw string.
Returns
string[]
An array of strings with all permutations expanded.
parseOptions()
staticparseOptions(command:Command,commandArgs:string[]): {options:string[];inputs:Record<string,string>; }
Parses flags and options out of a mutable args array, returning the collected options and input values. Flag args are removed from commandArgs in place as a side-effect.
Parameters
command
The command used to validate and look up option definitions.
commandArgs
string[]
The mutable argument list to parse from. Modified in place.
Returns
{ options: string[]; inputs: Record<string, string>; }
An object containing the parsed option keys and any input values keyed by the option's short name.
options
options:
string[]
inputs
inputs:
Record<string,string>