Skip to main content

oak_haskell/ast/
mod.rs

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