treerack/parse.go

69 lines
1.2 KiB
Go
Raw Normal View History

2017-06-25 17:51:08 +02:00
package parse
2017-06-25 23:38:32 +02:00
import "fmt"
2017-06-25 17:51:08 +02:00
type definition interface {
nodeName() string
parser(*registry, []string) (parser, error)
commitType() CommitType
}
type parser interface {
nodeName() string
setIncludedBy(parser, []string)
cacheIncluded(*context, *Node)
parse(Trace, *context)
}
func parserNotFound(name string) error {
return fmt.Errorf("parser not found: %s", name)
}
2017-06-25 23:38:32 +02:00
func cannotIncludeParsers(name string) error {
return fmt.Errorf("parser: %s cannot include other parsers", name)
}
2017-06-25 17:51:08 +02:00
func stringsContain(ss []string, s string) bool {
for _, si := range ss {
if si == s {
return true
}
}
return false
}
func copyIncludes(to, from map[string]CommitType) {
if from == nil {
return
}
for name, ct := range from {
to[name] = ct
}
}
func mergeIncludes(left, right map[string]CommitType) map[string]CommitType {
m := make(map[string]CommitType)
copyIncludes(m, left)
copyIncludes(m, right)
return m
}
func parse(t Trace, p parser, c *context) (*Node, error) {
p.parse(t, c)
if c.readErr != nil {
return nil, c.readErr
}
if !c.match {
return nil, ErrInvalidInput
}
if err := c.finalize(); err != nil {
return nil, err
}
return c.node, nil
}