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}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))
- }
}