scope.rs

  1// Copyright (c) 2024 Jonas Schäfer <jonas@zombofant.net>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7//! Identifiers used within generated code.
  8
  9use proc_macro2::Span;
 10use syn::*;
 11
 12use crate::types::ref_ty;
 13
 14/// Container struct for various identifiers used throughout the parser code.
 15///
 16/// This struct is passed around from the [`crate::compound::Compound`]
 17/// downward to the code generators in order to ensure that everyone is on the
 18/// same page about which identifiers are used for what.
 19///
 20/// The recommended usage is to bind the names which are needed into the local
 21/// scope like this:
 22///
 23/// ```text
 24/// # let scope = FromEventsScope::new();
 25/// let FromEventsScope {
 26///     ref attrs,
 27///     ..
 28/// } = scope;
 29/// ```
 30pub(crate) struct FromEventsScope {
 31    /// Accesses the `AttrMap` from code in
 32    /// [`crate::field::FieldBuilderPart::Init`].
 33    pub(crate) attrs: Ident,
 34
 35    /// Accesses the `String` of a `rxml::Event::Text` event from code in
 36    /// [`crate::field::FieldBuilderPart::Text`].
 37    pub(crate) text: Ident,
 38
 39    /// Accesses the builder data during parsing.
 40    ///
 41    /// This should not be used directly outside [`crate::compound`]. Most of
 42    /// the time, using [`Self::access_field`] is the correct way to access
 43    /// the builder data.
 44    pub(crate) builder_data_ident: Ident,
 45}
 46
 47impl FromEventsScope {
 48    /// Create a fresh scope with all necessary identifiers.
 49    pub(crate) fn new() -> Self {
 50        // Sadly, `Ident::new` is not `const`, so we have to create even the
 51        // well-known identifiers from scratch all the time.
 52        Self {
 53            attrs: Ident::new("attrs", Span::call_site()),
 54            text: Ident::new("__xso_proc_macro_text_data", Span::call_site()),
 55            builder_data_ident: Ident::new("__xso_proc_macro_builder_data", Span::call_site()),
 56        }
 57    }
 58
 59    /// Generate an expression which accesses the temporary value for the
 60    /// given `member` during parsing.
 61    pub(crate) fn access_field(&self, member: &Member) -> Expr {
 62        Expr::Field(ExprField {
 63            attrs: Vec::new(),
 64            base: Box::new(Expr::Path(ExprPath {
 65                attrs: Vec::new(),
 66                qself: None,
 67                path: self.builder_data_ident.clone().into(),
 68            })),
 69            dot_token: syn::token::Dot {
 70                spans: [Span::call_site()],
 71            },
 72            member: Member::Named(mangle_member(member)),
 73        })
 74    }
 75}
 76
 77/// Container struct for various identifiers used throughout the generator
 78/// code.
 79///
 80/// This struct is passed around from the [`crate::compound::Compound`]
 81/// downward to the code generators in order to ensure that everyone is on the
 82/// same page about which identifiers are used for what.
 83///
 84/// See [`FromEventsScope`] for recommendations on the usage.
 85pub(crate) struct AsItemsScope {
 86    /// Lifetime for data borrowed by the implementation.
 87    pub(crate) lifetime: Lifetime,
 88}
 89
 90impl AsItemsScope {
 91    /// Create a fresh scope with all necessary identifiers.
 92    pub(crate) fn new(lifetime: &Lifetime) -> Self {
 93        Self {
 94            lifetime: lifetime.clone(),
 95        }
 96    }
 97
 98    /// Create a reference to `ty`, borrowed for the lifetime of the AsXml
 99    /// impl.
100    pub(crate) fn borrow(&self, ty: Type) -> Type {
101        ref_ty(ty, self.lifetime.clone())
102    }
103}
104
105pub(crate) fn mangle_member(member: &Member) -> Ident {
106    match member {
107        Member::Named(member) => quote::format_ident!("f{}", member),
108        Member::Unnamed(member) => quote::format_ident!("f_u{}", member.index),
109    }
110}