More work

This commit is contained in:
greg 2020-03-07 04:54:38 -08:00
parent 9e898d86b3
commit 33d4b28786

View File

@ -17,7 +17,6 @@ use crate::builtin::Builtin;
type ParseResult<'a, T> = IResult<&'a str, T, VerboseError<&'a str>>; type ParseResult<'a, T> = IResult<&'a str, T, VerboseError<&'a str>>;
pub fn ws<I, O, E: ParseError<I>, F>(parser: F) -> impl Fn(I) -> IResult<I, O, E> pub fn ws<I, O, E: ParseError<I>, F>(parser: F) -> impl Fn(I) -> IResult<I, O, E>
where where
I: nom::InputTakeAtPosition, I: nom::InputTakeAtPosition,
@ -27,7 +26,6 @@ where
delimited(space0, parser, space0) delimited(space0, parser, space0)
} }
fn single_alphabetic_character(text: &str) -> ParseResult<char> { fn single_alphabetic_character(text: &str) -> ParseResult<char> {
let p = verify(take(1usize), |s: &str| s.chars().nth(0).map(|c| c.is_alphabetic()).unwrap_or(false)); let p = verify(take(1usize), |s: &str| s.chars().nth(0).map(|c| c.is_alphabetic()).unwrap_or(false));
map(p, |s: &str| s.chars().nth(0).unwrap())(text) map(p, |s: &str| s.chars().nth(0).unwrap())(text)
@ -78,7 +76,6 @@ fn number_literal(text: &str) -> ParseResult<ExpressionKind> {
Ok((text, ExpressionKind::NatLiteral(n))) Ok((text, ExpressionKind::NatLiteral(n)))
} }
fn binary_literal(text: &str) -> ParseResult<ExpressionKind> { fn binary_literal(text: &str) -> ParseResult<ExpressionKind> {
let p = preceded(tag("0b"), cut(take_while1(|c: char| c == '0' || c == '1'))); let p = preceded(tag("0b"), cut(take_while1(|c: char| c == '0' || c == '1')));
let (rest, n): (&str, u64) = map_res( let (rest, n): (&str, u64) = map_res(
@ -148,6 +145,7 @@ fn primary_expr(text: &str) -> ParseResult<ExpressionKind> {
literal, literal,
paren_expr, paren_expr,
identifier_expr, identifier_expr,
//if_expr,
))(text) ))(text)
} }
@ -161,19 +159,32 @@ fn invocation_argument(text: &str) -> ParseResult<InvocationArgument> {
))(text) ))(text)
} }
/*
fn if_expr(text: &str) -> ParseResult<ExpressionKind> {
let p = preceded(tag("if"), pair(discriminator, if_expr_body));
map(p, |(discriminator, body)| {
})(text)
let expr = IfExpression {
discriminator: Option<Box<Expression>>,
body: Box<IfExpressionBody>,
};
}
*/
fn call_expr(text: &str) -> ParseResult<ExpressionKind> { fn call_expr(text: &str) -> ParseResult<ExpressionKind> {
use nom::character::complete::char; use nom::character::complete::char;
let (text, expr) = primary_expr(text)?; let parse_call = opt(
let (text, call_part) = opt(
delimited(char('('), separated_list(char(','), invocation_argument), char(')')) delimited(char('('), separated_list(char(','), invocation_argument), char(')'))
)(text)?; );
let output = if let Some(arguments) = call_part { let p = pair(primary_expr, parse_call);
map(p, |(expr, call_part)| if let Some(arguments) = call_part {
let f = bx!(Expression { id: ItemId::new(0), kind: expr, type_anno: None }); let f = bx!(Expression { id: ItemId::new(0), kind: expr, type_anno: None });
ExpressionKind::Call { f, arguments } ExpressionKind::Call { f, arguments }
} else { } else {
expr expr
}; })(text)
Ok((text, output))
} }
fn prefix_expr(text: &str) -> ParseResult<ExpressionKind> { fn prefix_expr(text: &str) -> ParseResult<ExpressionKind> {