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

Function declarations without parentheses #159

Merged
merged 2 commits into from
Jan 8, 2023
Merged

Conversation

edemaine
Copy link
Collaborator

@edemaine edemaine commented Jan 8, 2023

Remove need for () in defining argumentless functions with function:

function name
  data?.part[0]?.name
function abort: void
  process.exit 1

One catch is that I removed the ability to have newlines between the function name and the opening parenthesis of the arguments. Otherwise we have issues with tracking indentation but also the following ambiguity:

function f
  (x)

I'd say this is an argumentless function f() that returns x. But currently it parses as the TS function declaration function f(x).

Note that JS does allow

function f
  (x)
{}

so this is technically an incompatibility with JS... Perhaps there should be a toggle?

Remove need for `()` in defining argumentless functions with `function`
@@ -1037,15 +1038,17 @@ FunctionDeclaration

FunctionSignature
# NOTE: Merged in async and generator with optionals
( Async __ )? Function Star? BindingIdentifier?:id __ NonEmptyParameters:parameters ReturnTypeSuffix?:suffix ->
( Async __ )?:async Function:func ( __ Star )?:star ( __ BindingIdentifier )?:wid _?:w Parameters:parameters ReturnTypeSuffix?:suffix ->
Copy link
Contributor

Choose a reason for hiding this comment

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

BindingIdentifier already includes __ inside its productions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, indeed. But we actually need a _? version, to support anonymous functions.

It'd be nice to have consistent names for rules that have or don't have whitespace at the start... The current grammar is a bit of a mix. Maybe a leading W or something?

Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely worth exploring some conventions now that the parser is getting large.

@STRd6
Copy link
Contributor

STRd6 commented Jan 8, 2023

With this change both of these are equivalent:

function f 5
f := -> 5

I think that's fine and some people may prefer one to the other. function f 5 looks a little weird to me but I understand how it makes sense and follows logically from optional empty parameters. The ones with nested blocks look good though and it wouldn't make sense to arbitrarily disallow the inline version.

Will someone prefer this style to the other? Probably. Will someone go nuts and annoy their teammates? Also probably, but that is true for many Civet features :)

@STRd6 STRd6 merged commit 5042e2a into master Jan 8, 2023
@edemaine
Copy link
Collaborator Author

edemaine commented Jan 8, 2023

Yeah, I had similar thoughts. I was going for the indented block case, where the parens really seemed unnecessary. The one-line tests were just seeing what it could do, and maybe shouldn't be encouraged e.g. via docs. It's particularly weird that function 5 f is equivalent to -> 5(f) while function f 5 is equivalent to function f { 5 }.

I wonder of we should introduce then for the one-line case, like if and such, so it's clear when you enter the body. Should we require it? Less clear.

@edemaine edemaine deleted the function-without-parens branch January 8, 2023 15:33
@STRd6
Copy link
Contributor

STRd6 commented Jan 8, 2023

I think 5 f -> 5(f) is a separate bug which might be fixed by making spaced application $skip on literals in the LHS.

then but perhaps spelled as returns might make the one-liner look more readable.

@edemaine edemaine mentioned this pull request Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants