diff --git a/rura-grammar/src/lib.rs b/rura-grammar/src/lib.rs index f681b7b..dfd9b01 100644 --- a/rura-grammar/src/lib.rs +++ b/rura-grammar/src/lib.rs @@ -10,9 +10,9 @@ use rura_core::keywords::{BOTTOM, UNIT}; use rura_core::{Constructor, Error, Input, Span}; use rura_parsing::{ binary, braced, closure_parameters, constant, constructor, constructor_parameters, elidable, - elidable_block, elided_block, expect, field, function_parameters, function_type, identifier, - parenthesized, prefixed, primitive_type, qualified_name, reference_type, skip_space, tuple, - tuple_type, type_arguments, type_parameters, unary, + elidable_block, elided_block, expect, field, function_arguments, function_parameters, + function_type, identifier, parenthesized, prefixed, primitive_type, qualified_name, + reference_type, skip_space, tuple, tuple_type, type_arguments, type_parameters, unary, }; pub fn module(i: &str, id: ModuleID) -> Result { @@ -165,9 +165,9 @@ fn let_statement(i: &mut Input) -> PResult { skip_space(identifier), opt(prefixed(":", skip_space(type_expression))), "=", - value_expression, + skip_space(value_expression), elidable(";"), - block_statement, + skip_space(block_statement), ) .with_span() .map(|((_, name, typ, _, value, _, body), span)| AST { @@ -208,7 +208,7 @@ fn value_expression(i: &mut Input) -> PResult { fn closure(i: &mut Input) -> PResult { ( closure_parameters, - alt((braced(block_statement), value_expression)), + skip_space(alt((braced(block_statement), value_expression))), ) .with_span() .map(|((parameters, body), span)| AST { @@ -220,7 +220,7 @@ fn closure(i: &mut Input) -> PResult { } fn indexing(i: &mut Input) -> PResult { - (alt((call, primary_value_expression)), ".", dec_uint) + (primary_value_expression, skip_space("."), dec_uint) .with_span() .map(|((tuple, _, index), span)| AST { span, @@ -237,7 +237,7 @@ fn call(i: &mut Input) -> PResult { 1.., skip_space(( opt(prefixed("::", type_arguments(type_expression))), - tuple(value_expression), + function_arguments(value_expression), )), ), ) @@ -304,9 +304,12 @@ fn matcher(i: &mut Input) -> PResult { skip_space(qualified_name), opt(skip_space(constructor_parameters)), "=>", - elidable_block(block_statement, ","), + skip_space(( + alt((braced(block_statement), value_expression)), + elidable(","), + )), ) - .map(|(constructor, arguments, _, body)| Matcher { + .map(|(constructor, arguments, _, (body, _))| Matcher { constructor, arguments, body, @@ -317,7 +320,7 @@ fn matcher(i: &mut Input) -> PResult { fn access(i: &mut Input) -> PResult { ( - primary_value_expression, + skip_space(primary_value_expression), repeat(1.., prefixed(".", identifier::)), ) .with_span() diff --git a/rura-grammar/src/tests/mod.rs b/rura-grammar/src/tests/mod.rs index 786af22..9c6c590 100644 --- a/rura-grammar/src/tests/mod.rs +++ b/rura-grammar/src/tests/mod.rs @@ -15,14 +15,23 @@ fn it_parses_braced_code() { const INPUT: &str = r#" mod m1 { fn f1 () { + match n { + C1=> { y . a } , + // FIXME: Currently don't know how to tackle this lol. + // FIXME: Why is this so slow on release mode? + // C2 ( x ) => ( ( ( (x . a) :: < T > () ) . b ) (x) ) . 10 , + C3 => | lol | 1 + 2 * 3 , + C4 { z } => | oh | { ! z } , + } } fn f2 < T > () -> i32 { - if 42 { - 42 + let n = if true { + let x = 42; + x } else { 69 - } + }; } } diff --git a/rura-parsing/src/lib.rs b/rura-parsing/src/lib.rs index e4be021..45ae956 100644 --- a/rura-parsing/src/lib.rs +++ b/rura-parsing/src/lib.rs @@ -469,14 +469,24 @@ where .map(|p: Vec<_>| p.into_iter().map(From::from).collect()) } +pub fn function_arguments<'a, O, F>(val: F) -> impl Parser, Box<[O]>, ContextError> +where + F: Parser, O, ContextError>, +{ + parenthesized(separated(0.., val, ",")).map(|p: Vec<_>| p.into_boxed_slice()) +} + pub fn constructor_parameters<'a, ID>(i: &mut Input<'a>) -> PResult> where ID: From>, { - parenthesized(separated(1.., skip_space(identifier), ",")) - .map(|v: Vec<_>| v.into_boxed_slice()) - .context(expect("constructor parameters")) - .parse_next(i) + alt(( + braced(separated(1.., skip_space(identifier), ",")), + parenthesized(separated(1.., skip_space(identifier), ",")), + )) + .map(|v: Vec<_>| v.into_boxed_slice()) + .context(expect("constructor parameters")) + .parse_next(i) } pub fn closure_parameters<'a, ID>(i: &mut Input<'a>) -> PResult>