typeql 3.8.3-rc0

TypeQL Language for Rust
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

use std::fmt::{self, Debug, Formatter, Write};

use crate::{
    common::{identifier::Identifier, token, Span, Spanned},
    pretty::{indent, Pretty},
    query::stage::{reduce::Reducer, Stage},
    type_::NamedTypeAny,
    util::write_joined,
    variable::Variable,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Function {
    pub span: Option<Span>,
    pub signature: Signature,
    pub block: FunctionBlock,
    pub unparsed: String,
}

impl Function {
    pub fn new(span: Option<Span>, signature: Signature, block: FunctionBlock, unparsed: String) -> Self {
        Self { span, signature, block, unparsed }
    }
}

impl Spanned for Function {
    fn span(&self) -> Option<Span> {
        self.span
    }
}

impl Pretty for Function {
    fn fmt(&self, indent_level: usize, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        indent(indent_level, f)?;
        write!(f, "{} ", token::Keyword::Fun)?;
        Pretty::fmt(&self.signature, indent_level, f)?;
        f.write_char(':')?;
        f.write_str("\n")?;
        Pretty::fmt(&self.block, indent_level + 1, f)?;
        Ok(())
    }
}

impl fmt::Display for Function {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            Pretty::fmt(self, 0, f)
        } else {
            write!(f, "{} ", token::Keyword::Fun)?;
            std::fmt::Display::fmt(&self.signature, f)?;
            write!(f, "{} ", token::Char::Colon)?;
            std::fmt::Display::fmt(&self.block, f)
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signature {
    pub span: Option<Span>,
    pub ident: Identifier,
    pub args: Vec<Argument>,
    pub output: Output,
}

impl Signature {
    pub(crate) fn new(span: Option<Span>, ident: Identifier, args: Vec<Argument>, output: Output) -> Self {
        Self { span, ident, args, output }
    }
}

impl Pretty for Signature {
    fn fmt(&self, indent_level: usize, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}(", self.ident)?;
        if !self.args.is_empty() {
            Pretty::fmt(&self.args[0], indent_level, f)?;
            self.args[1..self.args.len()].iter().try_for_each(|arg| {
                f.write_str(", ")?;
                Pretty::fmt(arg, indent_level, f)
            })?;
        }
        write!(f, ") -> ")?;
        Pretty::fmt(&self.output, indent_level, f)?;
        Ok(())
    }
}

impl fmt::Display for Signature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            Pretty::fmt(self, 0, f)
        } else {
            write!(f, "{}(", self.ident)?;
            if !self.args.is_empty() {
                write!(f, "{}", self.args[0])?;
                self.args[1..self.args.len()].iter().try_for_each(|arg| write!(f, ", {arg}"))?;
            }
            write!(f, ") -> {}", self.output)?;
            Ok(())
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Argument {
    pub span: Option<Span>,
    pub var: Variable,
    pub type_: NamedTypeAny,
}

impl Argument {
    pub fn new(span: Option<Span>, var: Variable, type_: NamedTypeAny) -> Self {
        Self { span, var, type_ }
    }
}

impl Pretty for Argument {}
impl fmt::Display for Argument {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.var, self.type_)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Output {
    Stream(Stream),
    Single(Single),
}

impl Spanned for Output {
    fn span(&self) -> Option<Span> {
        match self {
            Self::Stream(inner) => inner.span(),
            Self::Single(inner) => inner.span(),
        }
    }
}

impl Pretty for Output {
    fn fmt(&self, indent_level: usize, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Stream(inner) => Pretty::fmt(inner, indent_level, f),
            Self::Single(inner) => Pretty::fmt(inner, indent_level, f),
        }
    }
}

impl fmt::Display for Output {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Stream(inner) => fmt::Display::fmt(inner, f),
            Self::Single(inner) => fmt::Display::fmt(inner, f),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stream {
    pub span: Option<Span>,
    pub types: Vec<NamedTypeAny>,
}

impl Stream {
    pub fn new(span: Option<Span>, types: Vec<NamedTypeAny>) -> Self {
        Self { span, types }
    }
}

impl Spanned for Stream {
    fn span(&self) -> Option<Span> {
        self.span
    }
}

impl Pretty for Stream {}

impl fmt::Display for Stream {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{ ")?;
        write_joined!(f, ", ", self.types)?;
        write!(f, " }}")
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Single {
    pub span: Option<Span>,
    pub types: Vec<NamedTypeAny>,
}

impl Single {
    pub fn new(span: Option<Span>, types: Vec<NamedTypeAny>) -> Self {
        Self { span, types }
    }
}

impl Spanned for Single {
    fn span(&self) -> Option<Span> {
        self.span
    }
}

impl Pretty for Single {}

impl fmt::Display for Single {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write_joined!(f, ", ", self.types)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionBlock {
    pub span: Option<Span>,
    pub stages: Vec<Stage>,
    pub return_stmt: ReturnStatement,
}

impl FunctionBlock {
    pub fn new(span: Option<Span>, stages: Vec<Stage>, return_stmt: ReturnStatement) -> Self {
        Self { span, stages, return_stmt }
    }
}

impl Pretty for FunctionBlock {
    fn fmt(&self, indent_level: usize, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for stage in &self.stages {
            Pretty::fmt(stage, indent_level, f)?;
        }
        writeln!(f)?;
        Pretty::fmt(&self.return_stmt, indent_level, f)
    }
}

impl fmt::Display for FunctionBlock {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for stage in &self.stages {
            fmt::Display::fmt(stage, f)?;
        }
        write!(f, " ")?;
        fmt::Display::fmt(&self.return_stmt, f)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReturnStatement {
    Stream(ReturnStream),
    Single(ReturnSingle),
    Reduce(ReturnReduction),
}

impl Pretty for ReturnStatement {
    fn fmt(&self, indent_level: usize, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        indent(indent_level, f)?;
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for ReturnStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ReturnStatement::Stream(return_stream) => {
                write!(f, "{} {};", token::Keyword::Return, return_stream)
            }
            ReturnStatement::Single(return_single) => {
                write!(f, "{} {};", token::Keyword::Return, return_single)
            }
            ReturnStatement::Reduce(reduction) => {
                write!(f, "{} {};", token::Keyword::Return, reduction)
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReturnStream {
    pub span: Option<Span>,
    pub vars: Vec<Variable>,
}

impl ReturnStream {
    pub fn new(span: Option<Span>, vars: Vec<Variable>) -> Self {
        Self { span, vars }
    }
}

impl Spanned for ReturnStream {
    fn span(&self) -> Option<Span> {
        self.span
    }
}

impl Pretty for ReturnStream {}

impl fmt::Display for ReturnStream {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        assert!(!self.vars.is_empty());
        write!(f, "{} {}", token::Char::CurlyLeft, self.vars[0])?;
        for var in &self.vars[1..] {
            write!(f, ", {}", var)?;
        }
        write!(f, " {}", token::Char::CurlyRight)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReturnSingle {
    pub span: Option<Span>,
    pub selector: SingleSelector,
    pub vars: Vec<Variable>,
}

impl ReturnSingle {
    pub fn new(span: Option<Span>, selector: SingleSelector, vars: Vec<Variable>) -> Self {
        Self { span, selector, vars }
    }
}

impl Spanned for ReturnSingle {
    fn span(&self) -> Option<Span> {
        self.span
    }
}

impl Pretty for ReturnSingle {}

impl fmt::Display for ReturnSingle {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        assert!(!self.vars.is_empty());
        write!(f, "{} ", self.selector)?;
        write!(f, "{}", self.vars[0])?;
        for var in &self.vars[1..] {
            write!(f, ", {}", var)?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SingleSelector {
    First,
    Last,
}

impl fmt::Display for SingleSelector {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            SingleSelector::First => write!(f, "{}", token::Keyword::First),
            SingleSelector::Last => write!(f, "{}", token::Keyword::Last),
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ReturnReduction {
    Check(Check),
    Value(Vec<Reducer>, Option<Span>),
}

impl Spanned for ReturnReduction {
    fn span(&self) -> Option<Span> {
        match self {
            ReturnReduction::Check(check) => check.span(),
            ReturnReduction::Value(_, span) => *span,
        }
    }
}

impl Pretty for ReturnReduction {
    fn fmt(&self, _indent_level: usize, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for ReturnReduction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Check(inner) => fmt::Display::fmt(inner, f),
            Self::Value(inner, _) => {
                write_joined!(f, ", ", inner)?;
                Ok(())
            }
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Check {
    pub span: Option<Span>,
}

impl Check {
    pub fn new(span: Option<Span>) -> Self {
        Self { span }
    }
}

impl Spanned for Check {
    fn span(&self) -> Option<Span> {
        self.span
    }
}

impl Pretty for Check {}

impl fmt::Display for Check {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", token::Keyword::Check)
    }
}