forked from evepraisal/go-evepraisal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
39 lines (36 loc) · 1.13 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package evepraisal
import (
"github.com/evepraisal/go-evepraisal/parsers"
"github.com/evepraisal/go-evepraisal/typedb"
)
// NewContextMultiParser implements a parser that knows about what types exist. This makes it much more powerful
// and prevents accidentally parsing one format as another
func NewContextMultiParser(typeDB typedb.TypeDB, parserList []parsers.Parser) parsers.Parser {
return parsers.Parser(
func(input parsers.Input) (parsers.ParserResult, parsers.Input) {
multiParserResult := &parsers.MultiParserResult{}
left := input
for _, parser := range parserList {
if len(left) == 0 {
break
}
var result parsers.ParserResult
result, left = parser(left)
if result != nil && len(result.Lines()) > 0 {
foundRealType := false
for _, item := range parserResultToAppraisalItems(result) {
if typeDB.HasType(item.Name) {
foundRealType = true
break
}
}
// We don't like this result, move ahead!
if !foundRealType {
continue
}
multiParserResult.Results = append(multiParserResult.Results, result)
}
}
return multiParserResult, left
})
}