chore: remove unused methods

This commit is contained in:
Steven
2023-12-14 00:24:41 +08:00
parent 5266a62685
commit 3edce174d6
5 changed files with 15 additions and 50 deletions

View File

@@ -8,29 +8,17 @@ type Node interface {
// This method is used for debugging. // This method is used for debugging.
String() string String() string
// GetParent returns a parent node of this node.
GetParent() Node
// GetPrevSibling returns a previous sibling node of this node. // GetPrevSibling returns a previous sibling node of this node.
GetPrevSibling() Node GetPrevSibling() Node
// GetNextSibling returns a next sibling node of this node. // GetNextSibling returns a next sibling node of this node.
GetNextSibling() Node GetNextSibling() Node
// GetChildren returns children nodes of this node.
GetChildren() []Node
// SetParent sets a parent node to this node.
SetParent(Node)
// SetPrevSibling sets a previous sibling node to this node. // SetPrevSibling sets a previous sibling node to this node.
SetPrevSibling(Node) SetPrevSibling(Node)
// SetNextSibling sets a next sibling node to this node. // SetNextSibling sets a next sibling node to this node.
SetNextSibling(Node) SetNextSibling(Node)
// SetChildren sets children nodes to this node.
SetChildren([]Node)
} }
type NodeType int type NodeType int
@@ -49,17 +37,9 @@ func NewNodeType(name string) NodeType {
} }
type BaseNode struct { type BaseNode struct {
parent Node
prevSibling Node prevSibling Node
nextSibling Node nextSibling Node
children []Node
}
func (n *BaseNode) GetParent() Node {
return n.parent
} }
func (n *BaseNode) GetPrevSibling() Node { func (n *BaseNode) GetPrevSibling() Node {
@@ -70,14 +50,6 @@ func (n *BaseNode) GetNextSibling() Node {
return n.nextSibling return n.nextSibling
} }
func (n *BaseNode) GetChildren() []Node {
return n.children
}
func (n *BaseNode) SetParent(node Node) {
n.parent = node
}
func (n *BaseNode) SetPrevSibling(node Node) { func (n *BaseNode) SetPrevSibling(node Node) {
n.prevSibling = node n.prevSibling = node
} }
@@ -85,7 +57,3 @@ func (n *BaseNode) SetPrevSibling(node Node) {
func (n *BaseNode) SetNextSibling(node Node) { func (n *BaseNode) SetNextSibling(node Node) {
n.nextSibling = node n.nextSibling = node
} }
func (n *BaseNode) SetChildren(nodes []Node) {
n.children = nodes
}

View File

@@ -42,11 +42,11 @@ func (p *BlockquoteParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
} }
contentTokens := tokens[2:size] contentTokens := tokens[2:size]
blockquote := &ast.Blockquote{} children, err := ParseInline(contentTokens)
children, err := ParseInline(blockquote, contentTokens)
if err != nil { if err != nil {
return nil, err return nil, err
} }
blockquote.Children = children return &ast.Blockquote{
return blockquote, nil Children: children,
}, nil
} }

View File

@@ -65,13 +65,12 @@ func (p *HeadingParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
} }
contentTokens := tokens[level+1 : size] contentTokens := tokens[level+1 : size]
heading := &ast.Heading{ children, err := ParseInline(contentTokens)
Level: level,
}
children, err := ParseInline(heading, contentTokens)
if err != nil { if err != nil {
return nil, err return nil, err
} }
heading.Children = children return &ast.Heading{
return heading, nil Level: level,
Children: children,
}, nil
} }

View File

@@ -39,11 +39,11 @@ func (p *ParagraphParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
} }
contentTokens := tokens[:size] contentTokens := tokens[:size]
paragraph := &ast.Paragraph{} children, err := ParseInline(contentTokens)
children, err := ParseInline(paragraph, contentTokens)
if err != nil { if err != nil {
return nil, err return nil, err
} }
paragraph.Children = children return &ast.Paragraph{
return paragraph, nil Children: children,
}, nil
} }

View File

@@ -73,7 +73,7 @@ var defaultInlineParsers = []InlineParser{
NewTextParser(), NewTextParser(),
} }
func ParseInline(parent ast.Node, tokens []*tokenizer.Token) ([]ast.Node, error) { func ParseInline(tokens []*tokenizer.Token) ([]ast.Node, error) {
nodes := []ast.Node{} nodes := []ast.Node{}
var prevNode ast.Node var prevNode ast.Node
for len(tokens) > 0 { for len(tokens) > 0 {
@@ -86,8 +86,8 @@ func ParseInline(parent ast.Node, tokens []*tokenizer.Token) ([]ast.Node, error)
} }
tokens = tokens[size:] tokens = tokens[size:]
node.SetParent(parent)
if prevNode != nil { if prevNode != nil {
// Merge text nodes if possible.
if prevNode.Type() == ast.NodeTypeText && node.Type() == ast.NodeTypeText { if prevNode.Type() == ast.NodeTypeText && node.Type() == ast.NodeTypeText {
prevNode.(*ast.Text).Content += node.(*ast.Text).Content prevNode.(*ast.Text).Content += node.(*ast.Text).Content
break break
@@ -96,13 +96,11 @@ func ParseInline(parent ast.Node, tokens []*tokenizer.Token) ([]ast.Node, error)
prevNode.SetNextSibling(node) prevNode.SetNextSibling(node)
node.SetPrevSibling(prevNode) node.SetPrevSibling(prevNode)
} }
nodes = append(nodes, node) nodes = append(nodes, node)
prevNode = node prevNode = node
break break
} }
} }
} }
parent.SetChildren(nodes)
return nodes, nil return nodes, nil
} }