| 1 | use std::cmp::{Ord, Ordering}; |
| 2 | use std::fmt::{self, Debug, Display}; |
| 3 | |
| 4 | use proc_macro2::{Span, TokenStream}; |
| 5 | use syn::{spanned::Spanned, Ident}; |
| 6 | |
| 7 | use crate::graph::{Disambiguate, Node}; |
| 8 | use crate::parser::SkipCallback; |
| 9 | use crate::util::MaybeVoid; |
| 10 | |
| 11 | #[derive(Clone)] |
| 12 | pub struct Leaf<'t> { |
| 13 | pub ident: Option<&'t Ident>, |
| 14 | pub span: Span, |
| 15 | pub priority: usize, |
| 16 | pub field: MaybeVoid, |
| 17 | pub callback: Option<Callback>, |
| 18 | } |
| 19 | |
| 20 | #[derive(Clone)] |
| 21 | pub enum Callback { |
| 22 | Label(TokenStream), |
| 23 | Inline(Box<InlineCallback>), |
| 24 | #[allow(clippy::enum_variant_names)] |
| 25 | SkipCallback(SkipCallback), |
| 26 | Skip(Span), |
| 27 | } |
| 28 | |
| 29 | #[derive(Clone)] |
| 30 | pub struct InlineCallback { |
| 31 | pub arg: Ident, |
| 32 | pub body: TokenStream, |
| 33 | pub span: Span, |
| 34 | } |
| 35 | |
| 36 | impl From<InlineCallback> for Callback { |
| 37 | fn from(inline: InlineCallback) -> Callback { |
| 38 | Callback::Inline(Box::new(inline)) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl Callback { |
| 43 | pub fn span(&self) -> Span { |
| 44 | match self { |
| 45 | Callback::Label(tokens) => tokens.span(), |
| 46 | Callback::Inline(inline) => inline.span, |
| 47 | Callback::SkipCallback(callback) => callback.span(), |
| 48 | Callback::Skip(skip) => *skip, |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl<'t> Leaf<'t> { |
| 54 | pub fn new(ident: &'t Ident, span: Span) -> Self { |
| 55 | Leaf { |
| 56 | ident: Some(ident), |
| 57 | span, |
| 58 | priority: 0, |
| 59 | field: MaybeVoid::Void, |
| 60 | callback: None, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | pub fn new_skip(span: Span) -> Self { |
| 65 | Leaf { |
| 66 | ident: None, |
| 67 | span, |
| 68 | priority: 0, |
| 69 | field: MaybeVoid::Void, |
| 70 | callback: Some(Callback::Skip(span)), |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pub fn callback(mut self, callback: Option<Callback>) -> Self { |
| 75 | self.callback = callback; |
| 76 | self |
| 77 | } |
| 78 | |
| 79 | pub fn field(mut self, field: MaybeVoid) -> Self { |
| 80 | self.field = field; |
| 81 | self |
| 82 | } |
| 83 | |
| 84 | pub fn priority(mut self, priority: usize) -> Self { |
| 85 | self.priority = priority; |
| 86 | self |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl Disambiguate for Leaf<'_> { |
| 91 | fn cmp(left: &Leaf, right: &Leaf) -> Ordering { |
| 92 | Ord::cmp(&left.priority, &right.priority) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl<'t> From<Leaf<'t>> for Node<Leaf<'t>> { |
| 97 | fn from(leaf: Leaf<'t>) -> Self { |
| 98 | Node::Leaf(leaf) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl Debug for Leaf<'_> { |
| 103 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 104 | write!(f, "::{}", self)?; |
| 105 | |
| 106 | match self.callback { |
| 107 | Some(Callback::Label(ref label)) => write!(f, " ({})", label), |
| 108 | Some(Callback::Inline(_)) => f.write_str(" (<inline>)"), |
| 109 | Some(Callback::Skip(_)) => f.write_str(" (<skip>)"), |
| 110 | Some(Callback::SkipCallback(_)) => f.write_str(" (<skip callback>)"), |
| 111 | None => Ok(()), |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl Display for Leaf<'_> { |
| 117 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 118 | match self.ident { |
| 119 | Some(ident) => Display::fmt(ident, f), |
| 120 | None => f.write_str("<skip>"), |
| 121 | } |
| 122 | } |
| 123 | } |