git.delta.rocks / jrsonnet / refs/commits / 88a0ba11fe45

difftreelog

source

crates/jrsonnet-parser/src/expr.rs7.8 KiBsourcehistory
1use std::{2	fmt::{Debug, Display},3	ops::Deref,4	path::{Path, PathBuf},5	rc::Rc,6};78use gcmodule::Trace;9use jrsonnet_interner::IStr;10#[cfg(feature = "serde")]11use serde::{Deserialize, Serialize};1213#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]14#[derive(Debug, PartialEq, Trace)]15pub enum FieldName {16	/// {fixed: 2}17	Fixed(IStr),18	/// {["dyn"+"amic"]: 3}19	Dyn(LocExpr),20}2122#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]23#[derive(Debug, Clone, Copy, PartialEq, Trace)]24pub enum Visibility {25	/// :26	Normal,27	/// ::28	Hidden,29	/// :::30	Unhide,31}3233impl Visibility {34	pub fn is_visible(&self) -> bool {35		matches!(self, Self::Normal | Self::Unhide)36	}37}3839#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]40#[derive(Clone, Debug, PartialEq, Trace)]41pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4243#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]44#[derive(Debug, PartialEq, Trace)]45pub struct FieldMember {46	pub name: FieldName,47	pub plus: bool,48	pub params: Option<ParamsDesc>,49	pub visibility: Visibility,50	pub value: LocExpr,51}5253#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]54#[derive(Debug, PartialEq, Trace)]55pub enum Member {56	Field(FieldMember),57	BindStmt(BindSpec),58	AssertStmt(AssertStmt),59}6061#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]62#[derive(Debug, Clone, Copy, PartialEq, Trace)]63pub enum UnaryOpType {64	Plus,65	Minus,66	BitNot,67	Not,68}6970impl Display for UnaryOpType {71	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {72		use UnaryOpType::*;73		write!(74			f,75			"{}",76			match self {77				Plus => "+",78				Minus => "-",79				BitNot => "~",80				Not => "!",81			}82		)83	}84}8586#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]87#[derive(Debug, Clone, Copy, PartialEq, Trace)]88pub enum BinaryOpType {89	Mul,90	Div,9192	/// Implemented as intrinsic, put here for completeness93	Mod,9495	Add,96	Sub,9798	Lhs,99	Rhs,100101	Lt,102	Gt,103	Lte,104	Gte,105106	BitAnd,107	BitOr,108	BitXor,109110	Eq,111	Neq,112113	And,114	Or,115116	// Equialent to std.objectHasEx(a, b, true)117	In,118}119120impl Display for BinaryOpType {121	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {122		use BinaryOpType::*;123		write!(124			f,125			"{}",126			match self {127				Mul => "*",128				Div => "/",129				Mod => "%",130				Add => "+",131				Sub => "-",132				Lhs => "<<",133				Rhs => ">>",134				Lt => "<",135				Gt => ">",136				Lte => "<=",137				Gte => ">=",138				BitAnd => "&",139				BitOr => "|",140				BitXor => "^",141				Eq => "==",142				Neq => "!=",143				And => "&&",144				Or => "||",145				In => "in",146			}147		)148	}149}150151/// name, default value152#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]153#[derive(Debug, PartialEq, Trace)]154pub struct Param(pub IStr, pub Option<LocExpr>);155156/// Defined function parameters157#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]158#[derive(Debug, Clone, PartialEq, Trace)]159pub struct ParamsDesc(pub Rc<Vec<Param>>);160161impl Deref for ParamsDesc {162	type Target = Vec<Param>;163	fn deref(&self) -> &Self::Target {164		&self.0165	}166}167168#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]169#[derive(Debug, PartialEq, Trace)]170pub struct ArgsDesc {171	pub unnamed: Vec<LocExpr>,172	pub named: Vec<(IStr, LocExpr)>,173}174impl ArgsDesc {175	pub fn new(unnamed: Vec<LocExpr>, named: Vec<(IStr, LocExpr)>) -> Self {176		Self { unnamed, named }177	}178}179180#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]181#[derive(Debug, Clone, PartialEq, Trace)]182pub enum DestructRest {183	/// ...rest184	Keep(IStr),185	/// ...186	Drop,187}188189#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]190#[derive(Debug, Clone, PartialEq, Trace)]191pub enum Destruct {192	Full(IStr),193	#[cfg(feature = "exp-destruct")]194	Skip,195	#[cfg(feature = "exp-destruct")]196	Array {197		start: Vec<Destruct>,198		rest: Option<DestructRest>,199		end: Vec<Destruct>,200	},201	#[cfg(feature = "exp-destruct")]202	Object {203		fields: Vec<(IStr, Option<Destruct>)>,204		rest: Option<DestructRest>,205	},206}207208#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]209#[derive(Debug, Clone, PartialEq, Trace)]210pub enum BindSpec {211	Field {212		into: Destruct,213		value: LocExpr,214	},215	Function {216		name: IStr,217		params: ParamsDesc,218		value: LocExpr,219	},220}221222#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]223#[derive(Debug, PartialEq, Trace)]224pub struct IfSpecData(pub LocExpr);225226#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]227#[derive(Debug, PartialEq, Trace)]228pub struct ForSpecData(pub IStr, pub LocExpr);229230#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]231#[derive(Debug, PartialEq, Trace)]232pub enum CompSpec {233	IfSpec(IfSpecData),234	ForSpec(ForSpecData),235}236237#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]238#[derive(Debug, PartialEq, Trace)]239pub struct ObjComp {240	pub pre_locals: Vec<BindSpec>,241	pub key: LocExpr,242	pub plus: bool,243	pub value: LocExpr,244	pub post_locals: Vec<BindSpec>,245	pub compspecs: Vec<CompSpec>,246}247248#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]249#[derive(Debug, PartialEq, Trace)]250pub enum ObjBody {251	MemberList(Vec<Member>),252	ObjComp(ObjComp),253}254255#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]256#[derive(Debug, PartialEq, Clone, Copy, Trace)]257pub enum LiteralType {258	This,259	Super,260	Dollar,261	Null,262	True,263	False,264}265266#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]267#[derive(Debug, PartialEq, Trace)]268pub struct SliceDesc {269	pub start: Option<LocExpr>,270	pub end: Option<LocExpr>,271	pub step: Option<LocExpr>,272}273274/// Syntax base275#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]276#[derive(Debug, PartialEq, Trace)]277pub enum Expr {278	Literal(LiteralType),279280	/// String value: "hello"281	Str(IStr),282	/// Number: 1, 2.0, 2e+20283	Num(f64),284	/// Variable name: test285	Var(IStr),286287	/// Array of expressions: [1, 2, "Hello"]288	Arr(Vec<LocExpr>),289	/// Array comprehension:290	/// ```jsonnet291	///  ingredients: [292	///    { kind: kind, qty: 4 / 3 }293	///    for kind in [294	///      'Honey Syrup',295	///      'Lemon Juice',296	///      'Farmers Gin',297	///    ]298	///  ],299	/// ```300	ArrComp(LocExpr, Vec<CompSpec>),301302	/// Object: {a: 2}303	Obj(ObjBody),304	/// Object extension: var1 {b: 2}305	ObjExtend(LocExpr, ObjBody),306307	/// (obj)308	Parened(LocExpr),309310	/// -2311	UnaryOp(UnaryOpType, LocExpr),312	/// 2 - 2313	BinaryOp(LocExpr, BinaryOpType, LocExpr),314	/// assert 2 == 2 : "Math is broken"315	AssertExpr(AssertStmt, LocExpr),316	/// local a = 2; { b: a }317	LocalExpr(Vec<BindSpec>, LocExpr),318319	/// import "hello"320	Import(PathBuf),321	/// importStr "file.txt"322	ImportStr(PathBuf),323	/// importBin "file.txt"324	ImportBin(PathBuf),325	/// error "I'm broken"326	ErrorStmt(LocExpr),327	/// a(b, c)328	Apply(LocExpr, ArgsDesc, bool),329	/// a[b]330	Index(LocExpr, LocExpr),331	/// function(x) x332	Function(ParamsDesc, LocExpr),333	/// std.thisFile334	IntrinsicThisFile,335	/// std.id,336	IntrinsicId,337	/// std.primitiveEquals338	Intrinsic(IStr),339	/// if true == false then 1 else 2340	IfElse {341		cond: IfSpecData,342		cond_then: LocExpr,343		cond_else: Option<LocExpr>,344	},345	Slice(LocExpr, SliceDesc),346}347348/// file, begin offset, end offset349#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]350#[derive(Clone, PartialEq, Trace)]351#[skip_trace]352pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);353impl ExprLocation {354	pub fn belongs_to(&self, other: &ExprLocation) -> bool {355		other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2356	}357}358359impl Debug for ExprLocation {360	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {361		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)362	}363}364365/// Holds AST expression and its location in source file366#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]367#[derive(Clone, PartialEq, Trace)]368pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);369370impl Debug for LocExpr {371	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {372		if f.alternate() {373			write!(f, "{:#?}", self.0)?;374		} else {375			write!(f, "{:?}", self.0)?;376		}377		write!(f, " from {:?}", self.1)?;378		Ok(())379	}380}