Skip to content

Commit

Permalink
[test] more tests for braced code
Browse files Browse the repository at this point in the history
Signed-off-by: Anqur <[email protected]>
  • Loading branch information
anqurvanillapy committed Mar 1, 2024
1 parent 0361abb commit 9d35f84
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
25 changes: 14 additions & 11 deletions rura-grammar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Module, Error> {
Expand Down Expand Up @@ -165,9 +165,9 @@ fn let_statement(i: &mut Input) -> PResult<AST> {
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 {
Expand Down Expand Up @@ -208,7 +208,7 @@ fn value_expression(i: &mut Input) -> PResult<AST> {
fn closure(i: &mut Input) -> PResult<AST> {
(
closure_parameters,
alt((braced(block_statement), value_expression)),
skip_space(alt((braced(block_statement), value_expression))),
)
.with_span()
.map(|((parameters, body), span)| AST {
Expand All @@ -220,7 +220,7 @@ fn closure(i: &mut Input) -> PResult<AST> {
}

fn indexing(i: &mut Input) -> PResult<AST> {
(alt((call, primary_value_expression)), ".", dec_uint)
(primary_value_expression, skip_space("."), dec_uint)
.with_span()
.map(|((tuple, _, index), span)| AST {
span,
Expand All @@ -237,7 +237,7 @@ fn call(i: &mut Input) -> PResult<AST> {
1..,
skip_space((
opt(prefixed("::", type_arguments(type_expression))),
tuple(value_expression),
function_arguments(value_expression),
)),
),
)
Expand Down Expand Up @@ -304,9 +304,12 @@ fn matcher(i: &mut Input) -> PResult<Matcher> {
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,
Expand All @@ -317,7 +320,7 @@ fn matcher(i: &mut Input) -> PResult<Matcher> {

fn access(i: &mut Input) -> PResult<AST> {
(
primary_value_expression,
skip_space(primary_value_expression),
repeat(1.., prefixed(".", identifier::<Name>)),
)
.with_span()
Expand Down
15 changes: 12 additions & 3 deletions rura-grammar/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
}
}
Expand Down
18 changes: 14 additions & 4 deletions rura-parsing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Input<'a>, Box<[O]>, ContextError>
where
F: Parser<Input<'a>, O, ContextError>,
{
parenthesized(separated(0.., val, ",")).map(|p: Vec<_>| p.into_boxed_slice())
}

pub fn constructor_parameters<'a, ID>(i: &mut Input<'a>) -> PResult<Box<[ID]>>
where
ID: From<Input<'a>>,
{
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<Box<[ID]>>
Expand Down

0 comments on commit 9d35f84

Please sign in to comment.