difftreelog
feat(parser) import statement parsing
in: master
2 files changed
crates/jsonnet-parser/src/expr.rsdiffbeforeafterboth1use 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 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 Deref for ParamsDesc {82 type Target = Vec<Param>;83 fn deref(&self) -> &Self::Target {84 &self.085 }86}8788#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]89pub struct Arg(pub Option<String>, pub LocExpr);90#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]91pub struct ArgsDesc(pub Vec<Arg>);92impl Deref for ArgsDesc {93 type Target = Vec<Arg>;94 fn deref(&self) -> &Self::Target {95 &self.096 }97}9899#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]100pub struct BindSpec {101 pub name: String,102 pub params: Option<ParamsDesc>,103 pub value: LocExpr,104}105106#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]107pub struct IfSpecData(pub LocExpr);108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]109pub struct ForSpecData(pub String, pub LocExpr);110111#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]112pub enum CompSpec {113 IfSpec(IfSpecData),114 ForSpec(ForSpecData),115}116117#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]118pub enum ObjBody {119 MemberList(Vec<Member>),120 ObjComp {121 pre_locals: Vec<BindSpec>,122 key: LocExpr,123 value: LocExpr,124 post_locals: Vec<BindSpec>,125 rest: Vec<CompSpec>,126 },127}128129#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]130pub enum LiteralType {131 This,132 Super,133 Dollar,134 Null,135 True,136 False,137}138139#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]140pub struct SliceDesc {141 pub start: Option<LocExpr>,142 pub end: Option<LocExpr>,143 pub step: Option<LocExpr>,144}145146/// Syntax base147#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]148pub enum Expr {149 Literal(LiteralType),150151 /// String value: "hello"152 Str(String),153 /// Number: 1, 2.0, 2e+20154 Num(f64),155 /// Variable name: test156 Var(String),157158 /// Array of expressions: [1, 2, "Hello"]159 Arr(Vec<LocExpr>),160 /// Array comprehension:161 /// ```jsonnet162 /// ingredients: [163 /// { kind: kind, qty: 4 / 3 }164 /// for kind in [165 /// 'Honey Syrup',166 /// 'Lemon Juice',167 /// 'Farmers Gin',168 /// ]169 /// ],170 /// ```171 ArrComp(LocExpr, Vec<CompSpec>),172173 /// Object: {a: 2}174 Obj(ObjBody),175 /// Object extension: var1 {b: 2}176 ObjExtend(LocExpr, ObjBody),177178 /// (obj)179 Parened(LocExpr),180181 /// Params in function definition182 /// hello, world, test = 2183 Params(ParamsDesc),184 /// Args in function call185 /// 2 + 2, 3, named = 6186 Args(ArgsDesc),187188 /// -2189 UnaryOp(UnaryOpType, LocExpr),190 /// 2 - 2191 BinaryOp(LocExpr, BinaryOpType, LocExpr),192 /// assert 2 == 2 : "Math is broken"193 AssertExpr(AssertStmt, LocExpr),194 /// local a = 2; { b: a }195 LocalExpr(Vec<BindSpec>, LocExpr),196197 /// a = 3198 Bind(BindSpec),199 /// import "hello"200 Import(String),201 /// importStr "file.txt"202 ImportStr(String),203 /// error "I'm broken"204 Error(LocExpr),205 /// a(b, c)206 Apply(LocExpr, ArgsDesc, bool),207 ///208 Select(LocExpr, String),209 /// a[b]210 Index(LocExpr, LocExpr),211 /// a[1::2]212 Slice(LocExpr, SliceDesc),213 /// function(x) x214 Function(ParamsDesc, LocExpr),215 /// if true == false then 1 else 2216 IfElse {217 cond: IfSpecData,218 cond_then: LocExpr,219 cond_else: Option<LocExpr>,220 },221 /// if 2 = 3222 IfSpec(IfSpecData),223 /// for elem in array224 ForSpec(ForSpecData),225}226227/// file, begin offset, end offset228#[derive(Clone, PartialEq, Serialize, Deserialize)]229pub struct ExprLocation(pub PathBuf, pub usize, pub usize);230impl Debug for ExprLocation {231 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {232 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)233 }234}235236/// Holds AST expression and its location in source file+237#[derive(Clone, PartialEq, Serialize, Deserialize)]238pub struct LocExpr(pub Rc<Expr>, pub Option<Rc<ExprLocation>>);239impl Debug for LocExpr {240 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {241 write!(f, "{:?} from {:?}", self.0, self.1)242 }243}244245/// Creates LocExpr from Expr and ExprLocation components246#[macro_export]247macro_rules! loc_expr {248 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {249 LocExpr(250 std::rc::Rc::new($expr),251 if $need_loc {252 Some(std::rc::Rc::new(ExprLocation(253 $name.to_owned(),254 $start,255 $end,256 )))257 } else {258 None259 },260 )261 };262}263264/// Creates LocExpr without location info265#[macro_export]266macro_rules! loc_expr_todo {267 ($expr:expr) => {268 LocExpr(Rc::new($expr), None)269 };270}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 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 Deref for ParamsDesc {82 type Target = Vec<Param>;83 fn deref(&self) -> &Self::Target {84 &self.085 }86}8788#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]89pub struct Arg(pub Option<String>, pub LocExpr);90#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]91pub struct ArgsDesc(pub Vec<Arg>);92impl Deref for ArgsDesc {93 type Target = Vec<Arg>;94 fn deref(&self) -> &Self::Target {95 &self.096 }97}9899#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]100pub struct BindSpec {101 pub name: String,102 pub params: Option<ParamsDesc>,103 pub value: LocExpr,104}105106#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]107pub struct IfSpecData(pub LocExpr);108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]109pub struct ForSpecData(pub String, pub LocExpr);110111#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]112pub enum CompSpec {113 IfSpec(IfSpecData),114 ForSpec(ForSpecData),115}116117#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]118pub enum ObjBody {119 MemberList(Vec<Member>),120 ObjComp {121 pre_locals: Vec<BindSpec>,122 key: LocExpr,123 value: LocExpr,124 post_locals: Vec<BindSpec>,125 rest: Vec<CompSpec>,126 },127}128129#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]130pub enum LiteralType {131 This,132 Super,133 Dollar,134 Null,135 True,136 False,137}138139#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]140pub struct SliceDesc {141 pub start: Option<LocExpr>,142 pub end: Option<LocExpr>,143 pub step: Option<LocExpr>,144}145146/// Syntax base147#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]148pub enum Expr {149 Literal(LiteralType),150151 /// String value: "hello"152 Str(String),153 /// Number: 1, 2.0, 2e+20154 Num(f64),155 /// Variable name: test156 Var(String),157158 /// Array of expressions: [1, 2, "Hello"]159 Arr(Vec<LocExpr>),160 /// Array comprehension:161 /// ```jsonnet162 /// ingredients: [163 /// { kind: kind, qty: 4 / 3 }164 /// for kind in [165 /// 'Honey Syrup',166 /// 'Lemon Juice',167 /// 'Farmers Gin',168 /// ]169 /// ],170 /// ```171 ArrComp(LocExpr, Vec<CompSpec>),172173 /// Object: {a: 2}174 Obj(ObjBody),175 /// Object extension: var1 {b: 2}176 ObjExtend(LocExpr, ObjBody),177178 /// (obj)179 Parened(LocExpr),180181 /// Params in function definition182 /// hello, world, test = 2183 Params(ParamsDesc),184 /// Args in function call185 /// 2 + 2, 3, named = 6186 Args(ArgsDesc),187188 /// -2189 UnaryOp(UnaryOpType, LocExpr),190 /// 2 - 2191 BinaryOp(LocExpr, BinaryOpType, LocExpr),192 /// assert 2 == 2 : "Math is broken"193 AssertExpr(AssertStmt, LocExpr),194 /// local a = 2; { b: a }195 LocalExpr(Vec<BindSpec>, LocExpr),196197 /// a = 3198 Bind(BindSpec),199 /// import "hello"200 Import(PathBuf),201 /// importStr "file.txt"202 ImportStr(PathBuf),203 /// error "I'm broken"204 Error(LocExpr),205 /// a(b, c)206 Apply(LocExpr, ArgsDesc, bool),207 ///208 Select(LocExpr, String),209 /// a[b]210 Index(LocExpr, LocExpr),211 /// a[1::2]212 Slice(LocExpr, SliceDesc),213 /// function(x) x214 Function(ParamsDesc, LocExpr),215 /// if true == false then 1 else 2216 IfElse {217 cond: IfSpecData,218 cond_then: LocExpr,219 cond_else: Option<LocExpr>,220 },221 /// if 2 = 3222 IfSpec(IfSpecData),223 /// for elem in array224 ForSpec(ForSpecData),225}226227/// file, begin offset, end offset228#[derive(Clone, PartialEq, Serialize, Deserialize)]229pub struct ExprLocation(pub PathBuf, pub usize, pub usize);230impl Debug for ExprLocation {231 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {232 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)233 }234}235236/// Holds AST expression and its location in source file+237#[derive(Clone, PartialEq, Serialize, Deserialize)]238pub struct LocExpr(pub Rc<Expr>, pub Option<Rc<ExprLocation>>);239impl Debug for LocExpr {240 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {241 write!(f, "{:?} from {:?}", self.0, self.1)242 }243}244245/// Creates LocExpr from Expr and ExprLocation components246#[macro_export]247macro_rules! loc_expr {248 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {249 LocExpr(250 std::rc::Rc::new($expr),251 if $need_loc {252 Some(std::rc::Rc::new(ExprLocation(253 $name.to_owned(),254 $start,255 $end,256 )))257 } else {258 None259 },260 )261 };262}263264/// Creates LocExpr without location info265#[macro_export]266macro_rules! loc_expr_todo {267 ($expr:expr) => {268 LocExpr(Rc::new($expr), None)269 };270}crates/jsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-parser/src/lib.rs
+++ b/crates/jsonnet-parser/src/lib.rs
@@ -23,7 +23,7 @@
/// Standard C-like comments
rule comment()
= "//" (!['\n'][_])* "\n"
- / "/*" (!("*/")[_])* "*/"
+ / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"
/ "#" (!['\n'][_])* "\n"
rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")
@@ -89,18 +89,18 @@
/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}
/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}
// TODO: This is temporary workaround, i still dont know how to write this correctly btw.
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<1, 1> whole_line())+) " "*<0, 0> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<2, 2> whole_line())+) " "*<1, 1> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<3, 3> whole_line())+) " "*<2, 2> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<4, 4> whole_line())+) " "*<3, 3> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<5, 5> whole_line())+) " "*<4, 4> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<6, 6> whole_line())+) " "*<5, 5> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<7, 7> whole_line())+) " "*<6, 6> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<8, 8> whole_line())+) " "*<7, 7> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<9, 9> whole_line())+) " "*<8, 8> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<10, 10> whole_line())+) " "*<9, 9> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<11, 11> whole_line())+) " "*<10, 10> "|||" {deent(str)}
- / "|||" (!['\n']single_whitespace())+ "\n" str:$((" "*<12, 12> whole_line())+) " "*<11, 10> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<1, 1> whole_line())+) " "*<0, 0> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<2, 2> whole_line())+) " "*<1, 1> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<3, 3> whole_line())+) " "*<2, 2> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<4, 4> whole_line())+) " "*<3, 3> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<5, 5> whole_line())+) " "*<4, 4> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<6, 6> whole_line())+) " "*<5, 5> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<7, 7> whole_line())+) " "*<6, 6> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<8, 8> whole_line())+) " "*<7, 7> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<9, 9> whole_line())+) " "*<8, 8> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<10, 10> whole_line())+) " "*<9, 9> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<11, 11> whole_line())+) " "*<10, 10> "|||" {deent(str)}
+ / "|||" (!['\n']single_whitespace())* "\n" str:$((" "*<12, 12> whole_line())+) " "*<11, 10> "|||" {deent(str)}
pub rule field_name(s: &ParserSettings) -> expr::FieldName
= name:id() {expr::FieldName::Fixed(name)}
@@ -192,6 +192,9 @@
/ local_expr(s)
/ if_then_else_expr(s)
+ / l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)
+ / l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)
+
/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)
/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)
@@ -367,6 +370,14 @@
}
#[test]
+ fn imports() {
+ assert_eq!(
+ parse!("import \"hello\""),
+ el!(Expr::Import(PathBuf::from("hello"))),
+ );
+ }
+
+ #[test]
fn empty_object() {
assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));
}