use std::borrow::Cow;
use serde::Serialize;
use crate::Span;
use super::ArenaVec;
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct Ident<'src>(&'src str);
impl<'src> Ident<'src> {
pub const ERROR: Self = Self("");
#[inline]
pub fn name(s: &'src str) -> Self {
debug_assert!(!s.is_empty(), "Ident::name() called with empty string");
Self(s)
}
#[inline]
pub fn as_str(&self) -> Option<&'src str> {
if self.0.is_empty() {
None
} else {
Some(self.0)
}
}
#[inline]
pub fn is_error(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn or_error(&self) -> &'src str {
if self.0.is_empty() {
"<error>"
} else {
self.0
}
}
}
impl<'src> std::fmt::Debug for Ident<'src> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.0.is_empty() {
f.write_str("Ident::ERROR")
} else {
f.debug_tuple("Ident").field(&self.0).finish()
}
}
}
impl<'src> std::fmt::Display for Ident<'src> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.or_error())
}
}
impl<'src> serde::Serialize for Ident<'src> {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if self.0.is_empty() {
s.serialize_none()
} else {
s.serialize_str(self.0)
}
}
}
impl<'src> PartialEq<&str> for Ident<'src> {
fn eq(&self, other: &&str) -> bool {
!self.0.is_empty() && self.0 == *other
}
}
#[cfg(test)]
mod ident_layout_tests {
use super::Ident;
#[test]
fn ident_has_same_size_as_str_slice() {
assert_eq!(std::mem::size_of::<Ident>(), std::mem::size_of::<&str>());
assert_eq!(
std::mem::size_of::<Option<Ident>>(),
std::mem::size_of::<Option<&str>>()
);
}
}
pub enum Name<'arena, 'src> {
Simple { value: &'src str, span: Span },
Complex {
parts: ArenaVec<'arena, &'src str>,
kind: NameKind,
span: Span,
},
Error { span: Span },
}
impl<'arena, 'src> Name<'arena, 'src> {
#[inline]
pub fn span(&self) -> Span {
match self {
Self::Simple { span, .. } | Self::Complex { span, .. } | Self::Error { span } => *span,
}
}
#[inline]
pub fn kind(&self) -> NameKind {
match self {
Self::Simple { .. } => NameKind::Unqualified,
Self::Complex { kind, .. } => *kind,
Self::Error { .. } => NameKind::Error,
}
}
#[inline]
pub fn src_repr(&self, src: &'src str) -> &'src str {
match self {
Self::Simple { value, .. } => value,
Self::Complex { span, .. } => &src[span.start as usize..span.end as usize],
Self::Error { .. } => "",
}
}
#[inline]
pub fn to_string_repr(&self) -> Cow<'src, str> {
match self {
Self::Simple { value, .. } => Cow::Borrowed(value),
Self::Complex { parts, kind, .. } => {
let joined = parts.join("\\");
if *kind == NameKind::FullyQualified {
Cow::Owned(format!("\\{}", joined))
} else {
Cow::Owned(joined)
}
}
Self::Error { .. } => Cow::Borrowed(""),
}
}
#[inline]
pub fn join_parts(&self) -> Cow<'src, str> {
match self {
Self::Simple { value, .. } => Cow::Borrowed(value),
Self::Complex { parts, .. } => Cow::Owned(parts.join("\\")),
Self::Error { .. } => Cow::Borrowed(""),
}
}
#[inline]
pub fn parts_slice(&self) -> &[&'src str] {
match self {
Self::Simple { value, .. } => std::slice::from_ref(value),
Self::Complex { parts, .. } => parts,
Self::Error { .. } => &[],
}
}
}
impl<'arena, 'src> std::fmt::Debug for Name<'arena, 'src> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Simple { value, span } => f
.debug_struct("Name")
.field("parts", &std::slice::from_ref(value))
.field("kind", &NameKind::Unqualified)
.field("span", span)
.finish(),
Self::Complex { parts, kind, span } => f
.debug_struct("Name")
.field("parts", parts)
.field("kind", kind)
.field("span", span)
.finish(),
Self::Error { span } => {
let empty: [&str; 0] = [];
f.debug_struct("Name")
.field("parts", &empty)
.field("kind", &NameKind::Error)
.field("span", span)
.finish()
}
}
}
}
impl<'arena, 'src> serde::Serialize for Name<'arena, 'src> {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeStruct;
let mut st = s.serialize_struct("Name", 3)?;
match self {
Self::Simple { value, span } => {
st.serialize_field("parts", std::slice::from_ref(value))?;
st.serialize_field("kind", &NameKind::Unqualified)?;
st.serialize_field("span", span)?;
}
Self::Complex { parts, kind, span } => {
st.serialize_field("parts", parts)?;
st.serialize_field("kind", kind)?;
st.serialize_field("span", span)?;
}
Self::Error { span } => {
let empty: [&str; 0] = [];
st.serialize_field("parts", &empty[..])?;
st.serialize_field("kind", &NameKind::Error)?;
st.serialize_field("span", span)?;
}
}
st.end()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum NameKind {
Unqualified,
Qualified,
FullyQualified,
Relative,
Error,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum BuiltinType {
Int,
Integer,
Float,
Double,
String,
Bool,
Boolean,
Void,
Never,
Mixed,
Object,
Iterable,
Callable,
Array,
Self_,
Parent_,
Static,
Null,
True,
False,
}
impl BuiltinType {
#[inline]
pub fn as_str(self) -> &'static str {
match self {
Self::Int => "int",
Self::Integer => "integer",
Self::Float => "float",
Self::Double => "double",
Self::String => "string",
Self::Bool => "bool",
Self::Boolean => "boolean",
Self::Void => "void",
Self::Never => "never",
Self::Mixed => "mixed",
Self::Object => "object",
Self::Iterable => "iterable",
Self::Callable => "callable",
Self::Array => "array",
Self::Self_ => "self",
Self::Parent_ => "parent",
Self::Static => "static",
Self::Null => "null",
Self::True => "true",
Self::False => "false",
}
}
}
#[derive(Debug, Serialize)]
pub struct TypeHint<'arena, 'src> {
pub kind: TypeHintKind<'arena, 'src>,
pub span: Span,
}
#[derive(Debug)]
pub enum TypeHintKind<'arena, 'src> {
Named(Name<'arena, 'src>),
Keyword(BuiltinType, Span),
Nullable(&'arena TypeHint<'arena, 'src>),
Union(ArenaVec<'arena, TypeHint<'arena, 'src>>),
Intersection(ArenaVec<'arena, TypeHint<'arena, 'src>>),
}
impl<'arena, 'src> serde::Serialize for TypeHintKind<'arena, 'src> {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
match self {
Self::Named(name) => s.serialize_newtype_variant("TypeHintKind", 0, "Named", name),
Self::Nullable(inner) => {
s.serialize_newtype_variant("TypeHintKind", 2, "Nullable", inner)
}
Self::Union(types) => s.serialize_newtype_variant("TypeHintKind", 3, "Union", types),
Self::Intersection(types) => {
s.serialize_newtype_variant("TypeHintKind", 4, "Intersection", types)
}
Self::Keyword(builtin, span) => {
struct BuiltinNameRepr<'a>(&'a BuiltinType, &'a Span);
impl serde::Serialize for BuiltinNameRepr<'_> {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeStruct;
let mut st = s.serialize_struct("Name", 3)?;
st.serialize_field("parts", &[self.0.as_str()])?;
st.serialize_field("kind", &NameKind::Unqualified)?;
st.serialize_field("span", self.1)?;
st.end()
}
}
s.serialize_newtype_variant(
"TypeHintKind",
0,
"Named",
&BuiltinNameRepr(builtin, span),
)
}
}
}
}