2017-06-25 17:51:08 +02:00
|
|
|
package parse
|
|
|
|
|
|
|
|
type sequenceDefinition struct {
|
|
|
|
name string
|
|
|
|
commit CommitType
|
2017-06-25 23:38:32 +02:00
|
|
|
items []SequenceItem
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type sequenceParser struct {
|
|
|
|
name string
|
|
|
|
commit CommitType
|
|
|
|
items []parser
|
2017-06-25 23:38:32 +02:00
|
|
|
ranges [][]int
|
2017-06-25 17:51:08 +02:00
|
|
|
including []parser
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sequenceDefinition) nodeName() string { return d.name }
|
|
|
|
|
|
|
|
func (d *sequenceDefinition) parser(r *registry, path []string) (parser, error) {
|
|
|
|
if stringsContain(path, d.name) {
|
2017-06-25 23:38:32 +02:00
|
|
|
panic(cannotIncludeParsers(d.name))
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p, ok := r.parser(d.name)
|
|
|
|
if ok {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sp := &sequenceParser{
|
|
|
|
name: d.name,
|
|
|
|
commit: d.commit,
|
|
|
|
}
|
|
|
|
|
|
|
|
r.setParser(sp)
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
var (
|
|
|
|
items []parser
|
|
|
|
ranges [][]int
|
|
|
|
)
|
|
|
|
|
2017-06-25 17:51:08 +02:00
|
|
|
path = append(path, d.name)
|
2017-06-25 23:38:32 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
pi, ok := r.parser(item.Name)
|
2017-06-25 17:51:08 +02:00
|
|
|
if ok {
|
2017-06-25 23:38:32 +02:00
|
|
|
items = append(items, pi)
|
|
|
|
ranges = append(ranges, []int{item.Min, item.Max})
|
2017-06-25 17:51:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
itemDefinition, ok := r.definition(item.Name)
|
2017-06-25 17:51:08 +02:00
|
|
|
if !ok {
|
2017-06-25 23:38:32 +02:00
|
|
|
return nil, parserNotFound(item.Name)
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
pi, err := itemDefinition.parser(r, path)
|
2017-06-25 17:51:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
items = append(items, pi)
|
|
|
|
ranges = append(ranges, []int{item.Min, item.Max})
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// for single items, acts like a choice
|
2017-06-25 23:38:32 +02:00
|
|
|
if len(items) == 1 && ranges[0][0] == 1 && ranges[0][1] == 1 {
|
2017-06-25 17:51:08 +02:00
|
|
|
items[0].setIncludedBy(sp, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
sp.items = items
|
2017-06-25 23:38:32 +02:00
|
|
|
sp.ranges = ranges
|
2017-06-25 17:51:08 +02:00
|
|
|
return sp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sequenceDefinition) commitType() CommitType {
|
|
|
|
return d.commit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sequenceParser) nodeName() string { return p.name }
|
|
|
|
|
|
|
|
func (p *sequenceParser) setIncludedBy(i parser, path []string) {
|
|
|
|
if stringsContain(path, p.name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.including = append(p.including, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sequenceParser) cacheIncluded(c *context, n *Node) {
|
|
|
|
if !c.excluded(n.from, p.name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nc := newNode(p.name, p.commit, n.from, n.to)
|
|
|
|
nc.append(n)
|
|
|
|
c.cache.set(nc.from, p.name, nc)
|
|
|
|
|
|
|
|
// maybe it is enough to cache only those that are on the path
|
|
|
|
for _, i := range p.including {
|
|
|
|
i.cacheIncluded(c, nc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
should be possible to parse:
|
|
|
|
|
|
|
|
a = "0"
|
|
|
|
b = "1"
|
|
|
|
c = a* e b
|
|
|
|
d = a | c
|
|
|
|
e = b | d
|
|
|
|
|
|
|
|
input: 111
|
|
|
|
*/
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
// TODO: apply the quantifier migration to the syntax
|
|
|
|
|
2017-06-25 17:51:08 +02:00
|
|
|
func (p *sequenceParser) parse(t Trace, c *context) {
|
|
|
|
t = t.Extend(p.name)
|
|
|
|
t.Out1("parsing sequence", c.offset)
|
|
|
|
|
|
|
|
if p.commit&Documentation != 0 {
|
|
|
|
t.Out1("fail, doc")
|
|
|
|
c.fail(c.offset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.excluded(c.offset, p.name) {
|
|
|
|
t.Out1("excluded")
|
|
|
|
c.fail(c.offset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.exclude(c.offset, p.name)
|
|
|
|
defer c.include(c.offset, p.name)
|
|
|
|
|
|
|
|
items := p.items
|
2017-06-25 23:38:32 +02:00
|
|
|
ranges := p.ranges
|
|
|
|
var currentCount int
|
2017-06-25 17:51:08 +02:00
|
|
|
node := newNode(p.name, p.commit, c.offset, c.offset)
|
|
|
|
|
|
|
|
for len(items) > 0 {
|
|
|
|
m, ok := c.fromCache(items[0].nodeName())
|
|
|
|
if ok {
|
|
|
|
t.Out1("sequence item found in cache, match:", m, items[0].nodeName(), c.offset)
|
|
|
|
if m {
|
2017-06-25 23:38:32 +02:00
|
|
|
if c.node.tokenLength() > 0 {
|
|
|
|
node.append(c.node)
|
|
|
|
currentCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.node.tokenLength() == 0 || ranges[0][1] >= 0 && currentCount == ranges[0][1] {
|
|
|
|
items = items[1:]
|
|
|
|
ranges = ranges[1:]
|
|
|
|
currentCount = 0
|
|
|
|
}
|
|
|
|
|
2017-06-25 17:51:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:38:32 +02:00
|
|
|
if currentCount < ranges[0][0] {
|
|
|
|
c.cache.set(node.from, p.name, nil)
|
|
|
|
c.fail(node.from)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
items = items[1:]
|
|
|
|
ranges = ranges[1:]
|
|
|
|
currentCount = 0
|
|
|
|
continue
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
items[0].parse(t, c)
|
|
|
|
|
|
|
|
if !c.match {
|
2017-06-25 23:38:32 +02:00
|
|
|
if currentCount < ranges[0][0] {
|
|
|
|
t.Out1("fail, item failed")
|
|
|
|
c.cache.set(node.from, p.name, nil)
|
|
|
|
c.fail(node.from)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
items = items[1:]
|
|
|
|
ranges = ranges[1:]
|
|
|
|
currentCount = 0
|
|
|
|
continue
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.node.tokenLength() > 0 {
|
|
|
|
node.append(c.node)
|
2017-06-25 23:38:32 +02:00
|
|
|
currentCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.node.tokenLength() == 0 || ranges[0][1] >= 0 && currentCount == ranges[0][1] {
|
|
|
|
items = items[1:]
|
|
|
|
ranges = ranges[1:]
|
|
|
|
currentCount = 0
|
2017-06-25 17:51:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Out1("success, items parsed")
|
|
|
|
|
|
|
|
c.cache.set(node.from, p.name, node)
|
|
|
|
for _, i := range p.including {
|
|
|
|
i.cacheIncluded(c, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.success(node)
|
|
|
|
}
|