difftreelog
feat(parser) impl Display for op types
in: master
1 file changed
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth1#[cfg(feature = "deserialize")]2use serde::Deserialize;3#[cfg(feature = "serialize")]4use serde::Serialize;5use std::{fmt::Debug, ops::Deref, path::PathBuf, rc::Rc};6#[cfg(feature = "dump")]7use structdump_derive::Codegen;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 ErrorStmt(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}1#[cfg(feature = "deserialize")]2use serde::Deserialize;3#[cfg(feature = "serialize")]4use serde::Serialize;5use std::{6 fmt::{Debug, Display},7 ops::Deref,8 path::PathBuf,9 rc::Rc,10};11#[cfg(feature = "dump")]12use structdump_derive::Codegen;1314#[cfg_attr(feature = "dump", derive(Codegen))]15#[cfg_attr(feature = "serialize", derive(Serialize))]16#[cfg_attr(feature = "deserialize", derive(Deserialize))]17#[derive(Debug, PartialEq)]18pub enum FieldName {19 /// {fixed: 2}20 Fixed(Rc<str>),21 /// {["dyn"+"amic"]: 3}22 Dyn(LocExpr),23}2425#[cfg_attr(feature = "dump", derive(Codegen))]26#[cfg_attr(feature = "serialize", derive(Serialize))]27#[cfg_attr(feature = "deserialize", derive(Deserialize))]28#[derive(Debug, Clone, Copy, PartialEq)]29pub enum Visibility {30 /// :31 Normal,32 /// ::33 Hidden,34 /// :::35 Unhide,36}3738#[cfg_attr(feature = "dump", derive(Codegen))]39#[cfg_attr(feature = "serialize", derive(Serialize))]40#[cfg_attr(feature = "deserialize", derive(Deserialize))]41#[derive(Debug, PartialEq)]42pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);4344#[cfg_attr(feature = "dump", derive(Codegen))]45#[cfg_attr(feature = "serialize", derive(Serialize))]46#[cfg_attr(feature = "deserialize", derive(Deserialize))]47#[derive(Debug, PartialEq)]48pub struct FieldMember {49 pub name: FieldName,50 pub plus: bool,51 pub params: Option<ParamsDesc>,52 pub visibility: Visibility,53 pub value: LocExpr,54}5556#[cfg_attr(feature = "dump", derive(Codegen))]57#[cfg_attr(feature = "serialize", derive(Serialize))]58#[cfg_attr(feature = "deserialize", derive(Deserialize))]59#[derive(Debug, PartialEq)]60pub enum Member {61 Field(FieldMember),62 BindStmt(BindSpec),63 AssertStmt(AssertStmt),64}6566#[cfg_attr(feature = "dump", derive(Codegen))]67#[cfg_attr(feature = "serialize", derive(Serialize))]68#[cfg_attr(feature = "deserialize", derive(Deserialize))]69#[derive(Debug, Clone, Copy, PartialEq)]70pub enum UnaryOpType {71 Plus,72 Minus,73 BitNot,74 Not,75}76impl Display for UnaryOpType {77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {78 use UnaryOpType::*;79 write!(80 f,81 "{}",82 match self {83 Plus => "+",84 Minus => "-",85 BitNot => "~",86 Not => "!",87 }88 )89 }90}9192#[cfg_attr(feature = "dump", derive(Codegen))]93#[cfg_attr(feature = "serialize", derive(Serialize))]94#[cfg_attr(feature = "deserialize", derive(Deserialize))]95#[derive(Debug, Clone, Copy, PartialEq)]96pub enum BinaryOpType {97 Mul,98 Div,99100 Add,101 Sub,102103 Lhs,104 Rhs,105106 Lt,107 Gt,108 Lte,109 Gte,110111 BitAnd,112 BitOr,113 BitXor,114115 And,116 Or,117}118impl Display for BinaryOpType {119 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {120 use BinaryOpType::*;121 write!(122 f,123 "{}",124 match self {125 Mul => "*",126 Div => "/",127 Add => "+",128 Sub => "-",129 Lhs => "<<",130 Rhs => ">>",131 Lt => "<",132 Gt => ">",133 Lte => "<=",134 Gte => ">=",135 BitAnd => "&",136 BitOr => "|",137 BitXor => "^",138 And => "&&",139 Or => "||",140 }141 )142 }143}144145/// name, default value146#[cfg_attr(feature = "dump", derive(Codegen))]147#[cfg_attr(feature = "serialize", derive(Serialize))]148#[cfg_attr(feature = "deserialize", derive(Deserialize))]149#[derive(Debug, PartialEq)]150pub struct Param(pub Rc<str>, pub Option<LocExpr>);151152/// Defined function parameters153#[cfg_attr(feature = "dump", derive(Codegen))]154#[cfg_attr(feature = "serialize", derive(Serialize))]155#[cfg_attr(feature = "deserialize", derive(Deserialize))]156#[derive(Debug, Clone, PartialEq)]157pub struct ParamsDesc(pub Rc<Vec<Param>>);158impl Deref for ParamsDesc {159 type Target = Vec<Param>;160 fn deref(&self) -> &Self::Target {161 &self.0162 }163}164165#[cfg_attr(feature = "dump", derive(Codegen))]166#[cfg_attr(feature = "serialize", derive(Serialize))]167#[cfg_attr(feature = "deserialize", derive(Deserialize))]168#[derive(Debug, PartialEq)]169pub struct Arg(pub Option<String>, pub LocExpr);170171#[cfg_attr(feature = "dump", derive(Codegen))]172#[cfg_attr(feature = "serialize", derive(Serialize))]173#[cfg_attr(feature = "deserialize", derive(Deserialize))]174#[derive(Debug, PartialEq)]175pub struct ArgsDesc(pub Vec<Arg>);176impl Deref for ArgsDesc {177 type Target = Vec<Arg>;178 fn deref(&self) -> &Self::Target {179 &self.0180 }181}182183#[cfg_attr(feature = "dump", derive(Codegen))]184#[cfg_attr(feature = "serialize", derive(Serialize))]185#[cfg_attr(feature = "deserialize", derive(Deserialize))]186#[derive(Debug, Clone, PartialEq)]187pub struct BindSpec {188 pub name: Rc<str>,189 pub params: Option<ParamsDesc>,190 pub value: LocExpr,191}192193#[cfg_attr(feature = "dump", derive(Codegen))]194#[cfg_attr(feature = "serialize", derive(Serialize))]195#[cfg_attr(feature = "deserialize", derive(Deserialize))]196#[derive(Debug, PartialEq)]197pub struct IfSpecData(pub LocExpr);198199#[cfg_attr(feature = "dump", derive(Codegen))]200#[cfg_attr(feature = "serialize", derive(Serialize))]201#[cfg_attr(feature = "deserialize", derive(Deserialize))]202#[derive(Debug, PartialEq)]203pub struct ForSpecData(pub Rc<str>, pub LocExpr);204205#[cfg_attr(feature = "dump", derive(Codegen))]206#[cfg_attr(feature = "serialize", derive(Serialize))]207#[cfg_attr(feature = "deserialize", derive(Deserialize))]208#[derive(Debug, PartialEq)]209pub enum CompSpec {210 IfSpec(IfSpecData),211 ForSpec(ForSpecData),212}213214#[cfg_attr(feature = "dump", derive(Codegen))]215#[cfg_attr(feature = "serialize", derive(Serialize))]216#[cfg_attr(feature = "deserialize", derive(Deserialize))]217#[derive(Debug, PartialEq)]218pub struct ObjComp {219 pub pre_locals: Vec<BindSpec>,220 pub key: LocExpr,221 pub value: LocExpr,222 pub post_locals: Vec<BindSpec>,223 pub compspecs: Vec<CompSpec>,224}225226#[cfg_attr(feature = "dump", derive(Codegen))]227#[cfg_attr(feature = "serialize", derive(Serialize))]228#[cfg_attr(feature = "deserialize", derive(Deserialize))]229#[derive(Debug, PartialEq)]230pub enum ObjBody {231 MemberList(Vec<Member>),232 ObjComp(ObjComp),233}234235#[cfg_attr(feature = "dump", derive(Codegen))]236#[cfg_attr(feature = "serialize", derive(Serialize))]237#[cfg_attr(feature = "deserialize", derive(Deserialize))]238#[derive(Debug, PartialEq, Clone, Copy)]239pub enum LiteralType {240 This,241 Super,242 Dollar,243 Null,244 True,245 False,246}247248#[derive(Debug, PartialEq)]249pub struct SliceDesc {250 pub start: Option<LocExpr>,251 pub end: Option<LocExpr>,252 pub step: Option<LocExpr>,253}254255/// Syntax base256#[cfg_attr(feature = "dump", derive(Codegen))]257#[cfg_attr(feature = "serialize", derive(Serialize))]258#[cfg_attr(feature = "deserialize", derive(Deserialize))]259#[derive(Debug, PartialEq)]260pub enum Expr {261 Literal(LiteralType),262263 /// String value: "hello"264 Str(Rc<str>),265 /// Number: 1, 2.0, 2e+20266 Num(f64),267 /// Variable name: test268 Var(Rc<str>),269270 /// Array of expressions: [1, 2, "Hello"]271 Arr(Vec<LocExpr>),272 /// Array comprehension:273 /// ```jsonnet274 /// ingredients: [275 /// { kind: kind, qty: 4 / 3 }276 /// for kind in [277 /// 'Honey Syrup',278 /// 'Lemon Juice',279 /// 'Farmers Gin',280 /// ]281 /// ],282 /// ```283 ArrComp(LocExpr, Vec<CompSpec>),284285 /// Object: {a: 2}286 Obj(ObjBody),287 /// Object extension: var1 {b: 2}288 ObjExtend(LocExpr, ObjBody),289290 /// (obj)291 Parened(LocExpr),292293 /// -2294 UnaryOp(UnaryOpType, LocExpr),295 /// 2 - 2296 BinaryOp(LocExpr, BinaryOpType, LocExpr),297 /// assert 2 == 2 : "Math is broken"298 AssertExpr(AssertStmt, LocExpr),299 /// local a = 2; { b: a }300 LocalExpr(Vec<BindSpec>, LocExpr),301302 /// import "hello"303 Import(PathBuf),304 /// importStr "file.txt"305 ImportStr(PathBuf),306 /// error "I'm broken"307 ErrorStmt(LocExpr),308 /// a(b, c)309 Apply(LocExpr, ArgsDesc, bool),310 /// a[b]311 Index(LocExpr, LocExpr),312 /// function(x) x313 Function(ParamsDesc, LocExpr),314 /// if true == false then 1 else 2315 IfElse {316 cond: IfSpecData,317 cond_then: LocExpr,318 cond_else: Option<LocExpr>,319 },320}321322/// file, begin offset, end offset323#[cfg_attr(feature = "dump", derive(Codegen))]324#[cfg_attr(feature = "serialize", derive(Serialize))]325#[cfg_attr(feature = "deserialize", derive(Deserialize))]326#[derive(Clone, PartialEq)]327pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);328impl Debug for ExprLocation {329 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {330 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)331 }332}333334/// Holds AST expression and its location in source file335#[cfg_attr(feature = "dump", derive(Codegen))]336#[cfg_attr(feature = "serialize", derive(Serialize))]337#[cfg_attr(feature = "deserialize", derive(Deserialize))]338#[derive(Clone, PartialEq)]339pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);340impl Debug for LocExpr {341 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {342 write!(f, "{:?} from {:?}", self.0, self.1)343 }344}345346/// Creates LocExpr from Expr and ExprLocation components347#[macro_export]348macro_rules! loc_expr {349 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {350 LocExpr(351 std::rc::Rc::new($expr),352 if $need_loc {353 Some(ExprLocation($name, $start, $end))354 } else {355 None356 },357 )358 };359}360361/// Creates LocExpr without location info362#[macro_export]363macro_rules! loc_expr_todo {364 ($expr:expr) => {365 LocExpr(Rc::new($expr), None)366 };367}