/
tomefile
/
lib-parser
Обзор
Документация
Войти
/
tomefile
/
lib-parser
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
main
parser_hook.go
61 строка
1 KB
BubbleFish
docs: Improve code documentation
10 дек 2025, 22:13
Не верифицирован
10 дек 2025, 22:13
ac5c634
Код
Авторство
О чём код?
package libparser import ( "strings" liberrors "github.com/tomefile/lib-errors" ) // A function to be called on every Node before the parser attempts to append it to the tree. // // Return `nil` to discard the Node. type Hook func(original Node) (modified Node, derr *liberrors.DetailedError) // Discards Nodes of type T func ExcludeHook[T Node](node Node) (Node, *liberrors.DetailedError) { switch node.(type) { case T: return nil, nil } return node, nil } // Removes the UNIX shebang func NoShebangHook(node Node) (Node, *liberrors.DetailedError) { switch node := node.(type) { case *NodeComment: if strings.HasPrefix(node.Contents, "!") { return nil, nil } } return node, nil } // Puts the node to chan before it gets appended to the tree. // Useful for tracking the progress of parsing in very large files. // // # Example usage: // // channel := make(chan libparser.Node) // // parser := libparser.New(file) // parser.Hooks = []libparser.Hook{ // libparser.StreamHook(channel), // } // // go func() { // defer close(channel) // IMPORTANT! Without this you'll hang the process FOREVER. // if derr := parser.Run(); derr != nil { // ... // } // }() // // for node := range channel { // ... // } func StreamHook(channel chan Node) Hook { return func(node Node) (Node, *liberrors.DetailedError) { channel <- node return node, nil } }