pub mod visit;
use crate::span::{Span, Spanned};
use smol_str::SmolStr;
#[derive(Debug, Clone, PartialEq)]
pub struct Schema {
pub span: Span,
pub annotations: Vec<Annotation>,
pub namespace: Option<Spanned<NamespaceDecl>>,
pub imports: Vec<Spanned<ImportDecl>>,
pub declarations: Vec<Spanned<Decl>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NamespaceDecl {
pub path: Vec<Spanned<SmolStr>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImportDecl {
pub kind: ImportKind,
pub path: Vec<Spanned<SmolStr>>,
pub version: Option<Spanned<String>>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ImportKind {
Wildcard,
Named { names: Vec<Spanned<SmolStr>> },
Aliased { alias: Spanned<SmolStr> },
}
#[derive(Debug, Clone, PartialEq)]
pub enum Decl {
Message(MessageDecl),
Enum(EnumDecl),
Flags(FlagsDecl),
Union(UnionDecl),
Newtype(NewtypeDecl),
Config(ConfigDecl),
Alias(AliasDecl),
Const(ConstDecl),
Trait(TraitDecl),
Impl(ImplDecl),
}
#[derive(Debug, Clone, PartialEq)]
pub struct MessageDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub body: Vec<MessageBodyItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum MessageBodyItem {
Field(Spanned<MessageField>),
Tombstone(Spanned<Tombstone>),
Invariant(Spanned<MessageInvariant>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct MessageInvariant {
pub name: Option<Spanned<SmolStr>>,
pub condition: Spanned<WhereExpr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MessageField {
pub pre_annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub ordinal: Spanned<u32>,
pub post_ordinal_annotations: Vec<Annotation>,
pub ty: Spanned<TypeExpr>,
pub post_type_annotations: Vec<Annotation>,
pub where_clause: Option<Spanned<WhereExpr>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct EnumDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub backing: Option<Spanned<EnumBacking>>,
pub body: Vec<EnumBodyItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum EnumBodyItem {
Variant(Spanned<EnumVariant>),
Tombstone(Spanned<Tombstone>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct EnumVariant {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub ordinal: Spanned<u32>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum EnumBacking {
U8,
U16,
U32,
U64,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FlagsDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub body: Vec<FlagsBodyItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FlagsBodyItem {
Bit(Spanned<FlagsBit>),
Tombstone(Spanned<Tombstone>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct FlagsBit {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub ordinal: Spanned<u32>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UnionDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub body: Vec<UnionBodyItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum UnionBodyItem {
Variant(Spanned<UnionVariant>),
Tombstone(Spanned<Tombstone>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct UnionVariant {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub ordinal: Spanned<u32>,
pub fields: Vec<MessageBodyItem>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NewtypeDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub inner_type: Spanned<TypeExpr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ConfigDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub fields: Vec<Spanned<ConfigField>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ConfigField {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub ty: Spanned<TypeExpr>,
pub default_value: Spanned<DefaultValue>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TypeParam {
pub name: Spanned<SmolStr>,
pub bounds: Vec<Spanned<SmolStr>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AliasDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub type_params: Vec<TypeParam>,
pub target: Spanned<TypeExpr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ConstDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub ty: Spanned<TypeExpr>,
pub value: Spanned<ConstExpr>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ConstExpr {
Int(i64),
UInt(u64),
Float(f64),
Hex(u64),
Bool(bool),
ConstRef(SmolStr),
BinOp {
op: BinOpKind,
left: Box<ConstExpr>,
right: Box<ConstExpr>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOpKind {
Add,
Sub,
Mul,
Div,
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TraitDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub type_params: Vec<TypeParam>,
pub fields: Vec<MessageField>,
pub functions: Vec<TraitFnDecl>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TraitFnDecl {
pub name: Spanned<SmolStr>,
pub params: Vec<FnParam>,
pub return_type: Option<Spanned<TypeExpr>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FnParam {
pub name: Spanned<SmolStr>,
pub ty: Spanned<TypeExpr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImplFnDecl {
pub annotations: Vec<Annotation>,
pub name: Spanned<SmolStr>,
pub params: Vec<FnParam>,
pub return_type: Option<Spanned<TypeExpr>>,
pub body: ImplFnBody,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ImplFnBody {
External,
Block(Vec<Statement>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Int(i64),
UInt(u64),
Float(f64),
Bool(bool),
String(String),
Ident(SmolStr),
FieldAccess(Box<Expr>, Spanned<SmolStr>),
Call(Box<Expr>, Vec<Expr>),
MethodCall(Box<Expr>, Spanned<SmolStr>, Vec<Expr>),
Binary(BinOpKind, Box<Expr>, Box<Expr>),
Unary(UnaryOpKind, Box<Expr>),
SelfRef,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOpKind {
Neg,
Not,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Expr(Expr),
Let {
name: Spanned<SmolStr>,
ty: Option<Spanned<TypeExpr>>,
value: Expr,
},
Return(Option<Expr>),
Assign { target: Expr, value: Expr },
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImplDecl {
pub annotations: Vec<Annotation>,
pub trait_name: Spanned<SmolStr>,
pub type_args: Vec<Spanned<TypeExpr>>,
pub target_type: Spanned<SmolStr>,
pub functions: Vec<ImplFnDecl>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TypeExpr {
Primitive(PrimitiveType),
SubByte(SubByteType),
Semantic(SemanticType),
Named(SmolStr),
Qualified(SmolStr, SmolStr),
Generic(SmolStr, Box<Spanned<TypeExpr>>),
Optional(Box<Spanned<TypeExpr>>),
Array(Box<Spanned<TypeExpr>>),
FixedArray(Box<Spanned<TypeExpr>>, u64),
Set(Box<Spanned<TypeExpr>>),
Map(Box<Spanned<TypeExpr>>, Box<Spanned<TypeExpr>>),
Result(Box<Spanned<TypeExpr>>, Box<Spanned<TypeExpr>>),
Vec2(Box<Spanned<TypeExpr>>),
Vec3(Box<Spanned<TypeExpr>>),
Vec4(Box<Spanned<TypeExpr>>),
Quat(Box<Spanned<TypeExpr>>),
Mat3(Box<Spanned<TypeExpr>>),
Mat4(Box<Spanned<TypeExpr>>),
BitsInline(Vec<SmolStr>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PrimitiveType {
Bool,
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
F32,
F64,
Fixed32,
Fixed64,
Void,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SubByteType {
pub signed: bool,
pub bits: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SemanticType {
String,
Bytes,
Rgb,
Uuid,
Timestamp,
Hash,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Annotation {
pub span: Span,
pub name: Spanned<SmolStr>,
pub args: Option<Vec<AnnotationArg>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AnnotationArg {
pub span: Span,
pub key: Option<Spanned<SmolStr>>,
pub value: Spanned<AnnotationValue>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AnnotationValue {
Int(u64),
Hex(u64),
Str(String),
Bool(bool),
Ident(SmolStr),
UpperIdent(SmolStr),
}
#[derive(Debug, Clone, PartialEq)]
pub enum WhereExpr {
And(Box<Spanned<WhereExpr>>, Box<Spanned<WhereExpr>>),
Or(Box<Spanned<WhereExpr>>, Box<Spanned<WhereExpr>>),
Not(Box<Spanned<WhereExpr>>),
Cmp {
op: CmpOp,
operand: Box<Spanned<WhereOperand>>,
},
Range {
low: Box<Spanned<WhereOperand>>,
high: Box<Spanned<WhereOperand>>,
exclusive_high: bool, },
LenCmp {
op: CmpOp,
operand: Box<Spanned<WhereOperand>>,
},
LenRange {
low: Box<Spanned<WhereOperand>>,
high: Box<Spanned<WhereOperand>>,
exclusive_high: bool,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CmpOp {
Eq,
Ne,
Lt,
Gt,
Le,
Ge,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WhereOperand {
Int(i64),
Float(f64),
String(String),
Bool(bool),
Value,
ConstRef(SmolStr),
}
#[derive(Debug, Clone, PartialEq)]
pub enum DefaultValue {
None,
Bool(bool),
Int(i64),
UInt(u64),
Float(f64),
Str(String),
Ident(SmolStr),
UpperIdent(SmolStr),
Array(Vec<Spanned<DefaultValue>>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Tombstone {
pub ordinal: Spanned<u32>,
pub args: Vec<TombstoneArg>,
pub original_type: Option<Spanned<TypeExpr>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TombstoneArg {
pub span: Span,
pub key: Spanned<SmolStr>,
pub value: Spanned<AnnotationValue>,
}