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