### Installing bash-parser using npm (Bash) Source: https://github.com/vorpaljs/bash-parser/blob/master/readme.md This snippet demonstrates how to install the `bash-parser` library as a dependency in a Node.js project using the npm package manager. It saves the package to the `package.json` file, making it available for use in your project. ```bash npm install --save bash-parser ``` -------------------------------- ### Example AST Structure from bash-parser (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/readme.md This snippet illustrates the detailed structure of the Abstract Syntax Tree (AST) generated by `bash-parser` for the input `echo ciao`. It shows a root `Script` type containing a `SimpleCommand` with a `name` (echo) and a `suffix` (ciao), both represented as `Word` types, demonstrating the hierarchical representation of the parsed command. ```js { type: "Script", commands: [ { type: "SimpleCommand", name: { text: "echo", type: "Word" }, suffix: [ { text: "ciao", type: "Word" } ] } ] } ``` -------------------------------- ### Representing AST Node Location in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet illustrates the `loc` property available on AST nodes when the `insertLOC` option is enabled during parsing. It provides detailed start and end positions (row, column, and character index) within the source string, useful for debugging or source mapping. ```JavaScript { type: 'WORD', value: 'ls', loc: { start: { row: Number, col: Number, char: 42 }, end: { row: Number, col: Number, char: 43 } } } ``` -------------------------------- ### Defining ParameterExpansion AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents a parameter expansion operation within a `Word` node. The `op` and `word` properties describe the operator and the right-hand side `Word` for special parameters. `loc` properties indicate the start and end character indices of the substitution within the parent `Word`'s text. It includes properties for its type, the `parameter` string, optional `kind`, `word`, and `op` strings, and `loc` for position. ```js { type: 'ParameterExpansion', parameter: string, kind: ?string, word: ?string, op: ?string, loc: { start:Number, end:Number } } ``` -------------------------------- ### Defining CommandExpansion AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents a command substitution operation within a `Word` node. The `command` string is recursively parsed by `bash-parser` itself, and `commandAST` holds the resulting AST. `loc` properties indicate the start and end character indices of the substitution within the parent `Word`'s text. It includes properties for its type, the `command` string, a `resolved` boolean, the `commandAST` object, and `loc` for position. ```js { type: 'CommandExpansion', command: string, resolved: boolean, commandAST: Object, loc: { start:Number, end:Number } } ``` -------------------------------- ### Defining ArithmeticExpansion AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents an arithmetic expansion operation within a `Word` node. The `expression` is parsed using Babel parser, and `arithmeticAST` holds the resulting AST. `loc` properties indicate the start and end character indices of the substitution within the parent `Word`'s text. It includes properties for its type, the `expression` string, a `resolved` boolean, the `arithmeticAST` object, and `loc` for position. ```js { type: 'ArithmeticExpansion', expression: string, resolved: boolean, arithmeticAST: Object, loc: { start:Number, end:Number } } ``` -------------------------------- ### Defining the Name AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `Name` AST node, used to represent identifiers such as function names or `for` loop variable names. The `text` property holds the string value of the name, which must be alphanumeric or contain underscores and cannot start with a digit. ```JavaScript { type: 'Name', text: string } ``` -------------------------------- ### Parsing a Bash Command with bash-parser (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/readme.md This JavaScript snippet shows how to import the `bash-parser` library and use its `parse` function to convert a simple bash command string, 'echo ciao', into an Abstract Syntax Tree (AST). The `ast` variable will hold the resulting AST object, which can then be traversed or analyzed. ```js const parse = require('bash-parser'); const ast = parse('echo ciao'); ``` -------------------------------- ### Defining CaseItem AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents a single pattern item within a `Cases` list of a `Case` statement. It defines the pattern to match against and the corresponding set of statements to execute upon a match. The node includes properties for its type, the pattern as an array of `Word` nodes, and the body as a `CompoundList`. ```js { type: 'CaseItem', pattern: Array, body: CompoundList } ``` -------------------------------- ### Defining the Command AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `Command` AST node, representing a builtin or external command. It includes an optional `name` (a `Word` node), `prefix` for assignments or redirections before the command, and `suffix` for arguments or redirections after the command name. ```JavaScript { type: 'Command', name: ?Word, prefix: Array, suffix: Array } ``` -------------------------------- ### Defining Redirect AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents the redirection of input or output streams for a command. This node specifies the operation (`op`), the target file (`file`) or stream, and an optional `numberIo` for the file descriptor. It includes properties for its type, the redirection operator, the target file, and the I/O number. ```js { type: 'Redirect', op: string, file: word, numberIo: Number } ``` -------------------------------- ### Defining the Mode Object Type in Flow Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/mode.md This Flow type definition describes the `Mode` object, which encapsulates the core parsing logic. It includes properties for a `tokenizer` function, an array of `lexerPhases`, a `phaseCatalog` for named phase access, a compiled `grammar`, the `grammarSource` in JSON format, and an `astBuilder` for constructing the Abstract Syntax Tree. This object is returned by the `ModePlugin`'s `init` function. ```Flow export type Mode = { tokenizer: (options: Object, utils: Object) => (code: String) => Iterable, lexerPhases: Array, phaseCatalog: {[id:string]: LexerPhase} grammar: Object, grammarSource: Object, astBuilder: Object, }; ``` -------------------------------- ### Defining the Pipeline AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `Pipeline` AST node, representing a sequence of commands connected by pipe operators. The `commands` array holds the individual command nodes that are executed in parallel, with the output of one becoming the input of the next. ```JavaScript { type: 'Pipeline', commands: Array } ``` -------------------------------- ### Defining the Script AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `Script` AST node, which serves as the root of the Abstract Syntax Tree. It contains an array of `commands`, representing the top-level executable units within a Bash script, including logical expressions, pipelines, and control flow statements. ```JavaScript { type: 'Script', commands: Array } ``` -------------------------------- ### Defining Word AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents a `Word` node, a fundamental lexical unit in the AST. It consists of a series of characters and is subject to various shell expansions like tilde, parameter, command, arithmetic, pathname, field splitting, and quote removal. It includes properties for its type, the raw `text`, and an array of potential `expansion` nodes. ```js { type: 'Word', text: String, expansion: Array } ``` -------------------------------- ### Defining the ModePlugin Type in Flow Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/mode.md This Flow type definition outlines the structure of a `ModePlugin` object. It specifies an optional `inherits` property for mode inheritance and a required `init` factory function that receives a `parentMode` and returns a `Mode` object. This plugin is used to define different shell parsing behaviors. ```Flow type ModePlugin = { inherits?: string, init: (parentMode: Mode) => Mode }; ``` -------------------------------- ### Defining LexerPhase and Token Types in Flow Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/mode.md These Flow type definitions specify the `Token` and `LexerPhase` types. A `LexerPhase` is a higher-order function that transforms an iterable of `Token` objects. It accepts parser options, an array of preceding phases, and a `utils` object, returning a function that takes an input token iterable and produces an output token iterable. These phases are applied sequentially to process tokens. ```Flow export type Token = Object; type LexerPhase = (options: Object, previousPhases: Array,utils: Object) => (tokens: Iterable) => Iterable; ``` -------------------------------- ### Defining the CompoundList AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `CompoundList` AST node, which groups one or more commands, often forming the body of control flow statements like `for`, `if`, or `function` definitions. It can also include an optional array of `redirections` that apply to the entire list. ```JavaScript { type: 'CompoundList', commands: Array, redirections: Array } ``` -------------------------------- ### Defining the Case Statement AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `Case` AST node, representing a `case` conditional statement in Bash. It includes a `clause` (a `Word` node) whose value is matched against patterns, and a `cases` array containing `CaseItem` nodes, each with a pattern and a corresponding command list. ```JavaScript { type: 'Case', clause: Word, cases: Array } ``` -------------------------------- ### Defining the Function AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `Function` AST node, representing a Bash function definition. It includes the `name` of the function, an array of `redirections` that apply to the entire function body, and a `body` which is a `CompoundList` containing the function's commands. ```JavaScript { type: 'Function', name: Name, redirections: Array, body: CompoundList } ``` -------------------------------- ### Defining the For Loop AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `For` AST node, representing a `for` loop statement in Bash. It includes the loop variable `name`, a `wordlist` of items to iterate over, and a `do` property which is a `CompoundList` containing the commands to execute for each item. ```JavaScript { type: 'For', name: Name, wordlist: Array, do: CompoundList } ``` -------------------------------- ### Defining While Statement AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents a `While` loop statement in the AST. This node continuously executes the `do` CompoundList as long as the `clause` CompoundList returns a zero exit status. It includes properties for the loop condition `clause` and the loop body `do`. ```js { type: 'While', clause: CompoundList, do: CompoundList } ``` -------------------------------- ### Defining AssignmentWord AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents a special type of `Word` node specifically for assigning a value to an environment variable. Like a regular `Word`, it contains text and can undergo various expansions. It includes properties for its type, the raw `text`, and an array of potential `expansion` nodes. ```js { type: 'AssignmentWord', text: String, expansion: Array } ``` -------------------------------- ### Defining If Statement AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents an `If` statement in the AST. This node executes a `clause` (CompoundList) and uses its exit status to conditionally execute either the `then` CompoundList or the optional `else` CompoundList. It includes properties for the conditional `clause`, the `then` block, and the `else` block. ```js { type: 'If', clause: CompoundList, then: CompoundList, else: CompoundList } ``` -------------------------------- ### Defining the LogicalExpression AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `LogicalExpression` AST node, which represents `&&` (AND) or `||` (OR) operations between two commands. The `op` property specifies the operator, and `left` and `right` properties hold the respective command nodes, with execution of `right` depending on the success or failure of `left`. ```JavaScript { type: 'LogicalExpression', op: string, left: LogicalExpression | Pipeline | Command | Function | Subshell | For | Case | If | While | Until, right: LogicalExpression | Pipeline | Command | Function | Subshell | For | Case | If | While | Until } ``` -------------------------------- ### Defining Until Statement AST Node (JavaScript) Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md Represents an `Until` loop statement in the AST. This node continuously executes the `do` CompoundList as long as the `clause` CompoundList returns a non-zero exit status. It includes properties for the loop condition `clause` and the loop body `do`. ```js { type: 'Until', clause: CompoundList, do: CompoundList } ``` -------------------------------- ### Defining the Subshell AST Node in JavaScript Source: https://github.com/vorpaljs/bash-parser/blob/master/documents/ast.md This snippet defines the `Subshell` AST node, representing a group of commands executed in a separate shell environment. The `list` property contains a `CompoundList` node, which encapsulates the commands within the subshell. ```JavaScript { type: 'Subshell', list: CompoundList } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.