1use crate::value::JsonnetValue;
4
5#[derive(Debug, Clone, PartialEq)]
7pub enum StringInterpolationPart {
8 Literal(String),
10 Interpolation(Box<Expr>),
12}
13
14#[derive(Debug, Clone, PartialEq)]
16pub enum Expr {
17 Literal(JsonnetValue),
19
20 StringInterpolation(Vec<StringInterpolationPart>),
22
23 Var(String),
25
26 BinaryOp {
28 left: Box<Expr>,
29 op: BinaryOp,
30 right: Box<Expr>,
31 },
32
33 UnaryOp {
35 op: UnaryOp,
36 expr: Box<Expr>,
37 },
38
39 Array(Vec<Expr>),
41
42 Object(Vec<ObjectField>),
44
45 ArrayComp {
47 expr: Box<Expr>,
48 var: String,
49 array: Box<Expr>,
50 cond: Option<Box<Expr>>,
51 },
52
53 ObjectComp {
55 field: Box<ObjectField>,
56 var: String,
57 array: Box<Expr>,
58 },
59
60 Call {
62 func: Box<Expr>,
63 args: Vec<Expr>,
64 },
65
66 Index {
68 target: Box<Expr>,
69 index: Box<Expr>,
70 },
71
72 Slice {
74 target: Box<Expr>,
75 start: Option<Box<Expr>>,
76 end: Option<Box<Expr>>,
77 step: Option<Box<Expr>>,
78 },
79
80 Local {
82 bindings: Vec<(String, Expr)>,
83 body: Box<Expr>,
84 },
85
86 Function {
88 parameters: Vec<String>,
89 body: Box<Expr>,
90 },
91
92 If {
94 cond: Box<Expr>,
95 then_branch: Box<Expr>,
96 else_branch: Option<Box<Expr>>,
97 },
98
99 Assert {
101 cond: Box<Expr>,
102 message: Option<Box<Expr>>,
103 expr: Box<Expr>,
104 },
105
106 Import(String),
108
109 ImportStr(String),
111
112 Error(Box<Expr>),
114}
115
116#[derive(Debug, Clone, PartialEq)]
118pub struct ObjectField {
119 pub name: FieldName,
121 pub visibility: Visibility,
123 pub expr: Box<Expr>,
125}
126
127#[derive(Debug, Clone, PartialEq)]
129pub enum FieldName {
130 Fixed(String),
132 Computed(Box<Expr>),
134}
135
136#[derive(Debug, Clone, PartialEq)]
138pub enum Visibility {
139 Normal,
141 Hidden,
143 Forced,
145}
146
147#[derive(Debug, Clone, PartialEq, Copy)]
149pub enum BinaryOp {
150 Add,
152 Sub,
153 Mul,
154 Div,
155 Mod,
156
157 Eq,
159 Ne,
160 Lt,
161 Le,
162 Gt,
163 Ge,
164
165 And,
167 Or,
168
169 BitAnd,
171 BitOr,
172 BitXor,
173 ShiftL,
174 ShiftR,
175
176 In,
178
179 Concat,
181}
182
183#[derive(Debug, Clone, PartialEq, Copy)]
185pub enum UnaryOp {
186 Not,
188 BitNot,
190 Neg,
192 Pos,
194}
195
196#[derive(Debug, Clone, PartialEq)]
198pub enum Stmt {
199 Expr(Expr),
201 Local(Vec<(String, Expr)>),
203 Assert {
205 cond: Expr,
206 message: Option<Expr>,
207 },
208}
209
210#[derive(Debug, Clone, PartialEq)]
212pub struct Program {
213 pub statements: Vec<Stmt>,
214}
215
216impl Program {
217 pub fn new() -> Self {
219 Program {
220 statements: Vec::new(),
221 }
222 }
223
224 pub fn add_statement(&mut self, stmt: Stmt) {
226 self.statements.push(stmt);
227 }
228}
229
230impl Default for Program {
231 fn default() -> Self {
232 Self::new()
233 }
234}