git.delta.rocks / jrsonnet / refs/commits / 8d08861d28f9

difftreelog

source

crates/jrsonnet-parser/src/expr.rs9.0 KiBsourcehistory
1use jrsonnet_gc::{unsafe_empty_trace, Finalize, Trace};2use jrsonnet_interner::IStr;3#[cfg(feature = "deserialize")]4use serde::Deserialize;5#[cfg(feature = "serialize")]6use serde::Serialize;7use std::{8	fmt::{Debug, Display},9	ops::Deref,10	path::{Path, PathBuf},11	rc::Rc,12};1314#[cfg_attr(feature = "serialize", derive(Serialize))]15#[cfg_attr(feature = "deserialize", derive(Deserialize))]16#[derive(Debug, PartialEq, Trace)]17#[trivially_drop]18pub enum FieldName {19	/// {fixed: 2}20	Fixed(IStr),21	/// {["dyn"+"amic"]: 3}22	Dyn(LocExpr),23}2425#[cfg_attr(feature = "serialize", derive(Serialize))]26#[cfg_attr(feature = "deserialize", derive(Deserialize))]27#[derive(Debug, Clone, Copy, PartialEq, Trace)]28#[trivially_drop]29pub enum Visibility {30	/// :31	Normal,32	/// ::33	Hidden,34	/// :::35	Unhide,36}3738impl Visibility {39	pub fn is_visible(&self) -> bool {40		matches!(self, Self::Normal | Self::Unhide)41	}42}4344#[cfg_attr(feature = "serialize", derive(Serialize))]45#[cfg_attr(feature = "deserialize", derive(Deserialize))]46#[derive(Clone, Debug, PartialEq, Trace)]47#[trivially_drop]48pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4950#[cfg_attr(feature = "serialize", derive(Serialize))]51#[cfg_attr(feature = "deserialize", derive(Deserialize))]52#[derive(Debug, PartialEq, Trace)]53#[trivially_drop]54pub struct FieldMember {55	pub name: FieldName,56	pub plus: bool,57	pub params: Option<ParamsDesc>,58	pub visibility: Visibility,59	pub value: LocExpr,60}6162#[cfg_attr(feature = "serialize", derive(Serialize))]63#[cfg_attr(feature = "deserialize", derive(Deserialize))]64#[derive(Debug, PartialEq, Trace)]65#[trivially_drop]66pub enum Member {67	Field(FieldMember),68	BindStmt(BindSpec),69	AssertStmt(AssertStmt),70}7172#[cfg_attr(feature = "serialize", derive(Serialize))]73#[cfg_attr(feature = "deserialize", derive(Deserialize))]74#[derive(Debug, Clone, Copy, PartialEq, Trace)]75#[trivially_drop]76pub enum UnaryOpType {77	Plus,78	Minus,79	BitNot,80	Not,81}8283impl Display for UnaryOpType {84	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {85		use UnaryOpType::*;86		write!(87			f,88			"{}",89			match self {90				Plus => "+",91				Minus => "-",92				BitNot => "~",93				Not => "!",94			}95		)96	}97}9899#[cfg_attr(feature = "serialize", derive(Serialize))]100#[cfg_attr(feature = "deserialize", derive(Deserialize))]101#[derive(Debug, Clone, Copy, PartialEq, Trace)]102#[trivially_drop]103pub enum BinaryOpType {104	Mul,105	Div,106107	/// Implemented as intrinsic, put here for completeness108	Mod,109110	Add,111	Sub,112113	Lhs,114	Rhs,115116	Lt,117	Gt,118	Lte,119	Gte,120121	BitAnd,122	BitOr,123	BitXor,124125	Eq,126	Neq,127128	And,129	Or,130}131132impl Display for BinaryOpType {133	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {134		use BinaryOpType::*;135		write!(136			f,137			"{}",138			match self {139				Mul => "*",140				Div => "/",141				Mod => "%",142				Add => "+",143				Sub => "-",144				Lhs => "<<",145				Rhs => ">>",146				Lt => "<",147				Gt => ">",148				Lte => "<=",149				Gte => ">=",150				BitAnd => "&",151				BitOr => "|",152				BitXor => "^",153				Eq => "==",154				Neq => "!=",155				And => "&&",156				Or => "||",157			}158		)159	}160}161162/// name, default value163#[cfg_attr(feature = "serialize", derive(Serialize))]164#[cfg_attr(feature = "deserialize", derive(Deserialize))]165#[derive(Debug, PartialEq, Trace)]166#[trivially_drop]167pub struct Param(pub IStr, pub Option<LocExpr>);168169/// Defined function parameters170#[cfg_attr(feature = "serialize", derive(Serialize))]171#[cfg_attr(feature = "deserialize", derive(Deserialize))]172#[derive(Debug, Clone, PartialEq)]173pub struct ParamsDesc(pub Rc<Vec<Param>>);174175/// Safety:176/// AST is acyclic, and there should be no gc pointers177unsafe impl Trace for ParamsDesc {178	unsafe_empty_trace!();179}180impl Finalize for ParamsDesc {}181182impl Deref for ParamsDesc {183	type Target = Vec<Param>;184	fn deref(&self) -> &Self::Target {185		&self.0186	}187}188189#[cfg_attr(feature = "serialize", derive(Serialize))]190#[cfg_attr(feature = "deserialize", derive(Deserialize))]191#[derive(Debug, PartialEq, Trace)]192#[trivially_drop]193pub struct Arg(pub Option<String>, pub LocExpr);194195#[cfg_attr(feature = "serialize", derive(Serialize))]196#[cfg_attr(feature = "deserialize", derive(Deserialize))]197#[derive(Debug, PartialEq, Trace)]198#[trivially_drop]199pub struct ArgsDesc(pub Vec<Arg>);200201impl Deref for ArgsDesc {202	type Target = Vec<Arg>;203	fn deref(&self) -> &Self::Target {204		&self.0205	}206}207208#[cfg_attr(feature = "serialize", derive(Serialize))]209#[cfg_attr(feature = "deserialize", derive(Deserialize))]210#[derive(Debug, Clone, PartialEq, Trace)]211#[trivially_drop]212pub struct BindSpec {213	pub name: IStr,214	pub params: Option<ParamsDesc>,215	pub value: LocExpr,216}217218#[cfg_attr(feature = "serialize", derive(Serialize))]219#[cfg_attr(feature = "deserialize", derive(Deserialize))]220#[derive(Debug, PartialEq, Trace)]221#[trivially_drop]222pub struct IfSpecData(pub LocExpr);223224#[cfg_attr(feature = "serialize", derive(Serialize))]225#[cfg_attr(feature = "deserialize", derive(Deserialize))]226#[derive(Debug, PartialEq, Trace)]227#[trivially_drop]228pub struct ForSpecData(pub IStr, pub LocExpr);229230#[cfg_attr(feature = "serialize", derive(Serialize))]231#[cfg_attr(feature = "deserialize", derive(Deserialize))]232#[derive(Debug, PartialEq, Trace)]233#[trivially_drop]234pub enum CompSpec {235	IfSpec(IfSpecData),236	ForSpec(ForSpecData),237}238239#[cfg_attr(feature = "serialize", derive(Serialize))]240#[cfg_attr(feature = "deserialize", derive(Deserialize))]241#[derive(Debug, PartialEq, Trace)]242#[trivially_drop]243pub struct ObjComp {244	pub pre_locals: Vec<BindSpec>,245	pub key: LocExpr,246	pub value: LocExpr,247	pub post_locals: Vec<BindSpec>,248	pub compspecs: Vec<CompSpec>,249}250251#[cfg_attr(feature = "serialize", derive(Serialize))]252#[cfg_attr(feature = "deserialize", derive(Deserialize))]253#[derive(Debug, PartialEq, Trace)]254#[trivially_drop]255pub enum ObjBody {256	MemberList(Vec<Member>),257	ObjComp(ObjComp),258}259260#[cfg_attr(feature = "serialize", derive(Serialize))]261#[cfg_attr(feature = "deserialize", derive(Deserialize))]262#[derive(Debug, PartialEq, Clone, Copy, Trace)]263#[trivially_drop]264pub enum LiteralType {265	This,266	Super,267	Dollar,268	Null,269	True,270	False,271}272273#[derive(Debug, PartialEq, Trace)]274#[trivially_drop]275pub struct SliceDesc {276	pub start: Option<LocExpr>,277	pub end: Option<LocExpr>,278	pub step: Option<LocExpr>,279}280281/// Syntax base282#[cfg_attr(feature = "serialize", derive(Serialize))]283#[cfg_attr(feature = "deserialize", derive(Deserialize))]284#[derive(Debug, PartialEq, Trace)]285#[trivially_drop]286pub enum Expr {287	Literal(LiteralType),288289	/// String value: "hello"290	Str(IStr),291	/// Number: 1, 2.0, 2e+20292	Num(f64),293	/// Variable name: test294	Var(IStr),295296	/// Array of expressions: [1, 2, "Hello"]297	Arr(Vec<LocExpr>),298	/// Array comprehension:299	/// ```jsonnet300	///  ingredients: [301	///    { kind: kind, qty: 4 / 3 }302	///    for kind in [303	///      'Honey Syrup',304	///      'Lemon Juice',305	///      'Farmers Gin',306	///    ]307	///  ],308	/// ```309	ArrComp(LocExpr, Vec<CompSpec>),310311	/// Object: {a: 2}312	Obj(ObjBody),313	/// Object extension: var1 {b: 2}314	ObjExtend(LocExpr, ObjBody),315316	/// (obj)317	Parened(LocExpr),318319	/// -2320	UnaryOp(UnaryOpType, LocExpr),321	/// 2 - 2322	BinaryOp(LocExpr, BinaryOpType, LocExpr),323	/// assert 2 == 2 : "Math is broken"324	AssertExpr(AssertStmt, LocExpr),325	/// local a = 2; { b: a }326	LocalExpr(Vec<BindSpec>, LocExpr),327328	/// import "hello"329	Import(PathBuf),330	/// importStr "file.txt"331	ImportStr(PathBuf),332	/// error "I'm broken"333	ErrorStmt(LocExpr),334	/// a(b, c)335	Apply(LocExpr, ArgsDesc, bool),336	/// a[b]337	Index(LocExpr, LocExpr),338	/// function(x) x339	Function(ParamsDesc, LocExpr),340	/// std.primitiveEquals341	Intrinsic(IStr),342	/// if true == false then 1 else 2343	IfElse {344		cond: IfSpecData,345		cond_then: LocExpr,346		cond_else: Option<LocExpr>,347	},348}349350/// file, begin offset, end offset351#[cfg_attr(feature = "serialize", derive(Serialize))]352#[cfg_attr(feature = "deserialize", derive(Deserialize))]353#[derive(Clone, PartialEq, Trace)]354#[trivially_drop]355pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);356357impl Debug for ExprLocation {358	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {359		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)360	}361}362363/// Holds AST expression and its location in source file364#[cfg_attr(feature = "serialize", derive(Serialize))]365#[cfg_attr(feature = "deserialize", derive(Deserialize))]366#[derive(Clone, PartialEq)]367pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);368/// Safety:369/// AST is acyclic, and there should be no gc pointers370unsafe impl Trace for LocExpr {371	unsafe_empty_trace!();372}373impl Finalize for LocExpr {}374375impl Debug for LocExpr {376	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {377		if f.alternate() {378			write!(f, "{:#?}", self.0)?;379		} else {380			write!(f, "{:?}", self.0)?;381		}382		if let Some(loc) = &self.1 {383			write!(f, " from {:?}", loc)?;384		}385		Ok(())386	}387}388389/// Creates LocExpr from Expr and ExprLocation components390#[macro_export]391macro_rules! loc_expr {392	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {393		LocExpr(394			std::rc::Rc::new($expr),395			if $need_loc {396				Some(ExprLocation($name, $start, $end))397			} else {398				None399			},400		)401	};402}403404/// Creates LocExpr without location info405#[macro_export]406macro_rules! loc_expr_todo {407	($expr:expr) => {408		LocExpr(Rc::new($expr), None)409	};410}