git.delta.rocks / jrsonnet / refs/commits / 30ce98effbb6

difftreelog

feat(parser) use PathBuf for file names

Лач2020-06-06parent: #e53de5d.patch.diff
in: master

2 files changed

modifiedcrates/jsonnet-parser/src/expr.rsdiffbeforeafterboth
before · crates/jsonnet-parser/src/expr.rs
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 is desugared to std.mod54	// Mod,55	Add,56	Sub,5758	Lhs,59	Rhs,6061	Lt,62	Gt,63	Lte,64	Gte,6566	In,6768	// Eq/Ne is desugared to std.equals69	// Eq,70	// Ne,7172	BitAnd,73	BitOr,74	BitXor,7576	And,77	Or,78}7980/// name, default value81#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]82pub struct Param(pub String, pub Option<LocExpr>);83/// Defined function parameters84#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]85pub struct ParamsDesc(pub Vec<Param>);86impl ParamsDesc {87	pub fn with_defaults(&self) -> Vec<Param> {88		self.0.iter().filter(|e| e.1.is_some()).cloned().collect()89	}90}9192#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]93pub struct Arg(pub Option<String>, pub LocExpr);94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]95pub struct ArgsDesc(pub Vec<Arg>);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		first: ForSpecData,124		rest: Vec<CompSpec>,125	},126}127128#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]129pub enum LiteralType {130	This,131	Super,132	Dollar,133	Null,134	True,135	False,136}137138#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]139pub struct SliceDesc {140	pub start: Option<LocExpr>,141	pub end: Option<LocExpr>,142	pub step: Option<LocExpr>,143}144145/// Syntax base146#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]147pub enum Expr {148	Literal(LiteralType),149150	/// String value: "hello"151	Str(String),152	/// Number: 1, 2.0, 2e+20153	Num(f64),154	/// Variable name: test155	Var(String),156157	/// Array of expressions: [1, 2, "Hello"]158	Arr(Vec<LocExpr>),159	/// Array comprehension:160	/// ```jsonnet161	///  ingredients: [162	///    { kind: kind, qty: 4 / 3 }163	///    for kind in [164	///      'Honey Syrup',165	///      'Lemon Juice',166	///      'Farmers Gin',167	///    ]168	///  ],169	/// ```170	ArrComp(LocExpr, Vec<CompSpec>),171172	/// Object: {a: 2}173	Obj(ObjBody),174	/// Object extension: var1 {b: 2}175	ObjExtend(LocExpr, ObjBody),176177	/// (obj)178	Parened(LocExpr),179180	/// Params in function definition181	/// hello, world, test = 2182	Params(ParamsDesc),183	/// Args in function call184	/// 2 + 2, 3, named = 6185	Args(ArgsDesc),186187	/// -2188	UnaryOp(UnaryOpType, LocExpr),189	/// 2 - 2190	BinaryOp(LocExpr, BinaryOpType, LocExpr),191	/// assert 2 == 2 : "Math is broken"192	AssertExpr(AssertStmt, LocExpr),193	/// local a = 2; { b: a }194	LocalExpr(Vec<BindSpec>, LocExpr),195196	/// a = 3197	Bind(BindSpec),198	/// import "hello"199	Import(String),200	/// importStr "file.txt"201	ImportStr(String),202	/// error "I'm broken"203	Error(LocExpr),204	/// a(b, c)205	Apply(LocExpr, ArgsDesc),206	///207	Select(LocExpr, String),208	/// a[b]209	Index(LocExpr, LocExpr),210	/// a[1::2]211	Slice(LocExpr, SliceDesc),212	/// function(x) x213	Function(ParamsDesc, LocExpr),214	/// if true == false then 1 else 2215	IfElse {216		cond: IfSpecData,217		cond_then: LocExpr,218		cond_else: Option<LocExpr>,219	},220	/// if 2 = 3221	IfSpec(IfSpecData),222	/// for elem in array223	ForSpec(ForSpecData),224}225226/// file, begin offset, end offset227#[derive(Clone, PartialEq, Serialize, Deserialize)]228pub struct ExprLocation(pub String, pub usize, pub usize);229impl Debug for ExprLocation {230	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {231		write!(f, "{}:{:?}-{:?}", self.0, self.1, self.2)232	}233}234235/// Holds AST expression and its location in source file+236#[derive(Clone, PartialEq, Serialize, Deserialize)]237pub struct LocExpr(pub Rc<Expr>, pub Option<Rc<ExprLocation>>);238impl Debug for LocExpr {239	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {240		write!(f, "{:?} from {:?}", self.0, self.1)241	}242}243244/// Creates LocExpr from Expr and ExprLocation components245#[macro_export]246macro_rules! loc_expr {247	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {248		LocExpr(249			std::rc::Rc::new($expr),250			if $need_loc {251				Some(std::rc::Rc::new(ExprLocation(252					$name.to_owned(),253					$start,254					$end,255				)))256			} else {257				None258				},259			)260	};261}262263/// Creates LocExpr without location info264#[macro_export]265macro_rules! loc_expr_todo {266	($expr:expr) => {267		LocExpr(Rc::new($expr), None)268	};269}
after · crates/jsonnet-parser/src/expr.rs
1use serde::{Deserialize, Serialize};2use std::{fmt::Debug, 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	In,6667	BitAnd,68	BitOr,69	BitXor,7071	And,72	Or,73}7475/// name, default value76#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]77pub struct Param(pub String, pub Option<LocExpr>);78/// Defined function parameters79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]80pub struct ParamsDesc(pub Vec<Param>);81impl ParamsDesc {82	pub fn with_defaults(&self) -> Vec<Param> {83		self.0.iter().filter(|e| e.1.is_some()).cloned().collect()84	}85}8687#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]88pub struct Arg(pub Option<String>, pub LocExpr);89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]90pub struct ArgsDesc(pub Vec<Arg>);9192#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]93pub struct BindSpec {94	pub name: String,95	pub params: Option<ParamsDesc>,96	pub value: LocExpr,97}9899#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]100pub struct IfSpecData(pub LocExpr);101#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]102pub struct ForSpecData(pub String, pub LocExpr);103104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]105pub enum CompSpec {106	IfSpec(IfSpecData),107	ForSpec(ForSpecData),108}109110#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]111pub enum ObjBody {112	MemberList(Vec<Member>),113	ObjComp {114		pre_locals: Vec<BindSpec>,115		key: LocExpr,116		value: LocExpr,117		post_locals: Vec<BindSpec>,118		rest: Vec<CompSpec>,119	},120}121122#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]123pub enum LiteralType {124	This,125	Super,126	Dollar,127	Null,128	True,129	False,130}131132#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]133pub struct SliceDesc {134	pub start: Option<LocExpr>,135	pub end: Option<LocExpr>,136	pub step: Option<LocExpr>,137}138139/// Syntax base140#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]141pub enum Expr {142	Literal(LiteralType),143144	/// String value: "hello"145	Str(String),146	/// Number: 1, 2.0, 2e+20147	Num(f64),148	/// Variable name: test149	Var(String),150151	/// Array of expressions: [1, 2, "Hello"]152	Arr(Vec<LocExpr>),153	/// Array comprehension:154	/// ```jsonnet155	///  ingredients: [156	///    { kind: kind, qty: 4 / 3 }157	///    for kind in [158	///      'Honey Syrup',159	///      'Lemon Juice',160	///      'Farmers Gin',161	///    ]162	///  ],163	/// ```164	ArrComp(LocExpr, Vec<CompSpec>),165166	/// Object: {a: 2}167	Obj(ObjBody),168	/// Object extension: var1 {b: 2}169	ObjExtend(LocExpr, ObjBody),170171	/// (obj)172	Parened(LocExpr),173174	/// Params in function definition175	/// hello, world, test = 2176	Params(ParamsDesc),177	/// Args in function call178	/// 2 + 2, 3, named = 6179	Args(ArgsDesc),180181	/// -2182	UnaryOp(UnaryOpType, LocExpr),183	/// 2 - 2184	BinaryOp(LocExpr, BinaryOpType, LocExpr),185	/// assert 2 == 2 : "Math is broken"186	AssertExpr(AssertStmt, LocExpr),187	/// local a = 2; { b: a }188	LocalExpr(Vec<BindSpec>, LocExpr),189190	/// a = 3191	Bind(BindSpec),192	/// import "hello"193	Import(String),194	/// importStr "file.txt"195	ImportStr(String),196	/// error "I'm broken"197	Error(LocExpr),198	/// a(b, c)199	Apply(LocExpr, ArgsDesc),200	///201	Select(LocExpr, String),202	/// a[b]203	Index(LocExpr, LocExpr),204	/// a[1::2]205	Slice(LocExpr, SliceDesc),206	/// function(x) x207	Function(ParamsDesc, LocExpr),208	/// if true == false then 1 else 2209	IfElse {210		cond: IfSpecData,211		cond_then: LocExpr,212		cond_else: Option<LocExpr>,213	},214	/// if 2 = 3215	IfSpec(IfSpecData),216	/// for elem in array217	ForSpec(ForSpecData),218}219220/// file, begin offset, end offset221#[derive(Clone, PartialEq, Serialize, Deserialize)]222pub struct ExprLocation(pub PathBuf, pub usize, pub usize);223impl Debug for ExprLocation {224	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {225		write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)226	}227}228229/// Holds AST expression and its location in source file+230#[derive(Clone, PartialEq, Serialize, Deserialize)]231pub struct LocExpr(pub Rc<Expr>, pub Option<Rc<ExprLocation>>);232impl Debug for LocExpr {233	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {234		write!(f, "{:?} from {:?}", self.0, self.1)235	}236}237238/// Creates LocExpr from Expr and ExprLocation components239#[macro_export]240macro_rules! loc_expr {241	($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {242		LocExpr(243			std::rc::Rc::new($expr),244			if $need_loc {245				Some(std::rc::Rc::new(ExprLocation(246					$name.to_owned(),247					$start,248					$end,249				)))250			} else {251				None252				},253			)254	};255}256257/// Creates LocExpr without location info258#[macro_export]259macro_rules! loc_expr_todo {260	($expr:expr) => {261		LocExpr(Rc::new($expr), None)262	};263}
modifiedcrates/jsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jsonnet-parser/src/lib.rs
+++ b/crates/jsonnet-parser/src/lib.rs
@@ -4,7 +4,7 @@
 extern crate test;
 
 use peg::parser;
-use std::rc::Rc;
+use std::{path::PathBuf, rc::Rc};
 mod expr;
 pub use expr::*;
 
@@ -19,7 +19,7 @@
 
 pub struct ParserSettings {
 	pub loc_data: bool,
-	pub file_name: String,
+	pub file_name: PathBuf,
 }
 
 parser! {
@@ -118,14 +118,13 @@
 			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}
 			/ field:field(s) {expr::Member::Field(field)}
 		pub rule objinside(s: &ParserSettings) -> expr::ObjBody
-			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ first:forspec(s) rest:(_ rest:compspec(s) {rest})? {
+			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {
 				expr::ObjBody::ObjComp {
 					pre_locals,
 					key,
 					value,
 					post_locals,
-					first,
-					rest: rest.unwrap_or_default(),
+					rest: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(),
 				}
 			}
 			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}
@@ -301,6 +300,7 @@
 pub mod tests {
 	use super::{expr::*, parse};
 	use crate::ParserSettings;
+	use std::path::PathBuf;
 
 	macro_rules! parse {
 		($s:expr) => {
@@ -308,7 +308,7 @@
 				$s,
 				&ParserSettings {
 					loc_data: false,
-					file_name: "test.jsonnet".to_owned(),
+					file_name: PathBuf::from("/test.jsonnet"),
 					},
 				)
 			.unwrap()