git.delta.rocks / jrsonnet / refs/commits / 476a9c192b8a

difftreelog

source

crates/jrsonnet-parser/src/expr.rs8.0 KiBsourcehistory
1use std::{fmt::Debug, ops::Deref, path::PathBuf, rc::Rc};2#[cfg(feature = "dump")]3use structdump_derive::Codegen;4#[cfg(feature = "serialize")]5use serde::Serialize;6#[cfg(feature = "deserialize")]7use serde::Deserialize;89#[cfg_attr(feature = "dump", derive(Codegen))]10#[cfg_attr(feature = "serialize", derive(Serialize))]11#[cfg_attr(feature = "deserialize", derive(Deserialize))]12#[derive(Debug, PartialEq)]13pub enum FieldName {14	/// {fixed: 2}15	Fixed(Rc<str>),16	/// {["dyn"+"amic"]: 3}17	Dyn(LocExpr),18}1920#[cfg_attr(feature = "dump", derive(Codegen))]21#[cfg_attr(feature = "serialize", derive(Serialize))]22#[cfg_attr(feature = "deserialize", derive(Deserialize))]23#[derive(Debug, Clone, Copy, PartialEq)]24pub enum Visibility {25	/// :26	Normal,27	/// ::28	Hidden,29	/// :::30	Unhide,31}3233#[cfg_attr(feature = "dump", derive(Codegen))]34#[cfg_attr(feature = "serialize", derive(Serialize))]35#[cfg_attr(feature = "deserialize", derive(Deserialize))]36#[derive(Debug, PartialEq)]37pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);3839#[cfg_attr(feature = "dump", derive(Codegen))]40#[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 = "dump", derive(Codegen))]52#[cfg_attr(feature = "serialize", derive(Serialize))]53#[cfg_attr(feature = "deserialize", derive(Deserialize))]54#[derive(Debug, PartialEq)]55pub enum Member {56	Field(FieldMember),57	BindStmt(BindSpec),58	AssertStmt(AssertStmt),59}6061#[cfg_attr(feature = "dump", derive(Codegen))]62#[cfg_attr(feature = "serialize", derive(Serialize))]63#[cfg_attr(feature = "deserialize", derive(Deserialize))]64#[derive(Debug, Clone, Copy, PartialEq)]65pub enum UnaryOpType {66	Plus,67	Minus,68	BitNot,69	Not,70}7172#[cfg_attr(feature = "dump", derive(Codegen))]73#[cfg_attr(feature = "serialize", derive(Serialize))]74#[cfg_attr(feature = "deserialize", derive(Deserialize))]75#[derive(Debug, Clone, Copy, PartialEq)]76pub enum BinaryOpType {77	Mul,78	Div,7980	Add,81	Sub,8283	Lhs,84	Rhs,8586	Lt,87	Gt,88	Lte,89	Gte,9091	BitAnd,92	BitOr,93	BitXor,9495	And,96	Or,97}9899/// name, default value100#[cfg_attr(feature = "dump", derive(Codegen))]101#[cfg_attr(feature = "serialize", derive(Serialize))]102#[cfg_attr(feature = "deserialize", derive(Deserialize))]103#[derive(Debug, PartialEq)]104pub struct Param(pub Rc<str>, pub Option<LocExpr>);105106/// Defined function parameters107#[cfg_attr(feature = "dump", derive(Codegen))]108#[cfg_attr(feature = "serialize", derive(Serialize))]109#[cfg_attr(feature = "deserialize", derive(Deserialize))]110#[derive(Debug, Clone, PartialEq)]111pub struct ParamsDesc(pub Rc<Vec<Param>>);112impl Deref for ParamsDesc {113	type Target = Vec<Param>;114	fn deref(&self) -> &Self::Target {115		&self.0116	}117}118119#[cfg_attr(feature = "dump", derive(Codegen))]120#[cfg_attr(feature = "serialize", derive(Serialize))]121#[cfg_attr(feature = "deserialize", derive(Deserialize))]122#[derive(Debug, PartialEq)]123pub struct Arg(pub Option<String>, pub LocExpr);124125#[cfg_attr(feature = "dump", derive(Codegen))]126#[cfg_attr(feature = "serialize", derive(Serialize))]127#[cfg_attr(feature = "deserialize", derive(Deserialize))]128#[derive(Debug, PartialEq)]129pub struct ArgsDesc(pub Vec<Arg>);130impl Deref for ArgsDesc {131	type Target = Vec<Arg>;132	fn deref(&self) -> &Self::Target {133		&self.0134	}135}136137#[cfg_attr(feature = "dump", derive(Codegen))]138#[cfg_attr(feature = "serialize", derive(Serialize))]139#[cfg_attr(feature = "deserialize", derive(Deserialize))]140#[derive(Debug, Clone, PartialEq)]141pub struct BindSpec {142	pub name: Rc<str>,143	pub params: Option<ParamsDesc>,144	pub value: LocExpr,145}146147#[cfg_attr(feature = "dump", derive(Codegen))]148#[cfg_attr(feature = "serialize", derive(Serialize))]149#[cfg_attr(feature = "deserialize", derive(Deserialize))]150#[derive(Debug, PartialEq)]151pub struct IfSpecData(pub LocExpr);152153#[cfg_attr(feature = "dump", derive(Codegen))]154#[cfg_attr(feature = "serialize", derive(Serialize))]155#[cfg_attr(feature = "deserialize", derive(Deserialize))]156#[derive(Debug, PartialEq)]157pub struct ForSpecData(pub Rc<str>, pub LocExpr);158159#[cfg_attr(feature = "dump", derive(Codegen))]160#[cfg_attr(feature = "serialize", derive(Serialize))]161#[cfg_attr(feature = "deserialize", derive(Deserialize))]162#[derive(Debug, PartialEq)]163pub enum CompSpec {164	IfSpec(IfSpecData),165	ForSpec(ForSpecData),166}167168#[cfg_attr(feature = "dump", derive(Codegen))]169#[cfg_attr(feature = "serialize", derive(Serialize))]170#[cfg_attr(feature = "deserialize", derive(Deserialize))]171#[derive(Debug, PartialEq)]172pub struct ObjComp {173	pub pre_locals: Vec<BindSpec>,174	pub key: LocExpr,175	pub value: LocExpr,176	pub post_locals: Vec<BindSpec>,177	pub compspecs: Vec<CompSpec>,178}179180#[cfg_attr(feature = "dump", derive(Codegen))]181#[cfg_attr(feature = "serialize", derive(Serialize))]182#[cfg_attr(feature = "deserialize", derive(Deserialize))]183#[derive(Debug, PartialEq)]184pub enum ObjBody {185	MemberList(Vec<Member>),186	ObjComp(ObjComp),187}188189#[cfg_attr(feature = "dump", derive(Codegen))]190#[cfg_attr(feature = "serialize", derive(Serialize))]191#[cfg_attr(feature = "deserialize", derive(Deserialize))]192#[derive(Debug, PartialEq, Clone, Copy)]193pub enum LiteralType {194	This,195	Super,196	Dollar,197	Null,198	True,199	False,200}201202#[derive(Debug, PartialEq)]203pub struct SliceDesc {204	pub start: Option<LocExpr>,205	pub end: Option<LocExpr>,206	pub step: Option<LocExpr>,207}208209/// Syntax base210#[cfg_attr(feature = "dump", derive(Codegen))]211#[cfg_attr(feature = "serialize", derive(Serialize))]212#[cfg_attr(feature = "deserialize", derive(Deserialize))]213#[derive(Debug, PartialEq)]214pub enum Expr {215	Literal(LiteralType),216217	/// String value: "hello"218	Str(Rc<str>),219	/// Number: 1, 2.0, 2e+20220	Num(f64),221	/// Variable name: test222	Var(Rc<str>),223224	/// Array of expressions: [1, 2, "Hello"]225	Arr(Vec<LocExpr>),226	/// Array comprehension:227	/// ```jsonnet228	///  ingredients: [229	///    { kind: kind, qty: 4 / 3 }230	///    for kind in [231	///      'Honey Syrup',232	///      'Lemon Juice',233	///      'Farmers Gin',234	///    ]235	///  ],236	/// ```237	ArrComp(LocExpr, Vec<CompSpec>),238239	/// Object: {a: 2}240	Obj(ObjBody),241	/// Object extension: var1 {b: 2}242	ObjExtend(LocExpr, ObjBody),243244	/// (obj)245	Parened(LocExpr),246247	/// -2248	UnaryOp(UnaryOpType, LocExpr),249	/// 2 - 2250	BinaryOp(LocExpr, BinaryOpType, LocExpr),251	/// assert 2 == 2 : "Math is broken"252	AssertExpr(AssertStmt, LocExpr),253	/// local a = 2; { b: a }254	LocalExpr(Vec<BindSpec>, LocExpr),255256	/// import "hello"257	Import(PathBuf),258	/// importStr "file.txt"259	ImportStr(PathBuf),260	/// error "I'm broken"261	Error(LocExpr),262	/// a(b, c)263	Apply(LocExpr, ArgsDesc, bool),264	/// a[b]265	Index(LocExpr, LocExpr),266	/// function(x) x267	Function(ParamsDesc, LocExpr),268	/// if true == false then 1 else 2269	IfElse {270		cond: IfSpecData,271		cond_then: LocExpr,272		cond_else: Option<LocExpr>,273	},274}275276/// file, begin offset, end offset277#[cfg_attr(feature = "dump", derive(Codegen))]278#[cfg_attr(feature = "serialize", derive(Serialize))]279#[cfg_attr(feature = "deserialize", derive(Deserialize))]280#[derive(Clone, PartialEq)]281pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);282impl Debug for ExprLocation {283	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {284		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)285	}286}287288/// Holds AST expression and its location in source file289#[cfg_attr(feature = "dump", derive(Codegen))]290#[cfg_attr(feature = "serialize", derive(Serialize))]291#[cfg_attr(feature = "deserialize", derive(Deserialize))]292#[derive(Clone, PartialEq)]293pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);294impl Debug for LocExpr {295	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {296		write!(f, "{:?} from {:?}", self.0, self.1)297	}298}299300/// Creates LocExpr from Expr and ExprLocation components301#[macro_export]302macro_rules! loc_expr {303	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {304		LocExpr(305			std::rc::Rc::new($expr),306			if $need_loc {307				Some(ExprLocation($name, $start, $end))308			} else {309				None310				},311			)312	};313}314315/// Creates LocExpr without location info316#[macro_export]317macro_rules! loc_expr_todo {318	($expr:expr) => {319		LocExpr(Rc::new($expr), None)320	};321}