Merge branch 'separate-build-phase'

This commit is contained in:
Arpad Ryszka 2017-07-29 23:32:37 +02:00
commit 996b032a38
12 changed files with 654 additions and 508 deletions

View File

@ -3,6 +3,7 @@ package treerack
import ( import (
"os" "os"
"testing" "testing"
"time"
) )
func TestBoot(t *testing.T) { func TestBoot(t *testing.T) {
@ -20,12 +21,27 @@ func TestBoot(t *testing.T) {
defer f.Close() defer f.Close()
n0, err := b.Parse(f) var d time.Duration
if err != nil { const n = 120
t.Error(err) var n0 *Node
return for i := 0; i < n; i++ {
if _, err := f.Seek(0, 0); err != nil {
t.Error(err)
return
}
start := time.Now()
n0, err = b.Parse(f)
d += time.Now().Sub(start)
if err != nil {
t.Error(err)
return
}
} }
t.Log("duration:", d/n)
s0 := NewSyntax() s0 := NewSyntax()
if err := define(s0, n0); err != nil { if err := define(s0, n0); err != nil {
t.Error(err) t.Error(err)

84
char.go
View File

@ -3,32 +3,37 @@ package treerack
type charParser struct { type charParser struct {
name string name string
id int id int
commit CommitType
not bool not bool
chars []rune chars []rune
ranges [][]rune ranges [][]rune
includedBy []parser includedBy []int
} }
func newChar( func newChar(
name string, name string,
ct CommitType,
not bool, not bool,
chars []rune, chars []rune,
ranges [][]rune, ranges [][]rune,
) *charParser { ) *charParser {
return &charParser{ return &charParser{
name: name, name: name,
commit: ct,
not: not, not: not,
chars: chars, chars: chars,
ranges: ranges, ranges: ranges,
} }
} }
func (p *charParser) nodeName() string { return p.name } func (p *charParser) nodeName() string { return p.name }
func (p *charParser) nodeID() int { return p.id } func (p *charParser) nodeID() int { return p.id }
func (p *charParser) setID(id int) { p.id = id } func (p *charParser) setID(id int) { p.id = id }
func (p *charParser) commitType() CommitType { return Alias }
func (p *charParser) init(r *registry) error { return nil }
func (p *charParser) setIncludedBy(r *registry, includedBy int, parsers *idSet) error {
p.includedBy = appendIfMissing(p.includedBy, includedBy)
return nil
}
func (p *charParser) parser(r *registry, parsers *idSet) (parser, error) { func (p *charParser) parser(r *registry, parsers *idSet) (parser, error) {
if parsers.has(p.id) { if parsers.has(p.id) {
@ -43,20 +48,8 @@ func (p *charParser) parser(r *registry, parsers *idSet) (parser, error) {
return p, nil return p, nil
} }
func (p *charParser) commitType() CommitType { func (p *charParser) builder() builder {
return p.commit return p
}
func (p *charParser) setIncludedBy(includedBy parser, parsers *idSet) {
// if parsers.has(p.id) {
// panic(cannotIncludeParsers(p.name))
// }
p.includedBy = append(p.includedBy, includedBy)
}
func (p *charParser) storeIncluded(*context, *Node) {
panic(cannotIncludeParsers(p.name))
} }
func (p *charParser) match(t rune) bool { func (p *charParser) match(t rune) bool {
@ -77,33 +70,32 @@ func (p *charParser) match(t rune) bool {
func (p *charParser) parse(t Trace, c *context) { func (p *charParser) parse(t Trace, c *context) {
// t = t.Extend(p.name) // t = t.Extend(p.name)
// t.Out1("parsing char", c.offset) // t.Out1("parsing", c.offset)
// if p.commit&Documentation != 0 { if tok, ok := c.token(); !ok || !p.match(tok) {
// // t.Out1("fail, doc") // t.Out1("fail")
// c.fail(c.offset)
// return
// }
// if _, ok := c.fromStore(p.id); ok {
// // t.Out1("found in store, match:", m)
// return
// }
if tok, ok := c.token(); ok && p.match(tok) {
// t.Out1("success", string(tok))
// n := newNode(p.name, p.id, c.offset, c.offset+1, p.commit)
// c.store.set(c.offset, p.id, n)
// for _, includedBy := range p.includedBy {
// includedBy.storeIncluded(c, n)
// }
c.successChar()
return
} else {
// t.Out1("fail", string(tok))
// c.store.set(c.offset, p.id, nil)
c.fail(c.offset) c.fail(c.offset)
return return
} }
// t.Out1("success")
c.success(c.offset + 1)
for _, includedBy := range p.includedBy {
c.store.setMatch(c.offset, includedBy, c.offset+1)
}
}
func (p *charParser) build(c *context) ([]*Node, bool) {
t, ok := c.token()
if !ok {
panic("damaged parser context")
}
if !p.match(t) {
return nil, false
}
// always alias
c.offset++
return nil, true
} }

206
choice.go
View File

@ -1,10 +1,12 @@
package treerack package treerack
type choiceDefinition struct { type choiceDefinition struct {
name string name string
id int id int
commit CommitType commit CommitType
elements []string elements []string
includedBy []int
cbuilder *choiceBuilder
} }
type choiceParser struct { type choiceParser struct {
@ -12,7 +14,15 @@ type choiceParser struct {
id int id int
commit CommitType commit CommitType
elements []parser elements []parser
includedBy []parser includedBy []int
}
type choiceBuilder struct {
name string
id int
commit CommitType
elements []builder
includedBy []int
} }
func newChoice(name string, ct CommitType, elements []string) *choiceDefinition { func newChoice(name string, ct CommitType, elements []string) *choiceDefinition {
@ -23,9 +33,58 @@ func newChoice(name string, ct CommitType, elements []string) *choiceDefinition
} }
} }
func (d *choiceDefinition) nodeName() string { return d.name } func (d *choiceDefinition) nodeName() string { return d.name }
func (d *choiceDefinition) nodeID() int { return d.id } func (d *choiceDefinition) nodeID() int { return d.id }
func (d *choiceDefinition) setID(id int) { d.id = id } func (d *choiceDefinition) setID(id int) { d.id = id }
func (d *choiceDefinition) commitType() CommitType { return d.commit }
func (d *choiceDefinition) init(r *registry) error {
if d.cbuilder == nil {
d.cbuilder = &choiceBuilder{
name: d.name,
id: d.id,
commit: d.commit,
}
}
for _, e := range d.elements {
def, ok := r.definition(e)
if !ok {
return parserNotFound(e)
}
d.cbuilder.elements = append(d.cbuilder.elements, def.builder())
}
parsers := &idSet{}
parsers.set(d.id)
return setItemsIncludedBy(r, d.elements, d.id, parsers)
}
func (d *choiceDefinition) setIncludedBy(r *registry, includedBy int, parsers *idSet) error {
if parsers.has(d.id) {
return nil
}
d.includedBy = appendIfMissing(d.includedBy, includedBy)
if d.cbuilder == nil {
d.cbuilder = &choiceBuilder{
name: d.name,
id: d.id,
commit: d.commit,
}
}
d.cbuilder.includedBy = appendIfMissing(d.cbuilder.includedBy, includedBy)
parsers.set(d.id)
return setItemsIncludedBy(r, d.elements, includedBy, parsers)
}
// TODO:
// - it may be possible to initialize the parsers non-recursively
// - maybe the whole definition, parser and builder can be united
func (d *choiceDefinition) parser(r *registry, parsers *idSet) (parser, error) { func (d *choiceDefinition) parser(r *registry, parsers *idSet) (parser, error) {
p, ok := r.parser(d.name) p, ok := r.parser(d.name)
@ -34,9 +93,10 @@ func (d *choiceDefinition) parser(r *registry, parsers *idSet) (parser, error) {
} }
cp := &choiceParser{ cp := &choiceParser{
name: d.name, name: d.name,
id: d.id, id: d.id,
commit: d.commit, commit: d.commit,
includedBy: d.includedBy,
} }
r.setParser(cp) r.setParser(cp)
@ -48,7 +108,6 @@ func (d *choiceDefinition) parser(r *registry, parsers *idSet) (parser, error) {
element, ok := r.parser(e) element, ok := r.parser(e)
if ok { if ok {
elements = append(elements, element) elements = append(elements, element)
element.setIncludedBy(cp, parsers)
continue continue
} }
@ -62,7 +121,6 @@ func (d *choiceDefinition) parser(r *registry, parsers *idSet) (parser, error) {
return nil, err return nil, err
} }
element.setIncludedBy(cp, parsers)
elements = append(elements, element) elements = append(elements, element)
} }
@ -70,39 +128,26 @@ func (d *choiceDefinition) parser(r *registry, parsers *idSet) (parser, error) {
return cp, nil return cp, nil
} }
func (d *choiceDefinition) commitType() CommitType { func (d *choiceDefinition) builder() builder {
return d.commit if d.cbuilder == nil {
d.cbuilder = &choiceBuilder{
name: d.name,
id: d.id,
commit: d.commit,
}
}
return d.cbuilder
} }
func (p *choiceParser) nodeName() string { return p.name } func (p *choiceParser) nodeName() string { return p.name }
func (p *choiceParser) nodeID() int { return p.id } func (p *choiceParser) nodeID() int { return p.id }
func (p *choiceParser) setIncludedBy(includedBy parser, parsers *idSet) {
// if parsers.has(p.id) {
// return
// }
p.includedBy = append(p.includedBy, includedBy)
}
func (p *choiceParser) storeIncluded(c *context, n *Node) {
if !c.excluded(n.From, p.id) {
return
}
nc := newNode(p.name, p.id, n.From, n.To, p.commit)
nc.append(n)
c.store.set(nc.From, p.id, nc)
for _, includedBy := range p.includedBy {
includedBy.storeIncluded(c, nc)
}
}
func (p *choiceParser) parse(t Trace, c *context) { func (p *choiceParser) parse(t Trace, c *context) {
// t = t.Extend(p.name) // t = t.Extend(p.name)
// t.Out1("parsing choice", c.offset) // t.Out1("parsing choice", c.offset)
// TODO: don't add documentation
if p.commit&Documentation != 0 { if p.commit&Documentation != 0 {
// t.Out1("fail, doc") // t.Out1("fail, doc")
c.fail(c.offset) c.fail(c.offset)
@ -115,38 +160,43 @@ func (p *choiceParser) parse(t Trace, c *context) {
} }
if c.excluded(c.offset, p.id) { if c.excluded(c.offset, p.id) {
// t.Out1("excluded") // t.Out1("fail, excluded")
c.fail(c.offset) c.fail(c.offset)
return return
} }
c.exclude(c.offset, p.id) c.exclude(c.offset, p.id)
initialOffset := c.offset from := c.offset
to := c.offset
node := newNode(p.name, p.id, c.offset, c.offset, p.commit)
var match bool var match bool
var nextTo int
var elementIndex int
for { for {
elements := p.elements
var foundMatch bool var foundMatch bool
elementIndex = 0
for len(elements) > 0 { for elementIndex < len(p.elements) {
elements[0].parse(t, c) p.elements[elementIndex].parse(t, c)
elements = elements[1:] elementIndex++
c.offset = node.From nextTo = c.offset
c.offset = from
if !c.match || match && c.node.tokenLength() <= node.tokenLength() { if !c.match || match && nextTo <= to {
continue continue
} }
match = true match = true
foundMatch = true foundMatch = true
node = newNode(p.name, p.id, c.offset, c.offset, p.commit) to = nextTo
node.append(c.node)
c.store.set(node.From, p.id, node) c.store.setMatch(from, p.id, to)
for _, includedBy := range p.includedBy { for _, includedBy := range p.includedBy {
includedBy.storeIncluded(c, node) if c.excluded(from, includedBy) {
// t.Out1("storing included", includedBy)
c.store.setMatch(from, includedBy, to)
}
} }
} }
@ -156,14 +206,60 @@ func (p *choiceParser) parse(t Trace, c *context) {
} }
if match { if match {
c.success(to)
c.include(from, p.id)
// t.Out1("choice, success") // t.Out1("choice, success")
c.success(node)
c.include(initialOffset, p.id)
return return
} }
// t.Out1("fail") // t.Out1("fail")
c.store.set(node.From, p.id, nil) c.store.setNoMatch(from, p.id)
c.fail(node.From) c.fail(from)
c.include(initialOffset, p.id) c.include(from, p.id)
}
func (b *choiceBuilder) nodeName() string { return b.name }
func (b *choiceBuilder) nodeID() int { return b.id }
func (b *choiceBuilder) build(c *context) ([]*Node, bool) {
to, ok := c.store.takeMatch(c.offset, b.id)
if !ok {
return nil, false
}
for _, ib := range b.includedBy {
c.store.takeMatchLength(c.offset, ib, to)
}
var element builder
for _, e := range b.elements {
elementTo, match, _ := c.store.getMatch(c.offset, e.nodeID())
if match && elementTo == to {
element = e
break
}
}
if element == nil {
panic("damaged parse result")
}
from := c.offset
n, ok := element.build(c)
if !ok {
panic("damaged parse result")
}
if b.commit&Alias != 0 {
return n, true
}
return []*Node{{
Name: b.name,
From: from,
To: to,
Nodes: n,
tokens: c.tokens,
}}, true
} }

View File

@ -15,7 +15,7 @@ type context struct {
tokens []rune tokens []rune
match bool match bool
node *Node node *Node
isExcluded []*idSet isExcluded [][]int
} }
func newContext(r io.RuneReader) *context { func newContext(r io.RuneReader) *context {
@ -65,53 +65,51 @@ func (c *context) token() (rune, bool) {
} }
func (c *context) excluded(offset int, id int) bool { func (c *context) excluded(offset int, id int) bool {
if len(c.isExcluded) <= offset || c.isExcluded[offset] == nil { if len(c.isExcluded) <= id {
return false return false
} }
return c.isExcluded[offset].has(id) for i := range c.isExcluded[id] {
} if c.isExcluded[id][i] == offset {
return true
func (c *context) exclude(offset int, id int) {
if c.excluded(offset, id) {
return
}
if len(c.isExcluded) <= offset {
c.isExcluded = append(c.isExcluded, nil)
if cap(c.isExcluded) > offset {
c.isExcluded = c.isExcluded[:offset+1]
} else {
c.isExcluded = append(
c.isExcluded[:cap(c.isExcluded)],
make([]*idSet, offset+1-cap(c.isExcluded))...,
)
} }
} }
if c.isExcluded[offset] == nil { return false
c.isExcluded[offset] = &idSet{} }
func (c *context) exclude(offset int, id int) {
if len(c.isExcluded) <= id {
if cap(c.isExcluded) > id {
c.isExcluded = c.isExcluded[:id+1]
} else {
c.isExcluded = c.isExcluded[:cap(c.isExcluded)]
for i := cap(c.isExcluded); i <= id; i++ {
c.isExcluded = append(c.isExcluded, nil)
}
}
} }
c.isExcluded[offset].set(id) c.isExcluded[id] = append(c.isExcluded[id], offset)
} }
func (c *context) include(offset int, id int) { func (c *context) include(offset int, id int) {
if len(c.isExcluded) <= offset || c.isExcluded[offset] == nil { for i := range c.isExcluded[id] {
return if c.isExcluded[id][i] == offset {
c.isExcluded[id] = append(c.isExcluded[id][:i], c.isExcluded[id][i+1:]...)
break
}
} }
c.isExcluded[offset].unset(id)
} }
func (c *context) fromStore(id int) (bool, bool) { func (c *context) fromStore(id int) (bool, bool) {
n, m, ok := c.store.get(c.offset, id) to, m, ok := c.store.getMatch(c.offset, id)
if !ok { if !ok {
return false, false return false, false
} }
if m { if m {
c.success(n) c.success(to)
} else { } else {
c.fail(c.offset) c.fail(c.offset)
} }
@ -119,15 +117,8 @@ func (c *context) fromStore(id int) (bool, bool) {
return m, true return m, true
} }
func (c *context) success(n *Node) { func (c *context) success(to int) {
c.node = n c.offset = to
c.offset = n.To
c.match = true
}
func (c *context) successChar() {
c.node = nil
c.offset++
c.match = true c.match = true
} }
@ -136,8 +127,10 @@ func (c *context) fail(offset int) {
c.match = false c.match = false
} }
func (c *context) finalize() error { func (c *context) finalize(root parser) error {
if c.node.To < c.readOffset { rootID := root.nodeID()
to, match, found := c.store.getMatch(0, rootID)
if !found || !match || to < c.readOffset {
return ErrUnexpectedCharacter return ErrUnexpectedCharacter
} }
@ -152,6 +145,5 @@ func (c *context) finalize() error {
} }
} }
c.node.commit(c.tokens)
return nil return nil
} }

View File

@ -6,7 +6,7 @@ null = "null";
string = "\"" ([^\\"\b\f\n\r\t] | "\\" (["\\/bfnrt] | "u" [0-9a-f]{4}))* "\""; string = "\"" ([^\\"\b\f\n\r\t] | "\\" (["\\/bfnrt] | "u" [0-9a-f]{4}))* "\"";
number = "-"? ("0" | [1-9][0-9]*) ("." [0-9]+)? ([eE] [+\-]? [0-9]+)?; number = "-"? ("0" | [1-9][0-9]*) ("." [0-9]+)? ([eE] [+\-]? [0-9]+)?;
entry = string ws* ":" ws* value; entry = string ws* ":" ws* value;
object = "{" ws* (entry (ws* "," ws* entry)*)? ws* "}"; object = "{" ws* (entry ws* ("," ws* entry)*)? ws* "}";
array = "[" ws* (value (ws* "," ws* value)*)? ws* "]"; array = "[" ws* (value ws* ("," ws* value)*)? ws* "]";
value:alias = true | false | null | string | number | object | array; value:alias = true | false | null | string | number | object | array;
json = value; json:root = value;

View File

@ -15,8 +15,11 @@ coverage
custom tokens custom tokens
indentation indentation
streaming streaming
code generation go code generation go:
- find things that depend on the syntax input
- char matches can be generated into switches
code generation js code generation js
ws and nows flags
[problems] [problems]
can the root be an alias? check the commit mechanism can the root be an alias? check the commit mechanism

View File

@ -5,19 +5,26 @@ import "fmt"
type definition interface { type definition interface {
nodeName() string nodeName() string
nodeID() int nodeID() int
setID(int)
parser(*registry, *idSet) (parser, error)
commitType() CommitType commitType() CommitType
setID(int)
init(*registry) error
setIncludedBy(*registry, int, *idSet) error
parser(*registry, *idSet) (parser, error)
builder() builder
} }
type parser interface { type parser interface {
nodeName() string nodeName() string
nodeID() int nodeID() int
setIncludedBy(parser, *idSet)
storeIncluded(*context, *Node)
parse(Trace, *context) parse(Trace, *context)
} }
type builder interface {
nodeName() string
nodeID() int
build(*context) ([]*Node, bool)
}
func parserNotFound(name string) error { func parserNotFound(name string) error {
return fmt.Errorf("parser not found: %s", name) return fmt.Errorf("parser not found: %s", name)
} }
@ -26,9 +33,9 @@ func cannotIncludeParsers(name string) error {
return fmt.Errorf("parser: %s cannot include other parsers", name) return fmt.Errorf("parser: %s cannot include other parsers", name)
} }
func stringsContainDeprecated(ss []string, s string) bool { func intsContain(is []int, i int) bool {
for _, si := range ss { for _, ii := range is {
if si == s { if ii == i {
return true return true
} }
} }
@ -36,19 +43,59 @@ func stringsContainDeprecated(ss []string, s string) bool {
return false return false
} }
func parse(t Trace, p parser, c *context) (*Node, error) { func appendIfMissing(is []int, i int) []int {
if intsContain(is, i) {
return is
}
return append(is, i)
}
func setItemsIncludedBy(r *registry, items []string, includedBy int, parsers *idSet) error {
for _, item := range items {
di, ok := r.definition(item)
if !ok {
return ErrNoParsersDefined
}
di.setIncludedBy(r, includedBy, parsers)
}
return nil
}
func sequenceItemNames(items []SequenceItem) []string {
names := make([]string, len(items))
for i := range items {
names[i] = items[i].Name
}
return names
}
func parse(t Trace, p parser, c *context) error {
p.parse(t, c) p.parse(t, c)
if c.readErr != nil { if c.readErr != nil {
return nil, c.readErr return c.readErr
} }
if !c.match { if !c.match {
return nil, ErrInvalidInput return ErrInvalidInput
} }
if err := c.finalize(); err != nil { if err := c.finalize(p); err != nil {
return nil, err return err
} }
return c.node, nil return nil
}
func build(b builder, c *context) *Node {
c.offset = 0
n, ok := b.build(c)
if !ok || len(n) != 1 {
panic("damaged parse result")
}
return n[0]
} }

View File

@ -1,10 +1,13 @@
package treerack package treerack
type sequenceDefinition struct { type sequenceDefinition struct {
name string name string
id int id int
commit CommitType commit CommitType
items []SequenceItem items []SequenceItem
includedBy []int
ranges [][]int
sbuilder *sequenceBuilder
} }
type sequenceParser struct { type sequenceParser struct {
@ -13,7 +16,16 @@ type sequenceParser struct {
commit CommitType commit CommitType
items []parser items []parser
ranges [][]int ranges [][]int
includedBy []parser includedBy []int
}
type sequenceBuilder struct {
name string
id int
commit CommitType
items []builder
ranges [][]int
includedBy []int
} }
func newSequence(name string, ct CommitType, items []SequenceItem) *sequenceDefinition { func newSequence(name string, ct CommitType, items []SequenceItem) *sequenceDefinition {
@ -24,11 +36,79 @@ func newSequence(name string, ct CommitType, items []SequenceItem) *sequenceDefi
} }
} }
func (d *sequenceDefinition) nodeName() string { return d.name } func (d *sequenceDefinition) nodeName() string { return d.name }
func (d *sequenceDefinition) nodeID() int { return d.id } func (d *sequenceDefinition) nodeID() int { return d.id }
func (d *sequenceDefinition) setID(id int) { d.id = id } func (d *sequenceDefinition) setID(id int) { d.id = id }
func (d *sequenceDefinition) commitType() CommitType { return d.commit }
func (d *sequenceDefinition) includeItems() bool {
return len(d.items) == 1 && d.items[0].Min == 1 && d.items[0].Max == 1
}
func (d *sequenceDefinition) init(r *registry) error {
if d.sbuilder == nil {
d.sbuilder = &sequenceBuilder{
name: d.name,
id: d.id,
commit: d.commit,
}
}
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
}
d.ranges = append(d.ranges, []int{item.Min, item.Max})
def, ok := r.definition(item.Name)
if !ok {
return parserNotFound(item.Name)
}
d.sbuilder.items = append(d.sbuilder.items, def.builder())
}
d.sbuilder.ranges = d.ranges
if !d.includeItems() {
return nil
}
parsers := &idSet{}
parsers.set(d.id)
return setItemsIncludedBy(r, sequenceItemNames(d.items), d.id, parsers)
}
func (d *sequenceDefinition) setIncludedBy(r *registry, includedBy int, parsers *idSet) error {
if parsers.has(d.id) {
return nil
}
d.includedBy = appendIfMissing(d.includedBy, includedBy)
if d.sbuilder == nil {
d.sbuilder = &sequenceBuilder{
name: d.name,
id: d.id,
commit: d.commit,
}
}
d.sbuilder.includedBy = appendIfMissing(d.sbuilder.includedBy, includedBy)
if !d.includeItems() {
return nil
}
parsers.set(d.id)
return setItemsIncludedBy(r, sequenceItemNames(d.items), includedBy, parsers)
}
func (d *sequenceDefinition) parser(r *registry, parsers *idSet) (parser, error) { func (d *sequenceDefinition) parser(r *registry, parsers *idSet) (parser, error) {
// TODO: what is this for? test with sequence containing a sequence through a choice
if parsers.has(d.id) { if parsers.has(d.id) {
panic(cannotIncludeParsers(d.name)) panic(cannotIncludeParsers(d.name))
} }
@ -39,31 +119,21 @@ func (d *sequenceDefinition) parser(r *registry, parsers *idSet) (parser, error)
} }
sp := &sequenceParser{ sp := &sequenceParser{
name: d.name, name: d.name,
id: d.id, id: d.id,
commit: d.commit, commit: d.commit,
includedBy: d.includedBy,
} }
r.setParser(sp) r.setParser(sp)
var ( var items []parser
items []parser
ranges [][]int
)
parsers.set(d.id) parsers.set(d.id)
defer parsers.unset(d.id) defer parsers.unset(d.id)
for _, item := range d.items { 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) pi, ok := r.parser(item.Name)
if ok { if ok {
items = append(items, pi) items = append(items, pi)
ranges = append(ranges, []int{item.Min, item.Max})
continue continue
} }
@ -78,48 +148,28 @@ func (d *sequenceDefinition) parser(r *registry, parsers *idSet) (parser, error)
} }
items = append(items, pi) items = append(items, pi)
ranges = append(ranges, []int{item.Min, item.Max})
}
// for single items, acts like a choice
if len(items) == 1 && ranges[0][0] == 1 && ranges[0][1] == 1 {
items[0].setIncludedBy(sp, parsers)
} }
sp.items = items sp.items = items
sp.ranges = ranges sp.ranges = d.ranges
return sp, nil return sp, nil
} }
func (d *sequenceDefinition) commitType() CommitType { func (d *sequenceDefinition) builder() builder {
return d.commit if d.sbuilder == nil {
d.sbuilder = &sequenceBuilder{
name: d.name,
id: d.id,
commit: d.commit,
}
}
return d.sbuilder
} }
func (p *sequenceParser) nodeName() string { return p.name } func (p *sequenceParser) nodeName() string { return p.name }
func (p *sequenceParser) nodeID() int { return p.id } func (p *sequenceParser) nodeID() int { return p.id }
func (p *sequenceParser) setIncludedBy(includedBy parser, parsers *idSet) {
// if parsers.has(p.id) {
// return
// }
p.includedBy = append(p.includedBy, includedBy)
}
func (p *sequenceParser) storeIncluded(c *context, n *Node) {
if !c.excluded(n.From, p.id) {
return
}
nc := newNode(p.name, p.id, n.From, n.To, p.commit)
nc.append(n)
c.store.set(nc.From, p.id, nc)
for _, includedBy := range p.includedBy {
includedBy.storeIncluded(c, nc)
}
}
func (p *sequenceParser) parse(t Trace, c *context) { func (p *sequenceParser) parse(t Trace, c *context) {
// t = t.Extend(p.name) // t = t.Extend(p.name)
// t.Out1("parsing sequence", c.offset) // t.Out1("parsing sequence", c.offset)
@ -131,68 +181,119 @@ func (p *sequenceParser) parse(t Trace, c *context) {
} }
if c.excluded(c.offset, p.id) { if c.excluded(c.offset, p.id) {
// t.Out1("excluded") // t.Out1("fail, excluded")
c.fail(c.offset) c.fail(c.offset)
return return
} }
// if c.store.hasNoMatch(c.offset, p.id) {
// c.fail(c.offset)
// }
c.exclude(c.offset, p.id) c.exclude(c.offset, p.id)
initialOffset := c.offset
items := p.items itemIndex := 0
ranges := p.ranges
var currentCount int var currentCount int
node := newNode(p.name, p.id, c.offset, c.offset, p.commit) from := c.offset
to := c.offset
for len(items) > 0 { for itemIndex < len(p.items) {
var m bool // TODO: is it ok to parse before max range check? what if max=0
// var ok bool p.items[itemIndex].parse(t, c)
// m, ok = c.fromStore(items[0].nodeID()) if !c.match {
// if ok { if currentCount < p.ranges[itemIndex][0] {
// // t.Out1("sequence item found in store, match:", m, items[0].nodeName(), c.offset) // c.store.setNoMatch(from, p.id)
// } else { c.fail(from)
items[0].parse(t, c) c.include(from, p.id)
m = c.match // t.Out1("fail, not enough items")
// }
if !m {
if currentCount < ranges[0][0] {
// t.Out1("fail, item failed")
// c.store.set(node.From, p.id, nil)
c.fail(node.From)
c.include(initialOffset, p.id)
return return
} }
items = items[1:] itemIndex++
ranges = ranges[1:]
currentCount = 0 currentCount = 0
continue continue
} }
// nil as char parsed := c.offset > to
if c.node == nil { if parsed {
node.appendChar(c.offset)
currentCount++
} else if c.node.tokenLength() > 0 {
node.append(c.node)
currentCount++ currentCount++
} }
if c.node != nil && c.node.tokenLength() == 0 || ranges[0][1] >= 0 && currentCount == ranges[0][1] { to = c.offset
items = items[1:]
ranges = ranges[1:] if !parsed || p.ranges[itemIndex][1] >= 0 && currentCount == p.ranges[itemIndex][1] {
itemIndex++
currentCount = 0 currentCount = 0
} }
} }
// t.Out1("success, items parsed")
// c.store.set(node.From, p.id, node)
for _, includedBy := range p.includedBy { for _, includedBy := range p.includedBy {
includedBy.storeIncluded(c, node) if c.excluded(from, includedBy) {
// t.Out1("storing included", includedBy)
c.store.setMatch(from, includedBy, to)
}
} }
c.success(node) // t.Out1("success")
c.include(initialOffset, p.id) c.store.setMatch(from, p.id, to)
c.success(to)
c.include(from, p.id)
}
func (b *sequenceBuilder) nodeName() string { return b.name }
func (b *sequenceBuilder) nodeID() int { return b.id }
func (b *sequenceBuilder) build(c *context) ([]*Node, bool) {
to, ok := c.store.takeMatch(c.offset, b.id)
if !ok {
return nil, false
}
for _, ib := range b.includedBy {
c.store.takeMatchLength(c.offset, ib, to)
}
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] {
panic(b.name + ": damaged parse result")
}
itemIndex++
currentCount = 0
continue
}
parsed := c.offset > itemFrom
if parsed {
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
}
return []*Node{{
Name: b.name,
From: from,
To: to,
Nodes: nodes,
tokens: c.tokens,
}}, true
} }

377
store.go
View File

@ -1,270 +1,159 @@
package treerack package treerack
type storeEntry struct { // TODO:
match *idSet // - store it similarly to the excluded ones? sorted by offset?
noMatch *idSet // - use a helper field for the last accessed position to walk from there? for every offset?
nodes []*Node // - use a helper field to store the largest value and its index, too? for an offset?
}
type store struct { type store struct {
entries []*storeEntry noMatch []*idSet
match [][]int
} }
func (c *store) get(offset int, id int) (*Node, bool, bool) { func (s *store) hasNoMatch(offset, id int) bool {
if len(c.entries) <= offset { if len(s.noMatch) <= offset || s.noMatch[offset] == nil {
return nil, false, false return false
} }
tc := c.entries[offset] return s.noMatch[offset].has(id)
if tc == nil { }
return nil, false, false
func (s *store) getMatch(offset, id int) (int, bool, bool) {
if s.hasNoMatch(offset, id) {
return 0, false, true
} }
if tc.noMatch.has(id) { if len(s.match) <= offset {
return nil, false, true return 0, false, false
} }
if !tc.match.has(id) { var (
return nil, false, false found bool
} to int
)
for _, n := range tc.nodes { for i := 0; i < len(s.match[offset]); i += 2 {
if n.id == id { if s.match[offset][i] != id {
return n, true, true continue
}
found = true
if s.match[offset][i+1] > to {
to = s.match[offset][i+1]
} }
} }
return nil, false, false return to, found, found
} }
func (c *store) set(offset int, id int, n *Node) { func (s *store) takeMatch(offset, id int) (int, bool) {
if len(c.entries) <= offset { if s.hasNoMatch(offset, id) {
if cap(c.entries) > offset { return 0, false
c.entries = c.entries[:offset+1] }
if len(s.match) <= offset {
return 0, false
}
var (
found bool
to int
index int
)
for i := 0; i < len(s.match[offset]); i += 2 {
if s.match[offset][i] != id {
continue
}
found = true
if s.match[offset][i+1] > to {
to = s.match[offset][i+1]
index = i
}
}
if found {
s.match[offset][index] = -1
}
return to, found
}
func (s *store) takeMatchLength(offset, id, to int) (int, bool) {
if s.hasNoMatch(offset, id) {
return 0, false
}
if len(s.match) <= offset {
return 0, false
}
var (
found bool
// index int
)
for i := 0; i < len(s.match[offset]); i += 2 {
if s.match[offset][i] != id {
continue
}
found = true
if s.match[offset][i+1] == to {
s.match[offset][i] = -1
return to, true
//eindex = i
}
}
if found {
// s.match[offset][index] = -1
}
return to, found
}
func (s *store) ensureOffset(offset int) {
if len(s.match) > offset {
return
}
if cap(s.match) > offset {
s.match = s.match[:offset+1]
return
}
s.match = s.match[:cap(s.match)]
for i := len(s.match); i <= offset; i++ {
s.match = append(s.match, nil)
}
}
func (s *store) setMatch(offset, id, to int) {
if toe, match, ok := s.getMatch(offset, id); ok && match && toe == to {
return
}
s.ensureOffset(offset)
s.match[offset] = append(s.match[offset], id, to)
}
func (s *store) setNoMatch(offset, id int) {
if len(s.noMatch) <= offset {
if cap(s.noMatch) > offset {
s.noMatch = s.noMatch[:offset+1]
} else { } else {
c.entries = c.entries[:cap(c.entries)] s.noMatch = s.noMatch[:cap(s.noMatch)]
for len(c.entries) <= offset { for i := cap(s.noMatch); i <= offset; i++ {
c.entries = append(c.entries, nil) s.noMatch = append(s.noMatch, nil)
} }
} }
} }
tc := c.entries[offset] if s.noMatch[offset] == nil {
if tc == nil { s.noMatch[offset] = &idSet{}
tc = &storeEntry{
match: &idSet{},
noMatch: &idSet{},
}
c.entries[offset] = tc
} }
if n == nil { s.noMatch[offset].set(id)
if tc.match.has(id) {
return
}
tc.noMatch.set(id)
return
}
tc.match.set(id)
for i, ni := range tc.nodes {
if ni.id == id {
if n.tokenLength() > ni.tokenLength() {
tc.nodes[i] = n
}
return
}
}
tc.nodes = append(tc.nodes, n)
} }
/*
[][][]int
id, length, where to start in the underlying layer, which list in the layer
attibutes:
- sequence: length, the items in the layer below
- choice: the item below
features:
- there can be sequences or choices under choices
in every position:
- store the valid choices with the underlying parsed nodes
3D table: layer, choice, sequence
stored choice identified by: offset, layer, choice index
*/
func (c *store) inc() {
}
func (c *store) dec() {
}
func (c *store) get2(offset, id int) (int, bool) {
return 0, false
}
func (c *store) cache(offset, id int, match bool, length int) {
}
func (c *store) set2(offset, id int, match bool, length int) {
/*
c.cache(offset, id, match, length)
levels := c.offsetLevels[offset]
levels[c.currentLevel] = id
*/
}
/*
package treerack
type storeEntry struct {
match *idSet
noMatch *idSet
nodes []*Node
offset int
}
type store struct {
current *storeEntry
currentIndex int
entries []*storeEntry
}
func (s *store) find(offset int) *storeEntry {
if s.current == nil {
return nil
}
var seekPrev, seekNext bool
for {
switch {
case s.current.offset == offset:
return s.current
case s.current.offset < offset:
if seekPrev {
return nil
}
seekNext = true
s.currentIndex++
if s.currentIndex == len(s.entries) {
s.currentIndex = len(s.entries) - 1
return nil
}
s.current = s.entries[s.currentIndex]
case s.current.offset > offset:
if seekNext {
return nil
}
seekPrev = true
s.currentIndex--
if s.currentIndex == -1 {
s.currentIndex = 0
return nil
}
s.current = s.entries[s.currentIndex]
}
}
}
func (s *store) findCreate(offset int) *storeEntry {
entry := s.find(offset)
if entry != nil {
return entry
}
entry = &storeEntry{
match: &idSet{},
noMatch: &idSet{},
offset: offset,
}
switch {
case s.current != nil && s.current.offset > offset:
s.entries = append(
s.entries[:s.currentIndex],
append([]*storeEntry{entry}, s.entries[s.currentIndex:]...)...,
)
s.current = entry
case s.current != nil && s.current.offset < offset:
s.entries = append(
s.entries[:s.currentIndex + 1],
append([]*storeEntry{entry}, s.entries[s.currentIndex + 1:]...)...,
)
s.current = entry
s.currentIndex++
default:
s.entries = []*storeEntry{entry}
s.current = entry
s.currentIndex = 0
}
return entry
}
func (s *store) get(offset int, id int) (*Node, bool, bool) {
entry := s.find(offset)
if entry == nil {
return nil, false, false
}
if entry == nil {
return nil, false, false
}
if entry.noMatch.has(id) {
return nil, false, true
}
if !entry.match.has(id) {
return nil, false, false
}
for _, n := range entry.nodes {
if n.id == id {
return n, true, true
}
}
return nil, false, false
}
func (s *store) set(offset int, id int, n *Node) {
entry := s.findCreate(offset)
if n == nil {
if entry.match.has(id) {
return
}
entry.noMatch.set(id)
return
}
entry.match.set(id)
for i, ni := range entry.nodes {
if ni.id == id {
if n.tokenLength() > ni.tokenLength() {
entry.nodes[i] = n
}
return
}
}
entry.nodes = append(entry.nodes, n)
}
*/

View File

@ -29,6 +29,7 @@ type Syntax struct {
explicitRoot bool explicitRoot bool
root definition root definition
parser parser parser parser
builder builder
} }
var ( var (
@ -88,7 +89,7 @@ func childName(name string, childIndex int) string {
func (s *Syntax) Class(name string, ct CommitType, not bool, chars []rune, ranges [][]rune) error { func (s *Syntax) Class(name string, ct CommitType, not bool, chars []rune, ranges [][]rune) error {
cname := childName(name, 0) cname := childName(name, 0)
if err := s.register(newChar(cname, Alias, not, chars, ranges)); err != nil { if err := s.register(newChar(cname, not, chars, ranges)); err != nil {
return err return err
} }
@ -100,7 +101,7 @@ func (s *Syntax) CharSequence(name string, ct CommitType, chars []rune) error {
for i, ci := range chars { for i, ci := range chars {
ref := childName(name, i) ref := childName(name, i)
refs = append(refs, ref) refs = append(refs, ref)
if err := s.register(newChar(ref, Alias, false, []rune{ci}, nil)); err != nil { if err := s.register(newChar(ref, false, []rune{ci}, nil)); err != nil {
return err return err
} }
} }
@ -143,6 +144,10 @@ func (s *Syntax) Init() error {
return ErrRootAlias return ErrRootAlias
} }
for _, p := range s.registry.definitions {
p.init(s.registry)
}
var err error var err error
s.parser, err = s.root.parser(s.registry, &idSet{}) s.parser, err = s.root.parser(s.registry, &idSet{})
if err != nil { if err != nil {
@ -150,6 +155,7 @@ func (s *Syntax) Init() error {
return err return err
} }
s.builder = s.root.builder()
s.initialized = true s.initialized = true
return nil return nil
} }
@ -162,7 +168,7 @@ func (s *Syntax) Generate(w io.Writer) error {
return ErrNotImplemented return ErrNotImplemented
} }
// TODO: optimize top sequences to save memory // TODO: optimize top sequences to save memory, or just support streaming, or combine the two
func (s *Syntax) Parse(r io.Reader) (*Node, error) { func (s *Syntax) Parse(r io.Reader) (*Node, error) {
if err := s.Init(); err != nil { if err := s.Init(); err != nil {
@ -170,5 +176,9 @@ func (s *Syntax) Parse(r io.Reader) (*Node, error) {
} }
c := newContext(bufio.NewReader(r)) c := newContext(bufio.NewReader(r))
return parse(s.trace, s.parser, c) if err := parse(s.trace, s.parser, c); err != nil {
return nil, err
}
return build(s.builder, c), nil
} }

View File

@ -63,4 +63,4 @@ flag:alias = alias | doc | root;
definition = symbol (":" flag)* wsc* "=" wsc* expression; definition = symbol (":" flag)* wsc* "=" wsc* expression;
definitions:alias = definition (wsc* ";" (wsc | ";")* definition)*; definitions:alias = definition (wsc* ";" (wsc | ";")* definition)*;
syntax:root = (wsc | ";")* definitions? (wsc | ";")*; syntax:root = (wsc | ";")* definitions? (wsc | ";")*;

View File

@ -48,7 +48,7 @@ fn prevNode(compare, node, value) {
fn findNode(compare, node, value) { fn findNode(compare, node, value) {
switch { switch {
case node == empty: false case node == empty: false
case node.value == value: true case node.value == value: true
case compare(node.value, value) < 0: case compare(node.value, value) < 0:
findNode(compare, node.greater, value) findNode(compare, node.greater, value)
@ -69,8 +69,8 @@ fn delNode(compare, node, value) {
fn delCurrent() { fn delCurrent() {
match node { match node {
case {less, greater}: delBetween() case {less, greater}: delBetween()
case {less}: node.less case {less}: node.less
case {greater}: node.greater case {greater}: node.greater
} }
} }
@ -134,7 +134,7 @@ fn insertNode(compare, node, value) {
case empty: {value: value} case empty: {value: value}
case c > 0: insertGreater() -> balance() case c > 0: insertGreater() -> balance()
case c < 0: insertLess() -> balance() case c < 0: insertLess() -> balance()
default: node default: node
} }
} }