difftreelog
chore(parser) initial parser commit
in: master
6 files changed
.gitignorediffbeforeafterboth--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
Cargo.lockdiffbeforeafterboth--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,59 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "jsonnet-parser"
+version = "0.1.0"
+dependencies = [
+ "peg",
+]
+
+[[package]]
+name = "peg"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9075875c14bb21f25f11cad4b6ad2e4dd443b8fb83900b2fbdd6ebd744b82e97"
+dependencies = [
+ "peg-macros",
+ "peg-runtime",
+]
+
+[[package]]
+name = "peg-macros"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c24c165fd39e995246140cc78df55c56c6733ba87e6658cb3e197b8856c62852"
+dependencies = [
+ "peg-runtime",
+ "proc-macro2",
+ "quote",
+]
+
+[[package]]
+name = "peg-runtime"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0c1a2897e69d986c7986747ebad425cf03746ec5e3e09bb3b2600f91301ba864"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8872cf6f48eee44265156c111456a700ab3483686b3f96df4cf5481c89157319"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "42934bc9c8ab0d3b273a16d8551c8f0fcff46be73276ca083ec2414c15c4ba5e"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,2 @@
+[workspace]
+members = ["crates/jsonnet-parser"]
crates/jsonnet-parser/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/crates/jsonnet-parser/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "jsonnet-parser"
+version = "0.1.0"
+authors = ["Лач <iam@lach.pw>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+peg = "0.6.2"
crates/jsonnet-parser/src/expr.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jsonnet-parser/src/expr.rs
@@ -0,0 +1,206 @@
+#[derive(Debug, Clone, PartialEq)]
+pub enum FieldName {
+ /// {fixed: 2}
+ Fixed(String),
+ /// {["dyn"+"amic"]: 3}
+ Dyn(Box<Expr>),
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Visibility {
+ /// :
+ Normal,
+ /// ::
+ Hidden,
+ /// :::
+ Unhide,
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct AssertStmt(pub Box<Expr>, pub Option<Box<Expr>>);
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum FieldMember {
+ Value {
+ name: FieldName,
+ plus: bool,
+ visibility: Visibility,
+ value: Expr,
+ },
+ Function {
+ name: FieldName,
+ params: Params,
+ visibility: Visibility,
+ value: Expr,
+ },
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Member {
+ Field(FieldMember),
+ BindStmt(Bind),
+ AssertStmt(AssertStmt),
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum UnaryOp {
+ Plus,
+ Minus,
+ BitNot,
+ Not,
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum BinaryOp {
+ Mul,
+ Div,
+ Mod,
+
+ Add,
+ Sub,
+
+ Lhs,
+ Rhs,
+
+ Lt,
+ Gt,
+ Lte,
+ Gte,
+
+ In,
+
+ Eq,
+ Ne,
+
+ BitAnd,
+ BitOr,
+ And,
+ Or,
+
+ BitXor,
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Param {
+ Positional(String),
+ Named(String, Box<Expr>),
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct Params(pub Vec<Param>);
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Arg {
+ Positional(Box<Expr>),
+ Named(String, Box<Expr>),
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct Args(pub Vec<Arg>);
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Bind {
+ Value(String, Box<Expr>),
+ Function(String, Params, Box<Expr>),
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct IfSpec(pub Box<Expr>);
+#[derive(Debug, Clone, PartialEq)]
+pub struct ForSpec(pub String, pub Vec<IfSpec>);
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum CompSpec {
+ IfSpec(IfSpec),
+ ForSpec(ForSpec),
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum ObjBody {
+ MemberList(Vec<Member>),
+ ObjComp {
+ pre_locals: Vec<Bind>,
+ key: Box<Expr>,
+ value: Box<Expr>,
+ post_locals: Vec<Bind>,
+ first: ForSpec,
+ rest: Vec<CompSpec>,
+ },
+}
+
+#[derive(Debug, Clone, PartialEq)]
+pub enum Literal {
+ Null,
+ True,
+ False,
+ This,
+ Super,
+ Dollar,
+}
+
+/// Syntax base
+#[derive(Debug, Clone, PartialEq)]
+pub enum Expr {
+ /// Plain value: null/true/false
+ Literal(Literal),
+
+ /// String value: "hello"
+ Str(String),
+ /// Number: 1, 2.0, 2e+20
+ Num(f64),
+ /// Variable name: test
+ Var(String),
+
+ /// Array of expressions: [1, 2, "Hello"]
+ Arr(Vec<Expr>),
+ /// Array comprehension:
+ /// ```jsonnet
+ /// ingredients: [
+ /// { kind: kind, qty: 4 / 3 }
+ /// for kind in [
+ /// 'Honey Syrup',
+ /// 'Lemon Juice',
+ /// 'Farmers Gin',
+ /// ]
+ /// ],
+ /// ```
+ ArrComp(Box<Expr>, Vec<ForSpec>),
+
+ /// Object: {a: 2}
+ Obj(ObjBody),
+ /// Object extension: var1 {b: 2}
+ ObjExtend(Box<Expr>, ObjBody),
+
+ /// (obj)
+ Parened(Box<Expr>),
+
+ Params(Params),
+ Args(Args),
+
+ UnaryOp(UnaryOp, Box<Expr>),
+ BinaryOp(Box<Expr>, BinaryOp, Box<Expr>),
+ AssertExpr(AssertStmt, Box<Expr>),
+ LocalExpr(Vec<Bind>, Box<Expr>),
+
+ Bind(Bind),
+ Import(String),
+ ImportStr(String),
+ Error(Box<Expr>),
+ Apply(Box<Expr>, Args),
+ Select(Box<Expr>, String),
+ Index(Box<Expr>, Box<Expr>),
+ Slice {
+ value: Box<Expr>,
+ start: Option<Box<Expr>>,
+ end: Option<Box<Expr>>,
+ step: Option<Box<Expr>>,
+ },
+ Function(Params, Box<Expr>),
+ IfElse {
+ cond: IfSpec,
+ cond_then: Box<Expr>,
+ cond_else: Option<Box<Expr>>,
+ },
+ IfSpec(IfSpec),
+ ForSpec(ForSpec),
+}
crates/jsonnet-parser/src/lib.rsdiffbeforeafterbothno changes