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
2 changes: 2 additions & 0 deletions cfgrammar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ keywords = ["yacc", "grammar"]
[features]
serde = ["dep:serde", "serde/derive", "vob/serde"]
wincode = ["dep:wincode", "vob/wincode"]
_unstable_api = []
_unsealed_unstable_traits = ["_unstable_api"]

[lib]
name = "cfgrammar"
Expand Down
7 changes: 5 additions & 2 deletions cfgrammar/src/lib/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::{
/// * An error during parsing the section.
/// * An error resulting from a value in the section having an invalid value.
#[derive(Debug, Clone, PartialEq, Eq)]
#[doc(hidden)]
pub struct HeaderError<T> {
Comment thread
ltratt marked this conversation as resolved.
Resolved
pub kind: HeaderErrorKind,
pub locations: Vec<T>,
Expand Down Expand Up @@ -59,7 +58,6 @@ pub struct HeaderValue<T>(pub T, pub Value<T>);

#[derive(Debug, Eq, PartialEq, Clone)]
#[non_exhaustive]
#[doc(hidden)]
pub enum HeaderErrorKind {
MissingGrmtoolsSection,
IllegalName,
Expand Down Expand Up @@ -197,21 +195,26 @@ impl<T> HeaderError<T> {

static RE_LEADING_WS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[\p{Pattern_White_Space}]*").unwrap());

static RE_NAME: LazyLock<Regex> = LazyLock::new(|| {
RegexBuilder::new(r"^[A-Z][A-Z_\.]*")
.case_insensitive(true)
.build()
.unwrap()
});

#[doc(hidden)]
pub static RE_CRATE_DOT: LazyLock<Regex> = LazyLock::new(|| {
RegexBuilder::new(r"^[A-Z][A-Z_]*\.")
.case_insensitive(true)
.build()
.unwrap()
});

static RE_DIGITS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[0-9]+").unwrap());

static RE_STRING: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"^\"(\\.|[^"\\])*\""#).unwrap());

#[doc(hidden)]
pub static KEY_CRATE_MAP: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
let mut map = HashMap::new();
Expand Down
71 changes: 70 additions & 1 deletion cfgrammar/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "wincode")]
use wincode::{SchemaRead, SchemaWrite};

#[doc(hidden)]
#[cfg_attr(not(feature = "_unstable_api"), doc(hidden))]
pub mod header;

mod idxnewtype;
pub mod newlinecache;
pub mod span;
Expand All @@ -79,3 +80,71 @@ pub enum Symbol<StorageT> {
Rule(RIdx<StorageT>),
Token(TIdx<StorageT>),
}

/// This private module with pub items which is directly related to
/// the "Sealed trait" pattern. These items are used within the current
/// crate. See `unstable_api` module for enabling usage outside the crate.
mod unstable {
#![allow(unused)]
#![allow(unreachable_pub)]
pub struct UnstableApi;
pub trait UnstableTrait {}
}

/// A module for lifting restrictions on visibility by enabling unstable features.
///
/// See the sources for a complete list of features, and members.
pub mod unstable_api {
/// Unstable functions that take a value `UnstableApi` require
/// the "_unstable_api" feature. This feature controls
/// whether the value has `pub` visibility outside the crate.
#[cfg_attr(not(feature = "_unstable_api"), doc(hidden))]
Comment thread
ltratt marked this conversation as resolved.
Resolved
pub use crate::unstable::UnstableApi;

/// This is a a supertrait for traits that are considered to be Unstable.
/// Unstable traits do not provide any semver guarantees.
///
/// Enabling the `_unsealed_unstable traits` makes this supertrait publicly
/// Visible.
///
///
/// Declaring an unstable Api within the crate:
/// ```ignore_rust
/// // Within the crate use `crate::unstable::` .
/// pub trait Foo: crate::unstable::UnstableTrait {
/// fn foo(key: crate::unstable::UnstableApi);
/// }
/// ```
///
/// Deriving the trait outside the crate (requires feature `_unsealed_unstable_traits`)
/// ```ignore_rust
/// struct Bar;
/// impl unstable_api::UnstableTrait for Bar{}
/// impl Foo for Bar {
/// fn foo(key: unstable_api::UnstableApi) {
/// ...
/// }
/// }
/// ```
///
///
/// Calling an implementation of the trait outside the crate (requires feature `_unstable_api`:
/// ```ignore_rust
/// let x: &dyn Foo = ...;
/// x.foo(unstable_api::UnstableApi);
/// ```
#[cfg(feature = "_unsealed_unstable_traits")]
pub use crate::unstable::UnstableTrait;

/// An value that acts as a key to inform callers that they are
/// calling an unstable internal api. This value is public by default.
/// Access to it does not require any features to be enabled.
///
/// Q. When this should be used?
///
/// A. When generated code needs to call internal api within it,
/// where you do not want the caller to have to enable any features
/// to use the generated code.
#[doc(hidden)]
pub struct InternalPublicApi;
}
19 changes: 12 additions & 7 deletions cfgrammar/src/lib/yacc/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,13 @@ impl ASTWithValidityInfo {
}
}

#[cfg_attr(not(feature = "_unstable_api"), doc(hidden))]
/// Performs a lookup in the grmtools section for an entry with the key `crate_name.key_name` and returns it.
pub fn header_value_get(&self, key: &str) -> Option<(Span, &Value<Span>)> {
pub fn header_value_get(
&self,
key: &str,
_: crate::unstable_api::UnstableApi,
) -> Option<(Span, &Value<Span>)> {
if let Some(HeaderValue(span, value)) = self.grmtools_section.get(key) {
Some((*span, value))
} else {
Expand Down Expand Up @@ -1108,7 +1113,7 @@ start -> () : "a" { () };
),
),
] {
let value = ast_validity.header_value_get(&key);
let value = ast_validity.header_value_get(&key, crate::unstable_api::UnstableApi);
assert_eq!(value, Some((expected_span, &expected_value)));
}
assert_eq!(
Expand All @@ -1119,7 +1124,7 @@ start -> () : "a" { () };
vec![("test.unused", src.find_span("test.unused"))]
);
assert_eq!(
ast_validity.header_value_get("cfgrammar.yacckind"),
ast_validity.header_value_get("cfgrammar.yacckind", crate::unstable_api::UnstableApi),
Some((
src.find_span("yacckind"),
&Value::Namespaced("Grmtools".to_string(), src.find_span("Grmtools"))
Expand All @@ -1133,7 +1138,7 @@ start -> () : "a" { () };
);

assert_eq!(
ast_validity.header_value_get("lrpar.recoverer"),
ast_validity.header_value_get("lrpar.recoverer", crate::unstable_api::UnstableApi),
Some((
src.find_span("lrpar.recoverer"),
&Value::Namespaced("CPCTPlus".to_string(), src.find_span("CPCTPlus"))
Expand Down Expand Up @@ -1166,7 +1171,7 @@ start: "a" { () };
"#;
let ast_validity = ASTWithValidityInfo::from_str(src).unwrap();
assert_eq!(
ast_validity.header_value_get("cfgrammar.yacckind"),
ast_validity.header_value_get("cfgrammar.yacckind", crate::unstable_api::UnstableApi),
Some((
src.find_span("yacckind"),
&Value::Namespaced(
Expand Down Expand Up @@ -1197,7 +1202,7 @@ start: "a" { () };
"#;
let ast_validity = ASTWithValidityInfo::from_str(src).unwrap();
assert_eq!(
ast_validity.header_value_get("cfgrammar.yacckind"),
ast_validity.header_value_get("cfgrammar.yacckind", crate::unstable_api::UnstableApi),
Some((
src.find_span("yacckind"),
&Value::Namespaced(
Expand Down Expand Up @@ -1228,7 +1233,7 @@ start: "a" { () };
"#;
let ast_validity = ASTWithValidityInfo::from_str(src).unwrap();
assert_eq!(
ast_validity.header_value_get("cfgrammar.yacckind"),
ast_validity.header_value_get("cfgrammar.yacckind", crate::unstable_api::UnstableApi),
Some((
src.find_span("yacckind"),
&Value::Namespaced(
Expand Down
3 changes: 2 additions & 1 deletion lrlex/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub mod unstable_api {
/// Unstable functions that take a value `UnstableApi` require
/// the "_unstable_api" feature. This feature controls
/// whether the value has `pub` visibility outside the crate.
#[cfg(feature = "_unstable_api")]
#[cfg_attr(not(feature = "_unstable_api"), doc(hidden))]
pub use crate::unstable::UnstableApi;

/// This is a a supertrait for traits that are considered to be Unstable.
Expand Down Expand Up @@ -288,5 +288,6 @@ pub mod unstable_api {
/// A. When generated code needs to call internal api within it,
/// where you do not want the caller to have to enable any features
/// to use the generated code.
#[doc(hidden)]
pub struct InternalPublicApi;
}
Loading