class node

This commit is contained in:
vadim 2017-12-06 15:55:43 +02:00
parent 2d168cd2e2
commit 60d8874a6f
3 changed files with 575 additions and 541 deletions

61
node/stmt/class.go Normal file
View File

@ -0,0 +1,61 @@
package stmt
import (
"fmt"
"io"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/token"
)
type Class struct {
node.SimpleNode
token token.Token
modifiers []string
args node.Node
extends node.Node
implements node.Node
stmts node.Node
}
//TODO: stmts myst be []node.Node
func NewClass(token token.Token, modifiers []string, args node.Node, extends node.Node, implements node.Node, stmts node.Node) node.Node {
return Class{
node.SimpleNode{Name: "Class", Attributes: make(map[string]string)},
token,
modifiers,
args,
extends,
implements,
stmts,
}
}
func (n Class) 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.modifiers != nil {
fmt.Fprintf(out, "\n%vmotifiers:", indent+" ")
for _, nn := range n.modifiers {
fmt.Fprintf(out, "\n%v%q", indent+" ", nn)
}
}
if n.args != nil {
fmt.Fprintf(out, "\n%vargs:", indent+" ")
n.args.Print(out, indent+" ")
}
if n.extends != nil {
fmt.Fprintf(out, "\n%vextends:", indent+" ")
n.extends.Print(out, indent+" ")
}
if n.implements != nil {
fmt.Fprintf(out, "\n%vimplements:", indent+" ")
n.implements.Print(out, indent+" ")
}
fmt.Fprintf(out, "\n%vstmts:", indent+" ")
n.stmts.Print(out, indent+" ")
}

File diff suppressed because it is too large Load Diff

View File

@ -28,6 +28,7 @@ func Parse(src io.Reader, fName string) node.Node {
token token.Token token token.Token
value string value string
list []node.Node list []node.Node
strings []string
} }
%left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
@ -207,8 +208,9 @@ func Parse(src io.Reader, fName string) node.Node {
%type <node> variable_modifiers %type <node> variable_modifiers
%type <node> method_modifiers non_empty_member_modifiers member_modifier %type <node> method_modifiers non_empty_member_modifiers member_modifier
%type <node> class_modifiers use_type %type <node> use_type
%type <strings> class_modifiers
%type <list> encaps_list backticks_expr namespace_name catch_name_list catch_list %type <list> encaps_list backticks_expr namespace_name catch_name_list catch_list
%% %%
@ -455,27 +457,14 @@ is_variadic:
class_declaration_statement: class_declaration_statement:
class_modifiers T_CLASS T_STRING extends_from implements_list '{' class_statement_list '}' class_modifiers T_CLASS T_STRING extends_from implements_list '{' class_statement_list '}'
{ { $$ = stmt.NewClass($3, $1, nil, $4, $5, $7) }
$$ = node.NewSimpleNode("Class").
Attribute("name", $3.String()).
Append($1).
Append(node.NewSimpleNode("Extends").Append($4)).
Append(node.NewSimpleNode("Implements").Append($5)).
Append($7);
}
| T_CLASS T_STRING extends_from implements_list '{' class_statement_list '}' | T_CLASS T_STRING extends_from implements_list '{' class_statement_list '}'
{ { $$ = stmt.NewClass($2, nil, nil, $3, $4, $6) }
$$ = node.NewSimpleNode("Class").
Attribute("name", $2.String()).
Append(node.NewSimpleNode("Extends").Append($3)).
Append(node.NewSimpleNode("Implements").Append($4)).
Append($6);
}
; ;
class_modifiers: class_modifiers:
class_modifier { $$ = node.NewSimpleNode("Class").Attribute($1, "true") } class_modifier { $$ = []string{$1} }
| class_modifiers class_modifier { $$ = $1.Attribute($2, "true") } | class_modifiers class_modifier { $$ = append($1, $2) }
; ;
class_modifier: class_modifier:
@ -498,7 +487,7 @@ interface_declaration_statement:
; ;
extends_from: extends_from:
/* empty */ { $$ = node.NewSimpleNode(""); } /* empty */ { $$ = nil }
| T_EXTENDS name { $$ = $2; } | T_EXTENDS name { $$ = $2; }
; ;
@ -825,12 +814,7 @@ non_empty_for_exprs:
anonymous_class: anonymous_class:
T_CLASS ctor_arguments extends_from implements_list '{' class_statement_list '}' T_CLASS ctor_arguments extends_from implements_list '{' class_statement_list '}'
{ {
$$ = node.NewSimpleNode("AnonymousClass"). { $$ = stmt.NewClass($1, nil, $2, $3, $4, $6) }
Attribute("name", $1.String()).
Append($2).
Append($3).
Append($4).
Append($6);
} }
; ;