shape-ast 0.1.8

AST types and Pest grammar for the Shape 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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Function and annotation parsing for Shape
//!
//! This module handles parsing of:
//! - Function definitions with parameters and return types
//! - Function parameters with default values
//! - Annotations (@warmup, @strategy, etc.)

use crate::ast::{
    Annotation, BuiltinFunctionDecl, ForeignFunctionDef, FunctionDef, FunctionParameter,
    NativeAbiBinding,
};
use crate::error::Result;
use pest::iterators::Pair;

use super::expressions;
use super::statements;
use super::string_literals::parse_string_literal;
use super::types;
use super::types::parse_type_annotation;
use super::{Rule, pair_span};

/// Parse annotations
pub fn parse_annotations(pair: Pair<Rule>) -> Result<Vec<Annotation>> {
    let mut annotations = vec![];

    for annotation_pair in pair.into_inner() {
        if annotation_pair.as_rule() == Rule::annotation {
            annotations.push(parse_annotation(annotation_pair)?);
        }
    }

    Ok(annotations)
}

/// Parse a single annotation
pub fn parse_annotation(pair: Pair<Rule>) -> Result<Annotation> {
    let span = pair_span(&pair);
    let mut name = String::new();
    let mut args = Vec::new();

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::annotation_ref => {
                name = inner_pair.as_str().to_string();
            }
            Rule::annotation_name | Rule::ident => {
                name = inner_pair.as_str().to_string();
            }
            Rule::annotation_args => {
                for arg_pair in inner_pair.into_inner() {
                    if arg_pair.as_rule() == Rule::expression {
                        args.push(expressions::parse_expression(arg_pair)?);
                    }
                }
            }
            Rule::expression => {
                args.push(expressions::parse_expression(inner_pair)?);
            }
            _ => {}
        }
    }

    Ok(Annotation { name, args, span })
}

/// Parse a function parameter
pub fn parse_function_param(pair: Pair<Rule>) -> Result<FunctionParameter> {
    let mut pattern = None;
    let mut is_const = false;
    let mut is_reference = false;
    let mut is_mut_reference = false;
    let mut is_out = false;
    let mut type_annotation = None;
    let mut default_value = None;

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::param_const_keyword => {
                is_const = true;
            }
            Rule::param_ref_keyword => {
                is_reference = true;
                // Check for &mut: param_ref_keyword contains optional param_mut_keyword
                for child in inner_pair.into_inner() {
                    if child.as_rule() == Rule::param_mut_keyword {
                        is_mut_reference = true;
                    }
                }
            }
            Rule::param_out_keyword => {
                is_out = true;
            }
            Rule::destructure_pattern => {
                pattern = Some(super::items::parse_pattern(inner_pair)?);
            }
            Rule::type_annotation => {
                type_annotation = Some(parse_type_annotation(inner_pair)?);
            }
            Rule::expression => {
                default_value = Some(expressions::parse_expression(inner_pair)?);
            }
            _ => {}
        }
    }

    let pattern = pattern.ok_or_else(|| crate::error::ShapeError::ParseError {
        message: "expected pattern in function parameter".to_string(),
        location: None,
    })?;

    Ok(FunctionParameter {
        pattern,
        is_const,
        is_reference,
        is_mut_reference,
        is_out,
        type_annotation,
        default_value,
    })
}

/// Parse a function definition
pub fn parse_function_def(pair: Pair<Rule>) -> Result<FunctionDef> {
    let mut name = String::new();
    let mut name_span = crate::ast::Span::DUMMY;
    let mut type_params = None;
    let mut params = vec![];
    let mut return_type = None;
    let mut where_clause = None;
    let mut body = vec![];
    let mut annotations = vec![];
    let mut is_async = false;
    let mut is_comptime = false;

    // Parse all parts sequentially (can't use find() as it consumes the iterator)
    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::annotations => {
                annotations = parse_annotations(inner_pair)?;
            }
            Rule::async_keyword => {
                is_async = true;
            }
            Rule::comptime_keyword => {
                is_comptime = true;
            }
            Rule::ident => {
                if name.is_empty() {
                    name = inner_pair.as_str().to_string();
                    name_span = pair_span(&inner_pair);
                }
            }
            Rule::type_params => {
                type_params = Some(types::parse_type_params(inner_pair)?);
            }
            Rule::function_params => {
                for param_pair in inner_pair.into_inner() {
                    if param_pair.as_rule() == Rule::function_param {
                        params.push(parse_function_param(param_pair)?);
                    }
                }
            }
            Rule::return_type => {
                // Skip the "->" and get the type annotation
                if let Some(type_pair) = inner_pair.into_inner().next() {
                    return_type = Some(parse_type_annotation(type_pair)?);
                }
            }
            Rule::where_clause => {
                where_clause = Some(parse_where_clause(inner_pair)?);
            }
            Rule::function_body => {
                // Parse all statements in the function body
                body = statements::parse_statements(inner_pair.into_inner())?;
            }
            _ => {}
        }
    }

    Ok(FunctionDef {
        name,
        name_span,
        declaring_module_path: None,
        doc_comment: None,
        type_params,
        params,
        return_type,
        where_clause,
        body,
        annotations,
        is_async,
        is_comptime,
    })
}

/// Parse a declaration-only builtin function definition.
///
/// Grammar:
/// `builtin fn name<T>(params...) -> ReturnType;`
pub fn parse_builtin_function_decl(pair: Pair<Rule>) -> Result<BuiltinFunctionDecl> {
    let mut name = String::new();
    let mut name_span = crate::ast::Span::DUMMY;
    let mut type_params = None;
    let mut params = vec![];
    let mut return_type = None;

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::ident => {
                if name.is_empty() {
                    name = inner_pair.as_str().to_string();
                    name_span = pair_span(&inner_pair);
                }
            }
            Rule::type_params => {
                type_params = Some(types::parse_type_params(inner_pair)?);
            }
            Rule::function_params => {
                for param_pair in inner_pair.into_inner() {
                    if param_pair.as_rule() == Rule::function_param {
                        params.push(parse_function_param(param_pair)?);
                    }
                }
            }
            Rule::return_type => {
                if let Some(type_pair) = inner_pair.into_inner().next() {
                    return_type = Some(parse_type_annotation(type_pair)?);
                }
            }
            _ => {}
        }
    }

    let return_type = return_type.ok_or_else(|| crate::error::ShapeError::ParseError {
        message: "builtin function declaration requires an explicit return type".to_string(),
        location: None,
    })?;

    Ok(BuiltinFunctionDecl {
        name,
        name_span,
        doc_comment: None,
        type_params,
        params,
        return_type,
    })
}

/// Parse a foreign function definition: `fn python analyze(data: DataTable) -> number { ... }`
pub fn parse_foreign_function_def(pair: Pair<Rule>) -> Result<ForeignFunctionDef> {
    let mut language = String::new();
    let mut language_span = crate::ast::Span::DUMMY;
    let mut name = String::new();
    let mut name_span = crate::ast::Span::DUMMY;
    let mut type_params = None;
    let mut params = vec![];
    let mut return_type = None;
    let mut body_text = String::new();
    let mut body_span = crate::ast::Span::DUMMY;
    let mut annotations = vec![];
    let mut is_async = false;

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::annotations => {
                annotations = parse_annotations(inner_pair)?;
            }
            Rule::async_keyword => {
                is_async = true;
            }
            Rule::function_keyword => {}
            Rule::foreign_language_id => {
                language = inner_pair.as_str().to_string();
                language_span = pair_span(&inner_pair);
            }
            Rule::ident => {
                if name.is_empty() {
                    name = inner_pair.as_str().to_string();
                    name_span = pair_span(&inner_pair);
                }
            }
            Rule::type_params => {
                type_params = Some(types::parse_type_params(inner_pair)?);
            }
            Rule::function_params => {
                for param_pair in inner_pair.into_inner() {
                    if param_pair.as_rule() == Rule::function_param {
                        params.push(parse_function_param(param_pair)?);
                    }
                }
            }
            Rule::return_type => {
                if let Some(type_pair) = inner_pair.into_inner().next() {
                    return_type = Some(parse_type_annotation(type_pair)?);
                }
            }
            Rule::foreign_body => {
                body_span = pair_span(&inner_pair);
                body_text = dedent_foreign_body(inner_pair.as_str());
            }
            _ => {}
        }
    }

    Ok(ForeignFunctionDef {
        language,
        language_span,
        name,
        name_span,
        doc_comment: None,
        type_params,
        params,
        return_type,
        body_text,
        body_span,
        annotations,
        is_async,
        native_abi: None,
    })
}

/// Parse a native ABI declaration:
/// `extern "C" fn name(args...) -> Ret from "library" [as "symbol"];`
pub fn parse_extern_native_function_def(pair: Pair<Rule>) -> Result<ForeignFunctionDef> {
    let mut abi = String::new();
    let mut abi_span = crate::ast::Span::DUMMY;
    let mut name = String::new();
    let mut name_span = crate::ast::Span::DUMMY;
    let mut type_params = None;
    let mut params = Vec::new();
    let mut return_type = None;
    let mut library: Option<String> = None;
    let mut symbol: Option<String> = None;
    let mut annotations = Vec::new();
    let mut is_async = false;

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::annotations => {
                annotations = parse_annotations(inner_pair)?;
            }
            Rule::async_keyword => {
                is_async = true;
            }
            Rule::extern_abi => {
                abi_span = pair_span(&inner_pair);
                abi = parse_extern_abi(inner_pair)?;
            }
            Rule::function_keyword => {}
            Rule::ident => {
                if name.is_empty() {
                    name = inner_pair.as_str().to_string();
                    name_span = pair_span(&inner_pair);
                }
            }
            Rule::type_params => {
                type_params = Some(types::parse_type_params(inner_pair)?);
            }
            Rule::function_params => {
                for param_pair in inner_pair.into_inner() {
                    if param_pair.as_rule() == Rule::function_param {
                        params.push(parse_function_param(param_pair)?);
                    }
                }
            }
            Rule::return_type => {
                if let Some(type_pair) = inner_pair.into_inner().next() {
                    return_type = Some(parse_type_annotation(type_pair)?);
                }
            }
            Rule::extern_native_link => {
                for link_part in inner_pair.into_inner() {
                    match link_part.as_rule() {
                        Rule::extern_native_library => {
                            library = Some(parse_string_literal(link_part.as_str())?);
                        }
                        Rule::extern_native_symbol => {
                            symbol = Some(parse_string_literal(link_part.as_str())?);
                        }
                        _ => {}
                    }
                }
            }
            _ => {}
        }
    }

    let library = library.ok_or_else(|| crate::error::ShapeError::ParseError {
        message: "extern native declaration requires `from \"library\"`".to_string(),
        location: None,
    })?;

    if abi.trim() != "C" {
        return Err(crate::error::ShapeError::ParseError {
            message: format!(
                "unsupported extern ABI '{}': only \"C\" is currently supported",
                abi
            ),
            location: None,
        });
    }

    let symbol = symbol.unwrap_or_else(|| name.clone());

    Ok(ForeignFunctionDef {
        // Keep foreign-language compatibility for downstream compilation/runtime
        // while carrying explicit native ABI metadata.
        language: "native".to_string(),
        language_span: abi_span,
        name,
        name_span,
        doc_comment: None,
        type_params,
        params,
        return_type,
        body_text: String::new(),
        body_span: crate::ast::Span::DUMMY,
        annotations,
        is_async,
        native_abi: Some(NativeAbiBinding {
            abi,
            library,
            symbol,
            package_key: None,
        }),
    })
}

pub(crate) fn parse_extern_abi(pair: Pair<Rule>) -> Result<String> {
    let inner = pair
        .into_inner()
        .next()
        .ok_or_else(|| crate::error::ShapeError::ParseError {
            message: "extern declaration is missing ABI name".to_string(),
            location: None,
        })?;

    match inner.as_rule() {
        Rule::string => parse_string_literal(inner.as_str()),
        Rule::ident => Ok(inner.as_str().to_string()),
        _ => Err(crate::error::ShapeError::ParseError {
            message: format!("unsupported extern ABI token: {:?}", inner.as_rule()),
            location: None,
        }),
    }
}

/// Strip common leading whitespace from foreign body text.
///
/// Similar to Python's `textwrap.dedent`. This is critical for Python blocks
/// since the body is indented inside Shape code but needs to be dedented
/// for the foreign language runtime.
///
/// Note: The Pest parser's implicit WHITESPACE rule consumes the newline and
/// leading whitespace between `{` and the first token of `foreign_body`. This
/// means the first line has its leading whitespace eaten by the parser, while
/// subsequent lines retain their original indentation. We compute `min_indent`
/// from lines after the first, then strip that amount only from those lines.
/// The first line is kept as-is.
fn dedent_foreign_body(text: &str) -> String {
    let lines: Vec<&str> = text.lines().collect();
    if lines.is_empty() {
        return String::new();
    }
    if lines.len() == 1 {
        return lines[0].trim_start().to_string();
    }

    // Compute min_indent from lines after the first, since the parser already
    // consumed the first line's leading whitespace.
    let min_indent = lines
        .iter()
        .skip(1)
        .filter(|line| !line.trim().is_empty())
        .map(|line| line.len() - line.trim_start().len())
        .min()
        .unwrap_or(0);

    // First line: keep as-is (parser already stripped its whitespace).
    // Subsequent lines: strip min_indent characters.
    let mut result = Vec::with_capacity(lines.len());
    result.push(lines[0]);
    for line in &lines[1..] {
        if line.len() >= min_indent {
            result.push(&line[min_indent..]);
        } else {
            result.push(line.trim());
        }
    }
    result.join("\n")
}

/// Parse a where clause: `where T: Bound1 + Bound2, U: Bound3`
pub fn parse_where_clause(pair: Pair<Rule>) -> Result<Vec<crate::ast::types::WherePredicate>> {
    let mut predicates = Vec::new();
    for child in pair.into_inner() {
        if child.as_rule() == Rule::where_predicate {
            predicates.push(parse_where_predicate(child)?);
        }
    }
    Ok(predicates)
}

fn parse_where_predicate(pair: Pair<Rule>) -> Result<crate::ast::types::WherePredicate> {
    let mut inner = pair.into_inner();

    let name_pair = inner
        .next()
        .ok_or_else(|| crate::error::ShapeError::ParseError {
            message: "expected type parameter name in where predicate".to_string(),
            location: None,
        })?;
    let type_name = name_pair.as_str().to_string();

    let mut bounds = Vec::new();
    for remaining in inner {
        if remaining.as_rule() == Rule::trait_bound_list {
            for bound_ident in remaining.into_inner() {
                if bound_ident.as_rule() == Rule::qualified_ident {
                    bounds.push(bound_ident.as_str().into());
                }
            }
        }
    }

    Ok(crate::ast::types::WherePredicate { type_name, bounds })
}