Skip to main content

nash_parse/
error.rs

1//! Syntax error types for the Nash parser.
2//!
3//! Ported from Elm's `Reporting/Error/Syntax.hs`.
4//! These types form a nested hierarchy that enables high-quality error messages.
5//!
6//! Note: `Type`, `Expr`, `Pattern` here are ERROR types describing parse failures,
7//! not AST types. They are allocated in the arena like everything else.
8
9use crate::{Col, Row};
10
11// =============================================================================
12// Top-level Error
13// =============================================================================
14
15#[derive(Debug)]
16pub enum Error<'a> {
17    ModuleNameUnspecified(&'a str),
18    ModuleNameMismatch {
19        expected: &'a str,
20        actual: &'a str,
21        row: Row,
22        col: Col,
23    },
24    ParseError(&'a Module<'a>),
25}
26
27// =============================================================================
28// Module Errors
29// =============================================================================
30
31#[derive(Debug)]
32pub enum Module<'a> {
33    Space(Space, Row, Col),
34    BadEnd(Row, Col),
35    Problem(Row, Col),
36    Validator(Row, Col),
37    Name(Row, Col),
38    Exposing(&'a Exposing, Row, Col),
39    FreshLine(Row, Col),
40    ImportStart(Row, Col),
41    ImportName(Row, Col),
42    ImportAs(Row, Col),
43    ImportAlias(Row, Col),
44    ImportExposing(Row, Col),
45    ImportExposingList(&'a Exposing, Row, Col),
46    ImportEnd(Row, Col),
47    ImportIndentName(Row, Col),
48    ImportIndentAlias(Row, Col),
49    ImportIndentExposingList(Row, Col),
50    Infix(Row, Col),
51    Declarations(&'a Decl<'a>, Row, Col),
52    Tests(&'a Tests<'a>, Row, Col),
53}
54
55#[derive(Debug)]
56pub enum Tests<'a> {
57    Space(Space, Row, Col),
58    Import(&'a Module<'a>, Row, Col),
59    Test(&'a Test<'a>, Row, Col),
60    Start(Row, Col),
61    IndentStart(Row, Col),
62    Alignment(u16, Row, Col),
63}
64
65#[derive(Debug)]
66pub enum Test<'a> {
67    Space(Space, Row, Col),
68    Name(StringError, Row, Col),
69    NameStart(Row, Col),
70    OnceOnUnitTest(Row, Col),
71    WithinOpen(Row, Col),
72    WithinKind(Row, Col),
73    WithinNumber(Number, Row, Col),
74    WithinDuplicate(Row, Col),
75    WithinEnd(Row, Col),
76    Equals(Row, Col),
77    Do(Row, Col),
78    Body(&'a Do<'a>, Row, Col),
79    Let(Row, Col),
80    Pattern(&'a Pattern<'a>, Row, Col),
81    Via(Row, Col),
82    Fuzzer(&'a Expr<'a>, Row, Col),
83    In(Row, Col),
84    IndentName(Row, Col),
85    IndentEquals(Row, Col),
86    IndentBody(Row, Col),
87    IndentBinder(Row, Col),
88    IndentIn(Row, Col),
89    BinderAlignment(u16, Row, Col),
90}
91
92#[derive(Debug)]
93pub enum Exposing {
94    Space(Space, Row, Col),
95    Start(Row, Col),
96    Value(Row, Col),
97    Operator(Row, Col),
98    OperatorReserved(BadOperator, Row, Col),
99    OperatorRightParen(Row, Col),
100    TypePrivacy(Row, Col),
101    TypeName(Row, Col),
102    End(Row, Col),
103    IndentEnd(Row, Col),
104    IndentValue(Row, Col),
105}
106
107// =============================================================================
108// Declaration Errors
109// =============================================================================
110
111#[derive(Debug)]
112pub enum Decl<'a> {
113    Start(Row, Col),
114    Space(Space, Row, Col),
115    Type(&'a DeclType<'a>, Row, Col),
116    Def(&'a str, &'a DeclDef<'a>, Row, Col),
117    FreshLineAfterDocComment(Row, Col),
118    Attribute(&'a Attribute<'a>, Row, Col),
119    Trait(&'a Trait<'a>, Row, Col),
120    Impl(&'a Impl<'a>, Row, Col),
121}
122
123#[derive(Debug)]
124pub enum Impl<'a> {
125    Space(Space, Row, Col),
126    Head(&'a Type<'a>, Row, Col),
127    BadHead(Row, Col),
128    Where(Row, Col),
129    Method(&'a str, &'a Def<'a>, Row, Col),
130    MethodName(Row, Col),
131    IndentHead(Row, Col),
132    IndentWhere(Row, Col),
133    IndentMethod(Row, Col),
134    Alignment(u16, Row, Col),
135}
136
137#[derive(Debug)]
138pub enum Trait<'a> {
139    Space(Space, Row, Col),
140    Name(Row, Col),
141    Param(&'a TypeParam<'a>, Row, Col),
142    Super(&'a Type<'a>, Row, Col),
143    SuperArg(Row, Col),
144    Where(Row, Col),
145    MethodName(Row, Col),
146    Colon(Row, Col),
147    Type(&'a Type<'a>, Row, Col),
148    Default(&'a str, &'a Def<'a>, Row, Col),
149    IndentName(Row, Col),
150    IndentParam(Row, Col),
151    IndentWhere(Row, Col),
152    IndentMethod(Row, Col),
153    IndentColon(Row, Col),
154    IndentType(Row, Col),
155    Alignment(u16, Row, Col),
156}
157
158#[derive(Debug)]
159pub enum Attribute<'a> {
160    Name(Row, Col),
161    Arg(&'a Expr<'a>, Row, Col),
162    End(Row, Col),
163    Space(Space, Row, Col),
164    FreshLine(Row, Col),
165    IndentArg(Row, Col),
166    IndentEnd(Row, Col),
167}
168
169#[derive(Debug)]
170pub enum DeclDef<'a> {
171    Space(Space, Row, Col),
172    Equals(Row, Col),
173    Type(&'a Type<'a>, Row, Col),
174    Arg(&'a Pattern<'a>, Row, Col),
175    Body(&'a Expr<'a>, Row, Col),
176    NameRepeat(Row, Col),
177    NameMatch(&'a str, Row, Col),
178    IndentType(Row, Col),
179    IndentEquals(Row, Col),
180    IndentBody(Row, Col),
181}
182
183#[derive(Debug)]
184pub enum DeclType<'a> {
185    Space(Space, Row, Col),
186    Name(Row, Col),
187    Alias(&'a TypeAlias<'a>, Row, Col),
188    Union(&'a CustomType<'a>, Row, Col),
189    IndentName(Row, Col),
190}
191
192#[derive(Debug)]
193pub enum TypeAlias<'a> {
194    Space(Space, Row, Col),
195    Name(Row, Col),
196    Param(&'a TypeParam<'a>, Row, Col),
197    Equals(Row, Col),
198    Body(&'a Type<'a>, Row, Col),
199    IndentEquals(Row, Col),
200    IndentBody(Row, Col),
201}
202
203#[derive(Debug)]
204pub enum CustomType<'a> {
205    Space(Space, Row, Col),
206    Name(Row, Col),
207    Param(&'a TypeParam<'a>, Row, Col),
208    Equals(Row, Col),
209    Bar(Row, Col),
210    Variant(Row, Col),
211    VariantArg(&'a Type<'a>, Row, Col),
212    IndentEquals(Row, Col),
213    IndentBar(Row, Col),
214    IndentAfterBar(Row, Col),
215    IndentAfterEquals(Row, Col),
216    Field(Row, Col),
217    FieldColon(Row, Col),
218    FieldType(&'a Type<'a>, Row, Col),
219    FieldEnd(Row, Col),
220    IndentField(Row, Col),
221    IndentFieldType(Row, Col),
222}
223
224// =============================================================================
225// Expression Errors
226// =============================================================================
227
228#[derive(Debug)]
229pub enum Expr<'a> {
230    Let(&'a Let<'a>, Row, Col),
231    Case(&'a Case<'a>, Row, Col),
232    If(&'a If<'a>, Row, Col),
233    List(&'a List<'a>, Row, Col),
234    Record(&'a Record<'a>, Row, Col),
235    Tuple(&'a Tuple<'a>, Row, Col),
236    Func(&'a Func<'a>, Row, Col),
237    Assert(&'a Keyword<'a>, Row, Col),
238    Fail(&'a Keyword<'a>, Row, Col),
239    Todo(&'a Keyword<'a>, Row, Col),
240    Trace(&'a Keyword<'a>, Row, Col),
241    Comptime(&'a Keyword<'a>, Row, Col),
242    Do(&'a Do<'a>, Row, Col),
243    Macro(&'a Macro<'a>, Row, Col),
244    Dot(Row, Col),
245    Access(Row, Col),
246    OperatorRight(&'a str, Row, Col),
247    OperatorReserved(BadOperator, Row, Col),
248    Start(Row, Col),
249    String(StringError, Row, Col),
250    Bytes(Bytes, Row, Col),
251    Number(Number, Row, Col),
252    Space(Space, Row, Col),
253    IndentOperatorRight(&'a str, Row, Col),
254}
255
256#[derive(Debug)]
257pub enum Keyword<'a> {
258    Space(Space, Row, Col),
259    Body(&'a Expr<'a>, Row, Col),
260    Message(&'a Expr<'a>, Row, Col),
261    IndentBody(Row, Col),
262    IndentMessage(Row, Col),
263}
264
265#[derive(Debug)]
266pub enum Do<'a> {
267    Space(Space, Row, Col),
268    Let(&'a Let<'a>, Row, Col),
269    Pattern(&'a Pattern<'a>, Row, Col),
270    Arrow(Row, Col),
271    Expr(&'a Expr<'a>, Row, Col),
272    LastNotExpr(Row, Col),
273    IndentStmt(Row, Col),
274    IndentArrow(Row, Col),
275    IndentExpr(Row, Col),
276    Alignment(u16, Row, Col),
277}
278
279#[derive(Debug)]
280pub enum Macro<'a> {
281    Open(Row, Col),
282    Arg(&'a Expr<'a>, Row, Col),
283    End(Row, Col),
284    Space(Space, Row, Col),
285    IndentArg(Row, Col),
286    IndentEnd(Row, Col),
287}
288
289#[derive(Debug)]
290pub enum Record<'a> {
291    Open(Row, Col),
292    End(Row, Col),
293    Field(Row, Col),
294    Equals(Row, Col),
295    Expr(&'a Expr<'a>, Row, Col),
296    Space(Space, Row, Col),
297    IndentOpen(Row, Col),
298    IndentEnd(Row, Col),
299    IndentField(Row, Col),
300    IndentEquals(Row, Col),
301    IndentExpr(Row, Col),
302}
303
304#[derive(Debug)]
305pub enum Tuple<'a> {
306    Expr(&'a Expr<'a>, Row, Col),
307    Space(Space, Row, Col),
308    End(Row, Col),
309    OperatorClose(Row, Col),
310    OperatorReserved(BadOperator, Row, Col),
311    IndentExpr1(Row, Col),
312    IndentExprN(Row, Col),
313    IndentEnd(Row, Col),
314}
315
316#[derive(Debug)]
317pub enum List<'a> {
318    Space(Space, Row, Col),
319    Open(Row, Col),
320    Expr(&'a Expr<'a>, Row, Col),
321    End(Row, Col),
322    IndentOpen(Row, Col),
323    IndentEnd(Row, Col),
324    IndentExpr(Row, Col),
325}
326
327#[derive(Debug)]
328pub enum Func<'a> {
329    Space(Space, Row, Col),
330    Arg(&'a Pattern<'a>, Row, Col),
331    Body(&'a Expr<'a>, Row, Col),
332    Arrow(Row, Col),
333    IndentArg(Row, Col),
334    IndentArrow(Row, Col),
335    IndentBody(Row, Col),
336}
337
338#[derive(Debug)]
339pub enum Case<'a> {
340    Space(Space, Row, Col),
341    Of(Row, Col),
342    Pattern(&'a Pattern<'a>, Row, Col),
343    Arrow(Row, Col),
344    Expr(&'a Expr<'a>, Row, Col),
345    Branch(&'a Expr<'a>, Row, Col),
346    IndentOf(Row, Col),
347    IndentExpr(Row, Col),
348    IndentPattern(Row, Col),
349    IndentArrow(Row, Col),
350    IndentBranch(Row, Col),
351    PatternAlignment(u16, Row, Col),
352}
353
354#[derive(Debug)]
355pub enum If<'a> {
356    Space(Space, Row, Col),
357    Then(Row, Col),
358    Else(Row, Col),
359    ElseBranchStart(Row, Col),
360    Condition(&'a Expr<'a>, Row, Col),
361    ThenBranch(&'a Expr<'a>, Row, Col),
362    ElseBranch(&'a Expr<'a>, Row, Col),
363    IndentCondition(Row, Col),
364    IndentThen(Row, Col),
365    IndentThenBranch(Row, Col),
366    IndentElseBranch(Row, Col),
367    IndentElse(Row, Col),
368}
369
370#[derive(Debug)]
371pub enum Let<'a> {
372    Space(Space, Row, Col),
373    In(Row, Col),
374    DefAlignment(u16, Row, Col),
375    DefName(Row, Col),
376    Def(&'a str, &'a Def<'a>, Row, Col),
377    Destruct(&'a Destruct<'a>, Row, Col),
378    Body(&'a Expr<'a>, Row, Col),
379    IndentDef(Row, Col),
380    IndentIn(Row, Col),
381    IndentBody(Row, Col),
382}
383
384#[derive(Debug)]
385pub enum Def<'a> {
386    Space(Space, Row, Col),
387    Type(&'a Type<'a>, Row, Col),
388    NameRepeat(Row, Col),
389    NameMatch(&'a str, Row, Col),
390    Arg(&'a Pattern<'a>, Row, Col),
391    Equals(Row, Col),
392    Body(&'a Expr<'a>, Row, Col),
393    IndentEquals(Row, Col),
394    IndentType(Row, Col),
395    IndentBody(Row, Col),
396    Alignment(u16, Row, Col),
397}
398
399#[derive(Debug)]
400pub enum Destruct<'a> {
401    Space(Space, Row, Col),
402    Pattern(&'a Pattern<'a>, Row, Col),
403    Equals(Row, Col),
404    Body(&'a Expr<'a>, Row, Col),
405    IndentEquals(Row, Col),
406    IndentBody(Row, Col),
407}
408
409// =============================================================================
410// Pattern Errors
411// =============================================================================
412
413#[derive(Debug)]
414pub enum Pattern<'a> {
415    Record(&'a PRecord, Row, Col),
416    Tuple(&'a PTuple<'a>, Row, Col),
417    List(&'a PList<'a>, Row, Col),
418    Start(Row, Col),
419    String(StringError, Row, Col),
420    Bytes(Bytes, Row, Col),
421    Number(Number, Row, Col),
422    Alias(Row, Col),
423    WildcardNotVar(&'a str, i32, Row, Col),
424    Space(Space, Row, Col),
425    IndentStart(Row, Col),
426    IndentAlias(Row, Col),
427}
428
429#[derive(Debug)]
430pub enum Bytes {
431    Endless,
432    OddLength,
433    BadHexDigit(u16),
434}
435
436#[derive(Debug)]
437pub enum PRecord {
438    Open(Row, Col),
439    End(Row, Col),
440    Field(Row, Col),
441    Space(Space, Row, Col),
442    IndentOpen(Row, Col),
443    IndentEnd(Row, Col),
444    IndentField(Row, Col),
445}
446
447#[derive(Debug)]
448pub enum PTuple<'a> {
449    Open(Row, Col),
450    End(Row, Col),
451    Expr(&'a Pattern<'a>, Row, Col),
452    Space(Space, Row, Col),
453    IndentEnd(Row, Col),
454    IndentExpr1(Row, Col),
455    IndentExprN(Row, Col),
456}
457
458#[derive(Debug)]
459pub enum PList<'a> {
460    Open(Row, Col),
461    End(Row, Col),
462    Expr(&'a Pattern<'a>, Row, Col),
463    Space(Space, Row, Col),
464    IndentOpen(Row, Col),
465    IndentEnd(Row, Col),
466    IndentExpr(Row, Col),
467}
468
469// =============================================================================
470// Type Errors
471// =============================================================================
472
473#[derive(Debug)]
474pub enum Type<'a> {
475    Record(&'a TRecord<'a>, Row, Col),
476    Tuple(&'a TTuple<'a>, Row, Col),
477    Start(Row, Col),
478    VarStart(Row, Col),
479    Context(Row, Col),
480    IndentAfterContext(Row, Col),
481    Space(Space, Row, Col),
482    IndentStart(Row, Col),
483}
484
485#[derive(Debug)]
486pub enum TypeParam<'a> {
487    Start(Row, Col),
488    Colon(Row, Col),
489    Kind(&'a Kind<'a>, Row, Col),
490    End(Row, Col),
491    Space(Space, Row, Col),
492    IndentColon(Row, Col),
493    IndentKind(Row, Col),
494    IndentEnd(Row, Col),
495}
496
497#[derive(Debug)]
498pub enum Kind<'a> {
499    Start(Row, Col),
500    Name(&'a str, Row, Col),
501    End(Row, Col),
502    Space(Space, Row, Col),
503    IndentStart(Row, Col),
504    Paren(&'a Kind<'a>, Row, Col),
505}
506
507#[derive(Debug)]
508pub enum TRecord<'a> {
509    Open(Row, Col),
510    End(Row, Col),
511    Field(Row, Col),
512    Colon(Row, Col),
513    Type(&'a Type<'a>, Row, Col),
514    Space(Space, Row, Col),
515    IndentOpen(Row, Col),
516    IndentField(Row, Col),
517    IndentColon(Row, Col),
518    IndentType(Row, Col),
519    IndentEnd(Row, Col),
520}
521
522#[derive(Debug)]
523pub enum TTuple<'a> {
524    Open(Row, Col),
525    End(Row, Col),
526    Type(&'a Type<'a>, Row, Col),
527    Space(Space, Row, Col),
528    IndentType1(Row, Col),
529    IndentTypeN(Row, Col),
530    IndentEnd(Row, Col),
531}
532
533// =============================================================================
534// Literal Errors (no lifetimes - leaf types)
535// =============================================================================
536
537#[derive(Debug)]
538pub enum StringError {
539    EndlessSingle,
540    EndlessMulti,
541    Escape(Escape),
542}
543
544#[derive(Debug)]
545pub enum Escape {
546    Unknown,
547    BadUnicodeFormat(u16),
548    BadUnicodeCode(u16),
549    BadUnicodeLength {
550        code: u16,
551        expected: i32,
552        actual: i32,
553    },
554}
555
556#[derive(Debug)]
557pub enum Number {
558    End,
559    Dot(i128),
560    HexDigit,
561    NoLeadingZero,
562}
563
564// =============================================================================
565// Misc (no lifetimes - leaf types)
566// =============================================================================
567
568#[derive(Debug)]
569pub enum Space {
570    HasTab,
571    EndlessMultiComment,
572}
573
574#[derive(Debug)]
575pub enum BadOperator {
576    Dot,
577    Pipe,
578    Arrow,
579    Equals,
580    HasType,
581    FatArrow,
582    LeftArrow,
583}