difftreelog
Merge pull request #27 from CertainLach/syntax-error-display
in: master
Syntax error display
8 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7,6 +7,7 @@
checksum = "5c96c3d1062ea7101741480185a6a1275eab01cbe8b20e378d1311bc056d2e08"
dependencies = [
"unicode-width",
+ "yansi-term",
]
[[package]]
@@ -513,3 +514,12 @@
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
+[[package]]
+name = "yansi-term"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fe5c30ade05e61656247b2e334a031dfd0cc466fadef865bdcdea8d537951bf1"
+dependencies = [
+ "winapi",
+]
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -55,6 +55,7 @@
# Explaining traces
[dependencies.annotate-snippets]
version = "0.9.0"
+features = ["color"]
optional = true
[build-dependencies]
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -78,7 +78,11 @@
ImportBadFileUtf8(PathBuf),
#[error("tried to import {1} from {0}, but imports is not supported")]
ImportNotSupported(PathBuf, PathBuf),
- #[error("syntax error")]
+ #[error(
+ "syntax error, expected one of {}, got {:?}",
+ .error.expected,
+ .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())
+ )]
ImportSyntaxError {
path: Rc<PathBuf>,
source_code: Rc<str>,
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -419,18 +419,12 @@
use Expr::*;
let LocExpr(expr, loc) = expr;
Ok(match &**expr {
- Literal(LiteralType::This) => Val::Obj(
- context
- .this()
- .clone()
- .ok_or_else(|| CantUseSelfOutsideOfObject)?,
- ),
- Literal(LiteralType::Dollar) => Val::Obj(
- context
- .dollar()
- .clone()
- .ok_or_else(|| NoTopLevelObjectFound)?,
- ),
+ Literal(LiteralType::This) => {
+ Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
+ }
+ Literal(LiteralType::Dollar) => {
+ Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)
+ }
Literal(LiteralType::True) => Val::Bool(true),
Literal(LiteralType::False) => Val::Bool(false),
Literal(LiteralType::Null) => Val::Null,
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ b/crates/jrsonnet-evaluator/src/trace/location.rs
@@ -1,5 +1,7 @@
#[derive(Clone, PartialEq, Debug)]
pub struct CodeLocation {
+ pub offset: usize,
+
pub line: usize,
pub column: usize,
@@ -25,6 +27,7 @@
let mut out = vec![
CodeLocation {
+ offset: 0,
column: 0,
line: 0,
line_start_offset: 0,
@@ -40,6 +43,7 @@
Some(x) if x.0 == pos => {
let out_idx = x.1;
with_no_known_line_ending.push(out_idx);
+ out[out_idx].offset = pos;
out[out_idx].line = line;
out[out_idx].column = column;
out[out_idx].line_start_offset = this_line_offset;
@@ -82,12 +86,14 @@
),
vec![
CodeLocation {
+ offset: 0,
line: 1,
column: 2,
line_start_offset: 0,
- line_end_offset: 11
+ line_end_offset: 11,
},
CodeLocation {
+ offset: 14,
line: 2,
column: 4,
line_start_offset: 12,
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,6 +1,6 @@
mod location;
-use crate::{EvaluationState, LocError};
+use crate::{error::Error, EvaluationState, LocError};
pub use location::*;
use std::path::PathBuf;
@@ -87,6 +87,33 @@
error: &LocError,
) -> Result<(), std::fmt::Error> {
writeln!(out, "{}", error.error())?;
+ if let Error::ImportSyntaxError {
+ path,
+ source_code,
+ error,
+ } = error.error()
+ {
+ use std::fmt::Write;
+ let mut n = self.resolver.resolve(path);
+ let mut offset = error.location.offset;
+ let is_eof = if offset >= source_code.len() {
+ offset = source_code.len() - 1;
+ true
+ } else {
+ false
+ };
+ let mut location = offset_to_location(source_code, &[offset])
+ .into_iter()
+ .next()
+ .unwrap();
+ if is_eof {
+ location.column += 1;
+ }
+
+ write!(n, ":").unwrap();
+ print_code_location(&mut n, &location, &location).unwrap();
+ write!(out, "{:<p$}{}", "", n, p = self.padding,)?;
+ }
let file_names = error
.trace()
.0
@@ -167,52 +194,101 @@
evaluation_state: &EvaluationState,
error: &LocError,
) -> Result<(), std::fmt::Error> {
- use annotate_snippets::{
- display_list::{DisplayList, FormatOptions},
- snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},
- };
writeln!(out, "{}", error.error())?;
+ if let Error::ImportSyntaxError {
+ path,
+ source_code,
+ error,
+ } = error.error()
+ {
+ let mut offset = error.location.offset;
+ if offset >= source_code.len() {
+ offset = source_code.len() - 1;
+ }
+ let mut location = offset_to_location(source_code, &[offset])
+ .into_iter()
+ .next()
+ .unwrap();
+ if location.column >= 1 {
+ location.column -= 1;
+ }
+
+ self.print_snippet(
+ out,
+ source_code,
+ path,
+ &location,
+ &location,
+ "^ syntax error",
+ )?;
+ }
let trace = &error.trace();
for item in trace.0.iter() {
let desc = &item.desc;
let source = item.location.clone();
let start_end = evaluation_state.map_source_locations(&source.0, &[source.1, source.2]);
- let source_fragment: String = evaluation_state
- .get_source(&source.0)
- .unwrap()
- .chars()
- .skip(start_end[0].line_start_offset)
- .take(start_end[1].line_end_offset - start_end[0].line_start_offset)
- .collect();
+ self.print_snippet(
+ out,
+ &evaluation_state.get_source(&source.0).unwrap(),
+ &source.0,
+ &start_end[0],
+ &start_end[1],
+ desc,
+ )?;
+ }
+ Ok(())
+ }
+}
+
+impl ExplainingFormat {
+ fn print_snippet(
+ &self,
+ out: &mut dyn std::fmt::Write,
+ source: &str,
+ origin: &PathBuf,
+ start: &CodeLocation,
+ end: &CodeLocation,
+ desc: &str,
+ ) -> Result<(), std::fmt::Error> {
+ use annotate_snippets::{
+ display_list::{DisplayList, FormatOptions},
+ snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},
+ };
+
+ let source_fragment: String = source
+ .chars()
+ .skip(start.line_start_offset)
+ .take(end.line_end_offset - end.line_start_offset)
+ .collect();
- let origin = self.resolver.resolve(&source.0);
- let snippet = Snippet {
- opt: FormatOptions {
- color: true,
- ..Default::default()
- },
- title: None,
- footer: vec![],
- slices: vec![Slice {
- source: &source_fragment,
- line_start: start_end[0].line,
- origin: Some(&origin),
- fold: false,
- annotations: vec![SourceAnnotation {
- label: desc,
- annotation_type: AnnotationType::Error,
- range: (
- source.1 - start_end[0].line_start_offset,
- source.2 - start_end[0].line_start_offset,
- ),
- }],
+ let origin = self.resolver.resolve(origin);
+ let snippet = Snippet {
+ opt: FormatOptions {
+ color: true,
+ ..Default::default()
+ },
+ title: None,
+ footer: vec![],
+ slices: vec![Slice {
+ source: &source_fragment,
+ line_start: start.line,
+ origin: Some(&origin),
+ fold: false,
+ annotations: vec![SourceAnnotation {
+ label: desc,
+ annotation_type: AnnotationType::Error,
+ range: (
+ start.offset - start.line_start_offset,
+ end.offset - start.line_start_offset,
+ ),
}],
- };
+ }],
+ };
- let dl = DisplayList::from(snippet);
- writeln!(out, "{}", dl)?;
- }
+ let dl = DisplayList::from(snippet);
+ writeln!(out, "{}", dl)?;
+
Ok(())
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -349,7 +349,7 @@
for v in arr.iter() {
out.push_str("---\n");
out.push_str(&v.manifest(format)?);
- out.push_str("\n");
+ out.push('\n');
}
out.push_str("...");
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth1use peg::parser;2use std::{path::PathBuf, rc::Rc};3mod expr;4pub use expr::*;5pub use peg;67#[derive(Default)]8pub struct ParserSettings {9 pub loc_data: bool,10 pub file_name: Rc<PathBuf>,11}1213parser! {14 grammar jsonnet_parser() for str {15 use peg::ParseLiteral;1617 /// Standard C-like comments18 rule comment()19 = "//" (!['\n'][_])* "\n"20 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"21 / "#" (!['\n'][_])* "\n"2223 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")24 rule _() = single_whitespace()*2526 /// For comma-delimited elements27 rule comma() = quiet!{_ "," _} / expected!("<comma>")28 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}29 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}30 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']31 /// Sequence of digits32 rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }33 /// Number in scientific notation format34 rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")3536 /// Reserved word followed by any non-alphanumberic37 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()38 rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")3940 rule keyword(id: &'static str)41 = ##parse_string_literal(id) end_of_ident()42 // Adds location data information to existing expression43 rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr44 = start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}4546 pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }47 pub rule params(s: &ParserSettings) -> expr::ParamsDesc48 = params:param(s) ** comma() comma()? {49 let mut defaults_started = false;50 for param in ¶ms {51 defaults_started = defaults_started || param.1.is_some();52 assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");53 }54 expr::ParamsDesc(Rc::new(params))55 }56 / { expr::ParamsDesc(Rc::new(Vec::new())) }5758 pub rule arg(s: &ParserSettings) -> expr::Arg59 = name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}60 / expr:expr(s) {expr::Arg(None, expr)}61 pub rule args(s: &ParserSettings) -> expr::ArgsDesc62 = args:arg(s) ** comma() comma()? {63 let mut named_started = false;64 for arg in &args {65 named_started = named_started || arg.0.is_some();66 assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");67 }68 expr::ArgsDesc(args)69 }70 / { expr::ArgsDesc(Vec::new()) }7172 pub rule bind(s: &ParserSettings) -> expr::BindSpec73 = name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}74 / name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}75 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt76 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }7778 pub rule whole_line() -> &'input str79 = str:$((!['\n'][_])* "\n") {str}80 pub rule string_block() -> String81 = "|||" (!['\n']single_whitespace())* "\n"82 empty_lines:$(['\n']*)83 prefix:[' ' | '\t']+ first_line:whole_line()84 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*85 [' ' | '\t']*<, {prefix.len() - 1}> "|||"86 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}87 pub rule string() -> String88 = "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}89 / "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}90 / "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}91 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}92 / string_block()9394 pub rule field_name(s: &ParserSettings) -> expr::FieldName95 = name:$(id()) {expr::FieldName::Fixed(name.into())}96 / name:string() {expr::FieldName::Fixed(name.into())}97 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}98 pub rule visibility() -> expr::Visibility99 = ":::" {expr::Visibility::Unhide}100 / "::" {expr::Visibility::Hidden}101 / ":" {expr::Visibility::Normal}102 pub rule field(s: &ParserSettings) -> expr::FieldMember103 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{104 name,105 plus: plus.is_some(),106 params: None,107 visibility,108 value,109 }}110 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{111 name,112 plus: false,113 params: Some(params),114 visibility,115 value,116 }}117 pub rule obj_local(s: &ParserSettings) -> BindSpec118 = keyword("local") _ bind:bind(s) {bind}119 pub rule member(s: &ParserSettings) -> expr::Member120 = bind:obj_local(s) {expr::Member::BindStmt(bind)}121 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}122 / field:field(s) {expr::Member::Field(field)}123 pub rule objinside(s: &ParserSettings) -> expr::ObjBody124 = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {125 let mut compspecs = vec![CompSpec::ForSpec(forspec)];126 compspecs.extend(others.unwrap_or_default());127 expr::ObjBody::ObjComp(expr::ObjComp{128 pre_locals,129 key,130 value,131 post_locals,132 compspecs,133 })134 }135 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}136 pub rule ifspec(s: &ParserSettings) -> IfSpecData137 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}138 pub rule forspec(s: &ParserSettings) -> ForSpecData139 = keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}140 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>141 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}142 pub rule local_expr(s: &ParserSettings) -> LocExpr143 = l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)144 pub rule string_expr(s: &ParserSettings) -> LocExpr145 = l(s, <s:string() {Expr::Str(s.into())}>)146 pub rule obj_expr(s: &ParserSettings) -> LocExpr147 = l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)148 pub rule array_expr(s: &ParserSettings) -> LocExpr149 = l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)150 pub rule array_comp_expr(s: &ParserSettings) -> LocExpr151 = l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {152 let mut specs = vec![CompSpec::ForSpec(forspec)];153 specs.extend(others.unwrap_or_default());154 Expr::ArrComp(expr, specs)155 }>)156 pub rule number_expr(s: &ParserSettings) -> LocExpr157 = l(s,<n:number() { expr::Expr::Num(n) }>)158 pub rule var_expr(s: &ParserSettings) -> LocExpr159 = l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)160 pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr161 = l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{162 cond,163 cond_then,164 cond_else,165 }}>)166167 pub rule literal(s: &ParserSettings) -> LocExpr168 = l(s,<v:(169 keyword("null") {LiteralType::Null}170 / keyword("true") {LiteralType::True}171 / keyword("false") {LiteralType::False}172 / keyword("self") {LiteralType::This}173 / keyword("$") {LiteralType::Dollar}174 / keyword("super") {LiteralType::Super}175 ) {Expr::Literal(v)}>)176177 pub rule expr_basic(s: &ParserSettings) -> LocExpr178 = literal(s)179180 / string_expr(s) / number_expr(s)181 / array_expr(s)182 / obj_expr(s)183 / array_expr(s)184 / array_comp_expr(s)185186 / l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)187 / l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)188189 / var_expr(s)190 / local_expr(s)191 / if_then_else_expr(s)192193 / l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)194 / l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)195196 / l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)197198 rule slice_part(s: &ParserSettings) -> Option<LocExpr>199 = e:(_ e:expr(s) _{e})? {e}200 pub rule slice_desc(s: &ParserSettings) -> SliceDesc201 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {202 let (end, step) = if let Some((end, step)) = pair {203 (end, step)204 }else{205 (None, None)206 };207208 SliceDesc { start, end, step }209 }210211 rule expr(s: &ParserSettings) -> LocExpr212 = start:position!() a:precedence! {213 a:(@) _ "||" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}214 --215 a:(@) _ "&&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}216 --217 a:(@) _ "|" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}218 --219 a:@ _ "^" _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}220 --221 a:(@) _ "&" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}222 --223 a:(@) _ "==" _ b:@ {loc_expr_todo!(Expr::Apply(224 el!(Expr::Intrinsic("equals".into())),225 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),226 true227 ))}228 a:(@) _ "!=" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(229 el!(Expr::Intrinsic("equals".into())),230 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),231 true232 ))))}233 --234 a:(@) _ "<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}235 a:(@) _ ">" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}236 a:(@) _ "<=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}237 a:(@) _ ">=" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}238 a:(@) _ keyword("in") _ b:@ {loc_expr_todo!(Expr::Apply(239 el!(Expr::Intrinsic("objectHasEx".into())), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),240 true241 ))}242 --243 a:(@) _ "<<" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}244 a:(@) _ ">>" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}245 --246 a:(@) _ "+" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}247 a:(@) _ "-" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}248 --249 a:(@) _ "*" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}250 a:(@) _ "/" _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}251 a:(@) _ "%" _ b:@ {loc_expr_todo!(Expr::Apply(252 el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),253 false254 ))}255 --256 "-" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}257 "!" _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}258 "~" _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }259 --260 a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(261 el!(Expr::Intrinsic("slice".into())),262 ArgsDesc(vec![263 Arg(None, a),264 Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),265 Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),266 Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),267 ]),268 true,269 ))}270 a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}271 a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}272 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}273 a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}274 --275 e:expr_basic(s) {e}276 "(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}277 } end:position!() {278 let LocExpr(e, _) = a;279 LocExpr(e, if s.loc_data {280 Some(ExprLocation(s.file_name.clone(), start, end))281 } else {282 None283 })284 }285 / e:expr_basic(s) {e}286287 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}288 }289}290291pub type ParseError = peg::error::ParseError<peg::str::LineCol>;292pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {293 jsonnet_parser::jsonnet(str, settings)294}295296#[macro_export]297macro_rules! el {298 ($expr:expr) => {299 LocExpr(std::rc::Rc::new($expr), None)300 };301}302303#[cfg(test)]304pub mod tests {305 use super::{expr::*, parse};306 use crate::ParserSettings;307 use std::path::PathBuf;308 use std::rc::Rc;309310 macro_rules! parse {311 ($s:expr) => {312 parse(313 $s,314 &ParserSettings {315 loc_data: false,316 file_name: Rc::new(PathBuf::from("/test.jsonnet")),317 },318 )319 .unwrap()320 };321 }322323 mod expressions {324 use super::*;325326 pub fn basic_math() -> LocExpr {327 el!(Expr::BinaryOp(328 el!(Expr::Num(2.0)),329 BinaryOpType::Add,330 el!(Expr::BinaryOp(331 el!(Expr::Num(2.0)),332 BinaryOpType::Mul,333 el!(Expr::Num(2.0)),334 )),335 ))336 }337 }338339 #[test]340 fn multiline_string() {341 assert_eq!(342 parse!("|||\n Hello world!\n a\n|||"),343 el!(Expr::Str("Hello world!\n a\n".into())),344 );345 assert_eq!(346 parse!("|||\n Hello world!\n a\n|||"),347 el!(Expr::Str("Hello world!\n a\n".into())),348 );349 assert_eq!(350 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),351 el!(Expr::Str("Hello world!\n\ta\n".into())),352 );353 assert_eq!(354 parse!("|||\n Hello world!\n a\n |||"),355 el!(Expr::Str("Hello world!\n a\n".into())),356 );357 }358359 #[test]360 fn slice() {361 parse!("a[1:]");362 parse!("a[1::]");363 parse!("a[:1:]");364 parse!("a[::1]");365 parse!("str[:len - 1]");366 }367368 #[test]369 fn string_escaping() {370 assert_eq!(371 parse!(r#""Hello, \"world\"!""#),372 el!(Expr::Str(r#"Hello, "world"!"#.into())),373 );374 assert_eq!(375 parse!(r#"'Hello \'world\'!'"#),376 el!(Expr::Str("Hello 'world'!".into())),377 );378 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);379 }380381 #[test]382 fn string_unescaping() {383 assert_eq!(384 parse!(r#""Hello\nWorld""#),385 el!(Expr::Str("Hello\nWorld".into())),386 );387 }388389 #[test]390 fn string_verbantim() {391 assert_eq!(392 parse!(r#"@"Hello\n""World""""#),393 el!(Expr::Str("Hello\\n\"World\"".into())),394 );395 }396397 #[test]398 fn imports() {399 assert_eq!(400 parse!("import \"hello\""),401 el!(Expr::Import(PathBuf::from("hello"))),402 );403 assert_eq!(404 parse!("importstr \"garnish.txt\""),405 el!(Expr::ImportStr(PathBuf::from("garnish.txt")))406 );407 }408409 #[test]410 fn empty_object() {411 assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));412 }413414 #[test]415 fn basic_math() {416 assert_eq!(417 parse!("2+2*2"),418 el!(Expr::BinaryOp(419 el!(Expr::Num(2.0)),420 BinaryOpType::Add,421 el!(Expr::BinaryOp(422 el!(Expr::Num(2.0)),423 BinaryOpType::Mul,424 el!(Expr::Num(2.0))425 ))426 ))427 );428 }429430 #[test]431 fn basic_math_with_indents() {432 assert_eq!(parse!("2 + 2 * 2 "), expressions::basic_math());433 }434435 #[test]436 fn basic_math_parened() {437 assert_eq!(438 parse!("2+(2+2*2)"),439 el!(Expr::BinaryOp(440 el!(Expr::Num(2.0)),441 BinaryOpType::Add,442 el!(Expr::Parened(expressions::basic_math())),443 ))444 );445 }446447 /// Comments should not affect parsing448 #[test]449 fn comments() {450 assert_eq!(451 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),452 el!(Expr::BinaryOp(453 el!(Expr::Num(2.0)),454 BinaryOpType::Add,455 el!(Expr::BinaryOp(456 el!(Expr::Num(3.0)),457 BinaryOpType::Mul,458 el!(Expr::Num(4.0))459 ))460 ))461 );462 }463464 /// Comments should be able to be escaped465 #[test]466 fn comment_escaping() {467 assert_eq!(468 parse!("2/*\\*/+*/ - 22"),469 el!(Expr::BinaryOp(470 el!(Expr::Num(2.0)),471 BinaryOpType::Sub,472 el!(Expr::Num(22.0))473 ))474 );475 }476477 #[test]478 fn suffix() {479 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));480 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));481 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));482 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))483 }484485 #[test]486 fn array_comp() {487 use Expr::*;488 assert_eq!(489 parse!("[std.deepJoin(x) for x in arr]"),490 el!(ArrComp(491 el!(Apply(492 el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),493 ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),494 false,495 )),496 vec![CompSpec::ForSpec(ForSpecData(497 "x".into(),498 el!(Var("arr".into()))499 ))]500 )),501 )502 }503504 #[test]505 fn reserved() {506 use Expr::*;507 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));508 assert_eq!(parse!("nulla"), el!(Var("nulla".into())));509 }510511 #[test]512 fn multiple_args_buf() {513 parse!("a(b, null_fields)");514 }515516 #[test]517 fn infix_precedence() {518 use Expr::*;519 assert_eq!(520 parse!("!a && !b"),521 el!(BinaryOp(522 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),523 BinaryOpType::And,524 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))525 ))526 );527 }528529 #[test]530 fn infix_precedence_division() {531 use Expr::*;532 assert_eq!(533 parse!("!a / !b"),534 el!(BinaryOp(535 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),536 BinaryOpType::Div,537 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))538 ))539 );540 }541542 #[test]543 fn double_negation() {544 use Expr::*;545 assert_eq!(546 parse!("!!a"),547 el!(UnaryOp(548 UnaryOpType::Not,549 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))550 ))551 )552 }553554 #[test]555 fn array_test_error() {556 parse!("[a for a in b if c for e in f]");557 // ^^^^ failed code558 }559560 #[test]561 fn can_parse_stdlib() {562 parse!(jrsonnet_stdlib::STDLIB_STR);563 }564565 // From source code566 /*567 #[bench]568 fn bench_parse_peg(b: &mut Bencher) {569 b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))570 }571 */572}1use peg::parser;2use std::{path::PathBuf, rc::Rc};3mod expr;4pub use expr::*;5pub use peg;67#[derive(Default)]8pub struct ParserSettings {9 pub loc_data: bool,10 pub file_name: Rc<PathBuf>,11}1213parser! {14 grammar jsonnet_parser() for str {15 use peg::ParseLiteral;1617 /// Standard C-like comments18 rule comment()19 = "//" (!['\n'][_])* "\n"20 / "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"21 / "#" (!['\n'][_])* "\n"2223 rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")24 rule _() = single_whitespace()*2526 /// For comma-delimited elements27 rule comma() = quiet!{_ "," _} / expected!("<comma>")28 rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}29 rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}30 rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']31 /// Sequence of digits32 rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }33 /// Number in scientific notation format34 rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")3536 /// Reserved word followed by any non-alphanumberic37 rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()38 rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")3940 rule keyword(id: &'static str) -> ()41 = ##parse_string_literal(id) end_of_ident()42 // Adds location data information to existing expression43 rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr44 = start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}4546 pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }47 pub rule params(s: &ParserSettings) -> expr::ParamsDesc48 = params:param(s) ** comma() comma()? {49 let mut defaults_started = false;50 for param in ¶ms {51 defaults_started = defaults_started || param.1.is_some();52 assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");53 }54 expr::ParamsDesc(Rc::new(params))55 }56 / { expr::ParamsDesc(Rc::new(Vec::new())) }5758 pub rule arg(s: &ParserSettings) -> expr::Arg59 = name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}60 / expr:expr(s) {expr::Arg(None, expr)}61 pub rule args(s: &ParserSettings) -> expr::ArgsDesc62 = args:arg(s) ** comma() comma()? {63 let mut named_started = false;64 for arg in &args {65 named_started = named_started || arg.0.is_some();66 assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");67 }68 expr::ArgsDesc(args)69 }70 / { expr::ArgsDesc(Vec::new()) }7172 pub rule bind(s: &ParserSettings) -> expr::BindSpec73 = name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}74 / name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}75 pub rule assertion(s: &ParserSettings) -> expr::AssertStmt76 = keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }7778 pub rule whole_line() -> &'input str79 = str:$((!['\n'][_])* "\n") {str}80 pub rule string_block() -> String81 = "|||" (!['\n']single_whitespace())* "\n"82 empty_lines:$(['\n']*)83 prefix:[' ' | '\t']+ first_line:whole_line()84 lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*85 [' ' | '\t']*<, {prefix.len() - 1}> "|||"86 {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}87 pub rule string() -> String88 = quiet!{ "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}89 / "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}90 / "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}91 / "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}92 / string_block() } / expected!("<string>")9394 pub rule field_name(s: &ParserSettings) -> expr::FieldName95 = name:$(id()) {expr::FieldName::Fixed(name.into())}96 / name:string() {expr::FieldName::Fixed(name.into())}97 / "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}98 pub rule visibility() -> expr::Visibility99 = ":::" {expr::Visibility::Unhide}100 / "::" {expr::Visibility::Hidden}101 / ":" {expr::Visibility::Normal}102 pub rule field(s: &ParserSettings) -> expr::FieldMember103 = name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{104 name,105 plus: plus.is_some(),106 params: None,107 visibility,108 value,109 }}110 / name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{111 name,112 plus: false,113 params: Some(params),114 visibility,115 value,116 }}117 pub rule obj_local(s: &ParserSettings) -> BindSpec118 = keyword("local") _ bind:bind(s) {bind}119 pub rule member(s: &ParserSettings) -> expr::Member120 = bind:obj_local(s) {expr::Member::BindStmt(bind)}121 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}122 / field:field(s) {expr::Member::Field(field)}123 pub rule objinside(s: &ParserSettings) -> expr::ObjBody124 = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {125 let mut compspecs = vec![CompSpec::ForSpec(forspec)];126 compspecs.extend(others.unwrap_or_default());127 expr::ObjBody::ObjComp(expr::ObjComp{128 pre_locals,129 key,130 value,131 post_locals,132 compspecs,133 })134 }135 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}136 pub rule ifspec(s: &ParserSettings) -> IfSpecData137 = keyword("if") _ expr:expr(s) {IfSpecData(expr)}138 pub rule forspec(s: &ParserSettings) -> ForSpecData139 = keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}140 pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>141 = s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}142 pub rule local_expr(s: &ParserSettings) -> LocExpr143 = l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)144 pub rule string_expr(s: &ParserSettings) -> LocExpr145 = l(s, <s:string() {Expr::Str(s.into())}>)146 pub rule obj_expr(s: &ParserSettings) -> LocExpr147 = l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)148 pub rule array_expr(s: &ParserSettings) -> LocExpr149 = l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)150 pub rule array_comp_expr(s: &ParserSettings) -> LocExpr151 = l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {152 let mut specs = vec![CompSpec::ForSpec(forspec)];153 specs.extend(others.unwrap_or_default());154 Expr::ArrComp(expr, specs)155 }>)156 pub rule number_expr(s: &ParserSettings) -> LocExpr157 = l(s,<n:number() { expr::Expr::Num(n) }>)158 pub rule var_expr(s: &ParserSettings) -> LocExpr159 = l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)160 pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr161 = l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{162 cond,163 cond_then,164 cond_else,165 }}>)166167 pub rule literal(s: &ParserSettings) -> LocExpr168 = l(s,<v:(169 keyword("null") {LiteralType::Null}170 / keyword("true") {LiteralType::True}171 / keyword("false") {LiteralType::False}172 / keyword("self") {LiteralType::This}173 / keyword("$") {LiteralType::Dollar}174 / keyword("super") {LiteralType::Super}175 ) {Expr::Literal(v)}>)176177 pub rule expr_basic(s: &ParserSettings) -> LocExpr178 = literal(s)179180 / string_expr(s) / number_expr(s)181 / array_expr(s)182 / obj_expr(s)183 / array_expr(s)184 / array_comp_expr(s)185186 / l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)187 / l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)188189 / var_expr(s)190 / local_expr(s)191 / if_then_else_expr(s)192193 / l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)194 / l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)195196 / l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)197198 rule slice_part(s: &ParserSettings) -> Option<LocExpr>199 = e:(_ e:expr(s) _{e})? {e}200 pub rule slice_desc(s: &ParserSettings) -> SliceDesc201 = start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {202 let (end, step) = if let Some((end, step)) = pair {203 (end, step)204 }else{205 (None, None)206 };207208 SliceDesc { start, end, step }209 }210211 rule binop(x: rule<()>) -> ()212 = quiet!{ x() } / expected!("<binary op>")213 rule unaryop(x: rule<()>) -> ()214 = quiet!{ x() } / expected!("<unary op>")215216 rule expr(s: &ParserSettings) -> LocExpr217 = start:position!() a:precedence! {218 a:(@) _ binop(<"||">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Or, b))}219 --220 a:(@) _ binop(<"&&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::And, b))}221 --222 a:(@) _ binop(<"|">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitOr, b))}223 --224 a:@ _ binop(<"^">) _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitXor, b))}225 --226 a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::BitAnd, b))}227 --228 a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::Apply(229 el!(Expr::Intrinsic("equals".into())),230 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),231 true232 ))}233 a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, el!(Expr::Apply(234 el!(Expr::Intrinsic("equals".into())),235 ArgsDesc(vec![Arg(None, a), Arg(None, b)]),236 true237 ))))}238 --239 a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lt, b))}240 a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gt, b))}241 a:(@) _ binop(<"<=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lte, b))}242 a:(@) _ binop(<">=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Gte, b))}243 a:(@) _ binop(<keyword("in")>) _ b:@ {loc_expr_todo!(Expr::Apply(244 el!(Expr::Intrinsic("objectHasEx".into())), ArgsDesc(vec![Arg(None, b), Arg(None, a), Arg(None, el!(Expr::Literal(LiteralType::True)))]),245 true246 ))}247 --248 a:(@) _ binop(<"<<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Lhs, b))}249 a:(@) _ binop(<">>">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Rhs, b))}250 --251 a:(@) _ binop(<"+">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Add, b))}252 a:(@) _ binop(<"-">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Sub, b))}253 --254 a:(@) _ binop(<"*">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Mul, b))}255 a:(@) _ binop(<"/">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BinaryOpType::Div, b))}256 a:(@) _ binop(<"%">) _ b:@ {loc_expr_todo!(Expr::Apply(257 el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),258 false259 ))}260 --261 unaryop(<"-">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}262 unaryop(<"!">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}263 unaryop(<"~">) _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }264 --265 a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(266 el!(Expr::Intrinsic("slice".into())),267 ArgsDesc(vec![268 Arg(None, a),269 Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),270 Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),271 Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),272 ]),273 true,274 ))}275 a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}276 a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}277 a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}278 a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}279 --280 e:expr_basic(s) {e}281 "(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}282 } end:position!() {283 let LocExpr(e, _) = a;284 LocExpr(e, if s.loc_data {285 Some(ExprLocation(s.file_name.clone(), start, end))286 } else {287 None288 })289 }290 / e:expr_basic(s) {e}291292 pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}293 }294}295296pub type ParseError = peg::error::ParseError<peg::str::LineCol>;297pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {298 jsonnet_parser::jsonnet(str, settings)299}300301#[macro_export]302macro_rules! el {303 ($expr:expr) => {304 LocExpr(std::rc::Rc::new($expr), None)305 };306}307308#[cfg(test)]309pub mod tests {310 use super::{expr::*, parse};311 use crate::ParserSettings;312 use std::path::PathBuf;313 use std::rc::Rc;314315 macro_rules! parse {316 ($s:expr) => {317 parse(318 $s,319 &ParserSettings {320 loc_data: false,321 file_name: Rc::new(PathBuf::from("/test.jsonnet")),322 },323 )324 .unwrap()325 };326 }327328 mod expressions {329 use super::*;330331 pub fn basic_math() -> LocExpr {332 el!(Expr::BinaryOp(333 el!(Expr::Num(2.0)),334 BinaryOpType::Add,335 el!(Expr::BinaryOp(336 el!(Expr::Num(2.0)),337 BinaryOpType::Mul,338 el!(Expr::Num(2.0)),339 )),340 ))341 }342 }343344 #[test]345 fn multiline_string() {346 assert_eq!(347 parse!("|||\n Hello world!\n a\n|||"),348 el!(Expr::Str("Hello world!\n a\n".into())),349 );350 assert_eq!(351 parse!("|||\n Hello world!\n a\n|||"),352 el!(Expr::Str("Hello world!\n a\n".into())),353 );354 assert_eq!(355 parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),356 el!(Expr::Str("Hello world!\n\ta\n".into())),357 );358 assert_eq!(359 parse!("|||\n Hello world!\n a\n |||"),360 el!(Expr::Str("Hello world!\n a\n".into())),361 );362 }363364 #[test]365 fn slice() {366 parse!("a[1:]");367 parse!("a[1::]");368 parse!("a[:1:]");369 parse!("a[::1]");370 parse!("str[:len - 1]");371 }372373 #[test]374 fn string_escaping() {375 assert_eq!(376 parse!(r#""Hello, \"world\"!""#),377 el!(Expr::Str(r#"Hello, "world"!"#.into())),378 );379 assert_eq!(380 parse!(r#"'Hello \'world\'!'"#),381 el!(Expr::Str("Hello 'world'!".into())),382 );383 assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);384 }385386 #[test]387 fn string_unescaping() {388 assert_eq!(389 parse!(r#""Hello\nWorld""#),390 el!(Expr::Str("Hello\nWorld".into())),391 );392 }393394 #[test]395 fn string_verbantim() {396 assert_eq!(397 parse!(r#"@"Hello\n""World""""#),398 el!(Expr::Str("Hello\\n\"World\"".into())),399 );400 }401402 #[test]403 fn imports() {404 assert_eq!(405 parse!("import \"hello\""),406 el!(Expr::Import(PathBuf::from("hello"))),407 );408 assert_eq!(409 parse!("importstr \"garnish.txt\""),410 el!(Expr::ImportStr(PathBuf::from("garnish.txt")))411 );412 }413414 #[test]415 fn empty_object() {416 assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));417 }418419 #[test]420 fn basic_math() {421 assert_eq!(422 parse!("2+2*2"),423 el!(Expr::BinaryOp(424 el!(Expr::Num(2.0)),425 BinaryOpType::Add,426 el!(Expr::BinaryOp(427 el!(Expr::Num(2.0)),428 BinaryOpType::Mul,429 el!(Expr::Num(2.0))430 ))431 ))432 );433 }434435 #[test]436 fn basic_math_with_indents() {437 assert_eq!(parse!("2 + 2 * 2 "), expressions::basic_math());438 }439440 #[test]441 fn basic_math_parened() {442 assert_eq!(443 parse!("2+(2+2*2)"),444 el!(Expr::BinaryOp(445 el!(Expr::Num(2.0)),446 BinaryOpType::Add,447 el!(Expr::Parened(expressions::basic_math())),448 ))449 );450 }451452 /// Comments should not affect parsing453 #[test]454 fn comments() {455 assert_eq!(456 parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),457 el!(Expr::BinaryOp(458 el!(Expr::Num(2.0)),459 BinaryOpType::Add,460 el!(Expr::BinaryOp(461 el!(Expr::Num(3.0)),462 BinaryOpType::Mul,463 el!(Expr::Num(4.0))464 ))465 ))466 );467 }468469 /// Comments should be able to be escaped470 #[test]471 fn comment_escaping() {472 assert_eq!(473 parse!("2/*\\*/+*/ - 22"),474 el!(Expr::BinaryOp(475 el!(Expr::Num(2.0)),476 BinaryOpType::Sub,477 el!(Expr::Num(22.0))478 ))479 );480 }481482 #[test]483 fn suffix() {484 // assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));485 // assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));486 // assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));487 // assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))488 }489490 #[test]491 fn array_comp() {492 use Expr::*;493 assert_eq!(494 parse!("[std.deepJoin(x) for x in arr]"),495 el!(ArrComp(496 el!(Apply(497 el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),498 ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),499 false,500 )),501 vec![CompSpec::ForSpec(ForSpecData(502 "x".into(),503 el!(Var("arr".into()))504 ))]505 )),506 )507 }508509 #[test]510 fn reserved() {511 use Expr::*;512 assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));513 assert_eq!(parse!("nulla"), el!(Var("nulla".into())));514 }515516 #[test]517 fn multiple_args_buf() {518 parse!("a(b, null_fields)");519 }520521 #[test]522 fn infix_precedence() {523 use Expr::*;524 assert_eq!(525 parse!("!a && !b"),526 el!(BinaryOp(527 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),528 BinaryOpType::And,529 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))530 ))531 );532 }533534 #[test]535 fn infix_precedence_division() {536 use Expr::*;537 assert_eq!(538 parse!("!a / !b"),539 el!(BinaryOp(540 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),541 BinaryOpType::Div,542 el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))543 ))544 );545 }546547 #[test]548 fn double_negation() {549 use Expr::*;550 assert_eq!(551 parse!("!!a"),552 el!(UnaryOp(553 UnaryOpType::Not,554 el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))555 ))556 )557 }558559 #[test]560 fn array_test_error() {561 parse!("[a for a in b if c for e in f]");562 // ^^^^ failed code563 }564565 #[test]566 fn can_parse_stdlib() {567 parse!(jrsonnet_stdlib::STDLIB_STR);568 }569570 // From source code571 /*572 #[bench]573 fn bench_parse_peg(b: &mut Bencher) {574 b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))575 }576 */577}