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

difftreelog

source

crates/jsonnet-parser/src/expr.rs5.4 KiBsourcehistory
1use serde::{Deserialize, Serialize};2use std::{fmt::Debug, rc::Rc};34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]5pub enum FieldName {6	/// {fixed: 2}7	Fixed(String),8	/// {["dyn"+"amic"]: 3}9	Dyn(LocExpr),10}1112#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]13pub enum Visibility {14	/// :15	Normal,16	/// ::17	Hidden,18	/// :::19	Unhide,20}2122#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]23pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);2425#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]26pub struct FieldMember {27	pub name: FieldName,28	pub plus: bool,29	pub params: Option<ParamsDesc>,30	pub visibility: Visibility,31	pub value: LocExpr,32}3334#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]35pub enum Member {36	Field(FieldMember),37	BindStmt(BindSpec),38	AssertStmt(AssertStmt),39}4041#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]42pub enum UnaryOpType {43	Plus,44	Minus,45	BitNot,46	Not,47}4849#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]50pub enum BinaryOpType {51	Mul,52	Div,53	Mod,5455	Add,56	Sub,5758	Lhs,59	Rhs,6061	Lt,62	Gt,63	Lte,64	Gte,6566	In,6768	Eq,69	Ne,7071	BitAnd,72	BitOr,73	BitXor,7475	And,76	Or,77}7879/// name, default value80#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]81pub struct Param(pub String, pub Option<LocExpr>);82/// Defined function parameters83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]84pub struct ParamsDesc(pub Vec<Param>);85impl ParamsDesc {86	pub fn with_defaults(&self) -> Vec<Param> {87		self.0.iter().filter(|e| e.1.is_some()).cloned().collect()88	}89}9091#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]92pub struct Arg(pub Option<String>, pub LocExpr);93#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]94pub struct ArgsDesc(pub Vec<Arg>);9596#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]97pub struct BindSpec {98	pub name: String,99	pub params: Option<ParamsDesc>,100	pub value: LocExpr,101}102103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]104pub struct IfSpecData(pub LocExpr);105#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]106pub struct ForSpecData(pub String, pub LocExpr);107108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]109pub enum CompSpec {110	IfSpec(IfSpecData),111	ForSpec(ForSpecData),112}113114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]115pub enum ObjBody {116	MemberList(Vec<Member>),117	ObjComp {118		pre_locals: Vec<BindSpec>,119		key: LocExpr,120		value: LocExpr,121		post_locals: Vec<BindSpec>,122		first: ForSpecData,123		rest: Vec<CompSpec>,124	},125}126127#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]128pub enum LiteralType {129	This,130	Super,131	Dollar,132	Null,133	True,134	False,135}136137#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]138pub struct SliceDesc {139	pub start: Option<LocExpr>,140	pub end: Option<LocExpr>,141	pub step: Option<LocExpr>,142}143144/// Syntax base145#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]146pub enum Expr {147	Literal(LiteralType),148149	/// String value: "hello"150	Str(String),151	/// Number: 1, 2.0, 2e+20152	Num(f64),153	/// Variable name: test154	Var(String),155156	/// Array of expressions: [1, 2, "Hello"]157	Arr(Vec<LocExpr>),158	/// Array comprehension:159	/// ```jsonnet160	///  ingredients: [161	///    { kind: kind, qty: 4 / 3 }162	///    for kind in [163	///      'Honey Syrup',164	///      'Lemon Juice',165	///      'Farmers Gin',166	///    ]167	///  ],168	/// ```169	ArrComp(LocExpr, Vec<CompSpec>),170171	/// Object: {a: 2}172	Obj(ObjBody),173	/// Object extension: var1 {b: 2}174	ObjExtend(LocExpr, ObjBody),175176	/// (obj)177	Parened(LocExpr),178179	/// Params in function definition180	/// hello, world, test = 2181	Params(ParamsDesc),182	/// Args in function call183	/// 2 + 2, 3, named = 6184	Args(ArgsDesc),185186	/// -2187	UnaryOp(UnaryOpType, LocExpr),188	/// 2 - 2189	BinaryOp(LocExpr, BinaryOpType, LocExpr),190	/// assert 2 == 2 : "Math is broken"191	AssertExpr(AssertStmt, LocExpr),192	/// local a = 2; { b: a }193	LocalExpr(Vec<BindSpec>, LocExpr),194195	/// a = 3196	Bind(BindSpec),197	/// import "hello"198	Import(String),199	/// importStr "file.txt"200	ImportStr(String),201	/// error "I'm broken"202	Error(LocExpr),203	/// a(b, c)204	Apply(LocExpr, ArgsDesc),205	///206	Select(LocExpr, String),207	/// a[b]208	Index(LocExpr, LocExpr),209	/// a[1::2]210	Slice(LocExpr, SliceDesc),211	/// function(x) x212	Function(ParamsDesc, LocExpr),213	/// if true == false then 1 else 2214	IfElse {215		cond: IfSpecData,216		cond_then: LocExpr,217		cond_else: Option<LocExpr>,218	},219	/// if 2 = 3220	IfSpec(IfSpecData),221	/// for elem in array222	ForSpec(ForSpecData),223}224225/// file, begin offset, end offset226#[derive(Clone, PartialEq, Serialize, Deserialize)]227pub struct ExprLocation(pub String, pub usize, pub usize);228impl Debug for ExprLocation {229	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {230		write!(f, "{}:{:?}-{:?}", self.0, self.1, self.2)231	}232}233234/// Holds AST expression and its location in source file+235#[derive(Clone, PartialEq, Serialize, Deserialize)]236pub struct LocExpr(pub Rc<Expr>, pub Option<Rc<ExprLocation>>);237impl Debug for LocExpr {238	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {239		write!(f, "{:?} from {:?}", self.0, self.1)240	}241}242243/// Creates LocExpr from Expr and ExprLocation components244#[macro_export]245macro_rules! loc_expr {246	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {247		LocExpr(248			std::rc::Rc::new($expr),249			if $need_loc {250				Some(std::rc::Rc::new(ExprLocation(251					$name.to_owned(),252					$start,253					$end,254				)))255			} else {256				None257				},258			)259	};260}261262/// Creates LocExpr without location info263#[macro_export]264macro_rules! loc_expr_todo {265	($expr:expr) => {266		LocExpr(Rc::new($expr), None)267	};268}