git.delta.rocks / jrsonnet / refs/commits / 66507e36aa95

difftreelog

feat(parser) import statement parsing

Лач2020-06-09parent: #3f3e405.patch.diff
in: master

2 files changed

modifiedcrates/jsonnet-parser/src/expr.rsdiffbeforeafterboth
197 /// a = 3197 /// a = 3
198 Bind(BindSpec),198 Bind(BindSpec),
199 /// import "hello"199 /// import "hello"
200 Import(String),200 Import(PathBuf),
201 /// importStr "file.txt"201 /// importStr "file.txt"
202 ImportStr(String),202 ImportStr(PathBuf),
203 /// error "I'm broken"203 /// error "I'm broken"
204 Error(LocExpr),204 Error(LocExpr),
205 /// a(b, c)205 /// a(b, c)
modifiedcrates/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![]))));
 	}