Skip to content

Commit

Permalink
Use path relative to manifest dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Jul 25, 2024
1 parent 82cdda8 commit 31bcc76
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 247 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "static-regular-grammar"
version = "2.0.1"
version = "2.0.2"
edition = "2021"
authors = ["Timothée Haudebourg <author@haudebourg.net>"]
categories = ["parsing"]
Expand Down
18 changes: 9 additions & 9 deletions src/byteset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ impl ByteSet {
ByteSet(RangeSet::new())
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
// pub fn is_empty(&self) -> bool {
// self.0.is_empty()
// }

pub fn len(&self) -> u16 {
self.0.len()
}
// pub fn len(&self) -> u16 {
// self.0.len()
// }

pub fn from_u8(c: u8, case_sensitive: bool) -> ByteSet {
let mut set = ByteSet::new();
Expand Down Expand Up @@ -97,9 +97,9 @@ impl ByteSet {
Ranges(self.0.iter())
}

pub fn first(&self) -> Option<u8> {
self.0.iter().next().and_then(|range| range.first())
}
// pub fn first(&self) -> Option<u8> {
// self.0.iter().next().and_then(|range| range.first())
// }

pub fn is_ascii(&self) -> bool {
for range in self.0.iter() {
Expand Down
18 changes: 9 additions & 9 deletions src/charset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ impl CharSet {
CharSet(RangeSet::new())
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
// pub fn is_empty(&self) -> bool {
// self.0.is_empty()
// }

pub fn len(&self) -> u64 {
self.0.len()
}
// pub fn len(&self) -> u64 {
// self.0.len()
// }

pub fn is_ascii(&self) -> bool {
self.0
Expand Down Expand Up @@ -127,9 +127,9 @@ impl CharSet {
Ranges(self.0.iter())
}

pub fn first(&self) -> Option<char> {
self.0.iter().next().and_then(|range| range.first())
}
// pub fn first(&self) -> Option<char> {
// self.0.iter().next().and_then(|range| range.first())
// }
}

pub struct Ranges<'a>(
Expand Down
19 changes: 17 additions & 2 deletions src/grammar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::Path;
use std::{
env,
path::{Path, PathBuf},
};

use proc_macro2::Span;
use serde::{Deserialize, Serialize};
Expand All @@ -14,6 +17,9 @@ pub enum GrammarError {
#[error(transparent)]
IO(#[from] std::io::Error),

#[error(transparent)]
Var(env::VarError),

#[error("invalid ABNF grammar: {0}")]
Abnf(::abnf::error::ParseError),

Expand Down Expand Up @@ -164,7 +170,16 @@ pub(crate) fn extract_grammar<T: Token>(
attrs: Vec<syn::Attribute>,
) -> Result<(Grammar<T>, [u8; 32]), (GrammarError, Span)> {
let (ty, data) = match path {
Some(path) => {
Some(relative_path) => {
let path = match env::var("CARGO_MANIFEST_DIR") {
Ok(manifest_dir) => {
let mut path: PathBuf = manifest_dir.into();
path.extend(relative_path);
path
}
Err(e) => return Err((GrammarError::Var(e), Span::call_site())),
};

let ty = match path.extension() {
Some(ext) if ext == "abnf" => GrammarType::Abnf,
_ => return Err((GrammarError::UnknownGrammarFormat, Span::call_site())),
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
//! ```
//!
//! [`rust-analyzer`](https://rust-analyzer.github.io/)
use std::env;

use indoc::formatdoc;
use proc_macro2::{Ident, Span, TokenStream};
use proc_macro_error::{abort, proc_macro_error};
Expand Down Expand Up @@ -196,6 +198,9 @@ pub fn derive_regular_grammar(input_tokens: proc_macro::TokenStream) -> proc_mac

#[derive(Debug, thiserror::Error)]
pub(crate) enum Error {
#[error(transparent)]
Var(env::VarError),

#[error("unexpected type parameter")]
UnexpectedTypeParameter,

Expand Down
11 changes: 9 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, path::PathBuf};
use std::{borrow::Cow, env, path::PathBuf};

use proc_macro2::{Ident, Span};

Expand Down Expand Up @@ -51,7 +51,14 @@ fn find_target_dir() -> Result<Cow<'static, str>, std::env::VarError> {

fn build_cache_path(ident: &Ident, path: Option<PathBuf>) -> Result<PathBuf, (Error, Span)> {
match path {
Some(path) => Ok(path),
Some(relative_path) => match env::var("CARGO_MANIFEST_DIR") {
Ok(manifest_dir) => {
let mut path: PathBuf = manifest_dir.into();
path.extend(&relative_path);
Ok(path)
}
Err(e) => Err((Error::Var(e), ident.span())),
},
None => {
let target =
find_target_dir().map_err(|e| (Error::TargetDirNotFound(e), ident.span()))?;
Expand Down
69 changes: 34 additions & 35 deletions src/token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::fmt;
use std::{fmt::Debug, hash::Hash};

use serde::{de::DeserializeOwned, Serialize};
Expand All @@ -19,8 +18,8 @@ pub trait Token: Copy {
/// Set of tokens.
type Set: TokenSet<Self>;

/// Token map.
type Map<V>: TokenMap<Self, V>;
// /// Token map.
// type Map<V>: TokenMap<Self, V>;

const UNICODE: bool;

Expand All @@ -30,7 +29,7 @@ pub trait Token: Copy {

fn from_u32(v: u32) -> Option<Self>;

fn fmt_token(&self, f: &mut fmt::Formatter) -> fmt::Result;
// fn fmt_token(&self, f: &mut fmt::Formatter) -> fmt::Result;

fn rust_type() -> proc_macro2::TokenStream;

Expand Down Expand Up @@ -60,9 +59,9 @@ pub trait Token: Copy {
None
}

fn rust_inner_into_ascii_method_body() -> Option<proc_macro2::TokenStream> {
None
}
// fn rust_inner_into_ascii_method_body() -> Option<proc_macro2::TokenStream> {
// None
// }

fn rust_empty_string() -> proc_macro2::TokenStream;
}
Expand All @@ -75,7 +74,7 @@ pub trait TokenRange<T: Token>: Debug + Copy + Ord + Hash {
Self::new(T::from_u8(b), T::from_u8(b))
}

fn peek(&self) -> Option<T>;
// fn peek(&self) -> Option<T>;
}

pub trait TokenSet<T: Token>:
Expand All @@ -97,45 +96,45 @@ pub trait TokenSet<T: Token>:
result
}

fn is_empty(&self) -> bool;
// fn is_empty(&self) -> bool;

fn len(&self) -> usize;
// fn len(&self) -> usize;

fn peek(&self) -> Option<T>;
// fn peek(&self) -> Option<T>;

fn intersects_range(&self, range: T::Range) -> bool;
// fn intersects_range(&self, range: T::Range) -> bool;

fn merge_with(&mut self, other: Self);
// fn merge_with(&mut self, other: Self);

fn rust_set(&self) -> proc_macro2::TokenStream;
}

pub trait TokenMap<K: Token, V>: Default + IntoIterator<Item = (K::Range, V)> {
type Iter<'a>: Iterator<Item = (&'a K::Range, &'a V)>
where
K::Range: 'a,
V: 'a,
Self: 'a;
// pub trait TokenMap<K: Token, V>: Default + IntoIterator<Item = (K::Range, V)> {
// // type Iter<'a>: Iterator<Item = (&'a K::Range, &'a V)>
// // where
// // K::Range: 'a,
// // V: 'a,
// // Self: 'a;

fn is_empty(&self) -> bool;
// // fn is_empty(&self) -> bool;

fn len(&self) -> usize;
// // fn len(&self) -> usize;

fn iter(&self) -> Self::Iter<'_>;
// // fn iter(&self) -> Self::Iter<'_>;

fn insert_range(&mut self, range: K::Range, value: V)
where
V: PartialEq + Clone;
// fn insert_range(&mut self, range: K::Range, value: V)
// where
// V: PartialEq + Clone;

fn insert(&mut self, set: K::Set, value: V)
where
V: PartialEq + Clone;
// // fn insert(&mut self, set: K::Set, value: V)
// // where
// // V: PartialEq + Clone;

fn update_range(&mut self, range: K::Range, f: impl Fn(Option<&V>) -> Option<V>)
where
V: PartialEq + Clone;
// // fn update_range(&mut self, range: K::Range, f: impl Fn(Option<&V>) -> Option<V>)
// // where
// // V: PartialEq + Clone;

fn update(&mut self, set: &K::Set, f: impl Fn(Option<&V>) -> Option<V>)
where
V: PartialEq + Clone;
}
// // fn update(&mut self, set: &K::Set, f: impl Fn(Option<&V>) -> Option<V>)
// // where
// // V: PartialEq + Clone;
// }
Loading

0 comments on commit 31bcc76

Please sign in to comment.