Skip to content

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

readonly static KEYWORD_IF: "if" = "if"


KEYWORD_THEN

readonly static KEYWORD_THEN: "then" = "then"


KEYWORD_ELIF

readonly static KEYWORD_ELIF: "elif" = "elif"


KEYWORD_ELSE

readonly static KEYWORD_ELSE: "else" = "else"


KEYWORD_FI

readonly static KEYWORD_FI: "fi" = "fi"


KEYWORD_WHILE

readonly static KEYWORD_WHILE: "while" = "while"


KEYWORD_FOR

readonly static KEYWORD_FOR: "for" = "for"


KEYWORD_DO

readonly static KEYWORD_DO: "do" = "do"


KEYWORD_DONE

readonly static KEYWORD_DONE: "done" = "done"


KEYWORD_IN

readonly static KEYWORD_IN: "in" = "in"


ARITHMETIC_PREFIX_TOKEN

readonly static ARITHMETIC_PREFIX_TOKEN: "((" = "(("


ARITHMETIC_SUFFIX_TOKEN

readonly static ARITHMETIC_SUFFIX_TOKEN: "))" = "))"

Methods

parseScript()

static parseScript(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

Block

An array of AST nodes representing the script execution flow.


parseArgument()

static parseArgument(input: string): Argument

Converts an input string into an ShellAST.Argument.

Parameters

input

string

Returns

Argument


expandBraces()

static expandBraces(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()

static parseOptions(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

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>

Built by Prozilla