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

difftreelog

source

crates/jsonnet-parser/src/expr.rs5.2 KiBsourcehistory
1use serde::{Deserialize, Serialize};2use std::{fmt::Debug, ops::Deref, path::PathBuf, 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,5354	Add,55	Sub,5657	Lhs,58	Rhs,5960	Lt,61	Gt,62	Lte,63	Gte,6465	BitAnd,66	BitOr,67	BitXor,6869	And,70	Or,71}7273/// name, default value74#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]75pub struct Param(pub String, pub Option<LocExpr>);76/// Defined function parameters77#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]78pub struct ParamsDesc(pub Vec<Param>);79impl Deref for ParamsDesc {80	type Target = Vec<Param>;81	fn deref(&self) -> &Self::Target {82		&self.083	}84}8586#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]87pub struct Arg(pub Option<String>, pub LocExpr);88#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]89pub struct ArgsDesc(pub Vec<Arg>);90impl Deref for ArgsDesc {91	type Target = Vec<Arg>;92	fn deref(&self) -> &Self::Target {93		&self.094	}95}9697#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]98pub struct BindSpec {99	pub name: String,100	pub params: Option<ParamsDesc>,101	pub value: LocExpr,102}103104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]105pub struct IfSpecData(pub LocExpr);106#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]107pub struct ForSpecData(pub String, pub LocExpr);108109#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]110pub enum CompSpec {111	IfSpec(IfSpecData),112	ForSpec(ForSpecData),113}114115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]116pub enum ObjBody {117	MemberList(Vec<Member>),118	ObjComp {119		pre_locals: Vec<BindSpec>,120		key: LocExpr,121		value: LocExpr,122		post_locals: Vec<BindSpec>,123		compspecs: 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	/// -2180	UnaryOp(UnaryOpType, LocExpr),181	/// 2 - 2182	BinaryOp(LocExpr, BinaryOpType, LocExpr),183	/// assert 2 == 2 : "Math is broken"184	AssertExpr(AssertStmt, LocExpr),185	/// local a = 2; { b: a }186	LocalExpr(Vec<BindSpec>, LocExpr),187188	/// import "hello"189	Import(PathBuf),190	/// importStr "file.txt"191	ImportStr(PathBuf),192	/// error "I'm broken"193	Error(LocExpr),194	/// a(b, c)195	Apply(LocExpr, ArgsDesc, bool),196	/// a[b]197	Index(LocExpr, LocExpr),198	/// function(x) x199	Function(ParamsDesc, LocExpr),200	/// if true == false then 1 else 2201	IfElse {202		cond: IfSpecData,203		cond_then: LocExpr,204		cond_else: Option<LocExpr>,205	},206}207208/// file, begin offset, end offset209#[derive(Clone, PartialEq, Serialize, Deserialize)]210pub struct ExprLocation(pub PathBuf, pub usize, pub usize);211impl Debug for ExprLocation {212	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {213		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)214	}215}216217/// Holds AST expression and its location in source file+218#[derive(Clone, PartialEq, Serialize, Deserialize)]219pub struct LocExpr(pub Rc<Expr>, pub Option<Rc<ExprLocation>>);220impl Debug for LocExpr {221	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {222		write!(f, "{:?} from {:?}", self.0, self.1)223	}224}225226/// Creates LocExpr from Expr and ExprLocation components227#[macro_export]228macro_rules! loc_expr {229	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {230		LocExpr(231			std::rc::Rc::new($expr),232			if $need_loc {233				Some(std::rc::Rc::new(ExprLocation(234					$name.to_owned(),235					$start,236					$end,237				)))238			} else {239				None240				},241			)242	};243}244245/// Creates LocExpr without location info246#[macro_export]247macro_rules! loc_expr_todo {248	($expr:expr) => {249		LocExpr(Rc::new($expr), None)250	};251}