Skip to main content

nash_source/
lib.rs

1use nash_region::{Located, Region};
2
3#[derive(Debug)]
4pub struct Module<'a> {
5    pub kind: ModuleKind,
6    pub name: Option<&'a Located<&'a str>>,
7    pub exports: &'a Located<Exposing<'a>>,
8    pub docs: &'a Docs<'a>,
9    /// Ordinary comments in source order, including nested block text.
10    pub comments: &'a [&'a SourceComment<'a>],
11    pub imports: &'a [&'a Import<'a>],
12    pub values: &'a [&'a Located<Value<'a>>],
13    pub unions: &'a [&'a Located<Union<'a>>],
14    pub aliases: &'a [&'a Located<Alias<'a>>],
15    pub traits: &'a [&'a Located<Trait<'a>>],
16    pub impls: &'a [&'a Located<Impl<'a>>],
17    pub tests: Option<&'a Tests<'a>>,
18    pub binops: &'a [&'a Located<Infix<'a>>],
19}
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum ModuleKind {
23    Normal,
24    Validator(Region),
25}
26
27#[derive(Debug)]
28pub struct Import<'a> {
29    pub import: &'a Located<&'a str>,
30    pub alias: Option<&'a str>,
31    pub exposing: &'a Exposing<'a>,
32}
33
34#[derive(Debug)]
35pub struct Value<'a> {
36    pub docs: Option<&'a Comment<'a>>,
37    pub name: &'a Located<&'a str>,
38    pub arguments: &'a [&'a Located<Pattern<'a>>],
39    pub body: &'a Located<Expr<'a>>,
40    pub annotation: Option<&'a Annotation<'a>>,
41    pub attributes: &'a [&'a Attribute<'a>],
42}
43
44#[derive(Debug)]
45pub struct Attribute<'a> {
46    pub name: &'a Located<&'a str>,
47    pub args: &'a [&'a Located<Expr<'a>>],
48}
49
50#[derive(Debug)]
51pub struct Trait<'a> {
52    pub docs: Option<&'a Comment<'a>>,
53    pub name: &'a Located<&'a str>,
54    pub params: &'a [&'a TypeParam<'a>],
55    pub supers: &'a [&'a Located<Constraint<'a>>],
56    pub methods: &'a [&'a TraitMethod<'a>],
57    pub attributes: &'a [&'a Attribute<'a>],
58}
59
60#[derive(Debug)]
61pub struct TraitMethod<'a> {
62    pub name: &'a Located<&'a str>,
63    pub annotation: &'a Annotation<'a>,
64    pub default: Option<&'a Located<Def<'a>>>,
65}
66
67#[derive(Debug)]
68pub struct Impl<'a> {
69    pub docs: Option<&'a Comment<'a>>,
70    pub context: &'a [&'a Located<Constraint<'a>>],
71    pub head: &'a Located<Constraint<'a>>,
72    pub methods: &'a [&'a Located<Def<'a>>],
73    pub attributes: &'a [&'a Attribute<'a>],
74}
75
76#[derive(Debug)]
77pub struct Tests<'a> {
78    pub imports: &'a [&'a Import<'a>],
79    pub tests: &'a [&'a Located<Test<'a>>],
80}
81
82#[derive(Debug)]
83pub struct Test<'a> {
84    pub name: &'a Located<&'a str>,
85    pub expect: Expect,
86    pub budget: Option<Budget>,
87    pub body: TestBody<'a>,
88}
89
90#[derive(Debug)]
91pub struct Block<'a> {
92    pub stmts: &'a [&'a Located<Stmt<'a>>],
93    pub last: &'a Located<Expr<'a>>,
94}
95
96#[derive(Debug)]
97pub enum TestBody<'a> {
98    Unit(&'a Block<'a>),
99    Prop {
100        binders: &'a [&'a Located<ViaBinder<'a>>],
101        body: &'a Block<'a>,
102    },
103}
104
105#[derive(Debug)]
106pub struct ViaBinder<'a> {
107    pub pattern: &'a Located<Pattern<'a>>,
108    pub generator: &'a Located<Expr<'a>>,
109}
110
111#[derive(Clone, Copy, Debug, PartialEq, Eq)]
112pub enum Expect {
113    Pass,
114    Fail,
115    FailOnce,
116}
117
118#[derive(Clone, Copy, Debug, PartialEq, Eq)]
119pub enum Budget {
120    Cpu(i128),
121    Mem(i128),
122    Both { cpu: i128, mem: i128 },
123}
124
125/// A type annotation with an optional constraint context.
126#[derive(Debug)]
127pub struct Annotation<'a> {
128    pub constraints: &'a [&'a Located<Constraint<'a>>],
129    pub typ: &'a Located<Type<'a>>,
130}
131
132#[derive(Debug)]
133pub struct Constraint<'a> {
134    pub class: &'a Located<&'a str>,
135    pub module: Option<&'a str>,
136    pub args: &'a [&'a Located<Type<'a>>],
137}
138
139// type Maybe a
140//   = Just a
141//   | Nothing
142#[derive(Debug)]
143pub struct Union<'a> {
144    pub docs: Option<&'a Comment<'a>>,
145    pub name: &'a Located<&'a str>,
146    // type vars
147    pub arguments: &'a [&'a TypeParam<'a>],
148    pub ctors: &'a [&'a Ctor<'a>],
149    pub attributes: &'a [&'a Attribute<'a>],
150}
151
152#[derive(Debug)]
153pub struct Ctor<'a> {
154    pub name: &'a Located<&'a str>,
155    pub arguments: CtorArgs<'a>,
156}
157
158#[derive(Debug)]
159pub enum CtorArgs<'a> {
160    Positional(&'a [&'a Located<Type<'a>>]),
161    Labeled(&'a [(&'a Located<&'a str>, &'a Located<Type<'a>>)]),
162}
163
164#[derive(Debug)]
165pub struct Alias<'a> {
166    pub docs: Option<&'a Comment<'a>>,
167    pub name: &'a Located<&'a str>,
168    // type vars
169    pub arguments: &'a [&'a TypeParam<'a>],
170    pub typ: &'a Located<Type<'a>>,
171    pub attributes: &'a [&'a Attribute<'a>],
172}
173
174#[derive(Debug)]
175pub struct Infix<'a> {
176    pub op: &'a str,
177    pub associativity: Associativity,
178    pub precedence: Precedence,
179    pub name: &'a str,
180}
181
182#[derive(Clone, Copy, Debug, PartialEq, Eq)]
183pub enum Associativity {
184    Left,
185    None,
186    Right,
187}
188
189#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
190pub struct Precedence(pub u16);
191
192#[derive(Debug)]
193pub enum Expr<'a> {
194    Str(&'a str),
195    Bytes(&'a [u8]),
196    Int(i128),
197    Assert(&'a Located<Expr<'a>>),
198    Fail(Option<&'a Located<Expr<'a>>>),
199    Todo(Option<&'a Located<Expr<'a>>>),
200    Trace {
201        message: &'a Located<Expr<'a>>,
202        body: &'a Located<Expr<'a>>,
203    },
204    Comptime(&'a Located<Expr<'a>>),
205    Do {
206        stmts: &'a [&'a Located<Stmt<'a>>],
207        last: &'a Located<Expr<'a>>,
208    },
209    MacroCall {
210        name: &'a Located<&'a str>,
211        module: Option<&'a str>,
212        args: &'a [&'a Located<Expr<'a>>],
213    },
214    LeftSection {
215        left: &'a Located<Expr<'a>>,
216        operator: &'a str,
217    },
218    RightSection {
219        operator: &'a str,
220        right: &'a Located<Expr<'a>>,
221    },
222    Var {
223        kind: VarType,
224        name: &'a str,
225    },
226    VarQual {
227        kind: VarType,
228        module: &'a str,
229        name: &'a str,
230    },
231    List(&'a [&'a Located<Expr<'a>>]),
232    Op(&'a str),
233    Negate(&'a Located<Expr<'a>>),
234    BinOps {
235        operands: &'a [&'a BinOpOperand<'a>],
236        last: &'a Located<Expr<'a>>,
237    },
238    Lambda {
239        parameters: &'a [&'a Located<Pattern<'a>>],
240        body: &'a Located<Expr<'a>>,
241    },
242    Call {
243        function: &'a Located<Expr<'a>>,
244        arguments: &'a [&'a Located<Expr<'a>>],
245    },
246    If {
247        branches: &'a [&'a IfBranch<'a>],
248        final_else: &'a Located<Expr<'a>>,
249    },
250    Let {
251        defs: &'a [&'a Located<Def<'a>>],
252        body: &'a Located<Expr<'a>>,
253    },
254    Case {
255        scrutinee: &'a Located<Expr<'a>>,
256        arms: &'a [&'a CaseArm<'a>],
257    },
258    Accessor(&'a str),
259    Access {
260        record: &'a Located<Expr<'a>>,
261        field: &'a Located<&'a str>,
262    },
263    Update {
264        record: &'a Located<&'a str>,
265        fields: &'a [&'a FieldAssign<'a>],
266    },
267    Record {
268        fields: &'a [&'a FieldAssign<'a>],
269        /// Parentheses force a positional record argument to a labeled constructor.
270        grouped: bool,
271    },
272    Unit,
273    Tuple {
274        first: &'a Located<Expr<'a>>,
275        second: &'a Located<Expr<'a>>,
276        rest: &'a [&'a Located<Expr<'a>>],
277    },
278}
279
280#[derive(Debug)]
281pub enum Stmt<'a> {
282    Let(&'a [&'a Located<Def<'a>>]),
283    Bind {
284        pattern: &'a Located<Pattern<'a>>,
285        expr: &'a Located<Expr<'a>>,
286    },
287    Expr(&'a Located<Expr<'a>>),
288}
289
290#[derive(Debug)]
291pub enum VarType {
292    LowVar,
293    CapVar,
294}
295
296#[derive(Debug)]
297pub struct IfBranch<'a> {
298    pub condition: &'a Located<Expr<'a>>,
299    pub then_branch: &'a Located<Expr<'a>>,
300}
301
302/// An operand in a binary operator chain: expression followed by operator.
303#[derive(Debug)]
304pub struct BinOpOperand<'a> {
305    pub expr: &'a Located<Expr<'a>>,
306    pub op: &'a Located<&'a str>,
307}
308
309#[derive(Debug)]
310pub enum Def<'a> {
311    Define {
312        name: &'a Located<&'a str>,
313        args: &'a [&'a Located<Pattern<'a>>],
314        body: &'a Located<Expr<'a>>,
315        annotation: Option<&'a Annotation<'a>>,
316    },
317    Destruct {
318        pattern: &'a Located<Pattern<'a>>,
319        body: &'a Located<Expr<'a>>,
320    },
321}
322
323#[derive(Debug)]
324pub struct CaseArm<'a> {
325    pub pattern: &'a Located<Pattern<'a>>,
326    pub body: &'a Located<Expr<'a>>,
327}
328
329#[derive(Debug)]
330pub struct FieldAssign<'a> {
331    pub field: &'a Located<&'a str>,
332    pub value: &'a Located<Expr<'a>>,
333}
334
335#[derive(Debug)]
336pub enum Pattern<'a> {
337    Anything,
338    Var(&'a str),
339    Record(&'a [&'a Located<&'a str>]),
340    Alias {
341        pattern: &'a Located<Pattern<'a>>,
342        name: &'a Located<&'a str>,
343    },
344    Unit,
345    Pair {
346        first: &'a Located<Pattern<'a>>,
347        second: &'a Located<Pattern<'a>>,
348    },
349    Tuple {
350        first: &'a Located<Pattern<'a>>,
351        second: &'a Located<Pattern<'a>>,
352        rest: &'a [&'a Located<Pattern<'a>>],
353    },
354    Ctor {
355        region: Region,
356        name: &'a str,
357        args: &'a [&'a Located<Pattern<'a>>],
358    },
359    CtorQual {
360        region: Region,
361        module: &'a str,
362        name: &'a str,
363        args: &'a [&'a Located<Pattern<'a>>],
364    },
365    List(&'a [&'a Located<Pattern<'a>>]),
366    Cons {
367        head: &'a Located<Pattern<'a>>,
368        tail: &'a Located<Pattern<'a>>,
369    },
370    Str(&'a str),
371    Bytes(&'a [u8]),
372    Int(i128),
373}
374
375#[derive(Debug)]
376pub enum Type<'a> {
377    Repr {
378        typ: &'a Located<Type<'a>>,
379        repr: &'a Located<Repr>,
380    },
381    Lambda {
382        from: &'a Located<Type<'a>>,
383        to: &'a Located<Type<'a>>,
384    },
385    Var(&'a str),
386    VarApp {
387        region: Region,
388        name: &'a str,
389        args: &'a [&'a Located<Type<'a>>],
390    },
391    Type {
392        region: Region,
393        name: &'a str,
394        args: &'a [&'a Located<Type<'a>>],
395    },
396    TypeQual {
397        region: Region,
398        module: &'a str,
399        name: &'a str,
400        args: &'a [&'a Located<Type<'a>>],
401    },
402    Record(&'a [&'a FieldType<'a>]),
403    Unit,
404    Tuple {
405        first: &'a Located<Type<'a>>,
406        second: &'a Located<Type<'a>>,
407        rest: &'a [&'a Located<Type<'a>>],
408    },
409}
410
411impl<'a> Type<'a> {
412    /// Inspect shape without discarding the annotation from the stored type.
413    pub fn unannotated(&self) -> &Self {
414        let mut typ = self;
415        while let Self::Repr { typ: inner, .. } = typ {
416            typ = &inner.value;
417        }
418        typ
419    }
420}
421
422#[derive(Debug)]
423pub struct TypeParam<'a> {
424    pub name: &'a Located<&'a str>,
425    pub repr: Option<&'a Located<Repr>>,
426}
427
428/// Representation annotation sugar; Haskell 98 kinds have no surface syntax.
429#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
430pub enum Repr {
431    Big,
432    Const,
433    Term,
434    Storable,
435    Little,
436}
437
438#[derive(Debug)]
439pub struct FieldType<'a> {
440    pub field: &'a Located<&'a str>,
441    pub typ: &'a Located<Type<'a>>,
442}
443
444#[derive(Debug)]
445pub enum Docs<'a> {
446    NoDocs(Region),
447    YesDocs {
448        overview: &'a Comment<'a>,
449        comments: &'a [&'a (&'a str, &'a Comment<'a>)],
450    },
451}
452
453#[derive(Clone, Copy, Debug, PartialEq, Eq)]
454pub enum CommentKind {
455    Line,
456    Block,
457}
458
459#[derive(Debug)]
460pub struct SourceComment<'a> {
461    /// Includes delimiters, excludes the line ending for line comments.
462    pub region: Region,
463    pub kind: CommentKind,
464    /// Exact text between delimiters, without a line comment's CRLF or LF.
465    pub text: &'a str,
466}
467
468#[derive(Debug)]
469pub struct Comment<'a> {
470    /// Includes the opening and closing doc-comment delimiters.
471    pub region: Region,
472    pub snippet: &'a Snippet<'a>,
473}
474
475#[derive(Debug)]
476pub struct Snippet<'a> {
477    pub data: &'a [u8], // already the relevant slice
478    // offset: usize,
479    // length: usize,
480    pub off_row: usize,
481    pub off_col: usize,
482}
483
484#[derive(Debug)]
485pub enum Exposing<'a> {
486    Open,
487    Explicit(&'a [&'a Exposed<'a>]),
488}
489
490#[derive(Debug)]
491pub enum Exposed<'a> {
492    Lower(&'a Located<&'a str>),
493    Upper {
494        name: &'a Located<&'a str>,
495        privacy: Privacy,
496    },
497    LowerType {
498        name: &'a Located<&'a str>,
499        privacy: Privacy,
500    },
501    Operator {
502        region: Region,
503        op: &'a str,
504    },
505}
506
507#[derive(Debug)]
508pub enum Privacy {
509    Public(Region),
510    Private,
511}