/** * @file ast.h * @brief The parse tree. * * A word is a list of parts. Each part is either literal text or one * expansion, and carries a flag saying whether it was quoted. Nothing is * encoded in-band in the text, which keeps the representation independent * of the character set. */ #ifndef ZASH_AST_H #define ZASH_AST_H #include "shell.h" /* The tree types refer to one another. */ typedef struct node node; typedef struct word word; typedef struct wpart wpart; typedef struct param param; typedef struct redir redir; typedef struct caseitem caseitem; /** * @brief Kinds of word part. */ typedef enum part_kind { P_LIT, P_PARAM, P_CMDSUB, P_ARITH } part_kind; /** * @brief One part of a word. */ typedef struct wpart { struct wpart *next; part_kind kind; bool quoted; /**< result is not split or globbed */ union { struct { const char *s; size_t len; } lit; param *param; node *cmd; /**< P_CMDSUB; nullptr for an empty $() */ struct wpart *arith; /**< P_ARITH: the expression text */ } u; } wpart; /** * @brief What a parameter name refers to, decided when it is parsed. */ typedef enum param_type { PT_VAR, PT_POS, PT_SPECIAL } param_type; /** * @brief The operator of a parameter expansion. */ typedef enum param_op { PS_NORMAL, PS_LENGTH, /**< `${#x}` */ PS_MINUS, /**< `${x-w}` */ PS_EQUAL, /**< `${x=w}` */ PS_QUESTION, /**< `${x?w}` */ PS_PLUS, /**< `${x+w}` */ PS_TRIML, /**< `${x#w}` */ PS_TRIMLL, /**< `${x##w}` */ PS_TRIMR, /**< `${x%w}` */ PS_TRIMRR, /**< `${x%%w}` */ } param_op; /** * @brief A parameter expansion: `$x`, `${x}`, `${x:-word}` and so on. */ typedef struct param { const char *name; size_t nlen; param_type type; param_op op; bool colon; /**< the ":" form: treat empty as unset */ char special; /**< PT_SPECIAL: one of @ * # ? - $ ! */ int pos; /**< PT_POS */ wpart *arg; } param; /** * @brief A word of a command. */ typedef struct word { struct word *next; wpart *parts; const char *plain; /**< set when the word is this exact string as written */ size_t assign_len; /**< length of NAME when the word looks like NAME=... */ } word; /** * @brief Redirection operators. */ typedef enum redir_op { R_IN, /**< < */ R_OUT, /**< > */ R_CLOBBER, /**< >| */ R_APPEND, /**< >> */ R_RDWR, /**< <> */ R_DUPIN, /**< <& */ R_DUPOUT, /**< >& */ R_HERE, /**< << and <<- */ } redir_op; /** * @brief One redirection. */ typedef struct redir { struct redir *next; int fd; redir_op op; word *target; /**< file name, fd number, or here-document body */ } redir; /** * @brief One `pattern) list ;;` item of a `case` command. */ typedef struct caseitem { struct caseitem *next; word *patterns; node *body; } caseitem; /** * @brief Kinds of command. */ typedef enum node_kind { N_SIMPLE, N_PIPE, N_AND, N_OR, N_SEQ, N_NOT, N_BG, N_SUBSHELL, N_GROUP, N_IF, N_WHILE, N_UNTIL, N_FOR, N_CASE, N_FUNC, N_ARITH, /**< (( expression )) */ N_COND, /**< [[ expression ]] */ N_TIME, /**< time pipeline */ } node_kind; /** * @brief Kinds of node in a `[[ ]]` expression. */ typedef enum cond_kind { CN_AND, /**< left && right */ CN_OR, /**< left || right */ CN_NOT, /**< ! left */ CN_UNARY, /**< -op a */ CN_BINARY, /**< a op b */ CN_STRING, /**< a: true if not empty */ } cond_kind; /** * @brief Binary operators of `[[ ]]`. */ typedef enum cond_op { CO_MATCH, /**< `=`, `==`: a matches the pattern b */ CO_NOMATCH, /**< `!=`: a does not match the pattern b */ CO_REGEX, /**< `=~`: a matches the extended regular expression b */ CO_LESS, /**< `<`: a sorts before b */ CO_GREATER, /**< `>`: a sorts after b */ CO_EQ, /**< `-eq` (operands are arithmetic expressions) */ CO_NE, /**< `-ne` */ CO_LT, /**< `-lt` */ CO_LE, /**< `-le` */ CO_GT, /**< `-gt` */ CO_GE, /**< `-ge` */ CO_NEWER, /**< `-nt` */ CO_OLDER, /**< `-ot` */ CO_SAMEFILE, /**< `-ef` */ } cond_op; /** * @brief One node of a `[[ ]]` expression. * * The operands are words that have not been expanded: they are expanded * when the expression is evaluated, without field splitting or pathname * expansion, and only as far as `&&` and `||` require. */ typedef struct cond { cond_kind kind; cond_op op; /**< CN_BINARY: the operator */ char uop; /**< CN_UNARY: the operator's letter */ struct cond *left; /**< CN_AND, CN_OR, CN_NOT */ struct cond *right; /**< CN_AND, CN_OR */ word *a; /**< CN_UNARY, CN_BINARY, CN_STRING */ word *b; /**< CN_BINARY */ } cond; /** * @brief A command. */ typedef struct node { node_kind kind; int lineno; redir *redirs; union { struct { word *assigns; word *args; } simple; struct { struct node **cmds; int n; } pipe; struct { struct node *left; struct node *right; } bin; /**< N_AND, N_OR, N_SEQ */ struct node *body; /**< N_NOT, N_BG, N_SUBSHELL, N_GROUP */ struct { struct node *body; /**< the pipeline, or nullptr for none */ bool posix; /**< `-p`: the POSIX output format */ } time; /**< N_TIME */ wpart *arith; /**< N_ARITH: one P_ARITH part */ cond *cond; /**< N_COND: the expression */ struct { struct node *cond; struct node *then_part; struct node *else_part; } if_; struct { struct node *cond; struct node *body; } loop; struct { const char *var; word *words; bool has_in; struct node *body; } for_; struct { word *subject; caseitem *items; } case_; struct { const char *name; struct node *body; arena *arena; /**< owns the body */ } func; } u; } node; #endif