refactor definition phase
This commit is contained in:
parent
65678eb8bc
commit
c43dd97310
3
char.go
3
char.go
@ -82,10 +82,11 @@ func (p *charParser) parse(c *context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.success(c.offset + 1)
|
|
||||||
for _, includedBy := range p.includedBy {
|
for _, includedBy := range p.includedBy {
|
||||||
c.store.setMatch(c.offset, includedBy, c.offset+1)
|
c.store.setMatch(c.offset, includedBy, c.offset+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.success(c.offset + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *charParser) build(c *context) ([]*Node, bool) {
|
func (p *charParser) build(c *context) ([]*Node, bool) {
|
||||||
|
41
define.go
41
define.go
@ -5,16 +5,8 @@ import "strconv"
|
|||||||
func dropComments(n *Node) *Node {
|
func dropComments(n *Node) *Node {
|
||||||
ncc := *n
|
ncc := *n
|
||||||
nc := &ncc
|
nc := &ncc
|
||||||
|
nc.Nodes = filterNodes(func(n *Node) bool { return n.Name != "comment" }, n.Nodes)
|
||||||
nc.Nodes = nil
|
nc.Nodes = mapNodes(dropComments, nc.Nodes)
|
||||||
for _, ni := range n.Nodes {
|
|
||||||
if ni.Name == "comment" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
nc.Nodes = append(nc.Nodes, dropComments(ni))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nc
|
return nc
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,21 +52,6 @@ func defineMember(s *Syntax, defaultName string, ct CommitType, n *Node) (string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func defineMembers(s *Syntax, name string, ct CommitType, n ...*Node) ([]string, error) {
|
|
||||||
var refs []string
|
|
||||||
for i, ni := range n {
|
|
||||||
nmi := childName(name, i)
|
|
||||||
ref, err := defineMember(s, nmi, ct, ni)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
refs = append(refs, ref)
|
|
||||||
}
|
|
||||||
|
|
||||||
return refs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func defineClass(s *Syntax, name string, ct CommitType, n []*Node) error {
|
func defineClass(s *Syntax, name string, ct CommitType, n []*Node) error {
|
||||||
var (
|
var (
|
||||||
not bool
|
not bool
|
||||||
@ -184,12 +161,18 @@ func defineSequence(s *Syntax, name string, ct CommitType, n ...*Node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func defineChoice(s *Syntax, name string, ct CommitType, n ...*Node) error {
|
func defineChoice(s *Syntax, name string, ct CommitType, n ...*Node) error {
|
||||||
nows := ct & NoWhitespace
|
var refs []string
|
||||||
refs, err := defineMembers(s, name, Alias|nows, n...)
|
memberCT := ct&NoWhitespace | Alias
|
||||||
|
for i, ni := range n {
|
||||||
|
nmi := childName(name, i)
|
||||||
|
ref, err := defineMember(s, nmi, memberCT, ni)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
refs = append(refs, ref)
|
||||||
|
}
|
||||||
|
|
||||||
return s.choice(name, ct, refs...)
|
return s.choice(name, ct, refs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,10 +206,6 @@ func defineDefinition(s *Syntax, n *Node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func define(s *Syntax, n *Node) error {
|
func define(s *Syntax, n *Node) error {
|
||||||
if n.Name != "syntax" {
|
|
||||||
return ErrInvalidSyntax
|
|
||||||
}
|
|
||||||
|
|
||||||
n = dropComments(n)
|
n = dropComments(n)
|
||||||
|
|
||||||
for _, ni := range n.Nodes {
|
for _, ni := range n.Nodes {
|
||||||
|
20
node.go
20
node.go
@ -11,6 +11,26 @@ type Node struct {
|
|||||||
tokens []rune
|
tokens []rune
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mapNodes(m func(n *Node) *Node, n []*Node) []*Node {
|
||||||
|
var nn []*Node
|
||||||
|
for i := range n {
|
||||||
|
nn = append(nn, m(n[i]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nn
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterNodes(f func(n *Node) bool, n []*Node) []*Node {
|
||||||
|
var nn []*Node
|
||||||
|
for i := range n {
|
||||||
|
if f(n[i]) {
|
||||||
|
nn = append(nn, n[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nn
|
||||||
|
}
|
||||||
|
|
||||||
func newNode(name string, id int, from, to int, ct CommitType) *Node {
|
func newNode(name string, id int, from, to int, ct CommitType) *Node {
|
||||||
return &Node{
|
return &Node{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
@ -234,7 +234,6 @@ func (p *sequenceParser) parse(c *context) {
|
|||||||
p.items[itemIndex].parse(c)
|
p.items[itemIndex].parse(c)
|
||||||
if !c.match {
|
if !c.match {
|
||||||
if currentCount < p.ranges[itemIndex][0] {
|
if currentCount < p.ranges[itemIndex][0] {
|
||||||
// c.store.setNoMatch(from, p.id)
|
|
||||||
c.fail(from)
|
c.fail(from)
|
||||||
|
|
||||||
if !p.allChars {
|
if !p.allChars {
|
||||||
|
46
syntax.go
46
syntax.go
@ -78,6 +78,28 @@ func isValidSymbol(n string) bool {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Syntax) applyRoot(d definition) error {
|
||||||
|
explicitRoot := d.commitType()&Root != 0
|
||||||
|
if explicitRoot && s.explicitRoot {
|
||||||
|
return ErrMultipleRoots
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.root != nil && (explicitRoot || !s.explicitRoot) {
|
||||||
|
s.root.setCommitType(s.root.commitType() &^ Root)
|
||||||
|
}
|
||||||
|
|
||||||
|
if explicitRoot || !s.explicitRoot {
|
||||||
|
s.root = d
|
||||||
|
s.root.setCommitType(s.root.commitType() | Root)
|
||||||
|
}
|
||||||
|
|
||||||
|
if explicitRoot {
|
||||||
|
s.explicitRoot = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Syntax) register(d definition) error {
|
func (s *Syntax) register(d definition) error {
|
||||||
if s.initialized {
|
if s.initialized {
|
||||||
return ErrSyntaxInitialized
|
return ErrSyntaxInitialized
|
||||||
@ -87,30 +109,10 @@ func (s *Syntax) register(d definition) error {
|
|||||||
s.registry = newRegistry()
|
s.registry = newRegistry()
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.commitType()&Root != 0 {
|
if err := s.applyRoot(d); err != nil {
|
||||||
if s.explicitRoot {
|
return err
|
||||||
return ErrMultipleRoots
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.root != nil {
|
|
||||||
s.root.setCommitType(s.root.commitType() &^ Root)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.root = d
|
|
||||||
s.root.setCommitType(s.root.commitType() | Root)
|
|
||||||
s.explicitRoot = true
|
|
||||||
} else if !s.explicitRoot {
|
|
||||||
if s.root != nil {
|
|
||||||
s.root.setCommitType(s.root.commitType() &^ Root)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.root = d
|
|
||||||
s.root.setCommitType(s.root.commitType() | Root)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: verify that definition names match the symbol criteria, or figure a better naming for the
|
|
||||||
// whitespace
|
|
||||||
|
|
||||||
return s.registry.setDefinition(d)
|
return s.registry.setDefinition(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user