mech_syntax/
functions.rs

1#[macro_use]
2use crate::*;
3
4#[cfg(not(feature = "no-std"))] use core::fmt;
5#[cfg(feature = "no-std")] use alloc::fmt;
6#[cfg(feature = "no-std")] use alloc::string::String;
7#[cfg(feature = "no-std")] use alloc::vec::Vec;
8use nom::{
9  IResult,
10  branch::alt,
11  sequence::tuple as nom_tuple,
12  combinator::{opt, eof},
13  multi::{many1, many_till, many0, separated_list1,separated_list0},
14  Err,
15  Err::Failure
16};
17
18// function_define := identifier, "(", list0(list_separator, function_arg), ")", "=", (function_out_args | function_out_arg), define_operator, list1((whitespace1 | statement_separator), statement), period ;
19pub fn function_define(input: ParseString) -> ParseResult<FunctionDefine> {
20  let ((input, name)) = identifier(input)?;
21  let ((input, _)) = left_parenthesis(input)?;
22  let ((input, input_args)) = separated_list0(list_separator, function_arg)(input)?;
23  let ((input, _)) = right_parenthesis(input)?;
24  let ((input, _)) = whitespace0(input)?;
25  let ((input, _)) = equal(input)?;
26  let ((input, _)) = whitespace0(input)?;
27  let ((input, output)) = alt((function_out_args,function_out_arg))(input)?;
28  let ((input, _)) = define_operator(input)?;
29  let ((input, statements)) = separated_list1(alt((whitespace1,statement_separator)), statement)(input)?;
30  let ((input, _)) = period(input)?;
31  Ok((input,FunctionDefine{name,input: input_args,output,statements}))
32}
33
34// function_out_args := "(", list1(list_separator, function_arg), ")" ;
35pub fn function_out_args(input: ParseString) -> ParseResult<Vec<FunctionArgument>> {
36  let ((input, _)) = left_parenthesis(input)?;
37  let ((input, args)) = separated_list1(list_separator,function_arg)(input)?;
38  let ((input, _)) = right_parenthesis(input)?;
39  Ok((input, args))
40}
41
42// function_out_arg := function_arg ;
43pub fn function_out_arg(input: ParseString) -> ParseResult<Vec<FunctionArgument>> {
44  let ((input, arg)) = function_arg(input)?;
45  Ok((input, vec![arg]))
46}
47
48// function_arg := identifier, kind_annotation ;
49pub fn function_arg(input: ParseString) -> ParseResult<FunctionArgument> {
50  let ((input, name)) = identifier(input)?;
51  let ((input, kind)) = kind_annotation(input)?;
52  Ok((input, FunctionArgument{ name, kind }))
53}
54
55// argument_list := "(", list0(",", call_arg_with_biding | call_arg), ")" ;
56pub fn argument_list(input: ParseString) -> ParseResult<ArgumentList> {
57  let (input, _) = left_parenthesis(input)?;
58  let (input, args) = separated_list0(list_separator, alt((call_arg_with_binding,call_arg)))(input)?;
59  let (input, _) = right_parenthesis(input)?;
60  Ok((input, args))
61}
62
63// function_call := identifier, argument_list ;
64pub fn function_call(input: ParseString) -> ParseResult<FunctionCall> {
65  let (input, name) = identifier(input)?;
66  let (input, args) = argument_list(input)?;
67  Ok((input, FunctionCall{name,args} ))
68}
69
70// call_arg_with_binding := identifier, colon, expression ;
71pub fn call_arg_with_binding(input: ParseString) -> ParseResult<(Option<Identifier>,Expression)> {
72  let (input, arg_name) = identifier(input)?;
73  let (input, _) = whitespace0(input)?;
74  let (input, _) = colon(input)?;
75  let (input, _) = whitespace0(input)?;
76  let (input, expr) = expression(input)?;
77  Ok((input, (Some(arg_name), expr)))
78}
79
80// call_arg := expression ;
81pub fn call_arg(input: ParseString) -> ParseResult<(Option<Identifier>,Expression)> {
82  let (input, expr) = expression(input)?;
83  Ok((input, (None, expr)))
84}