Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ exclude = [".github/", "bors.toml", "rustfmt.toml"]
members = ["xtask"]

[dependencies]
rustc-hash = "2.1.1"
rustc-hash = { version = "2.1.1", default-features = false }
hashbrown = { version = "0.15.2", features = [
"inline-more",
"raw-entry",
], default-features = false }
text-size = "1.1.1"
countme = "3.0.1"

serde = { version = "1.0.218", optional = true, default-features = false }

[dev-dependencies]
m_lexer = "0.0.4"

[features]
serde1 = ["serde", "text-size/serde"]
std = ["rustc-hash/std"]
serde1 = ["dep:serde"]
9 changes: 5 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{borrow::Cow, fmt, iter, marker::PhantomData, ops::Range};
use alloc::borrow::Cow;
use core::{fmt, hash::Hash, iter, marker::PhantomData, ops::Range};

use crate::{
Direction, GreenNode, GreenNodeData, GreenToken, NodeOrToken, SyntaxKind, SyntaxText,
TextRange, TextSize, TokenAtOffset, WalkEvent, cursor, green::GreenTokenData,
};

pub trait Language: Sized + Copy + fmt::Debug + Eq + Ord + std::hash::Hash {
type Kind: Sized + Copy + fmt::Debug + Eq + Ord + std::hash::Hash;
pub trait Language: Sized + Copy + fmt::Debug + Eq + Ord + Hash {
type Kind: Sized + Copy + fmt::Debug + Eq + Ord + Hash;

fn kind_from_raw(raw: SyntaxKind) -> Self::Kind;
fn kind_to_raw(kind: Self::Kind) -> SyntaxKind;
Expand Down Expand Up @@ -68,7 +69,7 @@ impl<L: Language> fmt::Debug for SyntaxToken<L> {
let text = self.text();
for idx in 21..25 {
if text.is_char_boundary(idx) {
let text = format!("{} ...", &text[..idx]);
let text = alloc::format!("{} ...", &text[..idx]);
return write!(f, " {:?}", text);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/arc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Vendored and stripped down version of triomphe
use std::{
alloc::{self, Layout},
use crate::alloc::alloc::{self, Layout};
use crate::alloc::boxed::Box;
use core::{
cmp::Ordering,
hash::{Hash, Hasher},
marker::PhantomData,
Expand Down Expand Up @@ -86,7 +87,7 @@ impl<T: ?Sized> Arc<T> {
/// allocation
#[inline]
pub(crate) fn ptr_eq(this: &Self, other: &Self) -> bool {
std::ptr::addr_eq(this.ptr(), other.ptr())
ptr::addr_eq(this.ptr(), other.ptr())
}

pub(crate) fn ptr(&self) -> *mut ArcInner<T> {
Expand Down Expand Up @@ -120,7 +121,7 @@ impl<T: ?Sized> Clone for Arc<T> {
// We abort because such a program is incredibly degenerate, and we
// don't care to support it.
if old_size > MAX_REFCOUNT {
std::process::abort();
panic!("Arc reference count overflow");
}

unsafe { Arc { p: ptr::NonNull::new_unchecked(self.ptr()), phantom: PhantomData } }
Expand Down
2 changes: 1 addition & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! We also provide an [`AstNode`] trait for typed AST wrapper APIs over rowan
//! nodes.

use std::{
use core::{
fmt,
hash::{Hash, Hasher},
iter::successors,
Expand Down
13 changes: 13 additions & 0 deletions src/countme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use core::marker::PhantomData;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Count<T: 'static> {
marker: PhantomData<fn(T)>,
}

impl<T: 'static> Count<T> {
#[inline]
pub(crate) const fn new() -> Self {
Self { marker: PhantomData }
}
}
6 changes: 4 additions & 2 deletions src/cow_mut.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use core::ops::{Deref, DerefMut};

#[derive(Debug)]
pub(crate) enum CowMut<'a, T> {
Owned(T),
Borrowed(&'a mut T),
}

impl<T> std::ops::Deref for CowMut<'_, T> {
impl<T> Deref for CowMut<'_, T> {
type Target = T;
fn deref(&self) -> &T {
match self {
Expand All @@ -14,7 +16,7 @@ impl<T> std::ops::Deref for CowMut<'_, T> {
}
}

impl<T> std::ops::DerefMut for CowMut<'_, T> {
impl<T> DerefMut for CowMut<'_, T> {
fn deref_mut(&mut self) -> &mut T {
match self {
CowMut::Owned(it) => it,
Expand Down
21 changes: 12 additions & 9 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@
// when the tree is mutable.
// - TBD

use std::{
borrow::Cow,
use alloc::{
borrow::{Cow, ToOwned},
boxed::Box,
string::ToString,
};
use core::{
cell::Cell,
fmt,
hash::{Hash, Hasher},
Expand All @@ -92,11 +96,10 @@ use std::{
ptr, slice,
};

use countme::Count;

use crate::{
Direction, GreenNode, GreenToken, NodeOrToken, SyntaxText, TextRange, TextSize, TokenAtOffset,
WalkEvent,
countme::Count,
green::{GreenChild, GreenElementRef, GreenNodeData, GreenTokenData, SyntaxKind},
sll,
utility_types::Delta,
Expand Down Expand Up @@ -285,7 +288,7 @@ impl NodeData {
fn inc_rc(&self) {
let rc = match self.rc.get().checked_add(1) {
Some(it) => it,
None => std::process::abort(),
None => panic!("reference count overflow"),
};
self.rc.set(rc)
}
Expand Down Expand Up @@ -327,7 +330,7 @@ impl NodeData {
}
}
#[inline]
fn green_siblings(&self) -> slice::Iter<GreenChild> {
fn green_siblings(&self) -> slice::Iter<'_, GreenChild> {
match &self.parent().map(|it| &it.green) {
Some(Green::Node { ptr }) => unsafe { &*ptr.get().as_ptr() }.children().raw,
Some(Green::Token { .. }) => {
Expand Down Expand Up @@ -607,7 +610,7 @@ impl SyntaxNode {
assert!(self.can_take_ptr());
let ret = self.ptr;
// don't change the refcount when self gets dropped
std::mem::forget(self);
mem::forget(self);
ret
}

Expand Down Expand Up @@ -1000,7 +1003,7 @@ impl SyntaxToken {
assert!(self.can_take_ptr());
let ret = self.ptr;
// don't change the refcount when self gets dropped
std::mem::forget(self);
mem::forget(self);
ret
}

Expand Down Expand Up @@ -1055,7 +1058,7 @@ impl SyntaxToken {

#[inline]
pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode> + use<> {
std::iter::successors(self.parent(), SyntaxNode::parent)
iter::successors(self.parent(), SyntaxNode::parent)
}

pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
Expand Down
8 changes: 4 additions & 4 deletions src/green.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ mod tests {

#[test]
fn test_size_of() {
use std::mem::size_of;
use core::mem::size_of;

eprintln!("GreenNode {}", size_of::<GreenNode>());
eprintln!("GreenToken {}", size_of::<GreenToken>());
eprintln!("GreenElement {}", size_of::<GreenElement>());
std::eprintln!("GreenNode {}", size_of::<GreenNode>());
std::eprintln!("GreenToken {}", size_of::<GreenToken>());
std::eprintln!("GreenElement {}", size_of::<GreenElement>());
}
}
3 changes: 2 additions & 1 deletion src/green/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::num::NonZeroUsize;
use alloc::vec::Vec;
use core::num::NonZeroUsize;

use crate::{
NodeOrToken,
Expand Down
2 changes: 1 addition & 1 deletion src/green/element.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;
use alloc::borrow::{Cow, ToOwned};

use crate::{
GreenNodeData, NodeOrToken, TextSize,
Expand Down
15 changes: 9 additions & 6 deletions src/green/node.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::{
borrow::{Borrow, Cow},
use alloc::{
borrow::{Cow, ToOwned},
vec::Vec,
};
use core::{
borrow::Borrow,
fmt,
iter::{self, FusedIterator},
mem::{self, ManuallyDrop},
ops, ptr, slice,
};

use countme::Count;

use crate::{
GreenToken, NodeOrToken, TextRange, TextSize,
arc::{Arc, HeaderSlice, ThinArc},
countme::Count,
green::{GreenElement, GreenElementRef, SyntaxKind},
};

Expand Down Expand Up @@ -247,7 +250,7 @@ impl GreenNode {

impl GreenChild {
#[inline]
pub(crate) fn as_ref(&self) -> GreenElementRef {
pub(crate) fn as_ref(&self) -> GreenElementRef<'_> {
match self {
GreenChild::Node { node, .. } => NodeOrToken::Node(node),
GreenChild::Token { token, .. } => NodeOrToken::Token(token),
Expand Down Expand Up @@ -361,7 +364,7 @@ mod test {
#[cfg(target_pointer_width = "64")]
fn check_green_child_size() {
use super::GreenChild;
use std::mem;
use core::mem;

assert_eq!(mem::size_of::<GreenChild>(), mem::size_of::<usize>() * 2);
}
Expand Down
3 changes: 2 additions & 1 deletion src/green/node_cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::vec::Vec;
use core::hash::{BuildHasherDefault, Hash, Hasher};
use hashbrown::hash_map::RawEntryMut;
use rustc_hash::FxHasher;
use std::hash::{BuildHasherDefault, Hash, Hasher};

use crate::{
GreenNode, GreenNodeData, GreenToken, GreenTokenData, NodeOrToken, SyntaxKind,
Expand Down
8 changes: 4 additions & 4 deletions src/green/token.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::{
use alloc::borrow::ToOwned;
use core::{
borrow::Borrow,
fmt,
mem::{self, ManuallyDrop},
ops, ptr,
};

use countme::Count;

use crate::{
TextSize,
arc::{Arc, HeaderSlice, ThinArc},
countme::Count,
green::SyntaxKind,
};

Expand Down Expand Up @@ -96,7 +96,7 @@ impl GreenTokenData {
/// Text of this Token.
#[inline]
pub fn text(&self) -> &str {
unsafe { std::str::from_utf8_unchecked(self.data.slice()) }
unsafe { core::str::from_utf8_unchecked(self.data.slice()) }
}

/// Returns the length of the text covered by this token.
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A generic library for lossless syntax trees.
//! See `examples/s_expressions.rs` for a tutorial.
#![no_std]
#![forbid(
// missing_debug_implementations,
unconditional_recursion,
Expand All @@ -8,13 +9,19 @@
)]
#![deny(unsafe_code)]

extern crate alloc;
#[cfg(test)]
extern crate std;

#[allow(unsafe_code)]
mod green;
#[allow(unsafe_code)]
pub mod cursor;

pub mod api;
mod countme;
mod syntax_text;
mod text_size;
mod utility_types;

mod cow_mut;
Expand All @@ -26,7 +33,7 @@ mod arc;
mod serde_impls;
pub mod ast;

pub use text_size::{TextLen, TextRange, TextSize};
pub use crate::text_size::{TextLen, TextRange, TextSize};

pub use crate::{
api::{
Expand Down
2 changes: 1 addition & 1 deletion src/serde_impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt;
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};
use std::fmt;

use crate::{
NodeOrToken,
Expand Down
2 changes: 1 addition & 1 deletion src/sll.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Sorted Linked List

use std::{cell::Cell, cmp::Ordering, ptr};
use core::{cell::Cell, cmp::Ordering, ptr};

use crate::utility_types::Delta;

Expand Down
7 changes: 4 additions & 3 deletions src/syntax_text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use alloc::string::{String, ToString};
use core::{cmp, fmt};

use crate::{
TextRange, TextSize,
Expand Down Expand Up @@ -202,7 +203,7 @@ fn zip_texts<I: Iterator<Item = (SyntaxToken, TextRange)>>(xs: &mut I, ys: &mut
if !(x_text.starts_with(y_text) || y_text.starts_with(x_text)) {
return Some(());
}
let advance = std::cmp::min(x.1.len(), y.1.len());
let advance = cmp::min(x.1.len(), y.1.len());
x.1 = TextRange::new(x.1.start() + advance, x.1.end());
y.1 = TextRange::new(y.1.start() + advance, y.1.end());
}
Expand All @@ -211,7 +212,7 @@ fn zip_texts<I: Iterator<Item = (SyntaxToken, TextRange)>>(xs: &mut I, ys: &mut
impl Eq for SyntaxText {}

mod private {
use std::ops;
use core::ops;

use crate::{TextRange, TextSize};

Expand Down
Loading
Loading