use crate::v2::span::Span;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsName {
pub text: String,
pub span: Span,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CsTypeKind {
Class,
Struct,
Interface,
Enum,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CsNodeKind {
Unit,
Using,
Namespace(CsName),
Type {
kind: CsTypeKind,
name: CsName,
bases: Vec<CsName>,
},
Method(CsName),
Ctor(CsName),
Property(CsName),
EnumMember(CsName),
Param(CsName),
Decl,
NameDecl(CsName),
Block,
NameRef {
name: CsName,
after_dot: bool,
receiver: Option<String>,
},
Attribute(CsName),
Error,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsNode {
pub kind: CsNodeKind,
pub span: Span,
pub children: Vec<CsNode>,
}
impl CsNode {
pub fn new(kind: CsNodeKind, span: Span, children: Vec<CsNode>) -> Self {
CsNode {
kind,
span,
children,
}
}
pub fn leaf(kind: CsNodeKind, span: Span) -> Self {
CsNode {
kind,
span,
children: Vec::new(),
}
}
pub fn declared_name(&self) -> Option<&CsName> {
match &self.kind {
CsNodeKind::Namespace(n)
| CsNodeKind::Type { name: n, .. }
| CsNodeKind::Method(n)
| CsNodeKind::Ctor(n)
| CsNodeKind::Property(n)
| CsNodeKind::EnumMember(n)
| CsNodeKind::Param(n)
| CsNodeKind::NameDecl(n) => Some(n),
_ => None,
}
}
pub fn is_scope(&self) -> bool {
matches!(
self.kind,
CsNodeKind::Unit
| CsNodeKind::Namespace(_)
| CsNodeKind::Type { .. }
| CsNodeKind::Method(_)
| CsNodeKind::Ctor(_)
| CsNodeKind::Property(_)
| CsNodeKind::Block
)
}
pub fn walk(&self, f: &mut impl FnMut(&CsNode)) {
f(self);
for c in &self.children {
c.walk(f);
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsUnit {
pub root: CsNode,
pub errors: Vec<Span>,
}