use alloc::boxed::Box;
use alloc::sync::Arc;
use core::fmt;
use crate::source::{Source, SourceOrigin, TextContent};
use crate::spec::CallableSpec;
use crate::state::{InvocationSyntax, Lang};
use super::arguments::{ParsedArguments, ParsedSlots};
pub enum NodeKind<L: Lang> {
Chars {
content: TextContent,
},
Group(Box<GroupData<L>>),
Callable(Box<CallableData<L>>),
Comment(Box<CommentData>),
List,
}
impl<L: Lang> NodeKind<L> {
pub fn chars(content: impl Into<TextContent>) -> NodeKind<L> {
NodeKind::Chars { content: content.into() }
}
pub fn group(data: GroupData<L>) -> NodeKind<L> {
NodeKind::Group(Box::new(data))
}
pub fn callable(data: CallableData<L>) -> NodeKind<L> {
NodeKind::Callable(Box::new(data))
}
pub fn comment(
start: impl Into<TextContent>,
content: impl Into<TextContent>,
post_space: impl Into<TextContent>,
) -> NodeKind<L> {
NodeKind::Comment(Box::new(CommentData {
start: start.into(),
content: content.into(),
post_space: post_space.into(),
}))
}
pub fn list() -> NodeKind<L> {
NodeKind::List
}
pub const fn as_str(&self) -> &'static str {
match self {
NodeKind::Chars { .. } => "Chars",
NodeKind::Group(_) => "Group",
NodeKind::Callable(_) => "Callable",
NodeKind::Comment(_) => "Comment",
NodeKind::List => "List",
}
}
pub(crate) fn materialized(&self, source: &Source<L::SourceOrigin>) -> NodeKind<L> {
match self {
NodeKind::Chars { content } => NodeKind::Chars {
content: content.materialized(source),
},
NodeKind::Group(data) => {
NodeKind::Group(Box::new(data.materialized(source)))
}
NodeKind::Callable(data) => {
NodeKind::Callable(Box::new(data.materialized(source)))
}
NodeKind::Comment(data) => {
NodeKind::Comment(Box::new(data.materialized(source)))
}
NodeKind::List => NodeKind::List,
}
}
}
pub struct GroupData<L: Lang> {
pub group_type: Option<L::GroupTypeId>,
pub open: TextContent,
pub close: TextContent,
}
impl<L: Lang> GroupData<L> {
pub fn new(
group_type: L::GroupTypeId,
open: impl Into<TextContent>,
close: impl Into<TextContent>,
) -> GroupData<L> {
GroupData {
group_type: Some(group_type),
open: open.into(),
close: close.into(),
}
}
pub fn untyped(
open: impl Into<TextContent>,
close: impl Into<TextContent>,
) -> GroupData<L> {
GroupData { group_type: None, open: open.into(), close: close.into() }
}
fn materialized(&self, source: &Source<L::SourceOrigin>) -> GroupData<L> {
GroupData {
group_type: self.group_type,
open: self.open.materialized(source),
close: self.close.materialized(source),
}
}
}
pub struct CallableData<L: Lang> {
pub callable_type: L::CallableTypeId,
pub name: Box<str>,
pub spec: Arc<dyn CallableSpec<L>>,
pub arguments: ParsedArguments<L>,
pub slots: ParsedSlots<L>,
pub invocation_syntax: L::InvocationSyntax,
}
impl<L: Lang> CallableData<L> {
fn materialized(&self, source: &Source<L::SourceOrigin>) -> CallableData<L> {
CallableData {
callable_type: self.callable_type,
name: self.name.clone(),
spec: Arc::clone(&self.spec),
arguments: self.arguments.clone(),
slots: self.slots.clone(),
invocation_syntax: self.invocation_syntax.materialized(source),
}
}
}
#[derive(Clone, Debug)]
pub struct CommentData {
pub start: TextContent,
pub content: TextContent,
pub post_space: TextContent,
}
impl CommentData {
fn materialized<O: SourceOrigin>(&self, source: &Source<O>) -> CommentData {
CommentData {
start: self.start.materialized(source),
content: self.content.materialized(source),
post_space: self.post_space.materialized(source),
}
}
}
impl<L: Lang> Clone for NodeKind<L> {
fn clone(&self) -> Self {
match self {
NodeKind::Chars { content } => NodeKind::Chars { content: content.clone() },
NodeKind::Group(data) => NodeKind::Group(data.clone()),
NodeKind::Callable(data) => NodeKind::Callable(data.clone()),
NodeKind::Comment(data) => NodeKind::Comment(data.clone()),
NodeKind::List => NodeKind::List,
}
}
}
impl<L: Lang> fmt::Debug for NodeKind<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NodeKind::Chars { content } => {
f.debug_struct("Chars").field("content", content).finish()
}
NodeKind::Group(data) => f.debug_tuple("Group").field(data).finish(),
NodeKind::Callable(data) => f.debug_tuple("Callable").field(data).finish(),
NodeKind::Comment(data) => f.debug_tuple("Comment").field(data).finish(),
NodeKind::List => f.debug_struct("List").finish(),
}
}
}
impl<L: Lang> Clone for GroupData<L> {
fn clone(&self) -> Self {
GroupData {
group_type: self.group_type,
open: self.open.clone(),
close: self.close.clone(),
}
}
}
impl<L: Lang> fmt::Debug for GroupData<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GroupData")
.field("group_type", &self.group_type)
.field("open", &self.open)
.field("close", &self.close)
.finish()
}
}
impl<L: Lang> Clone for CallableData<L> {
fn clone(&self) -> Self {
CallableData {
callable_type: self.callable_type,
name: self.name.clone(),
spec: Arc::clone(&self.spec),
arguments: self.arguments.clone(),
slots: self.slots.clone(),
invocation_syntax: self.invocation_syntax.clone(),
}
}
}
impl<L: Lang> fmt::Debug for CallableData<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CallableData")
.field("callable_type", &self.callable_type)
.field("name", &self.name)
.field("spec", &self.spec)
.field("arguments", &self.arguments)
.field("slots", &self.slots)
.field("invocation_syntax", &self.invocation_syntax)
.finish()
}
}