Expand description

Shell command language syntax.

This module contains types that represent abstract syntax trees (ASTs) of the shell language.

Some types in this module has the type parameter <H = HereDoc>. As a user of ASTs, you will never have to specify the parameter other than the default HereDoc. The parameter with non-default types is used internally by the parser to create intermediate ASTs that lack sub-trees for here-documents, as the contents of here-documents have to be parsed separately from the normal flow of source code.

Syntactic elements

The AST type that represents the whole shell script is List, which is a vector of Items. An Item is a possibly asynchronous AndOrList, which is a sequence of conditionally executed Pipelines. A Pipeline is a sequence of Commands separated by |.

There are several types of Commands, namely SimpleCommand, CompoundCommand and FunctionDefinition, where CompoundCommand in turn comes in many variants.

Lexical elements

Tokens that make up commands may contain quotations and expansions. A Word, a sequence of WordUnits, represents such a token that appears in a simple command and some kinds of other commands.

In some contexts, tilde expansion and single- and double-quotes are not recognized while other kinds of expansions are allowed. Such part is represented as Text, a sequence of TextUnits.

Parsing

Most AST types defined in this module implement the FromStr trait, which means you can easily get an AST out of source code by calling parse on a &str. However, all locations in ASTs constructed this way will only have unknown source.

use std::str::FromStr;
let list: List = "diff foo bar; echo $?".parse().unwrap();
assert_eq!(list.to_string(), "diff foo bar; echo $?");

use yash_syntax::source::Source;
let word: Word = "foo".parse().unwrap();
assert_eq!(word.location.code.source, Source::Unknown);

To include substantial source information in the AST, you need to prepare a lexer with source information and then use it to parse the source code. See the parser module for details.

Displaying

Most AST types support the Display trait, which allows you to convert an AST to a source code string. Note that the Display trait implementations always produce single-line source code with here-document contents omitted. To pretty-format an AST in multiple lines with here-document contents included, you can use … TODO TBD.

Re-exports

pub use TextUnit::*;
pub use WordUnit::*;
pub use Value::*;

Structs

Pipelines separated by && and ||.

Assignment word.

Branch item of a case compound command.

elif-then clause.

File descriptor.

Compound command with redirections.

Function definition command.

Here-document.

Element of a List.

Sequence of and-or lists separated by ; or &.

Parameter expansion enclosed in braces.

Commands separated by |

Redirection.

Command that involves assignments, redirections, and word expansions.

Parameter expansion modifier that conditionally substitutes the value being expanded.

String that may contain some expansions.

Parameter expansion modifier that removes the beginning or end of the value being expanded.

Token that may involve expansions and quotes.

Enums

Condition that decides if a Pipeline in an and-or list should be executed.

Element of a pipe sequence.

Command that contains other commands.

Attribute that modifies a parameter expansion.

Part of a redirection that defines the nature of the resulting file descriptor.

Redirection operators.

Condition that triggers a switch.

Flag that specifies how the value is substituted in a switch.

Element of a Text, i.e., something that can be expanded.

Flag that specifies pattern matching strategy in a trim.

Flag that specifies which side of the expanded value is removed in a trim.

Value of an assignment.

Element of a Word, i.e., text with quotes and tilde expansion.

Traits

Possibly literal syntax element.

Removing quotes from syntax without performing expansion.