bhc-parser 0.2.3

Parser for Haskell 2026 source code
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# bhc-parser

Recursive-descent parser for Haskell 2026.

## Overview

`bhc-parser` transforms a token stream into an Abstract Syntax Tree (AST). It implements:

- **Recursive descent**: Predictable, debuggable parsing
- **Operator precedence**: Pratt parsing for expressions
- **Error recovery**: Continue parsing after errors
- **Full H26 support**: All Haskell 2026 syntax

## Core Types

| Type | Description |
|------|-------------|
| `Parser` | Main parser state |
| `ParseResult<T>` | Result with diagnostic support |
| `ParseError` | Structured parse errors |
| `Precedence` | Operator precedence levels |

## Quick Start

```rust
use bhc_lexer::Lexer;
use bhc_parser::Parser;
use bhc_span::SourceFile;

let source = r#"
module Main where

main :: IO ()
main = putStrLn "Hello, World!"
"#;

let file = SourceFile::new(FileId::new(0), "Main.hs".into(), source.into());
let lexer = Lexer::new(&file);
let tokens: Vec<Token> = lexer.collect();

let mut parser = Parser::new(&tokens);
let module = parser.parse_module()?;
```

## Parsing Methods

### Module Level

```rust
impl Parser {
    /// Parse a complete module
    pub fn parse_module(&mut self) -> ParseResult<Module>;

    /// Parse module header: `module Foo.Bar (exports) where`
    pub fn parse_module_header(&mut self) -> ParseResult<Option<ModuleHeader>>;

    /// Parse import declaration
    pub fn parse_import(&mut self) -> ParseResult<ImportDecl>;

    /// Parse a single declaration
    pub fn parse_decl(&mut self) -> ParseResult<Decl>;
}
```

### Declarations

```rust
impl Parser {
    /// Parse type signature: `foo :: Int -> Int`
    pub fn parse_type_sig(&mut self) -> ParseResult<TypeSig>;

    /// Parse function binding: `foo x = x + 1`
    pub fn parse_fun_bind(&mut self) -> ParseResult<FunBind>;

    /// Parse data declaration: `data Maybe a = Nothing | Just a`
    pub fn parse_data_decl(&mut self) -> ParseResult<DataDecl>;

    /// Parse class declaration: `class Eq a where ...`
    pub fn parse_class_decl(&mut self) -> ParseResult<ClassDecl>;

    /// Parse instance declaration: `instance Eq Int where ...`
    pub fn parse_instance_decl(&mut self) -> ParseResult<InstanceDecl>;
}
```

### Expressions

```rust
impl Parser {
    /// Parse any expression
    pub fn parse_expr(&mut self) -> ParseResult<Expr>;

    /// Parse with operator precedence
    pub fn parse_expr_prec(&mut self, min_prec: Precedence) -> ParseResult<Expr>;

    /// Parse atomic expression (no operators)
    pub fn parse_atom(&mut self) -> ParseResult<Expr>;
}
```

### Patterns

```rust
impl Parser {
    /// Parse a pattern
    pub fn parse_pat(&mut self) -> ParseResult<Pat>;

    /// Parse atomic pattern (no infix constructors)
    pub fn parse_apat(&mut self) -> ParseResult<Pat>;
}
```

### Types

```rust
impl Parser {
    /// Parse a type expression
    pub fn parse_type(&mut self) -> ParseResult<Type>;

    /// Parse type with context: `Eq a => a -> a -> Bool`
    pub fn parse_qualified_type(&mut self) -> ParseResult<Type>;

    /// Parse atomic type
    pub fn parse_atype(&mut self) -> ParseResult<Type>;
}
```

## Operator Precedence

The parser uses Pratt parsing for expressions:

```rust
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Precedence {
    Lowest,      // 0
    Or,          // 2  (||)
    And,         // 3  (&&)
    Compare,     // 4  (==, /=, <, >, <=, >=)
    Concat,      // 5  (++, <>)
    Add,         // 6  (+, -)
    Mul,         // 7  (*, /, `div`, `mod`)
    Power,       // 8  (^, ^^, **)
    Compose,     // 9  (.)
    App,         // 10 (function application)
}

#[derive(Clone, Copy)]
pub enum Assoc {
    Left,
    Right,
    None,
}
```

### Operator Table

Standard Haskell fixities:

```rust
const OPERATORS: &[(&str, Precedence, Assoc)] = &[
    ("$",  Precedence::Lowest,  Assoc::Right),
    ("||", Precedence::Or,      Assoc::Right),
    ("&&", Precedence::And,     Assoc::Right),
    ("==", Precedence::Compare, Assoc::None),
    ("/=", Precedence::Compare, Assoc::None),
    ("<",  Precedence::Compare, Assoc::None),
    (">",  Precedence::Compare, Assoc::None),
    ("<=", Precedence::Compare, Assoc::None),
    (">=", Precedence::Compare, Assoc::None),
    ("++", Precedence::Concat,  Assoc::Right),
    ("<>", Precedence::Concat,  Assoc::Right),
    ("+",  Precedence::Add,     Assoc::Left),
    ("-",  Precedence::Add,     Assoc::Left),
    ("*",  Precedence::Mul,     Assoc::Left),
    ("/",  Precedence::Mul,     Assoc::Left),
    ("^",  Precedence::Power,   Assoc::Right),
    (".",  Precedence::Compose, Assoc::Right),
];
```

### Pratt Parsing

```rust
impl Parser {
    fn parse_expr_prec(&mut self, min_prec: Precedence) -> ParseResult<Expr> {
        let mut lhs = self.parse_atom()?;

        while let Some((op, prec, assoc)) = self.peek_operator() {
            if prec < min_prec {
                break;
            }

            self.advance(); // consume operator

            let next_prec = match assoc {
                Assoc::Left => Precedence::from(prec.0 + 1),
                Assoc::Right => prec,
                Assoc::None => Precedence::from(prec.0 + 1),
            };

            let rhs = self.parse_expr_prec(next_prec)?;
            lhs = Expr::InfixApp(Box::new(lhs), op, Box::new(rhs));
        }

        Ok(lhs)
    }
}
```

## Error Recovery

The parser continues after errors to report multiple issues:

```rust
pub enum ParseError {
    /// Unexpected token
    Unexpected {
        found: TokenKind,
        expected: Vec<TokenKind>,
        span: Span,
    },

    /// Missing token
    Missing {
        expected: TokenKind,
        span: Span,
    },

    /// Invalid syntax construct
    InvalidSyntax {
        message: String,
        span: Span,
    },

    /// Unmatched bracket
    UnmatchedBracket {
        kind: BracketKind,
        open_span: Span,
    },
}
```

### Recovery Strategies

```rust
impl Parser {
    /// Skip to next declaration
    fn recover_to_decl(&mut self) {
        while !self.at_eof() {
            if self.at_decl_start() {
                break;
            }
            self.advance();
        }
    }

    /// Skip to closing bracket
    fn recover_to_close(&mut self, close: TokenKind) {
        let mut depth = 1;
        while !self.at_eof() && depth > 0 {
            match self.current_kind() {
                k if k.is_open_bracket() => depth += 1,
                k if k == close => depth -= 1,
                _ => {}
            }
            self.advance();
        }
    }
}
```

## Import Parsing

```rust
pub struct ImportDecl {
    /// Is this a qualified import?
    pub qualified: bool,
    /// Module being imported
    pub module: ModuleName,
    /// Alias: `import Foo as F`
    pub alias: Option<ModuleName>,
    /// Import spec: `(foo, bar)` or `hiding (baz)`
    pub spec: Option<ImportSpec>,
    /// Is this a package import? (H26)
    pub package: Option<String>,
    pub span: Span,
}

// import qualified Data.Map as M (lookup, insert)
// import "base" Data.List hiding (map)
```

## GADT Parsing

```rust
// data Expr a where
//   Lit  :: Int -> Expr Int
//   Add  :: Expr Int -> Expr Int -> Expr Int
//   IsZero :: Expr Int -> Expr Bool

impl Parser {
    fn parse_gadt_decl(&mut self) -> ParseResult<GadtDecl> {
        self.expect(TokenKind::Data)?;
        let name = self.parse_ident()?;
        let ty_vars = self.parse_ty_var_binders()?;
        self.expect(TokenKind::Where)?;

        let constrs = self.parse_gadt_constructors()?;

        Ok(GadtDecl { name, ty_vars, constrs, .. })
    }

    fn parse_gadt_constructor(&mut self) -> ParseResult<GadtConDecl> {
        let name = self.parse_con_ident()?;
        self.expect(TokenKind::DoubleColon)?;
        let ty = self.parse_type()?;
        Ok(GadtConDecl { name, ty, .. })
    }
}
```

## Layout Handling

The parser consumes layout tokens from the lexer:

```rust
impl Parser {
    /// Check for virtual semicolon (new declaration)
    fn at_semi(&self) -> bool {
        matches!(
            self.current_kind(),
            TokenKind::Semi | TokenKind::VirtualSemi
        )
    }

    /// Check for virtual close brace (end of block)
    fn at_close(&self) -> bool {
        matches!(
            self.current_kind(),
            TokenKind::RBrace | TokenKind::VirtualRBrace
        )
    }

    /// Parse a layout block
    fn parse_layout_block<T>(
        &mut self,
        parse_item: impl Fn(&mut Self) -> ParseResult<T>,
    ) -> ParseResult<Vec<T>> {
        let mut items = Vec::new();

        // Expect open brace (explicit or virtual)
        self.expect_open_brace()?;

        while !self.at_close() && !self.at_eof() {
            items.push(parse_item(self)?);

            // Expect semicolon between items
            if !self.at_close() {
                self.expect_semi()?;
            }
        }

        self.expect_close_brace()?;
        Ok(items)
    }
}
```

## Do Notation

```rust
// do
//   x <- getLine
//   let y = process x
//   putStrLn y
//   return ()

impl Parser {
    fn parse_do_expr(&mut self) -> ParseResult<Expr> {
        self.expect(TokenKind::Do)?;
        let stmts = self.parse_layout_block(Self::parse_stmt)?;

        // Last statement must be expression
        if let Some(Stmt::Bind(..)) = stmts.last() {
            return Err(ParseError::InvalidSyntax {
                message: "do block must end with expression".into(),
                span: stmts.last().unwrap().span(),
            });
        }

        Ok(Expr::Do(stmts))
    }

    fn parse_stmt(&mut self) -> ParseResult<Stmt> {
        if self.at(TokenKind::Let) {
            // let x = 1
            self.advance();
            let decls = self.parse_layout_block(Self::parse_decl)?;
            Ok(Stmt::Let(decls))
        } else {
            let expr = self.parse_expr()?;

            if self.at(TokenKind::LeftArrow) {
                // x <- action
                self.advance();
                // The expr we parsed is actually a pattern
                let pat = self.expr_to_pat(expr)?;
                let rhs = self.parse_expr()?;
                Ok(Stmt::Bind(pat, rhs))
            } else {
                Ok(Stmt::Expr(expr))
            }
        }
    }
}
```

## Configuration

```rust
pub struct ParserConfig {
    /// Enable H26 extensions
    pub h26_extensions: bool,

    /// Enable M9 dependent types
    pub m9_dependent_types: bool,

    /// Maximum expression nesting depth
    pub max_depth: u32,

    /// Error recovery mode
    pub recover: bool,
}

impl Default for ParserConfig {
    fn default() -> Self {
        Self {
            h26_extensions: true,
            m9_dependent_types: false,
            max_depth: 256,
            recover: true,
        }
    }
}
```

## Performance

- Single-pass parsing
- No backtracking for common constructs
- Token lookahead limited to 2 tokens
- Direct AST construction (no intermediate representation)

## Diagnostics Integration

```rust
impl Parser {
    pub fn parse_with_diagnostics(
        &mut self,
        handler: &mut DiagnosticHandler,
    ) -> Option<Module> {
        match self.parse_module() {
            Ok(module) => Some(module),
            Err(errors) => {
                for err in errors {
                    handler.emit(err.into_diagnostic());
                }
                None
            }
        }
    }
}
```

## Testing

```rust
#[cfg(test)]
mod tests {
    use super::*;

    fn parse_expr(s: &str) -> Expr {
        let file = SourceFile::new(FileId::new(0), "test".into(), s.into());
        let tokens: Vec<_> = Lexer::new(&file).collect();
        let mut parser = Parser::new(&tokens);
        parser.parse_expr().unwrap()
    }

    #[test]
    fn test_simple_app() {
        let expr = parse_expr("f x y");
        assert!(matches!(expr, Expr::App(..)));
    }

    #[test]
    fn test_operator_precedence() {
        let expr = parse_expr("1 + 2 * 3");
        // Should parse as 1 + (2 * 3)
        match expr {
            Expr::InfixApp(_, op, _) => {
                assert_eq!(op.as_str(), "+");
            }
            _ => panic!("expected infix app"),
        }
    }
}
```