difftreelog
refactor cleanup
in: master
9 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth45/// If this does not match `LIB_JSONNET_VERSION`45/// If this does not match `LIB_JSONNET_VERSION`46/// then there is a mismatch between header and compiled library.46/// then there is a mismatch between header and compiled library.47#[no_mangle]47#[no_mangle]48pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {48pub extern "C" fn jsonnet_version() -> &'static [u8; 12] {49 b"v0.20.0\0"49 b"v0.22.0-rc1\0"50}50}515152unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> {52unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> {crates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth4use jrsonnet_gcmodule::Acyclic;4use jrsonnet_gcmodule::Acyclic;5use jrsonnet_ir::visit::Visitor;5use jrsonnet_ir::visit::Visitor;6use jrsonnet_ir::{6use jrsonnet_ir::{IStr, Source, SourcePath};7 ArgsDesc, AssertExpr, AssertStmt, BindSpec, CompSpec, Destruct, Expr, ExprParam, ExprParams,8 FieldMember, FieldName, ForSpecData, IStr, IfElse, IfSpecData, ImportKind, ObjBody, Slice,9 SliceDesc, Source, SourcePath, Spanned,10};11use rustc_hash::FxHashMap;7use rustc_hash::FxHashMap;12823 self.0.push(Import {19 self.0.push(Import {24 path: ResolvePathOwned::Str(value.to_string()),20 path: ResolvePathOwned::Str(value.to_string()),25 expression,21 expression,26 })22 });27 }23 }28}24}2925crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth5extern crate self as jrsonnet_evaluator;5extern crate self as jrsonnet_evaluator;667mod arr;7mod arr;8// pub mod async_import;8pub mod async_import;9mod ctx;9mod ctx;10mod dynamic;10mod dynamic;11pub mod error;11pub mod error;crates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth7 fmt::{self, Debug},8 hash::{Hash, Hasher},9 num::Saturating,10 ops::ControlFlow,3};11};4125use educe::Educe;13use educe::Educe;crates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth3use jrsonnet_gcmodule::Acyclic;3use jrsonnet_gcmodule::Acyclic;4use jrsonnet_ir::{4use jrsonnet_ir::{5 unescape, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec,5 unescape, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec,6 Destruct, DestructRest, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr,6 Destruct, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse,7 IfElse, IfSpecData, ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers,7 IfSpecData, ImportKind, IndexPart, LiteralType, Member, ObjBody, ObjComp, ObjMembers, Slice,8 Slice, SliceDesc, Source, Span, Spanned, UnaryOpType, Visibility,8 SliceDesc, Source, Span, Spanned, UnaryOpType, Visibility,9};9};10use jrsonnet_lexer::{collect_lexed_str_block, Lexeme, Lexer, SyntaxKind, T};10use jrsonnet_lexer::{collect_lexed_str_block, Lexeme, Lexer, SyntaxKind, T};111130 }30 }31}31}323233type R<T> = Result<T, ParseError>;33type Result<T> = std::result::Result<T, ParseError>;343435struct Parser<'a> {35struct Parser<'a> {36 lexemes: Vec<Lexeme<'a>>,36 lexemes: Vec<Lexeme<'a>>,103 }103 }104 }104 }105105106 fn eat(&mut self, t: SyntaxKind) -> R<()> {106 fn eat(&mut self, t: SyntaxKind) -> Result<()> {107 if !self.at(t) {107 if !self.at(t) {108 return Err(self.error(format!(108 return Err(self.error(format!(109 "expected {}, got {}",109 "expected {}, got {}",138 }138 }139 }139 }140140141 fn expect_ident(&mut self) -> R<IStr> {141 fn expect_ident(&mut self) -> Result<IStr> {142 if !self.at(SyntaxKind::IDENT) {142 if !self.at(SyntaxKind::IDENT) {143 return Err(self.error(format!("expected identifier, got {}", self.current_desc())));143 return Err(self.error(format!("expected identifier, got {}", self.current_desc())));144 }144 }145 let text = self.text();145 let text = self.text();146 if is_reserved(text) {146 if is_reserved(text) {147 return Err(self.error(format!(147 return Err(self.error(format!("expected identifier, got reserved word '{text}'")));148 "expected identifier, got reserved word '{text}'"149 )));150 }148 }151 let s: IStr = text.into();149 let s: IStr = text.into();152 self.eat_any();150 self.eat_any();164 "assert"162 "assert"165 | "else" | "error"163 | "else" | "error"166 | "false" | "for"164 | "false" | "for"167 | "function" | "if"165 | "function"168 | "import" | "importstr"166 | "if" | "import"169 | "importbin" | "in"167 | "importstr"168 | "importbin"170 | "local" | "null"169 | "in" | "local"171 | "tailstrict" | "then"170 | "null" | "tailstrict"172 | "self" | "super"171 | "then" | "self"173 | "true"172 | "super" | "true"174 )173 )175}174}176175177fn spanned<T: Acyclic>(p: &mut Parser<'_>, cb: impl FnOnce(&mut Parser<'_>) -> R<T>) -> R<Spanned<T>> {176fn spanned<T: Acyclic>(177 p: &mut Parser<'_>,178 cb: impl FnOnce(&mut Parser<'_>) -> Result<T>,179) -> Result<Spanned<T>> {178 let start = p.span_start();180 let start = p.span_start();179 let v = cb(p)?;181 let v = cb(p)?;180 let end = p.span_end();182 let end = p.span_end();181 Ok(Spanned::new(v, Span(p.source.clone(), start, end)))183 Ok(Spanned::new(v, Span(p.source.clone(), start, end)))182}184}183185184fn parse_string_content(p: &mut Parser<'_>) -> R<IStr> {186fn parse_string_content(p: &mut Parser<'_>) -> Result<IStr> {185 let kind = p.peek();187 let kind = p.peek();186 let text = p.text();188 let text = p.text();187 let s = match kind {189 let s = match kind {188 SyntaxKind::STRING_DOUBLE => {190 SyntaxKind::STRING_DOUBLE => {189 let inner = &text[1..text.len() - 1];191 let inner = &text[1..text.len() - 1];190 unescape::unescape(inner)192 unescape::unescape(inner).ok_or_else(|| p.error("invalid string escape".into()))?191 .ok_or_else(|| p.error("invalid string escape".into()))?192 }193 }193 SyntaxKind::STRING_SINGLE => {194 SyntaxKind::STRING_SINGLE => {194 let inner = &text[1..text.len() - 1];195 let inner = &text[1..text.len() - 1];195 unescape::unescape(inner)196 unescape::unescape(inner).ok_or_else(|| p.error("invalid string escape".into()))?196 .ok_or_else(|| p.error("invalid string escape".into()))?197 }197 }198 SyntaxKind::STRING_DOUBLE_VERBATIM => {198 SyntaxKind::STRING_DOUBLE_VERBATIM => {199 let inner = &text[2..text.len() - 1];199 let inner = &text[2..text.len() - 1];236 )236 )237}237}238238239fn parse_number(p: &mut Parser<'_>) -> R<f64> {239fn parse_number(p: &mut Parser<'_>) -> Result<f64> {240 let text = p.text();240 let text = p.text();241 let n: f64 = text241 let n: f64 = text242 .replace('_', "")242 .replace('_', "")263 Some(t)263 Some(t)264}264}265265266fn assert_stmt(p: &mut Parser<'_>) -> R<AssertStmt> {266fn assert_stmt(p: &mut Parser<'_>) -> Result<AssertStmt> {267 p.eat(T![assert])?;267 p.eat(T![assert])?;268 let cond = spanned(p, expr)?;268 let cond = spanned(p, expr)?;269 let msg = if p.try_eat(T![:]) {269 let msg = if p.try_eat(T![:]) {274 Ok(AssertStmt(cond, msg))274 Ok(AssertStmt(cond, msg))275}275}276276277fn if_spec_data(p: &mut Parser<'_>) -> R<IfSpecData> {277fn if_spec_data(p: &mut Parser<'_>) -> Result<IfSpecData> {278 let v = spanned(p, |p| p.eat(T![if]))?;278 let v = spanned(p, |p| p.eat(T![if]))?;279 let cond = expr(p)?;279 let cond = expr(p)?;280 Ok(IfSpecData { span: v.span, cond })280 Ok(IfSpecData { span: v.span, cond })281}281}282282283fn if_else(p: &mut Parser<'_>) -> R<IfElse> {283fn if_else(p: &mut Parser<'_>) -> Result<IfElse> {284 let cond = if_spec_data(p)?;284 let cond = if_spec_data(p)?;285 p.eat(T![then])?;285 p.eat(T![then])?;286 let cond_then = expr(p)?;286 let cond_then = expr(p)?;296 })296 })297}297}298298299fn slice_desc(p: &mut Parser<'_>, start: Option<Spanned<Expr>>) -> R<SliceDesc> {299fn slice_desc(p: &mut Parser<'_>, start: Option<Spanned<Expr>>) -> Result<SliceDesc> {300 p.eat(T![:])?;300 p.eat(T![:])?;301 let end = if !p.at(T![:]) && !p.at(T![']']) {301 let end = if !p.at(T![:]) && !p.at(T![']']) {302 Some(spanned(p, expr)?)302 Some(spanned(p, expr)?)315 Ok(SliceDesc { start, end, step })315 Ok(SliceDesc { start, end, step })316}316}317317318fn destruct(p: &mut Parser<'_>) -> R<Destruct> {318fn destruct(p: &mut Parser<'_>) -> Result<Destruct> {319 if p.at_ident() {319 if p.at_ident() {320 return Ok(Destruct::Full(p.expect_ident()?));320 return Ok(Destruct::Full(p.expect_ident()?));321 }321 }322 #[cfg(not(feature = "exp-destruct"))]322 #[cfg(not(feature = "exp-destruct"))]323 return Err(p.error(format!(323 return Err(p.error(format!("expected identifier, got {}", p.current_desc())));324 "expected identifier, got {}",325 p.current_desc()326 )));327 #[cfg(feature = "exp-destruct")]324 #[cfg(feature = "exp-destruct")]328 {325 {329 if p.try_eat(T![?]) {326 if p.try_eat(T![?]) {343}340}344341345#[cfg(feature = "exp-destruct")]342#[cfg(feature = "exp-destruct")]346fn destruct_rest(p: &mut Parser<'_>) -> R<DestructRest> {343fn destruct_rest(p: &mut Parser<'_>) -> Result<jrsonnet_ir::DestructRest> {347 p.eat(T![...])?;344 p.eat(T![...])?;348 if p.at_ident() {345 if p.at_ident() {349 Ok(DestructRest::Keep(p.expect_ident()?))346 Ok(jrsonnet_ir::DestructRest::Keep(p.expect_ident()?))350 } else {347 } else {351 Ok(DestructRest::Drop)348 Ok(jrsonnet_ir::DestructRest::Drop)352 }349 }353}350}354351355#[cfg(feature = "exp-destruct")]352#[cfg(feature = "exp-destruct")]356fn destruct_array(p: &mut Parser<'_>) -> R<Destruct> {353fn destruct_array(p: &mut Parser<'_>) -> Result<Destruct> {357 p.eat(T!['['])?;354 p.eat(T!['['])?;358 let mut start = Vec::new();355 let mut start = Vec::new();359 let mut rest = None;356 let mut rest = None;391}388}392389393#[cfg(feature = "exp-destruct")]390#[cfg(feature = "exp-destruct")]394fn destruct_object(p: &mut Parser<'_>) -> R<Destruct> {391fn destruct_object(p: &mut Parser<'_>) -> Result<Destruct> {395 p.eat(T!['{'])?;392 p.eat(T!['{'])?;396 let mut fields = Vec::new();393 let mut fields = Vec::new();397 let mut rest = None;394 let mut rest = None;426 Ok(Destruct::Object { fields, rest })423 Ok(Destruct::Object { fields, rest })427}424}428425429fn params(p: &mut Parser<'_>) -> R<ExprParams> {426fn params(p: &mut Parser<'_>) -> Result<ExprParams> {430 if p.at(T![')']) {427 if p.at(T![')']) {431 return Ok(ExprParams::new(Vec::new()));428 return Ok(ExprParams::new(Vec::new()));432 }429 }452 Ok(ExprParams::new(result))449 Ok(ExprParams::new(result))453}450}454451455fn args(p: &mut Parser<'_>) -> R<ArgsDesc> {452fn args(p: &mut Parser<'_>) -> Result<ArgsDesc> {456 if p.at(T![')']) {453 if p.at(T![')']) {457 return Ok(ArgsDesc::new(Vec::new(), Vec::new()));454 return Ok(ArgsDesc::new(Vec::new(), Vec::new()));458 }455 }489 Ok(ArgsDesc::new(unnamed, named))486 Ok(ArgsDesc::new(unnamed, named))490}487}491488492fn bind(p: &mut Parser<'_>) -> R<BindSpec> {489fn bind(p: &mut Parser<'_>) -> Result<BindSpec> {493 #[cfg(feature = "exp-destruct")]490 #[cfg(feature = "exp-destruct")]494 {491 {495 if !p.at_ident() {492 if !p.at_ident() {520 }517 }521}518}522519523fn visibility(p: &mut Parser<'_>) -> R<Visibility> {520fn visibility(p: &mut Parser<'_>) -> Result<Visibility> {524 p.eat(T![:])?;521 p.eat(T![:])?;525 if p.try_eat(T![:]) {522 if p.try_eat(T![:]) {526 if p.try_eat(T![:]) {523 if p.try_eat(T![:]) {533 }530 }534}531}535532536fn field_name(p: &mut Parser<'_>) -> R<FieldName> {533fn field_name(p: &mut Parser<'_>) -> Result<FieldName> {537 if p.at_ident() {534 if p.at_ident() {538 Ok(FieldName::Fixed(p.expect_ident()?))535 Ok(FieldName::Fixed(p.expect_ident()?))539 } else if is_string_token(p.peek()) {536 } else if is_string_token(p.peek()) {548 }545 }549}546}550547551fn field(p: &mut Parser<'_>) -> R<FieldMember> {548fn field(p: &mut Parser<'_>) -> Result<FieldMember> {552 let name = spanned(p, field_name)?;549 let name = spanned(p, field_name)?;553550554 if p.at(T!['(']) {551 if p.at(T!['(']) {578 }575 }579}576}580577581fn member(p: &mut Parser<'_>) -> R<Member> {578fn member(p: &mut Parser<'_>) -> Result<Member> {582 if p.at(T![local]) {579 if p.at(T![local]) {583 p.eat(T![local])?;580 p.eat(T![local])?;584 Ok(Member::BindStmt(bind(p)?))581 Ok(Member::BindStmt(bind(p)?))589 }586 }590}587}591588592fn for_spec(p: &mut Parser<'_>) -> R<ForSpecData> {589fn for_spec(p: &mut Parser<'_>) -> Result<ForSpecData> {593 p.eat(T![for])?;590 p.eat(T![for])?;594 let d = destruct(p)?;591 let d = destruct(p)?;595 p.eat(T![in])?;592 p.eat(T![in])?;596 let over = expr(p)?;593 let over = expr(p)?;597 Ok(ForSpecData { destruct: d, over })594 Ok(ForSpecData { destruct: d, over })598}595}599596600fn compspecs(p: &mut Parser<'_>) -> R<Vec<CompSpec>> {597fn compspecs(p: &mut Parser<'_>) -> Result<Vec<CompSpec>> {601 let mut specs = Vec::new();598 let mut specs = Vec::new();602 specs.push(CompSpec::ForSpec(for_spec(p)?));599 specs.push(CompSpec::ForSpec(for_spec(p)?));603 loop {600 loop {613 Ok(specs)610 Ok(specs)614}611}615612616fn objinside(p: &mut Parser<'_>) -> R<ObjBody> {613fn objinside(p: &mut Parser<'_>) -> Result<ObjBody> {617 if p.at(T!['}']) {614 if p.at(T!['}']) {618 return Ok(ObjBody::MemberList(ObjMembers {615 return Ok(ObjBody::MemberList(ObjMembers {619 locals: Rc::new(Vec::new()),616 locals: Rc::new(Vec::new()),641 match m {638 match m {642 Member::Field(f) => {639 Member::Field(f) => {643 if field_member.is_some() {640 if field_member.is_some() {644 return Err(p.error(641 return Err(645 "object comprehension can only contain one field".into(),642 p.error("object comprehension can only contain one field".into())646 ));643 );647 }644 }648 field_member = Some(f);645 field_member = Some(f);649 }646 }650 Member::BindStmt(b) => locals.push(b),647 Member::BindStmt(b) => locals.push(b),651 Member::AssertStmt(_) => {648 Member::AssertStmt(_) => {652 return Err(p.error(649 return Err(p.error("asserts are unsupported in object comprehension".into()));653 "asserts are unsupported in object comprehension".into(),654 ));655 }650 }656 }651 }657 }652 }658 Ok(ObjBody::ObjComp(ObjComp {653 Ok(ObjBody::ObjComp(ObjComp {659 locals: Rc::new(locals),654 locals: Rc::new(locals),660 field: Rc::new(655 field: Rc::new(661 field_member.ok_or_else(|| {656 field_member.ok_or_else(|| p.error("missing object comprehension field".into()))?,662 p.error("missing object comprehension field".into())663 })?,664 ),657 ),665 compspecs: specs,658 compspecs: specs,666 }))659 }))683 }676 }684}677}685678686fn expr_basic(p: &mut Parser<'_>) -> R<Expr> {679fn expr_basic(p: &mut Parser<'_>) -> Result<Expr> {687 if let Some(lit) = literal(p) {680 if let Some(lit) = literal(p) {688 return Ok(Expr::Literal(lit));681 return Ok(Expr::Literal(lit));689 }682 }825 }818 }826}819}827820828/// Flush accumulated index parts into an Expr::Index wrapping `e`.829fn flush_index_parts(e: &mut Expr, parts: &mut Vec<IndexPart>) {821fn flush_index_parts(e: &mut Expr, parts: &mut Vec<IndexPart>) {830 if parts.is_empty() {822 if parts.is_empty() {831 return;823 return;837 };829 };838}830}839831840fn expr_suffix(p: &mut Parser<'_>) -> R<Expr> {832fn expr_suffix(p: &mut Parser<'_>) -> Result<Expr> {841 let mut e = expr_basic(p)?;833 let mut e = expr_basic(p)?;842 // Accumulate consecutive index parts (.field, [expr], ?.field, ?.[expr])834 // Accumulate consecutive index parts (.field, [expr], ?.field, ?.[expr])843 // into a single Expr::Index. This is critical for null-coalesce semantics:835 // into a single Expr::Index. This is critical for null-coalesce semantics:1006 }998 }1007}999}100810001009fn expr_bp(p: &mut Parser<'_>, min_bp: u8) -> R<Expr> {1001fn expr_bp(p: &mut Parser<'_>, min_bp: u8) -> Result<Expr> {1010 let mut lhs = if let Some(op) = unary_op(p.peek()) {1002 let mut lhs = if let Some(op) = unary_op(p.peek()) {1011 p.eat_any();1003 p.eat_any();1012 let rbp = prefix_binding_power(op);1004 let rbp = prefix_binding_power(op);1038 Ok(lhs)1030 Ok(lhs)1039}1031}104010321041fn expr(p: &mut Parser<'_>) -> R<Expr> {1033fn expr(p: &mut Parser<'_>) -> Result<Expr> {1042 expr_bp(p, 0)1034 expr_bp(p, 0)1043}1035}104410361045pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr, ParseError> {1037pub fn parse(str: &str, settings: &ParserSettings) -> Result<Expr> {1046 let mut p = Parser::new(str, settings.source.clone());1038 let mut p = Parser::new(str, settings.source.clone());1047 for lexeme in &p.lexemes {1039 for lexeme in &p.lexemes {1048 if let Some(desc) = lexeme.kind.error_description() {1040 if let Some(desc) = lexeme.kind.error_description() {1056 }1048 }1057 let e = expr(&mut p)?;1049 let e = expr(&mut p)?;1058 if !p.at_eof() {1050 if !p.at_eof() {1059 return Err(p.error(format!(1051 return Err(p.error(format!("expected end of file, got {}", p.current_desc(),)));1060 "expected end of file, got {}",1061 p.current_desc(),1062 )));1063 }1052 }1064 Ok(e)1053 Ok(e)1065}1054}106610551067pub fn string_to_expr(s: IStr, settings: &ParserSettings) -> Spanned<Expr> {1056pub fn string_to_expr(s: IStr, settings: &ParserSettings) -> Spanned<Expr> {1068 let len = s.len();1057 let len = s.len();1069 Spanned::new(1058 Spanned::new(Expr::Str(s), Span(settings.source.clone(), 0, len as u32))1070 Expr::Str(s),1071 Span(settings.source.clone(), 0, len as u32),1072 )1073}1059}107410601075#[cfg(test)]1061#[cfg(test)]crates/jrsonnet-ir/src/visit.rsdiffbeforeafterboth21 }21 }22}22}232324#[allow(unused_variables, reason = "used with exp-destruct")]24pub fn visit_destruct<V: Visitor>(v: &mut V, destruct: &Destruct) {25pub fn visit_destruct<V: Visitor>(v: &mut V, destruct: &Destruct) {25 match destruct {26 match destruct {26 Destruct::Full(_istr) => {}27 Destruct::Full(_istr) => {}crates/jrsonnet-rowan-parser/src/lex.rsdiffbeforeafterbothno syntactic changes
crates/jrsonnet-stdlib/src/manifest/ini.rsdiffbeforeafterboth3use jrsonnet_evaluator::{3use jrsonnet_evaluator::{4 manifest::{ManifestFormat, ToStringFormat},4 manifest::{ManifestFormat, ToStringFormat},5 typed::{FromUntyped, Typed},5 typed::{FromUntyped, Typed},6 ObjValue, Result, ResultExt, Val,6 IStr, ObjValue, Result, ResultExt, Val,7 IStr,8};7};9810pub struct IniFormat {9pub struct IniFormat {xtask/src/sourcegen/kinds.rsdiffbeforeafterboth124 | "STRING_SINGLE_VERBATIM" | "STRING_BLOCK" => "string".to_owned(),126 | "STRING_SINGLE_VERBATIM"127 | "STRING_BLOCK" => "string".to_owned(),125 "WHITESPACE" => "whitespace".to_owned(),128 "WHITESPACE" => "whitespace".to_owned(),126 "SINGLE_LINE_SLASH_COMMENT" | "SINGLE_LINE_HASH_COMMENT"129 "SINGLE_LINE_SLASH_COMMENT" | "SINGLE_LINE_HASH_COMMENT" | "MULTI_LINE_COMMENT" => {127 | "MULTI_LINE_COMMENT" => "comment".to_owned(),130 "comment".to_owned()131 }128 _ => name.to_lowercase(),132 _ => name.to_lowercase(),129 },133 },130 Self::Meta { name, .. } => name.to_lowercase(),134 Self::Meta { name, .. } => name.to_lowercase(),