harn-parser 0.7.39

Parser, AST, and type checker for the Harn programming language
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
mod decls;
mod error;
mod expressions;
mod patterns;
mod state;
mod statements;
mod types;

pub use error::ParserError;
pub use state::Parser;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::*;
    use harn_lexer::Lexer;

    fn parse_source(source: &str) -> Result<Vec<SNode>, ParserError> {
        let mut lexer = Lexer::new(source);
        let tokens = lexer.tokenize().unwrap();
        let mut parser = Parser::new(tokens);
        parser.parse()
    }

    #[test]
    fn parses_match_expression_with_let_in_arm_body() {
        let source = r#"
pipeline p() {
  let x = match 1 {
    1 -> {
      let a = 1
      a
    }
    _ -> { 0 }
  }
}
"#;

        assert!(parse_source(source).is_ok());
    }

    #[test]
    fn parses_line_leading_infix_continuation_operators() {
        let source = r#"
pipeline p() {
  let fallback = nil
    ?? "fallback"
  let same = 1
    == 1
  let smaller = 1
    < 2
}
"#;

        assert!(parse_source(source).is_ok());
    }

    #[test]
    fn parses_line_trailing_infix_continuation_operators() {
        // Sister case to the leading-operator test above. The trailing form
        // (operator at the end of the previous line, right operand on the
        // continuation line) used to error with "expected expression, found
        // \\n" because each binary-op parser advanced past the operator and
        // immediately tried to parse the right operand without skipping the
        // newline that followed.
        //
        // We assert against a raw source string here rather than a
        // conformance fixture because `harn fmt` canonicalizes the trailing
        // form back to the leading form on save, so a fixture round-trips
        // before the test ever validates the trailing form.
        let source = r#"
pipeline p() {
  let nc = nil ??
    "fallback"
  let conj = true &&
    true
  let disj = false ||
    true
  let same = 1 ==
    1
  let diff = 1 !=
    2
  let lt = 1 <
    2
  let gte = 2 >=
    2
  let sum = 1 +
    2
  let mul = 4 *
    2
  let div = 8 /
    2
  let pow = 2 **
    3
  let chain = nil ??
    nil ??
    "chain"
  let piped = 1 |>
    to_string
}
"#;

        assert!(parse_source(source).is_ok());
    }

    #[test]
    fn parses_public_declarations_and_generic_interfaces() {
        let source = r#"
pub pipeline build(task) extends base {
  return
}

pub enum Result {
  Ok(value: string),
  Err(message: string, code: int),
}

pub struct Config {
  host: string
  port?: int
}

interface Repository<T> {
  type Item
  fn get(id: string) -> T
  fn map<U>(value: T, f: fn(T) -> U) -> U
}
"#;

        let program = parse_source(source).expect("should parse");
        assert!(matches!(
            &program[0].node,
            Node::Pipeline {
                is_pub: true,
                extends: Some(base),
                ..
            } if base == "base"
        ));
        assert!(matches!(
            &program[1].node,
            Node::EnumDecl {
                is_pub: true,
                type_params,
                ..
            } if type_params.is_empty()
        ));
        assert!(matches!(
            &program[2].node,
            Node::StructDecl {
                is_pub: true,
                type_params,
                ..
            } if type_params.is_empty()
        ));
        assert!(matches!(
            &program[3].node,
            Node::InterfaceDecl {
                type_params,
                associated_types,
                methods,
                ..
            }
                if type_params.len() == 1
                    && associated_types.len() == 1
                    && methods.len() == 2
                    && methods[1].type_params.len() == 1
        ));
    }

    #[test]
    fn parses_generic_structs_and_enums() {
        let source = r#"
struct Pair<A, B> {
  first: A
  second: B
}

enum Option<T> {
  Some(value: T)
  None
}
"#;

        let program = parse_source(source).expect("should parse");
        assert!(matches!(
            &program[0].node,
            Node::StructDecl { type_params, .. } if type_params.len() == 2
        ));
        assert!(matches!(
            &program[1].node,
            Node::EnumDecl { type_params, .. } if type_params.len() == 1
        ));
    }

    #[test]
    fn parses_struct_literal_syntax_for_known_structs() {
        let source = r#"
struct Point {
  x: int
  y: int
}

pipeline test(task) {
  let point = Point { x: 3, y: 4 }
}
"#;

        let program = parse_source(source).expect("should parse");
        let pipeline = program
            .iter()
            .find(|node| matches!(node.node, Node::Pipeline { .. }))
            .expect("pipeline node");
        let body = match &pipeline.node {
            Node::Pipeline { body, .. } => body,
            _ => unreachable!(),
        };
        assert!(matches!(
            &body[0].node,
            Node::LetBinding { value, .. }
                if matches!(
                    value.node,
                    Node::StructConstruct { ref struct_name, ref fields }
                        if struct_name == "Point" && fields.len() == 2
                )
        ));
    }

    #[test]
    fn parses_struct_literal_syntax_without_prior_struct_decl() {
        let source = r#"
pipeline test(task) {
  let point = Point { x: 3, y: 4 }
}
"#;

        let program = parse_source(source).expect("should parse");
        let pipeline = program
            .iter()
            .find(|node| matches!(node.node, Node::Pipeline { .. }))
            .expect("pipeline node");
        let body = match &pipeline.node {
            Node::Pipeline { body, .. } => body,
            _ => unreachable!(),
        };
        assert!(matches!(
            &body[0].node,
            Node::LetBinding { value, .. }
                if matches!(
                    value.node,
                    Node::StructConstruct { ref struct_name, ref fields }
                        if struct_name == "Point" && fields.len() == 2
                )
        ));
    }

    #[test]
    fn parses_exponentiation_as_right_associative() {
        let mut lexer = Lexer::new("a ** b ** c");
        let tokens = lexer.tokenize().expect("tokens");
        let mut parser = Parser::new(tokens);
        let expr = parser.parse_single_expression().expect("expression");

        assert!(matches!(
            expr.node,
            Node::BinaryOp { ref op, ref left, ref right }
                if op == "**"
                    && matches!(left.node, Node::Identifier(ref name) if name == "a")
                    && matches!(
                        right.node,
                        Node::BinaryOp { ref op, ref left, ref right }
                            if op == "**"
                                && matches!(left.node, Node::Identifier(ref name) if name == "b")
                                && matches!(right.node, Node::Identifier(ref name) if name == "c")
                    )
        ));
    }

    #[test]
    fn parses_exponentiation_tighter_than_multiplication() {
        let mut lexer = Lexer::new("a * b ** c");
        let tokens = lexer.tokenize().expect("tokens");
        let mut parser = Parser::new(tokens);
        let expr = parser.parse_single_expression().expect("expression");

        assert!(matches!(
            expr.node,
            Node::BinaryOp { ref op, ref left, ref right }
                if op == "*"
                    && matches!(left.node, Node::Identifier(ref name) if name == "a")
                    && matches!(
                        right.node,
                        Node::BinaryOp { ref op, ref left, ref right }
                            if op == "**"
                                && matches!(left.node, Node::Identifier(ref name) if name == "b")
                                && matches!(right.node, Node::Identifier(ref name) if name == "c")
                    )
        ));
    }

    #[test]
    fn parses_semicolon_separated_statements_in_block() {
        let source = r#"
pipeline p(task) {
  let x = 1; let y = 2
}
"#;

        let program = parse_source(source).expect("should parse");
        let pipeline = program
            .iter()
            .find(|node| matches!(node.node, Node::Pipeline { .. }))
            .expect("pipeline node");
        let body = match &pipeline.node {
            Node::Pipeline { body, .. } => body,
            _ => unreachable!(),
        };
        assert_eq!(body.len(), 2, "semicolon should separate block statements");
    }

    #[test]
    fn parses_semicolon_separated_top_level_items() {
        let source = r#"fn first() {} ; fn second() {}"#;
        let program = parse_source(source).expect("should parse");
        assert_eq!(
            program.len(),
            2,
            "semicolon should separate top-level items"
        );
    }

    #[test]
    fn parses_return_and_yield_with_semicolon_terminators() {
        let source = r#"
fn generator() {
  yield; log("after yield")
}

pipeline p(task) {
  return; log("after return")
}
"#;

        let program = parse_source(source).expect("should parse");
        let generator = program
            .iter()
            .find(|node| matches!(&node.node, Node::FnDecl { name, .. } if name == "generator"))
            .expect("generator fn");
        let generator_body = match &generator.node {
            Node::FnDecl { body, .. } => body,
            _ => unreachable!(),
        };
        assert_eq!(generator_body.len(), 2);
        assert!(matches!(
            generator_body[0].node,
            Node::YieldExpr { value: None }
        ));

        let pipeline = program
            .iter()
            .find(|node| matches!(node.node, Node::Pipeline { .. }))
            .expect("pipeline node");
        let body = match &pipeline.node {
            Node::Pipeline { body, .. } => body,
            _ => unreachable!(),
        };
        assert_eq!(body.len(), 2);
        assert!(matches!(body[0].node, Node::ReturnStmt { value: None }));
    }

    #[test]
    fn parses_trailing_semicolons_before_brace_and_eof() {
        let block_source = r#"
pipeline p(task) {
  log(1);
}
"#;
        let eof_source = r#"fn only() {};"#;

        assert!(parse_source(block_source).is_ok());
        assert!(parse_source(eof_source).is_ok());
    }

    #[test]
    fn rejects_same_line_statements_without_separator() {
        let source = r#"pipeline p(task) { let x = 1 let y = 2 }"#;
        let err = parse_source(source).expect_err("missing separator should fail");
        assert!(
            err.to_string().contains("separator"),
            "expected separator error, got: {err}"
        );
    }

    #[test]
    fn rejects_semicolon_before_else_and_catch() {
        let if_err = parse_source(r#"pipeline p(task) { if true { log(1) }; else { log(2) } }"#)
            .expect_err("semicolon before else should fail");
        assert!(
            if_err.to_string().contains("separator") || if_err.to_string().contains("else"),
            "unexpected if error: {if_err}"
        );

        let try_err = parse_source(r#"pipeline p(task) { try { log(1) }; catch { log(2) } }"#)
            .expect_err("semicolon before catch should fail");
        assert!(
            try_err.to_string().contains("separator") || try_err.to_string().contains("catch"),
            "unexpected try error: {try_err}"
        );
    }

    #[test]
    fn rejects_empty_statement_from_double_semicolon() {
        let source = r#"pipeline p(task) { log(1);; log(2) }"#;
        assert!(
            parse_source(source).is_err(),
            "double semicolon should fail"
        );
    }
}