github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/cmds/core/elvish/parse/check_parse_tree_test.go (about)

     1  package parse
     2  
     3  import "fmt"
     4  
     5  // checkParseTree checks whether the parse tree part of a Node is well-formed.
     6  func checkParseTree(n Node) error {
     7  	children := n.Children()
     8  	if len(children) == 0 {
     9  		return nil
    10  	}
    11  
    12  	// Parent pointers of all children should point to me.
    13  	for i, ch := range children {
    14  		if ch.Parent() != n {
    15  			return fmt.Errorf("parent of child %d (%s) is wrong: %s", i, summary(ch), summary(n))
    16  		}
    17  	}
    18  
    19  	// The Begin of the first child should be equal to mine.
    20  	if children[0].Begin() != n.Begin() {
    21  		return fmt.Errorf("gap between node and first child: %s", summary(n))
    22  	}
    23  	// The End of the last child should be equal to mine.
    24  	nch := len(children)
    25  	if children[nch-1].End() != n.End() {
    26  		return fmt.Errorf("gap between node and last child: %s", summary(n))
    27  	}
    28  	// Consecutive children have consecutive position ranges.
    29  	for i := 0; i < nch-1; i++ {
    30  		if children[i].End() != children[i+1].Begin() {
    31  			return fmt.Errorf("gap between child %d and %d of: %s", i, i+1, summary(n))
    32  		}
    33  	}
    34  
    35  	// Check children recursively.
    36  	for _, ch := range n.Children() {
    37  		err := checkParseTree(ch)
    38  		if err != nil {
    39  			return err
    40  		}
    41  	}
    42  	return nil
    43  }