github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/parse/parseutil/parseutil.go (about)

     1  // Package parseutil contains utilities built on top of the parse package.
     2  package parseutil
     3  
     4  import (
     5  	"strings"
     6  
     7  	"github.com/markusbkk/elvish/pkg/parse"
     8  )
     9  
    10  // Wordify turns a piece of source code into words.
    11  func Wordify(src string) []string {
    12  	tree, _ := parse.Parse(parse.Source{Code: src}, parse.Config{})
    13  	return wordifyInner(tree.Root, nil)
    14  }
    15  
    16  func wordifyInner(n parse.Node, words []string) []string {
    17  	if len(parse.Children(n)) == 0 || isCompound(n) {
    18  		text := parse.SourceText(n)
    19  		if strings.TrimFunc(text, parse.IsWhitespace) != "" {
    20  			return append(words, text)
    21  		}
    22  		return words
    23  	}
    24  	for _, ch := range parse.Children(n) {
    25  		words = wordifyInner(ch, words)
    26  	}
    27  	return words
    28  }
    29  
    30  func isCompound(n parse.Node) bool {
    31  	_, ok := n.(*parse.Compound)
    32  	return ok
    33  }