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

@used_as_key dsl #214

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 51 additions & 1 deletion src/comment_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct RuleMetadata {
pub name: Option<String>,
pub is_newtype: bool,
pub no_alias: bool,
pub used_as_key: bool,
}

pub fn merge_metadata(r1: &RuleMetadata, r2: &RuleMetadata) -> RuleMetadata {
Expand All @@ -24,6 +25,7 @@ pub fn merge_metadata(r1: &RuleMetadata, r2: &RuleMetadata) -> RuleMetadata {
},
is_newtype: r1.is_newtype || r2.is_newtype,
no_alias: r1.no_alias || r2.no_alias,
used_as_key: r1.used_as_key || r2.used_as_key,
};
merged.verify();
merged
Expand All @@ -33,6 +35,7 @@ enum ParseResult {
NewType,
Name(String),
DontGenAlias,
UsedAsKey,
}

impl RuleMetadata {
Expand All @@ -54,6 +57,10 @@ impl RuleMetadata {
ParseResult::DontGenAlias => {
base.no_alias = true;
}

ParseResult::UsedAsKey => {
base.used_as_key = true;
}
}
}
base.verify();
Expand Down Expand Up @@ -88,9 +95,15 @@ fn tag_no_alias(input: &str) -> IResult<&str, ParseResult> {
Ok((input, ParseResult::DontGenAlias))
}

fn tag_used_as_key(input: &str) -> IResult<&str, ParseResult> {
let (input, _) = tag("@used_as_key")(input)?;

Ok((input, ParseResult::UsedAsKey))
}

fn whitespace_then_tag(input: &str) -> IResult<&str, ParseResult> {
let (input, _) = take_while(char::is_whitespace)(input)?;
let (input, result) = alt((tag_name, tag_newtype, tag_no_alias))(input)?;
let (input, result) = alt((tag_name, tag_newtype, tag_no_alias, tag_used_as_key))(input)?;

Ok((input, result))
}
Expand Down Expand Up @@ -130,6 +143,7 @@ fn parse_comment_name() {
name: Some("foo".to_string()),
is_newtype: false,
no_alias: false,
used_as_key: false,
}
))
);
Expand All @@ -145,6 +159,7 @@ fn parse_comment_newtype() {
name: None,
is_newtype: true,
no_alias: false,
used_as_key: false,
}
))
);
Expand All @@ -160,6 +175,39 @@ fn parse_comment_newtype_and_name() {
name: Some("foo".to_string()),
is_newtype: true,
no_alias: false,
used_as_key: false,
}
))
);
}

#[test]
fn parse_comment_newtype_and_name_and_used_as_key() {
assert_eq!(
rule_metadata("@newtype @used_as_key @name foo"),
Ok((
"",
RuleMetadata {
name: Some("foo".to_string()),
is_newtype: true,
no_alias: false,
used_as_key: true,
}
))
);
}

#[test]
fn parse_comment_used_as_key() {
assert_eq!(
rule_metadata("@used_as_key"),
Ok((
"",
RuleMetadata {
name: None,
is_newtype: false,
no_alias: false,
used_as_key: true,
}
))
);
Expand All @@ -175,6 +223,7 @@ fn parse_comment_newtype_and_name_inverse() {
name: Some("foo".to_string()),
is_newtype: true,
no_alias: false,
used_as_key: false,
}
))
);
Expand All @@ -190,6 +239,7 @@ fn parse_comment_name_noalias() {
name: Some("foo".to_string()),
is_newtype: false,
no_alias: true,
used_as_key: false,
}
))
);
Expand Down
9 changes: 8 additions & 1 deletion src/intermediate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,10 @@ impl<'a> IntermediateTypes<'a> {
domain.visit_types(self, &mut |ty| mark_used_as_key(ty, &mut used_as_key));
}
}
self.used_as_key = used_as_key;
// we use a separate one here to get around the borrow checker in the above visit_types
for ident in used_as_key {
self.mark_used_as_key(ident);
}
}

pub fn visit_types<F: FnMut(&ConceptualRustType)>(&self, f: &mut F) {
Expand Down Expand Up @@ -657,6 +660,10 @@ impl<'a> IntermediateTypes<'a> {
self.used_as_key.contains(name)
}

pub fn mark_used_as_key(&mut self, name: RustIdent) {
self.used_as_key.insert(name);
}

pub fn print_info(&self) {
if !self.plain_groups.is_empty() {
println!("\n\nPlain groups:");
Expand Down
3 changes: 3 additions & 0 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ fn parse_type(
&RuleMetadata::from(type1.comments_after_type.as_ref()),
&RuleMetadata::from(type_choice.comments_after_type.as_ref()),
);
if rule_metadata.used_as_key {
types.mark_used_as_key(type_name.clone());
}
match &type1.type2 {
Type2::Typename {
ident,
Expand Down
Loading