Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
STRd6 committed Mar 21, 2023
1 parent 8e425b2 commit 0a5d183
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
46 changes: 33 additions & 13 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -4081,11 +4081,17 @@ HoistableDeclaration
# https://262.ecma-international.org/#prod-LexicalDeclaration
LexicalDeclaration
# NOTE: Eliminated left recursion
LetOrConst LexicalBinding:binding ( __ Comma LexicalBinding )*:tail ->
# TODO: Add support for `let` and `const` declarations with multiple bindings in conditions
LetOrConst:d LexicalBinding:binding ( __ Comma LexicalBinding )*:tail ->
return {
type: "Declaration",
children: $0,
names: [...binding.names].concat(tail.flatMap(([,,b]) => b.names)),
binding: {
...binding.binding,
children: [d, ...binding.binding.children],
},
initializer: binding.initializer,
}
# NOTE: Added const shorthand
# NOTE: Using ExtendedExpression to allow for If/SwitchExpressions
Expand All @@ -4106,22 +4112,36 @@ LetAssignment

# https://262.ecma-international.org/#prod-LexicalBinding
LexicalBinding
BindingPattern TypeSuffix? Initializer ->
const children = [...$1.children]
if ($2) children./**/push($2)
children./**/push($3)
BindingPattern:binding TypeSuffix?:suffix _?:ws Initializer:initializer ->
const bindingChildren = [...binding.children]
if (suffix) bindingChildren.push(suffix)
if (ws) bindingChildren.push(...ws)
binding = {
...binding,
children: bindingChildren,
}

return {
children,
names: $1.names,
children: [binding, initializer],
names: binding.names,
binding,
initializer,
}

BindingIdentifier:binding TypeSuffix?:suffix _?:ws Initializer?:initializer ->
const bindingChildren = [...binding.children]
if (suffix) bindingChildren.push(suffix)
if (ws) bindingChildren.push(...ws)
binding = {
...binding,
children: bindingChildren,
}

BindingIdentifier TypeSuffix? Initializer? ->
const children = [...$1.children]
if ($2) children./**/push($2)
if ($3) children./**/push($3)
return {
children,
names: $1.names,
children: [binding, initializer],
names: binding.names,
binding,
initializer,
}

# https://262.ecma-international.org/#prod-Initializer
Expand Down
12 changes: 12 additions & 0 deletions test/if.civet
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,18 @@ describe "if", ->
}
"""

testCase """
longhand const
---
if const match = regex.exec string
console.log match
---
let ref;if (ref = regex.exec(string)) {
const match = ref;
console.log(match)
}
"""

// TODO
throws """
declaration in expression
Expand Down

0 comments on commit 0a5d183

Please sign in to comment.