git.delta.rocks / jrsonnet / refs/commits / b55d14a56b86

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	/// Implemented as intrinsic, put here for completeness101	Mod,102103	Add,104	Sub,105106	Lhs,107	Rhs,108109	Lt,110	Gt,111	Lte,112	Gte,113114	BitAnd,115	BitOr,116	BitXor,117118	And,119	Or,120}121impl Display for BinaryOpType {122	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {123		use BinaryOpType::*;124		write!(125			f,126			"{}",127			match self {128				Mul => "*",129				Div => "/",130				Mod => "%",131				Add => "+",132				Sub => "-",133				Lhs => "<<",134				Rhs => ">>",135				Lt => "<",136				Gt => ">",137				Lte => "<=",138				Gte => ">=",139				BitAnd => "&",140				BitOr => "|",141				BitXor => "^",142				And => "&&",143				Or => "||",144			}145		)146	}147}148149/// name, default value150#[cfg_attr(feature = "dump", derive(Codegen))]151#[cfg_attr(feature = "serialize", derive(Serialize))]152#[cfg_attr(feature = "deserialize", derive(Deserialize))]153#[derive(Debug, PartialEq)]154pub struct Param(pub Rc<str>, pub Option<LocExpr>);155156/// Defined function parameters157#[cfg_attr(feature = "dump", derive(Codegen))]158#[cfg_attr(feature = "serialize", derive(Serialize))]159#[cfg_attr(feature = "deserialize", derive(Deserialize))]160#[derive(Debug, Clone, PartialEq)]161pub struct ParamsDesc(pub Rc<Vec<Param>>);162impl Deref for ParamsDesc {163	type Target = Vec<Param>;164	fn deref(&self) -> &Self::Target {165		&self.0166	}167}168169#[cfg_attr(feature = "dump", derive(Codegen))]170#[cfg_attr(feature = "serialize", derive(Serialize))]171#[cfg_attr(feature = "deserialize", derive(Deserialize))]172#[derive(Debug, PartialEq)]173pub struct Arg(pub Option<String>, pub LocExpr);174175#[cfg_attr(feature = "dump", derive(Codegen))]176#[cfg_attr(feature = "serialize", derive(Serialize))]177#[cfg_attr(feature = "deserialize", derive(Deserialize))]178#[derive(Debug, PartialEq)]179pub struct ArgsDesc(pub Vec<Arg>);180impl Deref for ArgsDesc {181	type Target = Vec<Arg>;182	fn deref(&self) -> &Self::Target {183		&self.0184	}185}186187#[cfg_attr(feature = "dump", derive(Codegen))]188#[cfg_attr(feature = "serialize", derive(Serialize))]189#[cfg_attr(feature = "deserialize", derive(Deserialize))]190#[derive(Debug, Clone, PartialEq)]191pub struct BindSpec {192	pub name: Rc<str>,193	pub params: Option<ParamsDesc>,194	pub value: LocExpr,195}196197#[cfg_attr(feature = "dump", derive(Codegen))]198#[cfg_attr(feature = "serialize", derive(Serialize))]199#[cfg_attr(feature = "deserialize", derive(Deserialize))]200#[derive(Debug, PartialEq)]201pub struct IfSpecData(pub LocExpr);202203#[cfg_attr(feature = "dump", derive(Codegen))]204#[cfg_attr(feature = "serialize", derive(Serialize))]205#[cfg_attr(feature = "deserialize", derive(Deserialize))]206#[derive(Debug, PartialEq)]207pub struct ForSpecData(pub Rc<str>, pub LocExpr);208209#[cfg_attr(feature = "dump", derive(Codegen))]210#[cfg_attr(feature = "serialize", derive(Serialize))]211#[cfg_attr(feature = "deserialize", derive(Deserialize))]212#[derive(Debug, PartialEq)]213pub enum CompSpec {214	IfSpec(IfSpecData),215	ForSpec(ForSpecData),216}217218#[cfg_attr(feature = "dump", derive(Codegen))]219#[cfg_attr(feature = "serialize", derive(Serialize))]220#[cfg_attr(feature = "deserialize", derive(Deserialize))]221#[derive(Debug, PartialEq)]222pub struct ObjComp {223	pub pre_locals: Vec<BindSpec>,224	pub key: LocExpr,225	pub value: LocExpr,226	pub post_locals: Vec<BindSpec>,227	pub compspecs: Vec<CompSpec>,228}229230#[cfg_attr(feature = "dump", derive(Codegen))]231#[cfg_attr(feature = "serialize", derive(Serialize))]232#[cfg_attr(feature = "deserialize", derive(Deserialize))]233#[derive(Debug, PartialEq)]234pub enum ObjBody {235	MemberList(Vec<Member>),236	ObjComp(ObjComp),237}238239#[cfg_attr(feature = "dump", derive(Codegen))]240#[cfg_attr(feature = "serialize", derive(Serialize))]241#[cfg_attr(feature = "deserialize", derive(Deserialize))]242#[derive(Debug, PartialEq, Clone, Copy)]243pub enum LiteralType {244	This,245	Super,246	Dollar,247	Null,248	True,249	False,250}251252#[derive(Debug, PartialEq)]253pub struct SliceDesc {254	pub start: Option<LocExpr>,255	pub end: Option<LocExpr>,256	pub step: Option<LocExpr>,257}258259/// Syntax base260#[cfg_attr(feature = "dump", derive(Codegen))]261#[cfg_attr(feature = "serialize", derive(Serialize))]262#[cfg_attr(feature = "deserialize", derive(Deserialize))]263#[derive(Debug, PartialEq)]264pub enum Expr {265	Literal(LiteralType),266267	/// String value: "hello"268	Str(Rc<str>),269	/// Number: 1, 2.0, 2e+20270	Num(f64),271	/// Variable name: test272	Var(Rc<str>),273274	/// Array of expressions: [1, 2, "Hello"]275	Arr(Vec<LocExpr>),276	/// Array comprehension:277	/// ```jsonnet278	///  ingredients: [279	///    { kind: kind, qty: 4 / 3 }280	///    for kind in [281	///      'Honey Syrup',282	///      'Lemon Juice',283	///      'Farmers Gin',284	///    ]285	///  ],286	/// ```287	ArrComp(LocExpr, Vec<CompSpec>),288289	/// Object: {a: 2}290	Obj(ObjBody),291	/// Object extension: var1 {b: 2}292	ObjExtend(LocExpr, ObjBody),293294	/// (obj)295	Parened(LocExpr),296297	/// -2298	UnaryOp(UnaryOpType, LocExpr),299	/// 2 - 2300	BinaryOp(LocExpr, BinaryOpType, LocExpr),301	/// assert 2 == 2 : "Math is broken"302	AssertExpr(AssertStmt, LocExpr),303	/// local a = 2; { b: a }304	LocalExpr(Vec<BindSpec>, LocExpr),305306	/// import "hello"307	Import(PathBuf),308	/// importStr "file.txt"309	ImportStr(PathBuf),310	/// error "I'm broken"311	ErrorStmt(LocExpr),312	/// a(b, c)313	Apply(LocExpr, ArgsDesc, bool),314	/// a[b]315	Index(LocExpr, LocExpr),316	/// function(x) x317	Function(ParamsDesc, LocExpr),318	/// std.primitiveEquals319	Intrinsic(Rc<str>),320	/// if true == false then 1 else 2321	IfElse {322		cond: IfSpecData,323		cond_then: LocExpr,324		cond_else: Option<LocExpr>,325	},326}327328/// file, begin offset, end offset329#[cfg_attr(feature = "dump", derive(Codegen))]330#[cfg_attr(feature = "serialize", derive(Serialize))]331#[cfg_attr(feature = "deserialize", derive(Deserialize))]332#[derive(Clone, PartialEq)]333pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);334impl Debug for ExprLocation {335	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {336		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)337	}338}339340/// Holds AST expression and its location in source file341#[cfg_attr(feature = "dump", derive(Codegen))]342#[cfg_attr(feature = "serialize", derive(Serialize))]343#[cfg_attr(feature = "deserialize", derive(Deserialize))]344#[derive(Clone, PartialEq)]345pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);346impl Debug for LocExpr {347	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {348		write!(f, "{:?} from {:?}", self.0, self.1)349	}350}351352/// Creates LocExpr from Expr and ExprLocation components353#[macro_export]354macro_rules! loc_expr {355	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {356		LocExpr(357			std::rc::Rc::new($expr),358			if $need_loc {359				Some(ExprLocation($name, $start, $end))360			} else {361				None362				},363			)364	};365}366367/// Creates LocExpr without location info368#[macro_export]369macro_rules! loc_expr_todo {370	($expr:expr) => {371		LocExpr(Rc::new($expr), None)372	};373}