git.delta.rocks / jrsonnet / refs/commits / 3b72cd84a927

difftreelog

source

crates/jrsonnet-parser/src/expr.rs8.8 KiBsourcehistory
1#[cfg(feature = "deserialize")]2use serde::Deserialize;3#[cfg(feature = "serialize")]4use serde::Serialize;5use std::{6	fmt::{Debug, Display},7	ops::Deref,8	path::PathBuf,9	rc::Rc,10};11#[cfg(feature = "dump")]12use structdump_derive::Codegen;1314#[cfg_attr(feature = "dump", derive(Codegen))]15#[cfg_attr(feature = "serialize", derive(Serialize))]16#[cfg_attr(feature = "deserialize", derive(Deserialize))]17#[derive(Debug, PartialEq)]18pub enum FieldName {19	/// {fixed: 2}20	Fixed(Rc<str>),21	/// {["dyn"+"amic"]: 3}22	Dyn(LocExpr),23}2425#[cfg_attr(feature = "dump", derive(Codegen))]26#[cfg_attr(feature = "serialize", derive(Serialize))]27#[cfg_attr(feature = "deserialize", derive(Deserialize))]28#[derive(Debug, Clone, Copy, PartialEq)]29pub enum Visibility {30	/// :31	Normal,32	/// ::33	Hidden,34	/// :::35	Unhide,36}3738#[cfg_attr(feature = "dump", derive(Codegen))]39#[cfg_attr(feature = "serialize", derive(Serialize))]40#[cfg_attr(feature = "deserialize", derive(Deserialize))]41#[derive(Debug, PartialEq)]42pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4344#[cfg_attr(feature = "dump", derive(Codegen))]45#[cfg_attr(feature = "serialize", derive(Serialize))]46#[cfg_attr(feature = "deserialize", derive(Deserialize))]47#[derive(Debug, PartialEq)]48pub struct FieldMember {49	pub name: FieldName,50	pub plus: bool,51	pub params: Option<ParamsDesc>,52	pub visibility: Visibility,53	pub value: LocExpr,54}5556#[cfg_attr(feature = "dump", derive(Codegen))]57#[cfg_attr(feature = "serialize", derive(Serialize))]58#[cfg_attr(feature = "deserialize", derive(Deserialize))]59#[derive(Debug, PartialEq)]60pub enum Member {61	Field(FieldMember),62	BindStmt(BindSpec),63	AssertStmt(AssertStmt),64}6566#[cfg_attr(feature = "dump", derive(Codegen))]67#[cfg_attr(feature = "serialize", derive(Serialize))]68#[cfg_attr(feature = "deserialize", derive(Deserialize))]69#[derive(Debug, Clone, Copy, PartialEq)]70pub enum UnaryOpType {71	Plus,72	Minus,73	BitNot,74	Not,75}76impl Display for UnaryOpType {77	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {78		use UnaryOpType::*;79		write!(80			f,81			"{}",82			match self {83				Plus => "+",84				Minus => "-",85				BitNot => "~",86				Not => "!",87			}88		)89	}90}9192#[cfg_attr(feature = "dump", derive(Codegen))]93#[cfg_attr(feature = "serialize", derive(Serialize))]94#[cfg_attr(feature = "deserialize", derive(Deserialize))]95#[derive(Debug, Clone, Copy, PartialEq)]96pub enum BinaryOpType {97	Mul,98	Div,99100	Add,101	Sub,102103	Lhs,104	Rhs,105106	Lt,107	Gt,108	Lte,109	Gte,110111	BitAnd,112	BitOr,113	BitXor,114115	And,116	Or,117}118impl Display for BinaryOpType {119	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {120		use BinaryOpType::*;121		write!(122			f,123			"{}",124			match self {125				Mul => "*",126				Div => "/",127				Add => "+",128				Sub => "-",129				Lhs => "<<",130				Rhs => ">>",131				Lt => "<",132				Gt => ">",133				Lte => "<=",134				Gte => ">=",135				BitAnd => "&",136				BitOr => "|",137				BitXor => "^",138				And => "&&",139				Or => "||",140			}141		)142	}143}144145/// name, default value146#[cfg_attr(feature = "dump", derive(Codegen))]147#[cfg_attr(feature = "serialize", derive(Serialize))]148#[cfg_attr(feature = "deserialize", derive(Deserialize))]149#[derive(Debug, PartialEq)]150pub struct Param(pub Rc<str>, pub Option<LocExpr>);151152/// Defined function parameters153#[cfg_attr(feature = "dump", derive(Codegen))]154#[cfg_attr(feature = "serialize", derive(Serialize))]155#[cfg_attr(feature = "deserialize", derive(Deserialize))]156#[derive(Debug, Clone, PartialEq)]157pub struct ParamsDesc(pub Rc<Vec<Param>>);158impl Deref for ParamsDesc {159	type Target = Vec<Param>;160	fn deref(&self) -> &Self::Target {161		&self.0162	}163}164165#[cfg_attr(feature = "dump", derive(Codegen))]166#[cfg_attr(feature = "serialize", derive(Serialize))]167#[cfg_attr(feature = "deserialize", derive(Deserialize))]168#[derive(Debug, PartialEq)]169pub struct Arg(pub Option<String>, pub LocExpr);170171#[cfg_attr(feature = "dump", derive(Codegen))]172#[cfg_attr(feature = "serialize", derive(Serialize))]173#[cfg_attr(feature = "deserialize", derive(Deserialize))]174#[derive(Debug, PartialEq)]175pub struct ArgsDesc(pub Vec<Arg>);176impl Deref for ArgsDesc {177	type Target = Vec<Arg>;178	fn deref(&self) -> &Self::Target {179		&self.0180	}181}182183#[cfg_attr(feature = "dump", derive(Codegen))]184#[cfg_attr(feature = "serialize", derive(Serialize))]185#[cfg_attr(feature = "deserialize", derive(Deserialize))]186#[derive(Debug, Clone, PartialEq)]187pub struct BindSpec {188	pub name: Rc<str>,189	pub params: Option<ParamsDesc>,190	pub value: LocExpr,191}192193#[cfg_attr(feature = "dump", derive(Codegen))]194#[cfg_attr(feature = "serialize", derive(Serialize))]195#[cfg_attr(feature = "deserialize", derive(Deserialize))]196#[derive(Debug, PartialEq)]197pub struct IfSpecData(pub LocExpr);198199#[cfg_attr(feature = "dump", derive(Codegen))]200#[cfg_attr(feature = "serialize", derive(Serialize))]201#[cfg_attr(feature = "deserialize", derive(Deserialize))]202#[derive(Debug, PartialEq)]203pub struct ForSpecData(pub Rc<str>, pub LocExpr);204205#[cfg_attr(feature = "dump", derive(Codegen))]206#[cfg_attr(feature = "serialize", derive(Serialize))]207#[cfg_attr(feature = "deserialize", derive(Deserialize))]208#[derive(Debug, PartialEq)]209pub enum CompSpec {210	IfSpec(IfSpecData),211	ForSpec(ForSpecData),212}213214#[cfg_attr(feature = "dump", derive(Codegen))]215#[cfg_attr(feature = "serialize", derive(Serialize))]216#[cfg_attr(feature = "deserialize", derive(Deserialize))]217#[derive(Debug, PartialEq)]218pub struct ObjComp {219	pub pre_locals: Vec<BindSpec>,220	pub key: LocExpr,221	pub value: LocExpr,222	pub post_locals: Vec<BindSpec>,223	pub compspecs: Vec<CompSpec>,224}225226#[cfg_attr(feature = "dump", derive(Codegen))]227#[cfg_attr(feature = "serialize", derive(Serialize))]228#[cfg_attr(feature = "deserialize", derive(Deserialize))]229#[derive(Debug, PartialEq)]230pub enum ObjBody {231	MemberList(Vec<Member>),232	ObjComp(ObjComp),233}234235#[cfg_attr(feature = "dump", derive(Codegen))]236#[cfg_attr(feature = "serialize", derive(Serialize))]237#[cfg_attr(feature = "deserialize", derive(Deserialize))]238#[derive(Debug, PartialEq, Clone, Copy)]239pub enum LiteralType {240	This,241	Super,242	Dollar,243	Null,244	True,245	False,246}247248#[derive(Debug, PartialEq)]249pub struct SliceDesc {250	pub start: Option<LocExpr>,251	pub end: Option<LocExpr>,252	pub step: Option<LocExpr>,253}254255/// Syntax base256#[cfg_attr(feature = "dump", derive(Codegen))]257#[cfg_attr(feature = "serialize", derive(Serialize))]258#[cfg_attr(feature = "deserialize", derive(Deserialize))]259#[derive(Debug, PartialEq)]260pub enum Expr {261	Literal(LiteralType),262263	/// String value: "hello"264	Str(Rc<str>),265	/// Number: 1, 2.0, 2e+20266	Num(f64),267	/// Variable name: test268	Var(Rc<str>),269270	/// Array of expressions: [1, 2, "Hello"]271	Arr(Vec<LocExpr>),272	/// Array comprehension:273	/// ```jsonnet274	///  ingredients: [275	///    { kind: kind, qty: 4 / 3 }276	///    for kind in [277	///      'Honey Syrup',278	///      'Lemon Juice',279	///      'Farmers Gin',280	///    ]281	///  ],282	/// ```283	ArrComp(LocExpr, Vec<CompSpec>),284285	/// Object: {a: 2}286	Obj(ObjBody),287	/// Object extension: var1 {b: 2}288	ObjExtend(LocExpr, ObjBody),289290	/// (obj)291	Parened(LocExpr),292293	/// -2294	UnaryOp(UnaryOpType, LocExpr),295	/// 2 - 2296	BinaryOp(LocExpr, BinaryOpType, LocExpr),297	/// assert 2 == 2 : "Math is broken"298	AssertExpr(AssertStmt, LocExpr),299	/// local a = 2; { b: a }300	LocalExpr(Vec<BindSpec>, LocExpr),301302	/// import "hello"303	Import(PathBuf),304	/// importStr "file.txt"305	ImportStr(PathBuf),306	/// error "I'm broken"307	ErrorStmt(LocExpr),308	/// a(b, c)309	Apply(LocExpr, ArgsDesc, bool),310	/// a[b]311	Index(LocExpr, LocExpr),312	/// function(x) x313	Function(ParamsDesc, LocExpr),314	/// std.primitiveEquals315	Intrinsic(Rc<str>),316	/// if true == false then 1 else 2317	IfElse {318		cond: IfSpecData,319		cond_then: LocExpr,320		cond_else: Option<LocExpr>,321	},322}323324/// file, begin offset, end offset325#[cfg_attr(feature = "dump", derive(Codegen))]326#[cfg_attr(feature = "serialize", derive(Serialize))]327#[cfg_attr(feature = "deserialize", derive(Deserialize))]328#[derive(Clone, PartialEq)]329pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);330impl Debug for ExprLocation {331	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {332		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)333	}334}335336/// Holds AST expression and its location in source file337#[cfg_attr(feature = "dump", derive(Codegen))]338#[cfg_attr(feature = "serialize", derive(Serialize))]339#[cfg_attr(feature = "deserialize", derive(Deserialize))]340#[derive(Clone, PartialEq)]341pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);342impl Debug for LocExpr {343	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {344		write!(f, "{:?} from {:?}", self.0, self.1)345	}346}347348/// Creates LocExpr from Expr and ExprLocation components349#[macro_export]350macro_rules! loc_expr {351	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {352		LocExpr(353			std::rc::Rc::new($expr),354			if $need_loc {355				Some(ExprLocation($name, $start, $end))356			} else {357				None358				},359			)360	};361}362363/// Creates LocExpr without location info364#[macro_export]365macro_rules! loc_expr_todo {366	($expr:expr) => {367		LocExpr(Rc::new($expr), None)368	};369}