Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formal arg with ellipsis #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions nix-parser/src/parser/expr/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nom::combinator::{map, opt};
use nom::multi::many0;
use nom::sequence::{delimited, pair, preceded, terminated};

use super::{expr, util};
use super::{expr, util::error_expr_if};
use crate::ast::tokens::Ident;
use crate::ast::{ExprFnDecl, FnDeclFormals, FnDeclSimple, Formal};
use crate::error::{Errors, ExpectedFoundError};
Expand All @@ -23,30 +23,43 @@ pub fn fn_decl(input: Tokens) -> IResult<Partial<ExprFnDecl>> {
}

fn simple(input: Tokens) -> IResult<Partial<FnDeclSimple>> {
let expr = alt((expr, util::error_expr_if(tokens::eof, "<eof>")));
let expr = alt((expr, error_expr_if(tokens::eof, "<eof>")));
map_partial(pair_partial(identifier_arg, expr), |(ident, body)| {
let span = Span::merge(ident.span(), body.span());
FnDeclSimple::new(ident, body, span)
})(input)
}

fn formals(input: Tokens) -> IResult<Partial<FnDeclFormals>> {
let value = alt((expr, util::error_expr_if(tokens::comma, "comma")));
let value = alt((expr, error_expr_if(tokens::comma, "comma")));
let default = opt(preceded(tokens::op_question, verify_full(value)));
let formal = map(pair(tokens::identifier, default), |(name, def)| {
let name_span = name.span();
let default_span = def.as_ref().map(|d| d.span()).unwrap_or(name_span);
Partial::from(Formal::new(name, def, Span::merge(name_span, default_span)))
});

let args = separated_list_partial(tokens::comma, tokens::brace_right, formal);
// overloading trailing comma
let ellipsis = alt((
map(tokens::comma, |_| None),
map(preceded(tokens::comma, tokens::ellipsis), |ellipsis| {
Some(ellipsis)
}),
Comment on lines +45 to +47
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the closure could just as easily be replaced with:

map(preceded(tokens::comma, tokens::ellipsis), Some),

));
let args = pair_partial(
separated_list_partial(tokens::comma, tokens::brace_right, formal),
map(opt(ellipsis), |e: Option<_>| {
Partial::from(e.and_then(std::convert::identity))
}),
);
let term = pair(tokens::brace_right, tokens::colon);
let formals = delimited(tokens::brace_left, args, term);

let expr = alt((expr, util::error_expr_if(tokens::eof, "<eof>")));
map_partial_spanned(pair_partial(formals, expr), |span, (formals, expr)| {
FnDeclFormals::new(formals, None, None, expr, span)
})(input)
let expr = alt((expr, error_expr_if(tokens::eof, "<eof>")));
map_partial_spanned(
pair_partial(formals, expr),
|span, ((formals, ellipsis), expr)| FnDeclFormals::new(formals, ellipsis, None, expr, span),
)(input)
}

fn identifier_arg(input: Tokens) -> IResult<Partial<Ident>> {
Expand Down
3 changes: 3 additions & 0 deletions nix-parser/src/parser/expr/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use crate::parser::partial::Partial;
use crate::parser::IResult;
use crate::ToSpan;

/// parser transformer that peeks for a reasonable termination token and
/// provides designated grammatical hint `found` and hint for expected
/// "expression" grammatical tokens
pub fn error_expr_if<'a, O, F>(
parser: F,
found: &'a str,
Expand Down