1use jrsonnet_interner::IStr;2#[cfg(feature = "deserialize")]3use serde::Deserialize;4#[cfg(feature = "serialize")]5use serde::Serialize;6use std::{7 fmt::{Debug, Display},8 ops::Deref,9 path::PathBuf,10 rc::Rc,11};1213#[cfg_attr(feature = "serialize", derive(Serialize))]14#[cfg_attr(feature = "deserialize", derive(Deserialize))]15#[derive(Debug, PartialEq)]16pub enum FieldName {17 18 Fixed(IStr),19 20 Dyn(LocExpr),21}2223#[cfg_attr(feature = "serialize", derive(Serialize))]24#[cfg_attr(feature = "deserialize", derive(Deserialize))]25#[derive(Debug, Clone, Copy, PartialEq)]26pub enum Visibility {27 28 Normal,29 30 Hidden,31 32 Unhide,33}3435#[cfg_attr(feature = "serialize", derive(Serialize))]36#[cfg_attr(feature = "deserialize", derive(Deserialize))]37#[derive(Debug, PartialEq)]38pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);3940#[cfg_attr(feature = "serialize", derive(Serialize))]41#[cfg_attr(feature = "deserialize", derive(Deserialize))]42#[derive(Debug, PartialEq)]43pub struct FieldMember {44 pub name: FieldName,45 pub plus: bool,46 pub params: Option<ParamsDesc>,47 pub visibility: Visibility,48 pub value: LocExpr,49}5051#[cfg_attr(feature = "serialize", derive(Serialize))]52#[cfg_attr(feature = "deserialize", derive(Deserialize))]53#[derive(Debug, PartialEq)]54pub enum Member {55 Field(FieldMember),56 BindStmt(BindSpec),57 AssertStmt(AssertStmt),58}5960#[cfg_attr(feature = "serialize", derive(Serialize))]61#[cfg_attr(feature = "deserialize", derive(Deserialize))]62#[derive(Debug, Clone, Copy, PartialEq)]63pub enum UnaryOpType {64 Plus,65 Minus,66 BitNot,67 Not,68}69impl Display for UnaryOpType {70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {71 use UnaryOpType::*;72 write!(73 f,74 "{}",75 match self {76 Plus => "+",77 Minus => "-",78 BitNot => "~",79 Not => "!",80 }81 )82 }83}8485#[cfg_attr(feature = "serialize", derive(Serialize))]86#[cfg_attr(feature = "deserialize", derive(Deserialize))]87#[derive(Debug, Clone, Copy, PartialEq)]88pub enum BinaryOpType {89 Mul,90 Div,9192 93 Mod,9495 Add,96 Sub,9798 Lhs,99 Rhs,100101 Lt,102 Gt,103 Lte,104 Gte,105106 BitAnd,107 BitOr,108 BitXor,109110 And,111 Or,112}113impl Display for BinaryOpType {114 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {115 use BinaryOpType::*;116 write!(117 f,118 "{}",119 match self {120 Mul => "*",121 Div => "/",122 Mod => "%",123 Add => "+",124 Sub => "-",125 Lhs => "<<",126 Rhs => ">>",127 Lt => "<",128 Gt => ">",129 Lte => "<=",130 Gte => ">=",131 BitAnd => "&",132 BitOr => "|",133 BitXor => "^",134 And => "&&",135 Or => "||",136 }137 )138 }139}140141142#[cfg_attr(feature = "serialize", derive(Serialize))]143#[cfg_attr(feature = "deserialize", derive(Deserialize))]144#[derive(Debug, PartialEq)]145pub struct Param(pub IStr, pub Option<LocExpr>);146147148#[cfg_attr(feature = "serialize", derive(Serialize))]149#[cfg_attr(feature = "deserialize", derive(Deserialize))]150#[derive(Debug, Clone, PartialEq)]151pub struct ParamsDesc(pub Rc<Vec<Param>>);152impl Deref for ParamsDesc {153 type Target = Vec<Param>;154 fn deref(&self) -> &Self::Target {155 &self.0156 }157}158159#[cfg_attr(feature = "serialize", derive(Serialize))]160#[cfg_attr(feature = "deserialize", derive(Deserialize))]161#[derive(Debug, PartialEq)]162pub struct Arg(pub Option<String>, pub LocExpr);163164#[cfg_attr(feature = "serialize", derive(Serialize))]165#[cfg_attr(feature = "deserialize", derive(Deserialize))]166#[derive(Debug, PartialEq)]167pub struct ArgsDesc(pub Vec<Arg>);168impl Deref for ArgsDesc {169 type Target = Vec<Arg>;170 fn deref(&self) -> &Self::Target {171 &self.0172 }173}174175#[cfg_attr(feature = "serialize", derive(Serialize))]176#[cfg_attr(feature = "deserialize", derive(Deserialize))]177#[derive(Debug, Clone, PartialEq)]178pub struct BindSpec {179 pub name: IStr,180 pub params: Option<ParamsDesc>,181 pub value: LocExpr,182}183184#[cfg_attr(feature = "serialize", derive(Serialize))]185#[cfg_attr(feature = "deserialize", derive(Deserialize))]186#[derive(Debug, PartialEq)]187pub struct IfSpecData(pub LocExpr);188189#[cfg_attr(feature = "serialize", derive(Serialize))]190#[cfg_attr(feature = "deserialize", derive(Deserialize))]191#[derive(Debug, PartialEq)]192pub struct ForSpecData(pub IStr, pub LocExpr);193194#[cfg_attr(feature = "serialize", derive(Serialize))]195#[cfg_attr(feature = "deserialize", derive(Deserialize))]196#[derive(Debug, PartialEq)]197pub enum CompSpec {198 IfSpec(IfSpecData),199 ForSpec(ForSpecData),200}201202#[cfg_attr(feature = "serialize", derive(Serialize))]203#[cfg_attr(feature = "deserialize", derive(Deserialize))]204#[derive(Debug, PartialEq)]205pub struct ObjComp {206 pub pre_locals: Vec<BindSpec>,207 pub key: LocExpr,208 pub value: LocExpr,209 pub post_locals: Vec<BindSpec>,210 pub compspecs: Vec<CompSpec>,211}212213#[cfg_attr(feature = "serialize", derive(Serialize))]214#[cfg_attr(feature = "deserialize", derive(Deserialize))]215#[derive(Debug, PartialEq)]216pub enum ObjBody {217 MemberList(Vec<Member>),218 ObjComp(ObjComp),219}220221#[cfg_attr(feature = "serialize", derive(Serialize))]222#[cfg_attr(feature = "deserialize", derive(Deserialize))]223#[derive(Debug, PartialEq, Clone, Copy)]224pub enum LiteralType {225 This,226 Super,227 Dollar,228 Null,229 True,230 False,231}232233#[derive(Debug, PartialEq)]234pub struct SliceDesc {235 pub start: Option<LocExpr>,236 pub end: Option<LocExpr>,237 pub step: Option<LocExpr>,238}239240241#[cfg_attr(feature = "serialize", derive(Serialize))]242#[cfg_attr(feature = "deserialize", derive(Deserialize))]243#[derive(Debug, PartialEq)]244pub enum Expr {245 Literal(LiteralType),246247 248 Str(IStr),249 250 Num(f64),251 252 Var(IStr),253254 255 Arr(Vec<LocExpr>),256 257 258 259 260 261 262 263 264 265 266 267 ArrComp(LocExpr, Vec<CompSpec>),268269 270 Obj(ObjBody),271 272 ObjExtend(LocExpr, ObjBody),273274 275 Parened(LocExpr),276277 278 UnaryOp(UnaryOpType, LocExpr),279 280 BinaryOp(LocExpr, BinaryOpType, LocExpr),281 282 AssertExpr(AssertStmt, LocExpr),283 284 LocalExpr(Vec<BindSpec>, LocExpr),285286 287 Import(PathBuf),288 289 ImportStr(PathBuf),290 291 ErrorStmt(LocExpr),292 293 Apply(LocExpr, ArgsDesc, bool),294 295 Index(LocExpr, LocExpr),296 297 Function(ParamsDesc, LocExpr),298 299 Intrinsic(IStr),300 301 IfElse {302 cond: IfSpecData,303 cond_then: LocExpr,304 cond_else: Option<LocExpr>,305 },306}307308309#[cfg_attr(feature = "serialize", derive(Serialize))]310#[cfg_attr(feature = "deserialize", derive(Deserialize))]311#[derive(Clone, PartialEq)]312pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);313impl Debug for ExprLocation {314 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {315 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)316 }317}318319320#[cfg_attr(feature = "serialize", derive(Serialize))]321#[cfg_attr(feature = "deserialize", derive(Deserialize))]322#[derive(Clone, PartialEq)]323pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);324impl Debug for LocExpr {325 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {326 write!(f, "{:?} from {:?}", self.0, self.1)327 }328}329330331#[macro_export]332macro_rules! loc_expr {333 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {334 LocExpr(335 std::rc::Rc::new($expr),336 if $need_loc {337 Some(ExprLocation($name, $start, $end))338 } else {339 None340 },341 )342 };343}344345346#[macro_export]347macro_rules! loc_expr_todo {348 ($expr:expr) => {349 LocExpr(Rc::new($expr), None)350 };351}