Skip to content

Commit

Permalink
Add warnings for MacroBlock::parse
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Sep 16, 2024
1 parent 85920d9 commit 65556f5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 25 deletions.
48 changes: 31 additions & 17 deletions src/blocks/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
blocks::{ContentModel, IsBlock},
span::MatchedItem,
strings::CowStr,
warnings::MatchAndWarnings,
warnings::{MatchAndWarnings, Warning, WarningType},
HasSpan, Span,
};

Expand All @@ -30,26 +30,48 @@ impl<'src> MacroBlock<'src> {

// Line must end with `]`; otherwise, it's not a block macro.
if !line.item.ends_with(']') {
return empty_match_and_warnings();
return MatchAndWarnings {
item: None,
warnings: vec![],
};
}

let line_wo_brace = line.item.slice(0..line.item.len() - 1);

let Some(name) = line_wo_brace.take_ident() else {
return empty_match_and_warnings();
let Some(name) = line.item.take_ident() else {
return MatchAndWarnings {
item: None,
warnings: vec![Warning {
source: line.item,
warning: WarningType::InvalidMacroName,
}],
};
};

let Some(colons) = name.after.take_prefix("::") else {
return empty_match_and_warnings();
return MatchAndWarnings {
item: None,
warnings: vec![Warning {
source: name.after,
warning: WarningType::MacroMissingDoubleColon,
}],
};
};

let target = colons.after.take_while(|c| c != '[');

let Some(open_brace) = target.after.take_prefix("[") else {
return empty_match_and_warnings();
return MatchAndWarnings {
item: None,
warnings: vec![Warning {
source: target.after,
warning: WarningType::MacroMissingAttributeList,
}],
};
};

let maw_attrlist = Attrlist::parse(open_brace.after);
let attrlist = open_brace.after.slice(0..open_brace.after.len() - 1);
// Note that we already checked that this line ends with a close brace.

let maw_attrlist = Attrlist::parse(attrlist);

match maw_attrlist.item {
Some(attrlist) => MatchAndWarnings {
Expand Down Expand Up @@ -113,11 +135,3 @@ impl<'src> HasSpan<'src> for MacroBlock<'src> {
&self.source
}
}

fn empty_match_and_warnings<'src>(
) -> MatchAndWarnings<'src, Option<MatchedItem<'src, MacroBlock<'src>>>> {
MatchAndWarnings {
item: None,
warnings: vec![],
}
}
63 changes: 55 additions & 8 deletions src/tests/blocks/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,63 @@ fn err_only_spaces() {
}

#[test]
fn err_not_ident() {
assert!(MacroBlock::parse(Span::new("foo^xyz::bar[]"))
.unwrap_if_no_warnings()
.is_none());
fn err_macro_name_not_ident() {
let maw = MacroBlock::parse(Span::new("98xyz::bar[blah,blap]"));

assert!(maw.item.is_none());

assert_eq!(
maw.warnings,
vec![TWarning {
source: TSpan {
data: "98xyz::bar[blah,blap]",
line: 1,
col: 1,
offset: 0,
},
warning: WarningType::InvalidMacroName,
}]
);
}

#[test]
fn err_inline_syntax() {
assert!(MacroBlock::parse(Span::new("foo:bar[]"))
.unwrap_if_no_warnings()
.is_none());
fn err_missing_double_colon() {
let maw = MacroBlock::parse(Span::new("foo:bar[blah,blap]"));

assert!(maw.item.is_none());

assert_eq!(
maw.warnings,
vec![TWarning {
source: TSpan {
data: ":bar[blah,blap]",
line: 1,
col: 4,
offset: 3,
},
warning: WarningType::MacroMissingDoubleColon,
}]
);
}

#[test]
fn err_missing_attrlist() {
let maw = MacroBlock::parse(Span::new("foo::barblah,blap]"));

assert!(maw.item.is_none());

assert_eq!(
maw.warnings,
vec![TWarning {
source: TSpan {
data: "",
line: 1,
col: 19,
offset: 18,
},
warning: WarningType::MacroMissingAttributeList,
}]
);
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions src/warnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ pub enum WarningType {
)]
EmptyShorthandItem,

#[error("Macro name is not a valid identifier")]
InvalidMacroName,

#[error("Macro missing attribute list")]
MacroMissingAttributeList,

#[error("Macro missing :: separator")]
MacroMissingDoubleColon,

#[error("Missing comma after quoted attribute value")]
MissingCommaAfterQuotedAttributeValue,
}
Expand Down

0 comments on commit 65556f5

Please sign in to comment.