summaryrefslogtreecommitdiffhomepage
path: root/src/ast.h
blob: a9c61ff17eb72f6914dd72f28ec1971124ca64b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
 * @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; NULL 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 NULL 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