Gets a token from the file's token cache, or creates it if it does not already exist. This function should NOT be used for creating synthetic tokens that are not in the file in the first place.
( kind Kind, pos int, end int, parent *Node, flags TokenFlags, )
| 2772 | // Gets a token from the file's token cache, or creates it if it does not already exist. |
| 2773 | // This function should NOT be used for creating synthetic tokens that are not in the file in the first place. |
| 2774 | func (node *SourceFile) GetOrCreateToken( |
| 2775 | kind Kind, |
| 2776 | pos int, |
| 2777 | end int, |
| 2778 | parent *Node, |
| 2779 | flags TokenFlags, |
| 2780 | ) *TokenNode { |
| 2781 | node.tokenCacheMu.Lock() |
| 2782 | defer node.tokenCacheMu.Unlock() |
| 2783 | loc := core.NewTextRange(pos, end) |
| 2784 | key := TokenCacheKey{parent, loc} |
| 2785 | if token, ok := node.tokenCache[key]; ok { |
| 2786 | if token.Kind != kind { |
| 2787 | panic(fmt.Sprintf("Token cache mismatch: %v != %v", token.Kind, kind)) |
| 2788 | } |
| 2789 | return token |
| 2790 | } |
| 2791 | if parent.Flags&NodeFlagsReparsed != 0 { |
| 2792 | panic(fmt.Sprintf("Cannot create token from reparsed node of kind %v", parent.Kind)) |
| 2793 | } |
| 2794 | if node.tokenCache == nil { |
| 2795 | node.tokenCache = make(map[TokenCacheKey]*Node) |
| 2796 | } |
| 2797 | token := createToken(kind, node, pos, end, flags) |
| 2798 | token.Loc = loc |
| 2799 | token.Parent = parent |
| 2800 | node.tokenCache[key] = token |
| 2801 | return token |
| 2802 | } |
| 2803 | |
| 2804 | // `kind` should be a token kind. |
| 2805 | func createToken(kind Kind, file *SourceFile, pos, end int, flags TokenFlags) *Node { |
no test coverage detected