Skip to content

Commit e135bda

Browse files
authored
Fix parsing of variable declaration nodes combined with sequence node (#773)
Fixes #772
1 parent 0f99b19 commit e135bda

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

parser/parser.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,13 @@ func (p *parser) parseConditional(node Node) Node {
323323
p.next()
324324

325325
if !p.current.Is(Operator, ":") {
326-
expr1 = p.parseSequenceExpression()
326+
expr1 = p.parseExpression(0)
327327
p.expect(Operator, ":")
328-
expr2 = p.parseSequenceExpression()
328+
expr2 = p.parseExpression(0)
329329
} else {
330330
p.next()
331331
expr1 = node
332-
expr2 = p.parseSequenceExpression()
332+
expr2 = p.parseExpression(0)
333333
}
334334

335335
node = p.createNode(&ConditionalNode{

parser/parser_test.go

+53-19
Original file line numberDiff line numberDiff line change
@@ -683,37 +683,71 @@ world`},
683683
},
684684
},
685685
{
686-
"true ? 1; 2; 3 : 4",
687-
&ConditionalNode{
688-
Cond: &BoolNode{Value: true},
689-
Exp1: &SequenceNode{
690-
Nodes: []Node{
691-
&IntegerNode{Value: 1},
692-
&IntegerNode{Value: 2},
693-
&IntegerNode{Value: 3}}},
694-
Exp2: &IntegerNode{Value: 4}},
686+
"true ? 1 : 2; 3 ; 4",
687+
&SequenceNode{
688+
Nodes: []Node{
689+
&ConditionalNode{
690+
Cond: &BoolNode{Value: true},
691+
Exp1: &IntegerNode{Value: 1},
692+
Exp2: &IntegerNode{Value: 2}},
693+
&IntegerNode{Value: 3},
694+
&IntegerNode{Value: 4},
695+
},
696+
},
695697
},
696698
{
697-
"true ? 1 : 2; 3 ; 4",
699+
"true ? 1 : ( 2; 3; 4 )",
698700
&ConditionalNode{
699701
Cond: &BoolNode{Value: true},
700702
Exp1: &IntegerNode{Value: 1},
701703
Exp2: &SequenceNode{
702704
Nodes: []Node{
703705
&IntegerNode{Value: 2},
704706
&IntegerNode{Value: 3},
705-
&IntegerNode{Value: 4}}}},
707+
&IntegerNode{Value: 4},
708+
},
709+
},
710+
},
706711
},
707712
{
708713
"true ?: 1; 2; 3",
709-
&ConditionalNode{
710-
Cond: &BoolNode{Value: true},
711-
Exp1: &BoolNode{Value: true},
712-
Exp2: &SequenceNode{
713-
Nodes: []Node{
714-
&IntegerNode{Value: 1},
715-
&IntegerNode{Value: 2},
716-
&IntegerNode{Value: 3}}}},
714+
&SequenceNode{
715+
Nodes: []Node{
716+
&ConditionalNode{
717+
Cond: &BoolNode{Value: true},
718+
Exp1: &BoolNode{Value: true},
719+
Exp2: &IntegerNode{Value: 1}},
720+
&IntegerNode{Value: 2},
721+
&IntegerNode{Value: 3},
722+
},
723+
},
724+
},
725+
{
726+
`let x = true ? 1 : 2; x`,
727+
&VariableDeclaratorNode{
728+
Name: "x",
729+
Value: &ConditionalNode{
730+
Cond: &BoolNode{Value: true},
731+
Exp1: &IntegerNode{Value: 1},
732+
Exp2: &IntegerNode{Value: 2}},
733+
Expr: &IdentifierNode{Value: "x"}},
734+
},
735+
{
736+
"let x = true ? 1 : ( 2; 3; 4 ); x",
737+
&VariableDeclaratorNode{
738+
Name: "x",
739+
Value: &ConditionalNode{
740+
Cond: &BoolNode{Value: true},
741+
Exp1: &IntegerNode{Value: 1},
742+
Exp2: &SequenceNode{
743+
Nodes: []Node{
744+
&IntegerNode{Value: 2},
745+
&IntegerNode{Value: 3},
746+
&IntegerNode{Value: 4},
747+
},
748+
},
749+
},
750+
Expr: &IdentifierNode{Value: "x"}},
717751
},
718752
{
719753
"if true { 1; 2; 3 } else { 4; 5; 6 }",

0 commit comments

Comments
 (0)