github.com/elves/elvish@v0.15.0/pkg/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 := Children(n)
     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 Parent(ch) != 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].Range().From != n.Range().From {
    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].Range().To != n.Range().To {
    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].Range().To != children[i+1].Range().From {
    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 Children(n) {
    37  		err := checkParseTree(ch)
    38  		if err != nil {
    39  			return err
    40  		}
    41  	}
    42  	return nil
    43  }