Skip to main content

oak_haskell/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4/// An identifier in Haskell.
5#[derive(Clone, Debug, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct Identifier {
8    /// The name of the identifier.
9    pub name: String,
10    /// The span of the identifier in the source file.
11    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
12    pub span: Range<usize>,
13}
14
15/// The root of a Haskell Abstract Syntax Tree.
16#[derive(Clone, Debug, PartialEq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct HaskellRoot {
19    /// The name of the module, if present.
20    pub module_name: Option<Identifier>,
21    /// The items in the module.
22    pub items: Vec<Item>,
23}
24
25/// A top-level item in a Haskell module.
26#[derive(Clone, Debug, PartialEq)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub enum Item {
29    /// A function definition.
30    Function(Function),
31    /// A data type declaration.
32    DataDeclaration(DataDeclaration),
33    /// A type alias declaration.
34    TypeAlias(TypeAlias),
35    /// An import declaration.
36    Import(Import),
37}
38
39/// A function definition in Haskell.
40#[derive(Clone, Debug, PartialEq)]
41#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
42pub struct Function {
43    /// The name of the function.
44    pub name: Identifier,
45    /// The type signature of the function, if present.
46    pub type_signature: Option<Type>,
47    /// The equations defining the function.
48    pub equations: Vec<Equation>,
49    /// The span of the function in the source file.
50    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
51    pub span: Range<usize>,
52}
53
54/// An equation in a function definition.
55#[derive(Clone, Debug, PartialEq)]
56#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
57pub struct Equation {
58    /// The patterns in the equation.
59    pub patterns: Vec<Pattern>,
60    /// The body of the equation.
61    pub body: Expression,
62}
63
64/// A pattern in a function definition or case expression.
65#[derive(Clone, Debug, PartialEq)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67pub enum Pattern {
68    /// A wildcard pattern `_`.
69    Wildcard,
70    /// A variable pattern.
71    Variable(Identifier),
72    /// A constructor pattern.
73    Constructor(Identifier, Vec<Pattern>),
74    /// A literal pattern.
75    Literal(Literal),
76}
77
78/// An expression in Haskell.
79#[derive(Clone, Debug, PartialEq)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81pub enum Expression {
82    /// A variable expression.
83    Variable(Identifier),
84    /// A literal expression.
85    Literal(Literal),
86    /// A function application.
87    Application(Box<Expression>, Box<Expression>),
88    /// A lambda expression.
89    Lambda(Vec<Pattern>, Box<Expression>),
90    /// A let expression.
91    Let(Vec<Item>, Box<Expression>),
92    /// A case expression.
93    Case(Box<Expression>, Vec<CaseArm>),
94}
95
96/// A case arm in a case expression.
97#[derive(Clone, Debug, PartialEq)]
98#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
99pub struct CaseArm {
100    /// The pattern to match.
101    pub pattern: Pattern,
102    /// The body of the case arm.
103    pub body: Expression,
104}
105
106/// A type in Haskell.
107#[derive(Clone, Debug, PartialEq)]
108#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
109pub enum Type {
110    /// A type variable.
111    Variable(Identifier),
112    /// A type constructor.
113    Constructor(Identifier, Vec<Type>),
114    /// A function type.
115    Function(Box<Type>, Box<Type>),
116}
117
118/// A data type declaration in Haskell.
119#[derive(Clone, Debug, PartialEq)]
120#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
121pub struct DataDeclaration {
122    /// The name of the data type.
123    pub name: Identifier,
124    /// The type parameters of the data type.
125    pub type_params: Vec<Identifier>,
126    /// The constructors of the data type.
127    pub constructors: Vec<ConstructorDef>,
128}
129
130/// A constructor definition in a data declaration.
131#[derive(Clone, Debug, PartialEq)]
132#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
133pub struct ConstructorDef {
134    /// The name of the constructor.
135    pub name: Identifier,
136    /// The fields of the constructor.
137    pub fields: Vec<Type>,
138}
139
140/// A type alias declaration in Haskell.
141#[derive(Clone, Debug, PartialEq)]
142#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
143pub struct TypeAlias {
144    /// The name of the type alias.
145    pub name: Identifier,
146    /// The type parameters of the type alias.
147    pub type_params: Vec<Identifier>,
148    /// The target type of the type alias.
149    pub target: Type,
150}
151
152/// An import declaration in Haskell.
153#[derive(Clone, Debug, PartialEq)]
154#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
155pub struct Import {
156    /// The module to import.
157    pub module: Identifier,
158    /// Whether the import is qualified.
159    pub qualified: bool,
160    /// The name to import the module as, if present.
161    pub as_name: Option<Identifier>,
162}
163
164/// A literal in Haskell.
165#[derive(Clone, Debug, PartialEq)]
166#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
167pub enum Literal {
168    /// An integer literal.
169    Integer(i64),
170    /// A float literal.
171    Float(f64),
172    /// A string literal.
173    String(String),
174    /// A character literal.
175    Char(char),
176}