difftreelog
feat(parser) more serialization options
in: master
3 files changed
crates/jsonnet-parser/Cargo.tomldiffbeforeafterboth--- a/crates/jsonnet-parser/Cargo.toml
+++ b/crates/jsonnet-parser/Cargo.toml
@@ -6,16 +6,18 @@
[features]
default = []
-# Trace peg token parsing
-# trace = ["peg/trace"]
-# TODO:
-# serialize = ["serde"]
+serialize = ["serde"]
+deserialize = ["serde"]
+# Adds ability to dump AST as source code for easy embedding
+dump = ["structdump", "structdump-derive"]
[dependencies]
peg = "0.6.2"
-serde = { version = "1.0.114", features = ["derive", "rc"] }
unescape = "0.1.0"
+serde = { version = "1.0.114", features = ["derive", "rc"], optional = true }
+structdump = { version = "0.1.2", optional = true }
+structdump-derive = { version = "0.1.2", optional = true }
+
[dev-dependencies]
jsonnet-stdlib = { version = "0.1.0", path = "../jsonnet-stdlib" }
-bincode = "1.3.1"
crates/jsonnet-parser/src/expr.rsdiffbeforeafterboth1use serde::{Deserialize, Serialize};2use std::{fmt::Debug, ops::Deref, path::PathBuf, rc::Rc};34#[derive(Debug, PartialEq, Serialize, Deserialize)]5pub enum FieldName {6 /// {fixed: 2}7 Fixed(Rc<str>),8 /// {["dyn"+"amic"]: 3}9 Dyn(LocExpr),10}1112#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]13pub enum Visibility {14 /// :15 Normal,16 /// ::17 Hidden,18 /// :::19 Unhide,20}2122#[derive(Debug, PartialEq, Serialize, Deserialize)]23pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);2425#[derive(Debug, 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, 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, PartialEq, Serialize, Deserialize)]75pub struct Param(pub Rc<str>, pub Option<LocExpr>);76/// Defined function parameters77#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]78pub struct ParamsDesc(pub Rc<Vec<Param>>);79impl Deref for ParamsDesc {80 type Target = Vec<Param>;81 fn deref(&self) -> &Self::Target {82 &self.083 }84}8586#[derive(Debug, PartialEq, Serialize, Deserialize)]87pub struct Arg(pub Option<String>, pub LocExpr);88#[derive(Debug, 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: Rc<str>,100 pub params: Option<ParamsDesc>,101 pub value: LocExpr,102}103104#[derive(Debug, PartialEq, Serialize, Deserialize)]105pub struct IfSpecData(pub LocExpr);106#[derive(Debug, PartialEq, Serialize, Deserialize)]107pub struct ForSpecData(pub Rc<str>, pub LocExpr);108109#[derive(Debug, PartialEq, Serialize, Deserialize)]110pub enum CompSpec {111 IfSpec(IfSpecData),112 ForSpec(ForSpecData),113}114115#[derive(Debug, PartialEq, Serialize, Deserialize)]116pub struct ObjComp {117 pub pre_locals: Vec<BindSpec>,118 pub key: LocExpr,119 pub value: LocExpr,120 pub post_locals: Vec<BindSpec>,121 pub compspecs: Vec<CompSpec>,122}123124#[derive(Debug, PartialEq, Serialize, Deserialize)]125pub enum ObjBody {126 MemberList(Vec<Member>),127 ObjComp(ObjComp),128}129130#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Copy)]131pub enum LiteralType {132 This,133 Super,134 Dollar,135 Null,136 True,137 False,138}139140#[derive(Debug, PartialEq, Serialize, Deserialize)]141pub struct SliceDesc {142 pub start: Option<LocExpr>,143 pub end: Option<LocExpr>,144 pub step: Option<LocExpr>,145}146147/// Syntax base148#[derive(Debug, PartialEq, Serialize, Deserialize)]149pub enum Expr {150 Literal(LiteralType),151152 /// String value: "hello"153 Str(Rc<str>),154 /// Number: 1, 2.0, 2e+20155 Num(f64),156 /// Variable name: test157 Var(Rc<str>),158159 /// Array of expressions: [1, 2, "Hello"]160 Arr(Vec<LocExpr>),161 /// Array comprehension:162 /// ```jsonnet163 /// ingredients: [164 /// { kind: kind, qty: 4 / 3 }165 /// for kind in [166 /// 'Honey Syrup',167 /// 'Lemon Juice',168 /// 'Farmers Gin',169 /// ]170 /// ],171 /// ```172 ArrComp(LocExpr, Vec<CompSpec>),173174 /// Object: {a: 2}175 Obj(ObjBody),176 /// Object extension: var1 {b: 2}177 ObjExtend(LocExpr, ObjBody),178179 /// (obj)180 Parened(LocExpr),181182 /// -2183 UnaryOp(UnaryOpType, LocExpr),184 /// 2 - 2185 BinaryOp(LocExpr, BinaryOpType, LocExpr),186 /// assert 2 == 2 : "Math is broken"187 AssertExpr(AssertStmt, LocExpr),188 /// local a = 2; { b: a }189 LocalExpr(Vec<BindSpec>, LocExpr),190191 /// import "hello"192 Import(PathBuf),193 /// importStr "file.txt"194 ImportStr(PathBuf),195 /// error "I'm broken"196 Error(LocExpr),197 /// a(b, c)198 Apply(LocExpr, ArgsDesc, bool),199 /// a[b]200 Index(LocExpr, LocExpr),201 /// function(x) x202 Function(ParamsDesc, LocExpr),203 /// if true == false then 1 else 2204 IfElse {205 cond: IfSpecData,206 cond_then: LocExpr,207 cond_else: Option<LocExpr>,208 },209}210211/// file, begin offset, end offset212#[derive(Clone, PartialEq, Serialize, Deserialize)]213pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);214impl Debug for ExprLocation {215 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {216 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)217 }218}219220/// Holds AST expression and its location in source file+221#[derive(Clone, PartialEq, Serialize, Deserialize)]222pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);223impl Debug for LocExpr {224 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {225 write!(f, "{:?} from {:?}", self.0, self.1)226 }227}228229/// Creates LocExpr from Expr and ExprLocation components230#[macro_export]231macro_rules! loc_expr {232 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {233 LocExpr(234 std::rc::Rc::new($expr),235 if $need_loc {236 Some(ExprLocation($name, $start, $end))237 } else {238 None239 },240 )241 };242}243244/// Creates LocExpr without location info245#[macro_export]246macro_rules! loc_expr_todo {247 ($expr:expr) => {248 LocExpr(Rc::new($expr), None)249 };250}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}crates/jsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-parser/src/lib.rs
+++ b/crates/jsonnet-parser/src/lib.rs
@@ -575,11 +575,4 @@
fn bench_parse_peg(b: &mut Bencher) {
b.iter(|| parse!(jsonnet_stdlib::STDLIB_STR))
}
-
- // From serialized blob
- #[bench]
- fn bench_parse_serde_bincode(b: &mut Bencher) {
- let serialized = bincode::serialize(&parse!(jsonnet_stdlib::STDLIB_STR)).unwrap();
- b.iter(|| bincode::deserialize::<LocExpr>(&serialized))
- }
}