dotrain 6.0.1-alpha.24

.rain to rainlang composer
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! All data types of RainDocument/RainlangDocument parse tree

use std::collections::HashMap;
use super::super::error::ErrorCode;
use serde::{Serialize, Deserialize};
use super::super::parser::{rainlangdocument::RainlangDocument, raindocument::RainDocument};

#[cfg(feature = "js-api")]
use tsify::Tsify;

/// Type for start and end indexes of an ast node in a text, inclusive at start and exclusive at the end
#[cfg_attr(feature = "js-api", tsify::declare)]
pub type Offsets = [usize; 2];

/// Type for result of matches found in a text
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct ParsedItem(pub String, pub Offsets);

/// Type for encountered problem within the text
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct Problem {
    pub msg: String,
    pub position: Offsets,
    pub code: ErrorCode,
}

/// Type for AST Value node
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct Literal {
    pub value: String,
    pub position: Offsets,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "js-api", tsify(optional))]
    pub lhs_alias: Option<Vec<Alias>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "js-api", tsify(optional))]
    pub id: Option<String>,
}

/// Type of an opcode's descriptive details
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct OpcodeDetails {
    pub name: String,
    pub description: String,
    pub position: Offsets,
}

/// Type of an individual opcode's operand arguments
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct OperandArgItem {
    pub value: Option<String>,
    pub name: String,
    pub position: Offsets,
    pub description: String,
    pub binding_id: Option<(String, bool)>,
}

/// Type of an opcode's all operand arguments segment
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct OperandArg {
    pub position: Offsets,
    pub args: Vec<OperandArgItem>,
}

/// Type for AST Opcode node
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct Opcode {
    pub opcode: OpcodeDetails,
    pub operand: Option<u8>,
    pub output: Option<u8>,
    pub position: Offsets,
    pub parens: Offsets,
    pub inputs: Vec<Node>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "js-api", tsify(optional))]
    pub lhs_alias: Option<Vec<Alias>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "js-api", tsify(optional))]
    pub operand_args: Option<OperandArg>,
}

/// Type for AST Alias node
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct Alias {
    pub name: String,
    pub position: Offsets,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "js-api", tsify(optional))]
    pub lhs_alias: Option<Vec<Alias>>,
}

/// Type of a parsed comment
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct Comment {
    pub comment: String,
    pub position: Offsets,
}

/// Type of an import configurations (renames/rebindings)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct ImportConfiguration {
    pub problems: Vec<Problem>,
    pub groups: Vec<(ParsedItem, Option<ParsedItem>)>,
}

/// Type of an import meta sequence
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct ImportSequence {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "js-api", tsify(type = "IRainDocument", optional))]
    pub dotrain: Option<RainDocument>,
}

/// Type of import statements specified in a RainDocument
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct Import {
    pub name: String,
    pub name_position: Offsets,
    pub hash: String,
    pub hash_position: Offsets,
    pub position: Offsets,
    pub problems: Vec<Problem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "js-api", tsify(optional))]
    pub configuration: Option<ImportConfiguration>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[cfg_attr(feature = "js-api", tsify(optional))]
    pub sequence: Option<ImportSequence>,
}

/// Type of a pragma statement
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct PragmaStatement {
    pub keyword: Offsets,
    pub sources: Vec<(ParsedItem, Option<String>)>,
}

/// Type of an AST node
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub enum Node {
    Literal(Literal),
    Opcode(Opcode),
    Alias(Alias),
}

impl Node {
    pub fn position(&self) -> Offsets {
        match self {
            Node::Literal(v) => v.position,
            Node::Opcode(op) => op.position,
            Node::Alias(a) => a.position,
        }
    }
}

/// Type of a Rainlang Line (delimited by ",")
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct RainlangLine {
    pub nodes: Vec<Node>,
    pub position: Offsets,
    pub aliases: Vec<Alias>,
}

/// Type of a Rainlang Source (delimited by ";")
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct RainlangSource {
    pub lines: Vec<RainlangLine>,
    pub position: Offsets,
}

/// Type of a Rainlang parse tree
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct RainlangAST(Vec<RainlangSource>);

/// Type of a elided binding
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct ElidedBindingItem {
    pub msg: String,
}

/// Type of a constant binding
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct LiteralBindingItem {
    pub value: String,
}

/// Type of a quote binding
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct QuoteBindingItem {
    pub quote: String,
}

/// Type of an expression binding
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub enum BindingItem {
    Elided(ElidedBindingItem),
    Literal(LiteralBindingItem),
    Exp(RainlangDocument),
    Quote(QuoteBindingItem),
}

/// Type for a binding (named expressions)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct Binding {
    pub name: String,
    pub name_position: Offsets,
    pub content: String,
    pub content_position: Offsets,
    pub position: Offsets,
    pub problems: Vec<Problem>,
    pub item: BindingItem,
}

/// Type for a namespace leaf
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub struct NamespaceLeaf {
    pub hash: String,
    pub import_index: isize,
    pub element: Binding,
}

impl NamespaceLeaf {
    pub fn is_elided_binding(&self) -> bool {
        matches!(
            self.element,
            Binding {
                item: BindingItem::Elided(_),
                ..
            }
        )
    }

    pub fn unwrap_elided_binding(&self) -> &String {
        if let BindingItem::Elided(e) = &self.element.item {
            &e.msg
        } else {
            panic!("not an elided binding")
        }
    }

    pub fn is_constant_binding(&self) -> bool {
        matches!(
            self.element,
            Binding {
                item: BindingItem::Literal(_),
                ..
            }
        )
    }

    pub fn unwrap_constant_binding(&self) -> &String {
        if let BindingItem::Literal(c) = &self.element.item {
            &c.value
        } else {
            panic!("not a constant binding")
        }
    }

    pub fn is_exp_binding(&self) -> bool {
        matches!(
            self.element,
            Binding {
                item: BindingItem::Exp(_),
                ..
            }
        )
    }

    pub fn unwrap_exp_binding(&self) -> &RainlangDocument {
        if let BindingItem::Exp(e) = &self.element.item {
            e
        } else {
            panic!("not an exp binding")
        }
    }

    pub fn is_quote_binding(&self) -> bool {
        matches!(
            self.element,
            Binding {
                item: BindingItem::Quote(_),
                ..
            }
        )
    }

    pub fn unwrap_quote_binding(&self) -> &QuoteBindingItem {
        if let BindingItem::Quote(q) = &self.element.item {
            q
        } else {
            panic!("not a quote binding")
        }
    }
}

/// RainDocument's individual namespace item
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[cfg_attr(feature = "js-api", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
pub enum NamespaceItem {
    Leaf(NamespaceLeaf),
    Node(Namespace),
}

impl NamespaceItem {
    pub fn is_leaf(&self) -> bool {
        matches!(self, NamespaceItem::Leaf(_))
    }

    pub fn unwrap_leaf(&self) -> &NamespaceLeaf {
        match self {
            NamespaceItem::Leaf(leaf) => leaf,
            NamespaceItem::Node(_) => panic!("not a leaf"),
        }
    }

    pub fn unwrap_node(&self) -> &Namespace {
        match self {
            NamespaceItem::Leaf(_) => panic!("not a node"),
            NamespaceItem::Node(node) => node,
        }
    }

    pub fn unwrap_node_mut(&mut self) -> &mut Namespace {
        match self {
            NamespaceItem::Leaf(_) => panic!("not a node"),
            NamespaceItem::Node(node) => node,
        }
    }

    pub fn is_elided_binding(&self) -> bool {
        matches!(
            self,
            NamespaceItem::Leaf(NamespaceLeaf {
                element: Binding {
                    item: BindingItem::Elided(_),
                    ..
                },
                ..
            })
        )
    }

    pub fn unwrap_elided_binding(&self) -> &String {
        if let NamespaceItem::Leaf(NamespaceLeaf {
            element:
                Binding {
                    item: BindingItem::Elided(e),
                    ..
                },
            ..
        }) = self
        {
            &e.msg
        } else {
            panic!("not an elided binding")
        }
    }

    pub fn is_constant_binding(&self) -> bool {
        matches!(
            self,
            NamespaceItem::Leaf(NamespaceLeaf {
                element: Binding {
                    item: BindingItem::Literal(_),
                    ..
                },
                ..
            })
        )
    }

    pub fn unwrap_constant_binding(&self) -> &String {
        if let NamespaceItem::Leaf(NamespaceLeaf {
            element:
                Binding {
                    item: BindingItem::Literal(c),
                    ..
                },
            ..
        }) = self
        {
            &c.value
        } else {
            panic!("not a constant binding")
        }
    }

    pub fn is_exp_binding(&self) -> bool {
        matches!(
            self,
            NamespaceItem::Leaf(NamespaceLeaf {
                element: Binding {
                    item: BindingItem::Exp(_),
                    ..
                },
                ..
            })
        )
    }

    pub fn unwrap_exp_binding(&self) -> &RainlangDocument {
        if let NamespaceItem::Leaf(NamespaceLeaf {
            element:
                Binding {
                    item: BindingItem::Exp(e),
                    ..
                },
            ..
        }) = self
        {
            e
        } else {
            panic!("not an exp binding")
        }
    }

    pub fn is_quote_binding(&self) -> bool {
        matches!(
            self,
            NamespaceItem::Leaf(NamespaceLeaf {
                element: Binding {
                    item: BindingItem::Quote(_),
                    ..
                },
                ..
            })
        )
    }

    pub fn unwrap_quote_binding(&self) -> &QuoteBindingItem {
        if let NamespaceItem::Leaf(NamespaceLeaf {
            element:
                Binding {
                    item: BindingItem::Quote(q),
                    ..
                },
            ..
        }) = self
        {
            q
        } else {
            panic!("not a quote binding")
        }
    }
}

/// Type for a namespace in dotrain
#[cfg_attr(feature = "js-api", tsify::declare)]
pub type Namespace = HashMap<String, NamespaceItem>;