fix declare do if else else_if nodes

This commit is contained in:
vadim 2017-12-07 11:35:45 +02:00
parent 9608147c51
commit bb188f0637
8 changed files with 50 additions and 60 deletions

View File

@ -12,15 +12,15 @@ type Declare struct {
node.SimpleNode
token token.Token
consts []node.Node
stmts []node.Node
stmt node.Node
}
func NewDeclare(token token.Token, consts []node.Node, stmts []node.Node) node.Node {
func NewDeclare(token token.Token, consts []node.Node, stmt node.Node) node.Node {
return Declare{
node.SimpleNode{Name: "Declare", Attributes: make(map[string]string)},
token,
consts,
stmts,
stmt,
}
}
@ -34,10 +34,8 @@ func (n Declare) Print(out io.Writer, indent string) {
}
}
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
if n.stmt != nil {
fmt.Fprintf(out, "\n%vstmt:", indent+" ")
n.stmt.Print(out, indent+" ")
}
}

View File

@ -11,15 +11,15 @@ import (
type Do struct {
node.SimpleNode
token token.Token
stmts []node.Node
stmt node.Node
cond node.Node
}
func NewDo(token token.Token, stmts []node.Node, cond node.Node) node.Node {
func NewDo(token token.Token, stmt node.Node, cond node.Node) node.Node {
return Do{
node.SimpleNode{Name: "Do", Attributes: make(map[string]string)},
token,
stmts,
stmt,
cond,
}
}
@ -32,10 +32,8 @@ func (n Do) Print(out io.Writer, indent string) {
n.cond.Print(out, indent+" ")
}
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
if n.stmt != nil {
fmt.Fprintf(out, "\n%vstmt:", indent+" ")
n.stmt.Print(out, indent+" ")
}
}

View File

@ -11,23 +11,23 @@ import (
type Echo struct {
node.SimpleNode
token token.Token
stmts []node.Node
exprs []node.Node
}
func NewEcho(token token.Token, stmts []node.Node) node.Node {
func NewEcho(token token.Token, exprs []node.Node) node.Node {
return Echo{
node.SimpleNode{Name: "Echo", Attributes: make(map[string]string)},
token,
stmts,
exprs,
}
}
func (n Echo) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value)
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
if n.exprs != nil {
fmt.Fprintf(out, "\n%vexprs:", indent+" ")
for _, nn := range n.exprs {
nn.Print(out, indent+" ")
}
}

View File

@ -11,24 +11,22 @@ import (
type Else struct {
node.SimpleNode
token token.Token
stmts []node.Node
stmt node.Node
}
func NewElse(token token.Token, stmts []node.Node) node.Node {
func NewElse(token token.Token, stmt node.Node) node.Node {
return Else{
node.SimpleNode{Name: "Else", Attributes: make(map[string]string)},
token,
stmts,
stmt,
}
}
func (n Else) Print(out io.Writer, indent string) {
fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value)
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
if n.stmt != nil {
fmt.Fprintf(out, "\n%vstmt:", indent+" ")
n.stmt.Print(out, indent+" ")
}
}

View File

@ -12,15 +12,15 @@ type ElseIf struct {
node.SimpleNode
token token.Token
cond node.Node
stmts []node.Node
stmt node.Node
}
func NewElseIf(token token.Token, cond node.Node, stmts []node.Node) node.Node {
func NewElseIf(token token.Token, cond node.Node, stmt node.Node) node.Node {
return ElseIf{
node.SimpleNode{Name: "ElseIf", Attributes: make(map[string]string)},
token,
cond,
stmts,
stmt,
}
}
@ -32,10 +32,8 @@ func (n ElseIf) Print(out io.Writer, indent string) {
n.cond.Print(out, indent+" ")
}
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
if n.stmt != nil {
fmt.Fprintf(out, "\n%vstmt:", indent+" ")
n.stmt.Print(out, indent+" ")
}
}

View File

@ -12,24 +12,24 @@ type If struct {
node.SimpleNode
token token.Token
cond node.Node
stmts []node.Node
stmt node.Node
elseIf []node.Node
_else node.Node
}
func NewIf(token token.Token, cond node.Node, stmts []node.Node) node.Node {
func NewIf(token token.Token, cond node.Node, stmt node.Node) node.Node {
return If{
node.SimpleNode{Name: "If", Attributes: make(map[string]string)},
token,
cond,
stmts,
stmt,
nil,
nil,
}
}
func (n If) AddElseIf(elseIf node.Node) node.Node {
if (n.elseIf == nil) {
if n.elseIf == nil {
n.elseIf = make([]node.Node, 0)
}
@ -52,13 +52,11 @@ func (n If) Print(out io.Writer, indent string) {
n.cond.Print(out, indent+" ")
}
if n.stmts != nil {
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
for _, nn := range n.stmts {
nn.Print(out, indent+" ")
}
if n.stmt != nil {
fmt.Fprintf(out, "\n%vstmt:", indent+" ")
n.stmt.Print(out, indent+" ")
}
if n.elseIf != nil {
fmt.Fprintf(out, "\n%velseIfs:", indent+" ")
for _, nn := range n.elseIf {

View File

@ -2757,7 +2757,7 @@ yydefault:
yyDollar = yyS[yypt-0 : yypt+1]
//line parser/parser.y:342
{
yyVAL.node = node.NewSimpleNode("stmt")
yyVAL.node = node.NewSimpleNode("StmtList")
}
case 123:
yyDollar = yyS[yypt-1 : yypt+1]
@ -2825,7 +2825,7 @@ yydefault:
yyDollar = yyS[yypt-7 : yypt+1]
//line parser/parser.y:363
{
yyVAL.node = stmt.NewDo(yyDollar[1].token, yyDollar[2].node.(node.SimpleNode).Children, yyDollar[5].node)
yyVAL.node = stmt.NewDo(yyDollar[1].token, yyDollar[2].node, yyDollar[5].node)
}
case 134:
yyDollar = yyS[yypt-9 : yypt+1]
@ -2920,7 +2920,7 @@ yydefault:
yyDollar = yyS[yypt-5 : yypt+1]
//line parser/parser.y:398
{
yyVAL.node = stmt.NewDeclare(yyDollar[1].token, yyDollar[3].list, yyDollar[5].node.(node.SimpleNode).Children)
yyVAL.node = stmt.NewDeclare(yyDollar[1].token, yyDollar[3].list, yyDollar[5].node)
}
case 148:
yyDollar = yyS[yypt-1 : yypt+1]
@ -3246,13 +3246,13 @@ yydefault:
yyDollar = yyS[yypt-5 : yypt+1]
//line parser/parser.y:552
{
yyVAL.node = stmt.NewIf(yyDollar[1].token, yyDollar[3].node, yyDollar[5].node.(node.SimpleNode).Children)
yyVAL.node = stmt.NewIf(yyDollar[1].token, yyDollar[3].node, yyDollar[5].node)
}
case 203:
yyDollar = yyS[yypt-6 : yypt+1]
//line parser/parser.y:554
{
_elseIf := stmt.NewElseIf(yyDollar[2].token, yyDollar[4].node, yyDollar[6].node.(node.SimpleNode).Children)
_elseIf := stmt.NewElseIf(yyDollar[2].token, yyDollar[4].node, yyDollar[6].node)
yyVAL.node = yyDollar[1].node.(stmt.If).AddElseIf(_elseIf)
}
case 204:
@ -3265,7 +3265,7 @@ yydefault:
yyDollar = yyS[yypt-3 : yypt+1]
//line parser/parser.y:563
{
_else := stmt.NewElse(yyDollar[2].token, yyDollar[3].node.(node.SimpleNode).Children)
_else := stmt.NewElse(yyDollar[2].token, yyDollar[3].node)
yyVAL.node = yyDollar[1].node.(stmt.If).SetElse(_else)
}
case 206:

View File

@ -339,7 +339,7 @@ const_list:
inner_statement_list:
inner_statement_list inner_statement { $$ = $1.Append($2); }
| /* empty */ { $$ = node.NewSimpleNode("stmt") }
| /* empty */ { $$ = node.NewSimpleNode("StmtList") }
;
inner_statement:
@ -360,7 +360,7 @@ statement:
Append(node.NewSimpleNode("expr").Append($3)).
Append(node.NewSimpleNode("stmt").Append($5));
}
| T_DO statement T_WHILE '(' expr ')' ';' { $$ = stmt.NewDo($1, $2.(node.SimpleNode).Children, $5) }
| T_DO statement T_WHILE '(' expr ')' ';' { $$ = stmt.NewDo($1, $2, $5) }
| T_FOR '(' for_exprs ';' for_exprs ';' for_exprs ')' for_statement
{
$$ = node.NewSimpleNode("For").
@ -395,7 +395,7 @@ statement:
Append(node.NewSimpleNode("ForeachVariable").Append($7)).
Append($9);
}
| T_DECLARE '(' const_list ')' declare_statement { $$ = stmt.NewDeclare($1, $3, $5.(node.SimpleNode).Children) }
| T_DECLARE '(' const_list ')' declare_statement { $$ = stmt.NewDeclare($1, $3, $5) }
| ';' /* empty statement */ { $$ = node.NewSimpleNode(""); }
| T_TRY '{' inner_statement_list '}' catch_list finally_statement
{
@ -549,10 +549,10 @@ while_statement:
;
if_stmt_without_else:
T_IF '(' expr ')' statement { $$ = stmt.NewIf($1, $3, $5.(node.SimpleNode).Children) }
T_IF '(' expr ')' statement { $$ = stmt.NewIf($1, $3, $5) }
| if_stmt_without_else T_ELSEIF '(' expr ')' statement
{
_elseIf := stmt.NewElseIf($2, $4, $6.(node.SimpleNode).Children)
_elseIf := stmt.NewElseIf($2, $4, $6)
$$ = $1.(stmt.If).AddElseIf(_elseIf)
}
;
@ -561,7 +561,7 @@ if_stmt:
if_stmt_without_else %prec T_NOELSE { $$ = $1; }
| if_stmt_without_else T_ELSE statement
{
_else := stmt.NewElse($2, $3.(node.SimpleNode).Children)
_else := stmt.NewElse($2, $3)
$$ = $1.(stmt.If).SetElse(_else)
}
;