git.delta.rocks / jrsonnet / refs/commits / 30ce98effbb6

difftreelog

feat(parser) use PathBuf for file names

Лач2020-06-06parent: #e53de5d.patch.diff
in: master

2 files changed

modifiedcrates/jsonnet-parser/src/expr.rsdiffbeforeafterboth
1use serde::{Deserialize, Serialize};1use serde::{Deserialize, Serialize};
2use std::{fmt::Debug, rc::Rc};2use std::{fmt::Debug, path::PathBuf, rc::Rc};
33
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub enum FieldName {5pub enum FieldName {
50pub enum BinaryOpType {50pub enum BinaryOpType {
51 Mul,51 Mul,
52 Div,52 Div,
53 // Mod is desugared to std.mod53
54 // Mod,
55 Add,54 Add,
56 Sub,55 Sub,
5756
6564
66 In,65 In,
67
68 // Eq/Ne is desugared to std.equals
69 // Eq,
70 // Ne,
7166
72 BitAnd,67 BitAnd,
73 BitOr,68 BitOr,
120 key: LocExpr,115 key: LocExpr,
121 value: LocExpr,116 value: LocExpr,
122 post_locals: Vec<BindSpec>,117 post_locals: Vec<BindSpec>,
123 first: ForSpecData,
124 rest: Vec<CompSpec>,118 rest: Vec<CompSpec>,
125 },119 },
126}120}
225219
226/// file, begin offset, end offset220/// file, begin offset, end offset
227#[derive(Clone, PartialEq, Serialize, Deserialize)]221#[derive(Clone, PartialEq, Serialize, Deserialize)]
228pub struct ExprLocation(pub String, pub usize, pub usize);222pub struct ExprLocation(pub PathBuf, pub usize, pub usize);
229impl Debug for ExprLocation {223impl Debug for ExprLocation {
230 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {224 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231 write!(f, "{}:{:?}-{:?}", self.0, self.1, self.2)225 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)
232 }226 }
233}227}
234228
modifiedcrates/jsonnet-parser/src/lib.rsdiffbeforeafterboth
4extern crate test;4extern crate test;
55
6use peg::parser;6use peg::parser;
7use std::rc::Rc;7use std::{path::PathBuf, rc::Rc};
8mod expr;8mod expr;
9pub use expr::*;9pub use expr::*;
1010
1919
20pub struct ParserSettings {20pub struct ParserSettings {
21 pub loc_data: bool,21 pub loc_data: bool,
22 pub file_name: String,22 pub file_name: PathBuf,
23}23}
2424
25parser! {25parser! {
118 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}118 / assertion:assertion(s) {expr::Member::AssertStmt(assertion)}
119 / field:field(s) {expr::Member::Field(field)}119 / field:field(s) {expr::Member::Field(field)}
120 pub rule objinside(s: &ParserSettings) -> expr::ObjBody120 pub rule objinside(s: &ParserSettings) -> expr::ObjBody
121 = 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})? {121 = 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})? {
122 expr::ObjBody::ObjComp {122 expr::ObjBody::ObjComp {
123 pre_locals,123 pre_locals,
124 key,124 key,
125 value,125 value,
126 post_locals,126 post_locals,
127 first,
128 rest: rest.unwrap_or_default(),127 rest: [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat(),
129 }128 }
130 }129 }
131 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}130 / members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}
301pub mod tests {300pub mod tests {
302 use super::{expr::*, parse};301 use super::{expr::*, parse};
303 use crate::ParserSettings;302 use crate::ParserSettings;
303 use std::path::PathBuf;
304304
305 macro_rules! parse {305 macro_rules! parse {
306 ($s:expr) => {306 ($s:expr) => {
307 parse(307 parse(
308 $s,308 $s,
309 &ParserSettings {309 &ParserSettings {
310 loc_data: false,310 loc_data: false,
311 file_name: "test.jsonnet".to_owned(),311 file_name: PathBuf::from("/test.jsonnet"),
312 },312 },
313 )313 )
314 .unwrap()314 .unwrap()