|
| 1 | +use nom::{ |
| 2 | + branch::alt, |
| 3 | + bytes::complete::{tag, take_while}, |
| 4 | + character::complete::{alpha1, char, digit1, line_ending, multispace0, space0}, |
| 5 | + combinator::{map, map_res, not, opt, peek, recognize, value, verify}, |
| 6 | + multi::{fold_many0, many0, many1, separated_list0, separated_list1}, |
| 7 | + sequence::{delimited, pair, preceded, terminated, tuple}, |
| 8 | + IResult, |
| 9 | +}; |
| 10 | +use std::str::FromStr; |
| 11 | + |
| 12 | +use crate::ir::ast::{Type, ValueConstructor}; |
| 13 | + |
| 14 | +use crate::parser::parser_common::{keyword, separator, identifier}; |
| 15 | + |
| 16 | +pub fn parse_type(input: &str) -> IResult<&str, Type> { |
| 17 | + alt( |
| 18 | + (parse_basic_types, |
| 19 | +<
10000
div class="diff-text-inner"> parse_list_type, |
| 20 | + parse_tuple_type, |
| 21 | + parse_maybe_type, |
| 22 | + parse_result_type, |
| 23 | + parse_function_type, |
| 24 | + parse_adt_type) |
| 25 | + )(input) |
| 26 | +} |
| 27 | + |
| 28 | +fn parse_basic_types(input: &str) -> IResult<&str, Type> { |
| 29 | + map( |
| 30 | + alt((keyword("Int"), |
| 31 | + keyword("Real"), |
| 32 | + keyword("Boolean"), |
| 33 | + keyword("String"), |
| 34 | + keyword("Unit"), |
| 35 | + keyword("Any") |
| 36 | + )), |
| 37 | + |t| match t { |
| 38 | + "Int" => Type::TInteger, |
| 39 | + "Real" => Type::TReal, |
| 40 | + "Boolean" => Type::TBool, |
| 41 | + "String" => Type::TString, |
| 42 | + "Unit" => Type::TVoid, |
| 43 | + "Any" => Type::TAny, |
| 44 | + _ => unreachable!() |
| 45 | + } |
| 46 | + )(input) |
| 47 | +} |
| 48 | + |
| 49 | +fn parse_list_type(input: &str) -> IResult<&str, Type> { |
| 50 | + map(tuple( |
| 51 | + (preceded(multispace0, char('[')), |
| 52 | + preceded(multispace0, parse_type), |
| 53 | + preceded(multispace0, char(']')), |
| 54 | + )), |
| 55 | + |(_, t, _)| Type::TList(Box::new(t)) |
| 56 | + )(input) |
| 57 | +} |
| 58 | + |
| 59 | +fn parse_tuple_type(input: &str) -> IResult<&str, Type> { |
| 60 | + map(tuple( |
| 61 | + (preceded(multispace0, char('(')), |
| 62 | + preceded(multispace0, separated_list1(separator(","), parse_type)), |
| 63 | + preceded(multispace0, char(')')), |
| 64 | + )), |
| 65 | + |(_, ts, _)| Type::TTuple(ts) |
| 66 | + )(input) |
| 67 | +} |
| 68 | + |
| 69 | +fn parse_maybe_type(input: &str) -> IResult<&str, Type> { |
| 70 | + map(tuple( |
| 71 | + (preceded(multispace0, keyword("Maybe")), |
| 72 | + preceded(multispace0, char('[')), |
| 73 | + preceded(multispace0, parse_type), |
| 74 | + preceded(multispace0, char(']')), |
| 75 | + )), |
| 76 | + |(_, _, t, _)| Type::TMaybe(Box::new(t)) |
| 77 | + )(input) |
| 78 | +} |
| 79 | + |
| 80 | +fn parse_result_type(input: &str) -> IResult<&str, Type> { |
| 81 | + map(tuple( |
| 82 | + (preceded(multispace0, keyword("Result")), |
| 83 | + preceded(multispace0, char('[')), |
| 84 | + preceded(multispace0, parse_type), |
| 85 | + preceded(multispace0, char(',')), |
| 86 | + preceded(multispace0, parse_type), |
| 87 | + preceded(multispace0, char(']')), |
| 88 | + )), |
| 89 | + |(_, _, t_ok, _, t_err, _)| Type::TResult(Box::new(t_ok), Box::new(t_err)) |
| 90 | + )(input) |
| 91 | +} |
| 92 | + |
| 93 | +fn parse_function_type(input: &str) -> IResult<&str, Type> { |
| 94 | + map(tuple( |
| 95 | + (preceded(multispace0, char('(')), |
| 96 | + preceded(multispace0, separated_list0(separator(","), parse_type)), |
| 97 | + preceded(multispace0, char(')')), |
| 98 | + preceded(multispace0, tag("->")), |
| 99 | + preceded(multispace0, parse_type), |
| 100 | + )), |
| 101 | + |(_, t_args, _, _, t_ret)| Type::TFunction(Box::new(Some(t_ret)), t_args) |
| 102 | + )(input) |
| 103 | +} |
| 104 | + |
| 105 | +fn parse_adt_type(input: &str) -> IResult<&str, Type> { |
| 106 | + map( |
| 107 | + tuple(( |
| 108 | + keyword("data"), |
| 109 | + preceded(multispace0, identifier), |
| 110 | + preceded(multispace0, char(':')), |
| 111 | + many1(parse_adt_cons), |
| 112 | + preceded(multispace0, keyword("end")), |
| 113 | + )), |
| 114 | + |(_, name, _, cons, _)| Type::Tadt(name.to_string(), cons), |
| 115 | + )(input) |
| 116 | +} |
| 117 | + |
| 118 | +fn parse_adt_cons(input: &str) -> IResult<&str, ValueConstructor> { |
| 119 | + map( |
| 120 | + tuple(( |
| 121 | + preceded(multispace0, char('|')), |
| 122 | + preceded(multispace0, identifier), |
| 123 | + separated_list0(multispace0, parse_type), |
| 124 | + )), |
| 125 | + |(_, name, types)| ValueConstructor::new(name.to_string(), types), |
| 126 | + )(input) |
| 127 | +} |
| 128 | + |
| 129 | +#[cfg(test)] |
| 130 | +mod tests { |
| 131 | + use super::*; |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_parse_basic_types() { |
| 135 | + assert_eq!(parse_basic_types("Int"), Ok(("", Type::TInteger))); |
| 136 | + assert_eq!(parse_basic_types("Boolean"), Ok(("", Type::TBool))); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_parse_list_type() { |
| 141 | + assert_eq!( |
| 142 | + parse_list_type("[Int]"), |
| 143 | + Ok(("", Type::TList(Box::new(Type::TInteger)))) |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn test_parse_tuple_type() { |
| 149 | + assert_eq!( |
| 150 | + parse_tuple_type("(Int, Real)"), |
| 151 | + Ok(("", Type::TTuple(vec![Type::TInteger, Type::TReal]))) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + #[test] |
| 156 | + fn test_parse_maybe_type() { |
| 157 | + assert_eq!( |
| 158 | + parse_maybe_type("Maybe [Boolean]"), |
| 159 | + Ok(("", Type::TMaybe(Box::new(Type::TBool)))) |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn test_parse_result_type() { |
| 165 | + assert_eq!( |
| 166 | + parse_result_type("Result [Int, String]"), |
| 167 | + Ok(( |
| 168 | + "", |
| 169 | + Type::TResult(Box::new(Type::TInteger), Box::new(Type::TString)) |
| 170 | + )) |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn test_parse_function_type() { |
| 176 | + assert_eq!( |
| 177 | + parse_function_type("(Int, Boolean) -> String"), |
| 178 | + Ok(( |
| 179 | + "", |
| 180 | + Type::TFunction( |
| 181 | + Box::new(Some(Type::TString)), |
| 182 | + vec![Type::TInteger, Type::TBool] |
| 183 | + ) |
| 184 | + )) |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + #[ignore] |
| 190 | + fn test_parse_adt_type() { |
| 191 | + let input = "data Maybe:\n | Just Int\n | Nothing\nend"; |
| 192 | + let expected = Type::Tadt( |
| 193 | + "Maybe".to_string(), |
| 194 | + vec![ |
| 195 | + ValueConstructor::new("Just".to_string(), vec![Type::TInteger]), |
| 196 | + ValueConstructor::new("Nothing".to_string(), vec![]), |
| 197 | + ], |
| 198 | + ); |
| 199 | + assert_eq!(parse_adt_type(input), Ok(("", expected))); |
| 200 | + } |
| 201 | +} |
0 commit comments