Skip to main content

oak_crystal/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use core::range::Range;
4
5/// Identifier in the Crystal language
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[derive(Debug, PartialEq, Eq, Clone)]
8pub struct Identifier {
9    /// Name of the identifier.
10    pub name: String,
11    /// Range of the identifier in the source text.
12    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
13    pub span: Range<usize>,
14}
15
16/// Crystal AST root
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[derive(Debug, PartialEq, Clone)]
19pub struct CrystalRoot {
20    /// Items in the root.
21    pub items: Vec<Item>,
22}
23
24/// Top-level items: classes, modules, methods, etc.
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26#[derive(Debug, PartialEq, Clone)]
27pub enum Item {
28    /// Class declaration.
29    Class(ClassDeclaration),
30    /// Module declaration.
31    Module(ModuleDeclaration),
32    /// Method declaration.
33    Def(MethodDeclaration),
34    /// An expression.
35    Expression(Expression),
36}
37
38/// Class declaration in Crystal
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40#[derive(Debug, PartialEq, Clone)]
41pub struct ClassDeclaration {
42    /// Name of the class.
43    pub name: Identifier,
44    /// Body of the class.
45    pub body: Vec<Item>,
46    /// Range of the class declaration in the source text.
47    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
48    pub span: Range<usize>,
49}
50
51/// Module declaration in Crystal
52#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
53#[derive(Debug, PartialEq, Clone)]
54pub struct ModuleDeclaration {
55    /// Name of the module.
56    pub name: Identifier,
57    /// Body of the module.
58    pub body: Vec<Item>,
59    /// Range of the module declaration in the source text.
60    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
61    pub span: Range<usize>,
62}
63
64/// Method declaration in Crystal
65#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66#[derive(Debug, PartialEq, Clone)]
67pub struct MethodDeclaration {
68    /// Name of the method.
69    pub name: Identifier,
70    /// Parameters of the method.
71    pub params: Vec<Parameter>,
72    /// Body of the method.
73    pub body: Vec<Item>,
74    /// Range of the method declaration in the source text.
75    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
76    pub span: Range<usize>,
77}
78
79/// Parameter in a method declaration
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81#[derive(Debug, PartialEq, Clone)]
82pub struct Parameter {
83    /// Name of the parameter.
84    pub name: Identifier,
85    /// Type of the parameter.
86    pub type_name: Option<Identifier>,
87}
88
89/// Basic expression
90#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
91#[derive(Debug, PartialEq, Clone)]
92pub enum Expression {
93    /// A literal.
94    Literal(Literal),
95    /// A method call.
96    Call(Call),
97}
98
99/// Literals in Crystal
100#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
101#[derive(Debug, PartialEq, Clone)]
102pub enum Literal {
103    /// Numeric literal.
104    Number(String),
105    /// String literal.
106    String(String),
107    /// Boolean literal.
108    Boolean(bool),
109    /// `nil` literal.
110    Nil,
111}
112
113/// Method call
114#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
115#[derive(Debug, PartialEq, Clone)]
116pub struct Call {
117    /// Receiver of the call.
118    pub receiver: Option<Box<Expression>>,
119    /// Name of the method called.
120    pub name: Identifier,
121    /// Arguments passed to the call.
122    pub args: Vec<Expression>,
123}