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
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
5 changes: 5 additions & 0 deletions civet.dev/cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ console.log x, f(x), (f g x), g f x

### Functions

<Playground>
function abort
process.exit 1
</Playground>

<Playground>
(a: number, b: number) => a + b
</Playground>
Expand Down
29 changes: 17 additions & 12 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,8 @@ Parameters
return {
type: "Parameters",
children: [{$loc, token: "()"}],
names: []
names: [],
implicit: true,
}

NonEmptyParameters
Expand Down Expand Up @@ -765,18 +766,20 @@ ParameterElementDelimiter
BindingIdentifier
# NOTE: Added @param for binding identifiers
# The parser will allow them in const/let/var declarations but JS/TS doesn't allow them there
__:ws At AtIdentifierRef:ref ->
__:ws NWBindingIdentifier:identifier ->
return { ...identifier, children: [...ws, ...identifier.children] }

# Non-whitespace version of BindingIdentifier
NWBindingIdentifier
# NOTE: Added @param for binding identifiers
# The parser will allow them in const/let/var declarations but JS/TS doesn't allow them there
At AtIdentifierRef:ref ->
return {
type: "AtBinding",
children: [...ws, ref],
children: [ref],
ref,
}

__:ws Identifier:id ->
return {
...id,
children: [...ws, ...id.children],
}
Identifier:id

AtIdentifierRef
ReservedWord:r ->
Expand Down Expand Up @@ -1037,15 +1040,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 ( _? NWBindingIdentifier )?:wid _?:w Parameters:parameters ReturnTypeSuffix?:suffix ->
return {
type: "FunctionSignature",
id,
id: wid?.[1],
parameters,
returnType: suffix?.children?.[1]?.[0]?.[1]?.token,
ts: false,
block: null,
children: $0,
children: !parameters.implicit ? $0 :
[ async, func, star, wid, parameters, w, suffix ],
// move whitespace w to after implicit () in parameters
}

# https://262.ecma-international.org/#prod-FunctionExpression
Expand Down
80 changes: 80 additions & 0 deletions test/function.civet
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,86 @@ describe "function", ->
async function defaultLoad () {}
"""

describe "argumentless without parens", ->
testCase """
inline
---
function f 5
---
function f() {return 5 }
"""

testCase """
inline anonymous
---
function 5
---
function() {return 5 }
"""

testCase """
inline assigned
---
f := function 5
---
function f () {return 5 }
"""

testCase """
braced
---
function f { 5 }
---
function f() { return 5 }
"""

testCase """
anonymous braced
---
function { 5 }
---
function() { return 5 }
"""

testCase """
implicit block
---
function f
x := 5
x*x
---
function f() {
const x = 5
return x*x
}
"""

testCase """
implicit block anonymous
---
function
x := 5
x*x
---
function() {
const x = 5
return x*x
}
"""

testCase """
separate lines assigned
---
f := function
x := 5
x*x
---
function f () {
const x = 5
return x*x
}
"""

describe "@params", ->
testCase """
empty function body
Expand Down
11 changes: 11 additions & 0 deletions test/types/function.civet
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ describe "[TS] function", ->
}
"""

testCase """
argumentless without parens
---
function f: void
console.log 'hi'
---
function f(): void {
console.log('hi')
}
"""

testCase """
type parameters
---
Expand Down