Skip to main content

frost_parser/
ast.rs

1use compact_str::CompactString;
2use frost_lexer::Span;
3
4#[derive(Debug, Clone, PartialEq)]
5pub struct Program {
6    pub commands: Vec<CompleteCommand>,
7}
8
9#[derive(Debug, Clone, PartialEq)]
10pub struct CompleteCommand {
11    pub list: List,
12    pub is_async: bool,
13}
14
15#[derive(Debug, Clone, PartialEq)]
16pub struct List {
17    pub first: Pipeline,
18    pub rest: Vec<(ListOp, Pipeline)>,
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum ListOp {
23    And,
24    Or,
25}
26
27#[derive(Debug, Clone, PartialEq)]
28pub struct Pipeline {
29    pub bang: bool,
30    pub commands: Vec<Command>,
31    pub pipe_stderr: Vec<bool>,
32}
33
34#[derive(Debug, Clone, PartialEq)]
35pub enum Command {
36    Simple(SimpleCommand),
37    Subshell(Subshell),
38    BraceGroup(BraceGroup),
39    If(Box<IfClause>),
40    For(Box<ForClause>),
41    ArithFor(Box<ArithForClause>),
42    While(Box<WhileClause>),
43    Until(Box<UntilClause>),
44    Case(Box<CaseClause>),
45    Select(Box<SelectClause>),
46    Repeat(Box<RepeatClause>),
47    Always(Box<AlwaysClause>),
48    FunctionDef(Box<FunctionDef>),
49    Coproc(Box<Coproc>),
50    Time(Box<TimeClause>),
51}
52
53#[derive(Debug, Clone, PartialEq)]
54pub struct SimpleCommand {
55    pub assignments: Vec<Assignment>,
56    pub words: Vec<Word>,
57    pub redirects: Vec<Redirect>,
58}
59
60#[derive(Debug, Clone, PartialEq)]
61pub struct Word {
62    pub parts: Vec<WordPart>,
63    pub span: Span,
64}
65
66#[derive(Debug, Clone, PartialEq)]
67pub enum WordPart {
68    Literal(CompactString),
69    SingleQuoted(CompactString),
70    DoubleQuoted(Vec<WordPart>),
71    DollarVar(CompactString),
72    DollarBrace {
73        param: CompactString,
74        operator: Option<CompactString>,
75        arg: Option<Box<Word>>,
76    },
77    CommandSub(Box<Program>),
78    ArithSub(CompactString),
79    Glob(GlobKind),
80    Tilde(CompactString),
81}
82
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub enum GlobKind {
85    Star,
86    Question,
87    At,
88}
89
90#[derive(Debug, Clone, PartialEq)]
91pub struct Redirect {
92    pub fd: Option<u32>,
93    pub op: RedirectOp,
94    pub target: Word,
95    pub span: Span,
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99pub enum RedirectOp {
100    Less,
101    Greater,
102    DoubleGreater,
103    GreaterPipe,
104    GreaterBang,
105    AmpGreater,
106    AmpDoubleGreater,
107    DoubleLess,
108    TripleLess,
109    DoubleLessDash,
110    LessGreater,
111    FdDup,
112}
113
114#[derive(Debug, Clone, PartialEq)]
115pub struct Assignment {
116    pub name: CompactString,
117    pub op: AssignOp,
118    pub value: Option<Word>,
119    pub span: Span,
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub enum AssignOp {
124    Assign,
125    Append,
126}
127
128#[derive(Debug, Clone, PartialEq)]
129pub struct Subshell {
130    pub body: Vec<CompleteCommand>,
131    pub redirects: Vec<Redirect>,
132}
133
134#[derive(Debug, Clone, PartialEq)]
135pub struct BraceGroup {
136    pub body: Vec<CompleteCommand>,
137    pub redirects: Vec<Redirect>,
138}
139
140#[derive(Debug, Clone, PartialEq)]
141pub struct IfClause {
142    pub condition: Vec<CompleteCommand>,
143    pub then_body: Vec<CompleteCommand>,
144    pub elifs: Vec<(Vec<CompleteCommand>, Vec<CompleteCommand>)>,
145    pub else_body: Option<Vec<CompleteCommand>>,
146    pub redirects: Vec<Redirect>,
147}
148
149#[derive(Debug, Clone, PartialEq)]
150pub struct ForClause {
151    pub var: CompactString,
152    pub words: Option<Vec<Word>>,
153    pub body: Vec<CompleteCommand>,
154    pub redirects: Vec<Redirect>,
155}
156
157#[derive(Debug, Clone, PartialEq)]
158pub struct ArithForClause {
159    pub init: CompactString,
160    pub condition: CompactString,
161    pub step: CompactString,
162    pub body: Vec<CompleteCommand>,
163    pub redirects: Vec<Redirect>,
164}
165
166#[derive(Debug, Clone, PartialEq)]
167pub struct WhileClause {
168    pub condition: Vec<CompleteCommand>,
169    pub body: Vec<CompleteCommand>,
170    pub redirects: Vec<Redirect>,
171}
172
173#[derive(Debug, Clone, PartialEq)]
174pub struct UntilClause {
175    pub condition: Vec<CompleteCommand>,
176    pub body: Vec<CompleteCommand>,
177    pub redirects: Vec<Redirect>,
178}
179
180#[derive(Debug, Clone, PartialEq)]
181pub struct CaseClause {
182    pub word: Word,
183    pub items: Vec<CaseItem>,
184    pub redirects: Vec<Redirect>,
185}
186
187#[derive(Debug, Clone, PartialEq)]
188pub struct CaseItem {
189    pub patterns: Vec<Word>,
190    pub body: Vec<CompleteCommand>,
191    pub terminator: CaseTerminator,
192}
193
194#[derive(Debug, Clone, Copy, PartialEq, Eq)]
195pub enum CaseTerminator {
196    DoubleSemi,
197    SemiAnd,
198    SemiPipe,
199}
200
201#[derive(Debug, Clone, PartialEq)]
202pub struct SelectClause {
203    pub var: CompactString,
204    pub words: Option<Vec<Word>>,
205    pub body: Vec<CompleteCommand>,
206    pub redirects: Vec<Redirect>,
207}
208
209#[derive(Debug, Clone, PartialEq)]
210pub struct RepeatClause {
211    pub count: Word,
212    pub body: Vec<CompleteCommand>,
213    pub redirects: Vec<Redirect>,
214}
215
216#[derive(Debug, Clone, PartialEq)]
217pub struct AlwaysClause {
218    pub try_body: Vec<CompleteCommand>,
219    pub always_body: Vec<CompleteCommand>,
220    pub redirects: Vec<Redirect>,
221}
222
223#[derive(Debug, Clone, PartialEq)]
224pub struct FunctionDef {
225    pub name: CompactString,
226    pub body: Command,
227    pub redirects: Vec<Redirect>,
228}
229
230#[derive(Debug, Clone, PartialEq)]
231pub struct Coproc {
232    pub name: Option<CompactString>,
233    pub command: Command,
234}
235
236#[derive(Debug, Clone, PartialEq)]
237pub struct TimeClause {
238    pub pipeline: Pipeline,
239}