1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::{collections::HashMap, fmt::Display};
use anyhow::anyhow;
use enum_as_inner::EnumAsInner;
use semver::VersionReq;
use serde::{Deserialize, Serialize};
use crate::error::Span;
use super::*;
pub struct Statements(pub Vec<Stmt>);
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Stmt {
#[serde(skip)]
pub id: Option<usize>,
#[serde(flatten)]
pub kind: StmtKind,
#[serde(skip)]
pub span: Option<Span>,
}
#[derive(Debug, EnumAsInner, PartialEq, Clone, Serialize, Deserialize)]
pub enum StmtKind {
QueryDef(QueryDef),
FuncDef(FuncDef),
VarDef(VarDef),
TypeDef(TypeDef),
Main(Box<Expr>),
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default)]
pub struct QueryDef {
pub version: Option<VersionReq>,
#[serde(default)]
pub other: HashMap<String, String>,
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct FuncDef {
pub name: String,
pub positional_params: Vec<FuncParam>, pub named_params: Vec<FuncParam>, pub body: Box<Expr>,
pub return_ty: Option<Expr>,
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct FuncParam {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub ty_expr: Option<Expr>,
pub default_value: Option<Expr>,
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct VarDef {
pub name: String,
pub value: Box<Expr>,
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct TypeDef {
pub name: String,
pub value: Option<Expr>,
}
impl From<StmtKind> for Stmt {
fn from(kind: StmtKind) -> Self {
Stmt {
kind,
span: None,
id: None,
}
}
}
impl From<StmtKind> for anyhow::Error {
#[allow(unreachable_code)]
fn from(item: StmtKind) -> Self {
anyhow!("Failed to convert statement `{item}`")
}
}
impl Display for Statements {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for stmt in &self.0 {
write!(f, "{}", stmt.kind)?;
write!(f, "\n\n")?;
}
Ok(())
}
}
impl Display for StmtKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StmtKind::QueryDef(query) => {
write!(f, "prql")?;
if let Some(version) = &query.version {
write!(f, " version:{}", version)?;
}
for (key, value) in &query.other {
write!(f, " {key}:{value}")?;
}
write!(f, "\n\n")?;
}
StmtKind::Main(expr) => match &expr.kind {
ExprKind::Pipeline(pipeline) => {
for expr in &pipeline.exprs {
writeln!(f, "{expr}")?;
}
}
_ => writeln!(f, "{}", expr)?,
},
StmtKind::FuncDef(func_def) => {
writeln!(f, "{func_def}\n")?;
}
StmtKind::VarDef(var) => {
let pipeline = &var.value;
match &pipeline.kind {
ExprKind::FuncCall(_) => {
write!(f, "let {} = (\n {pipeline}\n)\n\n", var.name)?;
}
_ => {
write!(f, "let {} = {pipeline}\n\n", var.name)?;
}
};
}
StmtKind::TypeDef(ty_def) => {
if let Some(value) = &ty_def.value {
write!(f, "type {} = {value}\n\n", ty_def.name)?;
} else {
write!(f, "type {}\n\n", ty_def.name)?;
}
}
}
Ok(())
}
}
impl Display for FuncDef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "func {}", self.name)?;
for arg in &self.positional_params {
write!(f, " {}", arg.name)?;
}
for arg in &self.named_params {
write!(f, " {}:{}", arg.name, arg.default_value.as_ref().unwrap())?;
}
write!(f, " -> {}", self.body)
}
}