sycamore_view_parser/
ir.rs

1//! Intermediate representation for `view!` macro syntax.
2
3use proc_macro2::Span;
4use syn::spanned::Spanned;
5use syn::{Expr, Ident, LitStr, Path};
6
7/// A list of nodes. This is the top-level syntax node and entry-point for parsing.
8pub struct Root(pub Vec<Node>);
9
10pub enum Node {
11    Tag(TagNode),
12    Text(TextNode),
13    Dyn(DynNode),
14}
15
16pub enum NodeType {
17    Tag,
18    Text,
19    Dyn,
20}
21
22pub struct TagNode {
23    pub ident: TagIdent,
24    pub props: Vec<Prop>,
25    pub children: Root,
26}
27
28pub enum TagIdent {
29    /// A standard Rust path.
30    Path(Path),
31    /// A hyphenated ident. Can not include any paths.
32    /// This is used for custom elements support.
33    Hyphenated(String),
34}
35
36impl TagIdent {
37    pub fn span(&self) -> Span {
38        match self {
39            Self::Path(path) => path.span(),
40            Self::Hyphenated(_) => Span::call_site(),
41        }
42    }
43}
44
45pub struct Prop {
46    pub ty: PropType,
47    pub value: Expr,
48    pub span: Span,
49}
50
51pub enum PropType {
52    /// Syntax: `<name>=<expr>`.
53    Plain { ident: Ident },
54    /// Syntax: `<hyphenated-name>=<expr>`.
55    PlainHyphenated { ident: String },
56    /// Syntax: `"<quoted-name>"=<expr>`.
57    PlainQuoted { ident: String },
58    /// Syntax: `<dir>:<prop>=<expr>`.
59    Directive { dir: Ident, ident: Ident },
60    /// Syntax: `r#ref=<expr>`.
61    Ref,
62    /// Syntax: `..attributes=<expr>`
63    Spread,
64}
65
66pub struct TextNode {
67    pub value: LitStr,
68}
69
70pub struct DynNode {
71    pub value: Expr,
72}