Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always ref type #19

Merged
merged 9 commits into from
Jan 29, 2024
Merged
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
4 changes: 2 additions & 2 deletions bauble/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn simple_convert(src: &str) -> Result<Vec<Object>, Spanned<ConversionError>

let ctx = value::NoChecks;

convert_values("".to_string(), values, &value::Symbols::new(&ctx))
convert_values("".to_string(), values, &value::Symbols::new(&ctx), src)
}

pub fn convert(
Expand All @@ -57,5 +57,5 @@ pub fn convert(
let values =
parse(src).ok_or(ConversionError::ParseError.spanned(SimpleSpan::new(0, src.len())))?;

convert_values(file_name.into(), values, &value::Symbols::new(&ctx))
convert_values(file_name.into(), values, &value::Symbols::new(&ctx), src)
}
2 changes: 1 addition & 1 deletion bauble/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {

let ctx = NoChecks;

let objects = convert_values("test.rsn".to_string(), values, &Symbols::new(&ctx));
let objects = convert_values("test.rsn".to_string(), values, &Symbols::new(&ctx), &src);

match objects {
Ok(objects) => {
Expand Down
34 changes: 24 additions & 10 deletions bauble/src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ pub fn parser<'a>() -> impl Parser<'a, &'a str, Values, Error<'a>> {

let group = node
.padded_by(comments.clone())
.separated_by(just(','))
.separated_by(just(',').padded_by(comments.clone()))
.allow_trailing()
.collect()
.delimited_by(just('{'), just('}'))
.delimited_by(
just('{').padded_by(comments.clone()),
just('}').padded_by(comments.clone()),
)
.map(PathTreeEnd::Group);
path_start
.map_with(|v, e| v.spanned(e.span()))
Expand Down Expand Up @@ -161,7 +164,7 @@ pub fn parser<'a>() -> impl Parser<'a, &'a str, Values, Error<'a>> {
.map(|_| Value::Bool(true))
.or(just("false").map(|_| Value::Bool(false)));

let separator = just(',').padded_by(comments.clone()).padded();
let separator = just(',').padded_by(comments.clone());

let sequence = object
.clone()
Expand All @@ -173,10 +176,16 @@ pub fn parser<'a>() -> impl Parser<'a, &'a str, Values, Error<'a>> {
// Parser for arrays.
let array = sequence
.clone()
.delimited_by(just('['), just(']'))
.delimited_by(
just('[').padded_by(comments.clone()),
just(']').padded_by(comments.clone()),
)
.map(Value::Array);

let tuple = sequence.delimited_by(just('('), just(')'));
let tuple = sequence.delimited_by(
just('(').padded_by(comments.clone()),
just(')').padded_by(comments.clone()),
);

let structure = ident
.padded_by(comments.clone())
Expand All @@ -187,7 +196,10 @@ pub fn parser<'a>() -> impl Parser<'a, &'a str, Values, Error<'a>> {
.allow_trailing()
.collect::<Vec<_>>()
.padded()
.delimited_by(just('{'), just('}'))
.delimited_by(
just('{').padded_by(comments.clone()),
just('}').padded_by(comments.clone()),
)
.map(|fields| fields.into_iter().collect());

let reference = just('$').ignore_then(path).map_with(|path, e| {
Expand Down Expand Up @@ -223,21 +235,23 @@ pub fn parser<'a>() -> impl Parser<'a, &'a str, Values, Error<'a>> {
let map = object
.clone()
.padded_by(comments.clone())
.padded()
.then_ignore(just(':'))
.then(object)
.then(object.padded_by(comments.clone()))
.separated_by(separator)
.allow_trailing()
.collect::<Vec<_>>()
.padded()
.delimited_by(just('{'), just('}'))
.delimited_by(
just('{').padded_by(comments.clone()),
just('}').padded_by(comments.clone()),
)
.map(Value::Map);

// Parser for a tuple.
let tuple = tuple.map(|fields| Value::Tuple { name: None, fields });

let path_or = path_p
.separated_by(just('|'))
.separated_by(just('|').padded_by(comments.clone()))
.at_least(2)
.collect()
.map(Value::Or);
Expand Down
44 changes: 37 additions & 7 deletions bauble/src/value/asset_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ impl DataFieldsKind {

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OwnedTypeInfo {
Path { module: String, ident: String },
Path {
module: String,
ident: String,
always_ref: bool,
},
Kind(ValueKind),
Flatten(Vec<OwnedTypeInfo>),
}
Expand All @@ -34,7 +38,7 @@ impl Display for OwnedTypeInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OwnedTypeInfo::Kind(kind) => write!(f, "{kind}"),
OwnedTypeInfo::Path { module, ident } => write!(f, "{module}::{ident}"),
OwnedTypeInfo::Path { module, ident, .. } => write!(f, "{module}::{ident}"),
OwnedTypeInfo::Flatten(types) => {
write!(
f,
Expand All @@ -55,27 +59,52 @@ impl OwnedTypeInfo {
OwnedTypeInfo::Path {
module: module.into(),
ident: ident.into(),
always_ref: false,
}
}
}

#[derive(Debug, Eq, PartialEq)]
pub enum TypeInfo<'a> {
Path { module: &'a str, ident: &'a str },
Path {
module: &'a str,
ident: &'a str,
always_ref: bool,
},
Kind(ValueKind),
Flatten(&'a [&'a TypeInfo<'a>]),
}

impl<'a> TypeInfo<'a> {
pub const fn new(module: &'a str, ident: &'a str) -> Self {
TypeInfo::Path { module, ident }
TypeInfo::Path {
module,
ident,
always_ref: false,
}
}

pub const fn with_always_ref(self) -> Self {
match self {
TypeInfo::Path { module, ident, .. } => TypeInfo::Path {
module,
ident,
always_ref: true,
},
s => s,
}
}

pub fn to_owned(&self) -> OwnedTypeInfo {
match self {
TypeInfo::Path { module, ident } => OwnedTypeInfo::Path {
TypeInfo::Path {
module,
ident,
always_ref,
} => OwnedTypeInfo::Path {
module: module.to_string(),
ident: ident.to_string(),
always_ref: *always_ref,
},
&TypeInfo::Kind(kind) => OwnedTypeInfo::Kind(kind),
TypeInfo::Flatten(types) => {
Expand All @@ -87,10 +116,11 @@ impl<'a> TypeInfo<'a> {
pub fn contains(&self, other: &OwnedTypeInfo) -> bool {
match (self, other) {
(
TypeInfo::Path { module, ident },
TypeInfo::Path { module, ident, .. },
OwnedTypeInfo::Path {
module: m,
ident: i,
..
},
) => module == m && ident == i,
(TypeInfo::Kind(kind), OwnedTypeInfo::Kind(other_kind)) => kind == other_kind,
Expand Down Expand Up @@ -165,7 +195,7 @@ impl Reference {
pub fn get_module(&self) -> Option<Cow<str>> {
match self {
Reference::Any(type_info) => match type_info {
OwnedTypeInfo::Path { module, ident } => {
OwnedTypeInfo::Path { module, ident, .. } => {
Some(Cow::Owned(format!("{module}::{ident}")))
}
OwnedTypeInfo::Kind(_) | OwnedTypeInfo::Flatten(_) => None,
Expand Down
Loading
Loading