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
18pub 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
34pub 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
42pub fn function_out_arg(input: ParseString) -> ParseResult<Vec<FunctionArgument>> {
44 let ((input, arg)) = function_arg(input)?;
45 Ok((input, vec![arg]))
46}
47
48pub 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
55pub 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
63pub 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
70pub 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
80pub fn call_arg(input: ParseString) -> ParseResult<(Option<Identifier>,Expression)> {
82 let (input, expr) = expression(input)?;
83 Ok((input, (None, expr)))
84}