maud/maud_macros/src/ast.rs

234 lines
5.5 KiB
Rust
Raw Normal View History

2020-08-31 11:57:55 +03:00
use proc_macro2::{TokenStream, TokenTree};
use proc_macro_error::SpanRange;
use syn::Lit;
#[derive(Debug)]
pub enum Markup {
/// Used as a placeholder value on parse error.
ParseError {
span: SpanRange,
},
Block(Block),
Literal {
content: String,
2020-08-31 11:57:55 +03:00
span: SpanRange,
},
Symbol {
symbol: TokenStream,
},
Splice {
expr: TokenStream,
2020-08-31 11:57:55 +03:00
outer_span: SpanRange,
},
Element {
name: TokenStream,
2021-03-13 20:25:56 +11:00
attrs: Vec<Attr>,
body: ElementBody,
},
Let {
2020-08-31 11:57:55 +03:00
at_span: SpanRange,
tokens: TokenStream,
},
2018-05-29 19:18:30 +12:00
Special {
segments: Vec<Special>,
},
Match {
2020-08-31 11:57:55 +03:00
at_span: SpanRange,
head: TokenStream,
arms: Vec<MatchArm>,
2020-08-31 11:57:55 +03:00
arms_span: SpanRange,
},
}
impl Markup {
2020-08-31 11:57:55 +03:00
pub fn span(&self) -> SpanRange {
match *self {
Markup::ParseError { span } => span,
Markup::Block(ref block) => block.span(),
Markup::Literal { span, .. } => span,
Markup::Symbol { ref symbol } => span_tokens(symbol.clone()),
Markup::Splice { outer_span, .. } => outer_span,
2020-09-27 21:36:27 +10:00
Markup::Element {
ref name, ref body, ..
} => {
let name_span = span_tokens(name.clone());
2020-08-31 11:57:55 +03:00
name_span.join_range(body.span())
2020-09-27 21:36:27 +10:00
}
Markup::Let {
at_span,
ref tokens,
} => at_span.join_range(span_tokens(tokens.clone())),
Markup::Special { ref segments } => join_ranges(segments.iter().map(Special::span)),
Markup::Match {
at_span, arms_span, ..
} => at_span.join_range(arms_span),
}
}
}
#[derive(Debug)]
pub enum Attr {
Class {
2020-08-31 11:57:55 +03:00
dot_span: SpanRange,
name: Markup,
toggler: Option<Toggler>,
},
Id {
2020-08-31 11:57:55 +03:00
hash_span: SpanRange,
name: Markup,
},
Named {
named_attr: NamedAttr,
},
}
impl Attr {
2020-08-31 11:57:55 +03:00
pub fn span(&self) -> SpanRange {
match *self {
2020-09-27 21:36:27 +10:00
Attr::Class {
dot_span,
ref name,
ref toggler,
} => {
let name_span = name.span();
2020-08-31 11:57:55 +03:00
let dot_name_span = dot_span.join_range(name_span);
if let Some(toggler) = toggler {
2020-08-31 11:57:55 +03:00
dot_name_span.join_range(toggler.cond_span)
} else {
dot_name_span
}
2020-09-27 21:36:27 +10:00
}
Attr::Id {
hash_span,
ref name,
} => {
let name_span = name.span();
2020-08-31 11:57:55 +03:00
hash_span.join_range(name_span)
2020-09-27 21:36:27 +10:00
}
Attr::Named { ref named_attr } => named_attr.span(),
}
}
}
#[derive(Debug)]
pub enum ElementBody {
2020-08-31 11:57:55 +03:00
Void { semi_span: SpanRange },
Block { block: Block },
}
impl ElementBody {
2020-08-31 11:57:55 +03:00
pub fn span(&self) -> SpanRange {
match *self {
ElementBody::Void { semi_span } => semi_span,
ElementBody::Block { ref block } => block.span(),
}
}
}
#[derive(Debug)]
pub struct Block {
pub markups: Vec<Markup>,
2020-08-31 11:57:55 +03:00
pub outer_span: SpanRange,
}
impl Block {
2020-08-31 11:57:55 +03:00
pub fn span(&self) -> SpanRange {
self.outer_span
}
}
#[derive(Debug)]
pub struct Special {
2020-08-31 11:57:55 +03:00
pub at_span: SpanRange,
pub head: TokenStream,
pub body: Block,
}
impl Special {
2020-08-31 11:57:55 +03:00
pub fn span(&self) -> SpanRange {
let body_span = self.body.span();
2020-08-31 11:57:55 +03:00
self.at_span.join_range(body_span)
}
}
#[derive(Debug)]
pub struct NamedAttr {
pub name: TokenStream,
pub attr_type: AttrType,
}
impl NamedAttr {
2020-08-31 11:57:55 +03:00
fn span(&self) -> SpanRange {
let name_span = span_tokens(self.name.clone());
if let Some(attr_type_span) = self.attr_type.span() {
2020-08-31 11:57:55 +03:00
name_span.join_range(attr_type_span)
} else {
name_span
}
}
}
#[derive(Debug)]
pub enum AttrType {
2020-09-27 21:36:27 +10:00
Normal { value: Markup },
Optional { toggler: Toggler },
2020-09-27 21:36:27 +10:00
Empty { toggler: Option<Toggler> },
}
impl AttrType {
2020-08-31 11:57:55 +03:00
fn span(&self) -> Option<SpanRange> {
match *self {
AttrType::Normal { ref value } => Some(value.span()),
AttrType::Optional { ref toggler } => Some(toggler.span()),
2019-02-21 21:22:22 +13:00
AttrType::Empty { ref toggler } => toggler.as_ref().map(Toggler::span),
}
}
}
#[derive(Debug)]
pub struct Toggler {
pub cond: TokenStream,
2020-08-31 11:57:55 +03:00
pub cond_span: SpanRange,
}
impl Toggler {
2020-08-31 11:57:55 +03:00
fn span(&self) -> SpanRange {
self.cond_span
}
}
#[derive(Debug)]
pub struct MatchArm {
pub head: TokenStream,
pub body: Block,
}
2020-09-27 21:36:27 +10:00
pub fn span_tokens<I: IntoIterator<Item = TokenTree>>(tokens: I) -> SpanRange {
2020-08-31 11:57:55 +03:00
join_ranges(tokens.into_iter().map(|s| SpanRange::single_span(s.span())))
}
2020-09-27 21:36:27 +10:00
pub fn join_ranges<I: IntoIterator<Item = SpanRange>>(ranges: I) -> SpanRange {
2020-08-31 11:57:55 +03:00
let mut iter = ranges.into_iter();
let first = match iter.next() {
Some(span) => span,
2020-08-31 11:57:55 +03:00
None => return SpanRange::call_site(),
};
2020-08-31 11:57:55 +03:00
let last = iter.last().unwrap_or(first);
first.join_range(last)
}
pub fn name_to_string(name: TokenStream) -> String {
name.into_iter()
.map(|token| {
if let TokenTree::Literal(literal) = token {
match Lit::new(literal.clone()) {
Lit::Str(str) => str.value(),
_ => literal.to_string(),
}
} else {
token.to_string()
}
})
.collect()
}