2017-07-15 21:49:08 +02:00
|
|
|
package treerack
|
2017-06-25 17:51:08 +02:00
|
|
|
|
|
|
|
type sequenceDefinition struct {
|
2017-11-02 22:19:03 +01:00
|
|
|
name string
|
|
|
|
id int
|
|
|
|
commit CommitType
|
|
|
|
items []SequenceItem
|
|
|
|
itemDefs []definition
|
|
|
|
ranges [][]int
|
|
|
|
generalizations []int
|
|
|
|
sbuilder *sequenceBuilder
|
|
|
|
sparser *sequenceParser
|
|
|
|
allChars bool
|
|
|
|
validated bool
|
|
|
|
initialized bool
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type sequenceParser struct {
|
2017-11-02 22:19:03 +01:00
|
|
|
name string
|
|
|
|
id int
|
|
|
|
commit CommitType
|
|
|
|
items []parser
|
|
|
|
ranges [][]int
|
|
|
|
generalizations []int
|
|
|
|
allChars bool
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 16:25:17 +02:00
|
|
|
type sequenceBuilder struct {
|
2017-11-02 22:19:03 +01:00
|
|
|
name string
|
|
|
|
id int
|
|
|
|
commit CommitType
|
|
|
|
items []builder
|
|
|
|
ranges [][]int
|
|
|
|
allChars bool
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
func newSequence(name string, ct CommitType, items []SequenceItem) *sequenceDefinition {
|
2017-06-25 17:51:08 +02:00
|
|
|
return &sequenceDefinition{
|
|
|
|
name: name,
|
|
|
|
commit: ct,
|
|
|
|
items: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 22:54:15 +02:00
|
|
|
func (d *sequenceDefinition) nodeName() string { return d.name }
|
|
|
|
func (d *sequenceDefinition) setNodeName(n string) { d.name = n }
|
|
|
|
func (d *sequenceDefinition) nodeID() int { return d.id }
|
|
|
|
func (d *sequenceDefinition) setID(id int) { d.id = id }
|
|
|
|
func (d *sequenceDefinition) commitType() CommitType { return d.commit }
|
|
|
|
func (d *sequenceDefinition) setCommitType(ct CommitType) { d.commit = ct }
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-11-01 02:43:46 +01:00
|
|
|
func (d *sequenceDefinition) validate(r *registry) error {
|
|
|
|
if d.validated {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.validated = true
|
2017-08-06 20:43:52 +02:00
|
|
|
for i := range d.items {
|
2017-11-01 02:43:46 +01:00
|
|
|
ii, ok := r.definition(d.items[i].Name)
|
|
|
|
if !ok {
|
2017-08-06 20:43:52 +02:00
|
|
|
return parserNotFound(d.items[i].Name)
|
|
|
|
}
|
2017-11-01 02:43:46 +01:00
|
|
|
|
|
|
|
if err := ii.validate(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-06 20:43:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *sequenceDefinition) ensureBuilder() {
|
|
|
|
if d.sbuilder != nil {
|
2017-11-01 02:43:46 +01:00
|
|
|
return
|
2017-08-06 20:32:59 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
d.sbuilder = &sequenceBuilder{
|
2017-11-02 22:19:03 +01:00
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
2017-11-01 03:54:53 +01:00
|
|
|
}
|
2017-07-29 18:40:22 +02:00
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *sequenceDefinition) initRanges() {
|
2017-07-27 01:48:16 +02:00
|
|
|
for _, item := range d.items {
|
|
|
|
if item.Min == 0 && item.Max == 0 {
|
|
|
|
item.Min, item.Max = 1, 1
|
|
|
|
} else if item.Max == 0 {
|
|
|
|
item.Max = -1
|
|
|
|
}
|
2017-07-29 18:40:22 +02:00
|
|
|
|
|
|
|
d.ranges = append(d.ranges, []int{item.Min, item.Max})
|
2017-11-01 03:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-29 18:40:22 +02:00
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *sequenceDefinition) initItems(r *registry) {
|
|
|
|
allChars := true
|
|
|
|
for _, item := range d.items {
|
2017-08-06 21:47:53 +02:00
|
|
|
def := r.definitions[item.Name]
|
2017-11-01 03:54:53 +01:00
|
|
|
d.itemDefs = append(d.itemDefs, def)
|
|
|
|
def.init(r)
|
2017-07-29 18:40:22 +02:00
|
|
|
d.sbuilder.items = append(d.sbuilder.items, def.builder())
|
2017-07-30 02:35:51 +02:00
|
|
|
if allChars {
|
|
|
|
if _, isChar := def.(*charParser); !isChar {
|
|
|
|
allChars = false
|
|
|
|
}
|
|
|
|
}
|
2017-07-27 01:48:16 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 02:35:51 +02:00
|
|
|
d.sbuilder.allChars = allChars
|
2017-07-30 05:10:46 +02:00
|
|
|
d.allChars = allChars
|
2017-11-01 03:54:53 +01:00
|
|
|
}
|
2017-07-29 18:40:22 +02:00
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
func (d *sequenceDefinition) canHaveSpecializations() bool {
|
|
|
|
return len(d.items) == 1 && d.items[0].Max == 1
|
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *sequenceDefinition) init(r *registry) {
|
|
|
|
if d.initialized {
|
2017-11-01 02:43:46 +01:00
|
|
|
return
|
2017-07-27 01:48:16 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
d.initialized = true
|
|
|
|
d.initRanges()
|
|
|
|
d.ensureBuilder()
|
|
|
|
d.sbuilder.ranges = d.ranges
|
|
|
|
d.initItems(r)
|
2017-11-02 22:19:03 +01:00
|
|
|
if d.canHaveSpecializations() {
|
|
|
|
d.itemDefs[0].addGeneralization(d.id)
|
2017-11-01 03:54:53 +01:00
|
|
|
}
|
2017-07-27 01:48:16 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
func (d *sequenceDefinition) addGeneralization(g int) {
|
|
|
|
if intsContain(d.generalizations, g) {
|
2017-11-01 02:43:46 +01:00
|
|
|
return
|
2017-07-27 01:48:16 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
d.generalizations = append(d.generalizations, g)
|
2017-11-01 03:54:53 +01:00
|
|
|
d.ensureBuilder()
|
2017-11-02 22:19:03 +01:00
|
|
|
if d.canHaveSpecializations() {
|
|
|
|
d.itemDefs[0].addGeneralization(g)
|
2017-07-27 01:48:16 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *sequenceDefinition) createParser() {
|
|
|
|
d.sparser = &sequenceParser{
|
2017-11-02 22:19:03 +01:00
|
|
|
name: d.name,
|
|
|
|
id: d.id,
|
|
|
|
commit: d.commit,
|
|
|
|
generalizations: d.generalizations,
|
|
|
|
allChars: d.allChars,
|
|
|
|
ranges: d.ranges,
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
2017-11-01 03:54:53 +01:00
|
|
|
}
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *sequenceDefinition) createItemParsers() {
|
|
|
|
for _, item := range d.itemDefs {
|
|
|
|
pi := item.parser()
|
|
|
|
d.sparser.items = append(d.sparser.items, pi)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *sequenceDefinition) parser() parser {
|
|
|
|
if d.sparser != nil {
|
|
|
|
return d.sparser
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
d.createParser()
|
|
|
|
d.createItemParsers()
|
|
|
|
return d.sparser
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 03:54:53 +01:00
|
|
|
func (d *sequenceDefinition) builder() builder { return d.sbuilder }
|
2017-11-02 22:19:03 +01:00
|
|
|
|
|
|
|
func (p *sequenceParser) nodeName() string { return p.name }
|
|
|
|
func (p *sequenceParser) nodeID() int { return p.id }
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-10-31 21:09:30 +01:00
|
|
|
func (p *sequenceParser) parse(c *context) {
|
2017-08-06 03:42:35 +02:00
|
|
|
if !p.allChars {
|
2017-11-02 20:49:49 +01:00
|
|
|
if c.pending(c.offset, p.id) {
|
2017-08-06 03:42:35 +02:00
|
|
|
c.fail(c.offset)
|
|
|
|
return
|
|
|
|
}
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-11-02 20:49:49 +01:00
|
|
|
c.markPending(c.offset, p.id)
|
2017-08-06 03:42:35 +02:00
|
|
|
}
|
2017-06-25 17:51:08 +02:00
|
|
|
|
2017-07-17 04:23:29 +02:00
|
|
|
itemIndex := 0
|
2017-06-25 23:38:32 +02:00
|
|
|
var currentCount int
|
2017-07-17 04:23:29 +02:00
|
|
|
from := c.offset
|
|
|
|
to := c.offset
|
2017-07-30 02:35:51 +02:00
|
|
|
var parsed bool
|
2017-07-17 04:23:29 +02:00
|
|
|
|
|
|
|
for itemIndex < len(p.items) {
|
2017-11-02 22:19:03 +01:00
|
|
|
// TODO:
|
|
|
|
// - is it ok to parse before max range check? what if max=0
|
|
|
|
// - validate, normalize and document max=0
|
2017-10-31 21:09:30 +01:00
|
|
|
p.items[itemIndex].parse(c)
|
2017-11-02 20:49:49 +01:00
|
|
|
if !c.matchLast {
|
2017-07-17 04:23:29 +02:00
|
|
|
if currentCount < p.ranges[itemIndex][0] {
|
|
|
|
c.fail(from)
|
2017-08-06 03:42:35 +02:00
|
|
|
|
|
|
|
if !p.allChars {
|
2017-11-02 20:49:49 +01:00
|
|
|
c.unmarkPending(from, p.id)
|
2017-08-06 03:42:35 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:23:29 +02:00
|
|
|
itemIndex++
|
2017-06-25 23:38:32 +02:00
|
|
|
currentCount = 0
|
|
|
|
continue
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 02:35:51 +02:00
|
|
|
parsed = c.offset > to
|
2017-07-17 04:23:29 +02:00
|
|
|
if parsed {
|
2017-06-25 23:38:32 +02:00
|
|
|
currentCount++
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:23:29 +02:00
|
|
|
to = c.offset
|
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
// TODO: max cannot be 0
|
2017-07-17 04:23:29 +02:00
|
|
|
if !parsed || p.ranges[itemIndex][1] >= 0 && currentCount == p.ranges[itemIndex][1] {
|
|
|
|
itemIndex++
|
2017-06-25 23:38:32 +02:00
|
|
|
currentCount = 0
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 22:19:03 +01:00
|
|
|
for _, g := range p.generalizations {
|
|
|
|
if c.pending(from, g) {
|
|
|
|
c.results.setMatch(from, g, to)
|
2017-07-29 22:31:16 +02:00
|
|
|
}
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 20:49:49 +01:00
|
|
|
c.results.setMatch(from, p.id, to)
|
2017-07-17 04:23:29 +02:00
|
|
|
c.success(to)
|
2017-08-06 03:42:35 +02:00
|
|
|
if !p.allChars {
|
2017-11-02 20:49:49 +01:00
|
|
|
c.unmarkPending(from, p.id)
|
2017-08-06 03:42:35 +02:00
|
|
|
}
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
2017-07-29 16:25:17 +02:00
|
|
|
|
|
|
|
func (b *sequenceBuilder) nodeName() string { return b.name }
|
|
|
|
func (b *sequenceBuilder) nodeID() int { return b.id }
|
|
|
|
|
2017-07-29 18:40:22 +02:00
|
|
|
func (b *sequenceBuilder) build(c *context) ([]*Node, bool) {
|
2017-11-02 22:19:03 +01:00
|
|
|
to, ok := c.results.takeMatch(c.offset, b.id)
|
2017-07-29 18:40:22 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-10-28 16:54:06 +02:00
|
|
|
if to-c.offset == 0 && b.commit&Alias != 0 {
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
2017-07-30 02:35:51 +02:00
|
|
|
if b.allChars {
|
|
|
|
from := c.offset
|
|
|
|
c.offset = to
|
|
|
|
if b.commit&Alias != 0 {
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*Node{{
|
|
|
|
Name: b.name,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
tokens: c.tokens,
|
|
|
|
}}, true
|
2017-07-29 20:04:22 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 18:40:22 +02:00
|
|
|
from := c.offset
|
|
|
|
var (
|
|
|
|
itemIndex int
|
|
|
|
currentCount int
|
|
|
|
nodes []*Node
|
|
|
|
)
|
|
|
|
|
|
|
|
for itemIndex < len(b.items) {
|
|
|
|
itemFrom := c.offset
|
|
|
|
n, ok := b.items[itemIndex].build(c)
|
|
|
|
if !ok {
|
|
|
|
if currentCount < b.ranges[itemIndex][0] {
|
2017-07-29 20:04:22 +02:00
|
|
|
panic(b.name + ": damaged parse result")
|
2017-07-29 18:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
itemIndex++
|
|
|
|
currentCount = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed := c.offset > itemFrom
|
2017-10-28 16:54:06 +02:00
|
|
|
if parsed || len(n) > 0 {
|
2017-07-29 18:40:22 +02:00
|
|
|
nodes = append(nodes, n...)
|
|
|
|
currentCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
if !parsed || b.ranges[itemIndex][1] >= 0 && currentCount == b.ranges[itemIndex][1] {
|
|
|
|
itemIndex++
|
|
|
|
currentCount = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.commit&Alias != 0 {
|
|
|
|
return nodes, true
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 18:40:22 +02:00
|
|
|
return []*Node{{
|
|
|
|
Name: b.name,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Nodes: nodes,
|
|
|
|
tokens: c.tokens,
|
|
|
|
}}, true
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|