From 30ce98effbb6e373f74e408ef29d8d4f3c034e17 Mon Sep 17 00:00:00 2001 From: Лач Date: Sat, 06 Jun 2020 06:45:23 +0000 Subject: [PATCH] feat(parser): use PathBuf for file names --- --- a/crates/jsonnet-parser/src/expr.rs +++ b/crates/jsonnet-parser/src/expr.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use std::{fmt::Debug, rc::Rc}; +use std::{fmt::Debug, path::PathBuf, rc::Rc}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum FieldName { @@ -50,8 +50,7 @@ pub enum BinaryOpType { Mul, Div, - // Mod is desugared to std.mod - // Mod, + Add, Sub, @@ -64,10 +63,6 @@ Gte, In, - - // Eq/Ne is desugared to std.equals - // Eq, - // Ne, BitAnd, BitOr, @@ -120,7 +115,6 @@ key: LocExpr, value: LocExpr, post_locals: Vec, - first: ForSpecData, rest: Vec, }, } @@ -225,10 +219,10 @@ /// file, begin offset, end offset #[derive(Clone, PartialEq, Serialize, Deserialize)] -pub struct ExprLocation(pub String, pub usize, pub usize); +pub struct ExprLocation(pub PathBuf, pub usize, pub usize); impl Debug for ExprLocation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}:{:?}-{:?}", self.0, self.1, self.2) + write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2) } } --- a/crates/jsonnet-parser/src/lib.rs +++ b/crates/jsonnet-parser/src/lib.rs @@ -4,7 +4,7 @@ extern crate test; use peg::parser; -use std::rc::Rc; +use std::{path::PathBuf, rc::Rc}; mod expr; pub use expr::*; @@ -19,7 +19,7 @@ pub struct ParserSettings { pub loc_data: bool, - pub file_name: String, + pub file_name: PathBuf, } parser! { @@ -118,14 +118,13 @@ / assertion:assertion(s) {expr::Member::AssertStmt(assertion)} / field:field(s) {expr::Member::Field(field)} pub rule objinside(s: &ParserSettings) -> expr::ObjBody - = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ first:forspec(s) rest:(_ rest:compspec(s) {rest})? { + = 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})? { expr::ObjBody::ObjComp { pre_locals, key, value, post_locals, - first, - rest: rest.unwrap_or_default(), + rest: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(), } } / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)} @@ -301,6 +300,7 @@ pub mod tests { use super::{expr::*, parse}; use crate::ParserSettings; + use std::path::PathBuf; macro_rules! parse { ($s:expr) => { @@ -308,7 +308,7 @@ $s, &ParserSettings { loc_data: false, - file_name: "test.jsonnet".to_owned(), + file_name: PathBuf::from("/test.jsonnet"), }, ) .unwrap() -- gitstuff