difftreelog
refactor virtual file handling
in: master
4 files changed
crates/jrsonnet-parser/Cargo.tomldiffbeforeafterboth111112[dependencies]12[dependencies]13jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }13jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }14static_assertions = "1.1.0"141515peg = "0.8.0"16peg = "0.8.0"1617crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth1use std::{1use std::{2 fmt::{Debug, Display},2 fmt::{self, Debug, Display},3 ops::Deref,3 ops::Deref,4 path::{Path, PathBuf},5 rc::Rc,4 rc::Rc,6};5};7610#[cfg(feature = "serde")]9#[cfg(feature = "serde")]11use serde::{Deserialize, Serialize};10use serde::{Deserialize, Serialize};1112use crate::source::Source;121313#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]14#[derive(Debug, PartialEq, Trace)]15#[derive(Debug, PartialEq, Trace)]68}69}697070impl Display for UnaryOpType {71impl Display for UnaryOpType {71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {72 use UnaryOpType::*;73 use UnaryOpType::*;73 write!(74 write!(74 f,75 f,118}119}119120120impl Display for BinaryOpType {121impl Display for BinaryOpType {121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {122 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {122 use BinaryOpType::*;123 use BinaryOpType::*;123 write!(124 write!(124 f,125 f,326 LocalExpr(Vec<BindSpec>, LocExpr),327 LocalExpr(Vec<BindSpec>, LocExpr),327328328 /// import "hello"329 /// import "hello"329 Import(PathBuf),330 Import(IStr),330 /// importStr "file.txt"331 /// importStr "file.txt"331 ImportStr(PathBuf),332 ImportStr(IStr),332 /// importBin "file.txt"333 /// importBin "file.txt"333 ImportBin(PathBuf),334 ImportBin(IStr),334 /// error "I'm broken"335 /// error "I'm broken"335 ErrorStmt(LocExpr),336 ErrorStmt(LocExpr),336 /// a(b, c)337 /// a(b, c)358#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]359#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]359#[derive(Clone, PartialEq, Trace)]360#[derive(Clone, PartialEq, Trace)]360#[skip_trace]361#[skip_trace]362#[repr(C)]361pub struct ExprLocation(pub Rc<Path>, pub usize, pub usize);363pub struct ExprLocation(pub Source, pub u32, pub u32);362impl ExprLocation {364impl ExprLocation {363 pub fn belongs_to(&self, other: &ExprLocation) -> bool {365 pub fn belongs_to(&self, other: &ExprLocation) -> bool {364 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2366 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2365 }367 }366}368}369370#[cfg(target_pointer_width = "64")]371static_assertions::assert_eq_size!(ExprLocation, [u8; 16]);367372368impl Debug for ExprLocation {373impl Debug for ExprLocation {369 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {374 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {370 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)375 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)371 }376 }372}377}376#[derive(Clone, PartialEq, Trace)]381#[derive(Clone, PartialEq, Trace)]377pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);382pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);383384#[cfg(target_pointer_width = "64")]385static_assertions::assert_eq_size!(LocExpr, [u8; 24]);378386379impl Debug for LocExpr {387impl Debug for LocExpr {380 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {388 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {381 if f.alternate() {389 if f.alternate() {382 write!(f, "{:#?}", self.0)?;390 write!(f, "{:#?}", self.0)?;383 } else {391 } else {crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth1#![allow(clippy::redundant_closure_call)]1#![allow(clippy::redundant_closure_call)]223use std::{3use std::rc::Rc;4 path::{Path, PathBuf},5 rc::Rc,6};748use peg::parser;5use peg::parser;9mod expr;6mod expr;10pub use expr::*;7pub use expr::*;11pub use jrsonnet_interner::IStr;8pub use jrsonnet_interner::IStr;12pub use peg;9pub use peg;10mod source;13mod unescape;11mod unescape;12pub use source::Source;141315pub struct ParserSettings {14pub struct ParserSettings {16 pub file_name: Rc<Path>,15 pub file_name: Source,17}16}181719macro_rules! expr_bin {18macro_rules! expr_bin {232 pub rule var_expr(s: &ParserSettings) -> Expr231 pub rule var_expr(s: &ParserSettings) -> Expr233 = n:id() { expr::Expr::Var(n) }232 = n:id() { expr::Expr::Var(n) }234 pub rule id_loc(s: &ParserSettings) -> LocExpr233 pub rule id_loc(s: &ParserSettings) -> LocExpr235 = a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.file_name.clone(), a,b)) }234 = a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.file_name.clone(), a as u32,b as u32)) }236 pub rule if_then_else_expr(s: &ParserSettings) -> Expr235 pub rule if_then_else_expr(s: &ParserSettings) -> Expr237 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{236 = cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{238 cond,237 cond,263 / array_expr(s)262 / array_expr(s)264 / array_comp_expr(s)263 / array_comp_expr(s)265264266 / keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}265 / keyword("importstr") _ path:string() {Expr::ImportStr(path.into())}267 / keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}266 / keyword("importbin") _ path:string() {Expr::ImportBin(path.into())}268 / keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}267 / keyword("import") _ path:string() {Expr::Import(path.into())}269268270 / var_expr(s)269 / var_expr(s)271 / local_expr(s)270 / local_expr(s)299 use UnaryOpType::*;298 use UnaryOpType::*;300 rule expr(s: &ParserSettings) -> LocExpr299 rule expr(s: &ParserSettings) -> LocExpr301 = precedence! {300 = precedence! {302 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start, end)) }301 start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start as u32, end as u32)) }303 --302 --304 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}303 a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}305 --304 --357 let len = str.len();356 let len = str.len();358 LocExpr(357 LocExpr(359 Rc::new(Expr::Str(str)),358 Rc::new(Expr::Str(str)),360 ExprLocation(settings.file_name.clone(), 0, len),359 ExprLocation(settings.file_name.clone(), 0, len as u32),361 )360 )362}361}363362368 use BinaryOpType::*;367 use BinaryOpType::*;369368370 use super::{expr::*, parse};369 use super::{expr::*, parse};371 use crate::ParserSettings;370 use crate::{source::Source, ParserSettings};372371373 macro_rules! parse {372 macro_rules! parse {374 ($s:expr) => {373 ($s:expr) => {375 parse(374 parse(376 $s,375 $s,377 &ParserSettings {376 &ParserSettings {378 file_name: PathBuf::from("test.jsonnet").into(),377 file_name: Source::new(PathBuf::from("test.jsonnet")).unwrap(),379 },378 },380 )379 )381 .unwrap()380 .unwrap()386 ($expr:expr, $from:expr, $to:expr$(,)?) => {385 ($expr:expr, $from:expr, $to:expr$(,)?) => {387 LocExpr(386 LocExpr(388 std::rc::Rc::new($expr),387 std::rc::Rc::new($expr),389 ExprLocation(PathBuf::from("test.jsonnet").into(), $from, $to),388 ExprLocation(389 Source::new(PathBuf::from("test.jsonnet")).unwrap(),390 $from,391 $to,392 ),390 )393 )391 };394 };453 fn imports() {456 fn imports() {454 assert_eq!(457 assert_eq!(455 parse!("import \"hello\""),458 parse!("import \"hello\""),456 el!(Expr::Import(PathBuf::from("hello")), 0, 14),459 el!(Expr::Import("hello".into()), 0, 14),457 );460 );458 assert_eq!(461 assert_eq!(459 parse!("importstr \"garnish.txt\""),462 parse!("importstr \"garnish.txt\""),460 el!(Expr::ImportStr(PathBuf::from("garnish.txt")), 0, 23)463 el!(Expr::ImportStr("garnish.txt".into()), 0, 23)461 );464 );465 assert_eq!(466 parse!("importbin \"garnish.bin\""),467 el!(Expr::ImportBin("garnish.bin".into()), 0, 23)468 );462 }469 }463470464 #[test]471 #[test]720 fn add_location_info_to_all_sub_expressions() {727 fn add_location_info_to_all_sub_expressions() {721 use Expr::*;728 use Expr::*;722729723 let file_name: std::rc::Rc<std::path::Path> = PathBuf::from("test.jsonnet").into();730 let file_name = Source::new(PathBuf::from("test.jsonnet")).unwrap();724 let expr = parse(731 let expr = parse(725 "{} { local x = 1, x: x } + {}",732 "{} { local x = 1, x: x } + {}",726 &ParserSettings {733 &ParserSettings {760 ),767 ),761 );768 );762 }769 }763 // From source code764 /*765 #[bench]766 fn bench_parse_peg(b: &mut Bencher) {767 b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))768 }769 */770}770}771771crates/jrsonnet-parser/src/source.rsdiffbeforeafterbothno changes