Skip to content

Commit ad21d1d

Browse files
authored
Refactor link (#172)
* Remove not needed curly braces scope * Move link to method * Use separate declaration statements * Use break instead of loop label with continue * Use index available in loop instead of outer variable * Use separate declarations for variables
1 parent 766e348 commit ad21d1d

File tree

1 file changed

+117
-116
lines changed

1 file changed

+117
-116
lines changed

Diff for: tree/peg.go

+117-116
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,74 @@ func (t *Tree) warn(e error) {
445445
t.werr = fmt.Errorf("%w\nwarning: %w", t.werr, e)
446446
}
447447

448+
func (t *Tree) link(countsForRule *[TypeLast]uint, n *node, counts *[TypeLast]uint, countsByRule *[]*[TypeLast]uint, rule *node) {
449+
nodeType := n.GetType()
450+
id := counts[nodeType]
451+
counts[nodeType]++
452+
countsForRule[nodeType]++
453+
switch nodeType {
454+
case TypeAction:
455+
n.SetID(int(id))
456+
cp := n.Copy()
457+
name := fmt.Sprintf("Action%v", id)
458+
t.Actions = append(t.Actions, cp)
459+
n.Init()
460+
n.SetType(TypeName)
461+
n.SetString(name)
462+
n.SetID(t.RulesCount)
463+
464+
emptyRule := &node{Type: TypeRule, string: name, id: t.RulesCount}
465+
implicitPush := &node{Type: TypeImplicitPush}
466+
emptyRule.PushBack(implicitPush)
467+
implicitPush.PushBack(cp)
468+
implicitPush.PushBack(emptyRule.Copy())
469+
t.PushBack(emptyRule)
470+
t.RulesCount++
471+
472+
t.Rules[name] = emptyRule
473+
t.RuleNames = append(t.RuleNames, emptyRule)
474+
*countsByRule = append(*countsByRule, &[TypeLast]uint{})
475+
case TypeName:
476+
name := n.String()
477+
if _, ok := t.Rules[name]; !ok {
478+
emptyRule := &node{Type: TypeRule, string: name, id: t.RulesCount}
479+
implicitPush := &node{Type: TypeImplicitPush}
480+
emptyRule.PushBack(implicitPush)
481+
implicitPush.PushBack(&node{Type: TypeNil, string: "<nil>"})
482+
implicitPush.PushBack(emptyRule.Copy())
483+
t.PushBack(emptyRule)
484+
t.RulesCount++
485+
486+
t.Rules[name] = emptyRule
487+
t.RuleNames = append(t.RuleNames, emptyRule)
488+
*countsByRule = append(*countsByRule, &[TypeLast]uint{})
489+
}
490+
case TypePush:
491+
cp := rule.Copy()
492+
name := "PegText"
493+
cp.SetString(name)
494+
if _, ok := t.Rules[name]; !ok {
495+
emptyRule := &node{Type: TypeRule, string: name, id: t.RulesCount}
496+
emptyRule.PushBack(&node{Type: TypeNil, string: "<nil>"})
497+
t.PushBack(emptyRule)
498+
t.RulesCount++
499+
500+
t.Rules[name] = emptyRule
501+
t.RuleNames = append(t.RuleNames, emptyRule)
502+
*countsByRule = append(*countsByRule, &[TypeLast]uint{})
503+
}
504+
n.PushBack(cp)
505+
fallthrough
506+
case TypeImplicitPush:
507+
t.link(countsForRule, n.Front(), counts, countsByRule, rule)
508+
case TypeRule, TypeAlternate, TypeUnorderedAlternate, TypeSequence,
509+
TypePeekFor, TypePeekNot, TypeQuery, TypeStar, TypePlus:
510+
for _, node := range n.Slice() {
511+
t.link(countsForRule, node, counts, countsByRule, rule)
512+
}
513+
}
514+
}
515+
448516
func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) {
449517
t.AddImport("fmt")
450518
if t.Ast {
@@ -461,113 +529,45 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) {
461529

462530
counts := [TypeLast]uint{}
463531
countsByRule := make([]*[TypeLast]uint, t.RulesCount)
464-
{
465-
var rule *node
466-
var link func(countsForRule *[TypeLast]uint, n *node)
467-
link = func(countsForRule *[TypeLast]uint, n *node) {
468-
nodeType := n.GetType()
469-
id := counts[nodeType]
470-
counts[nodeType]++
471-
countsForRule[nodeType]++
472-
switch nodeType {
473-
case TypeAction:
474-
n.SetID(int(id))
475-
cp := n.Copy()
476-
name := fmt.Sprintf("Action%v", id)
477-
t.Actions = append(t.Actions, cp)
478-
n.Init()
479-
n.SetType(TypeName)
480-
n.SetString(name)
481-
n.SetID(t.RulesCount)
482-
483-
emptyRule := &node{Type: TypeRule, string: name, id: t.RulesCount}
484-
implicitPush := &node{Type: TypeImplicitPush}
485-
emptyRule.PushBack(implicitPush)
486-
implicitPush.PushBack(cp)
487-
implicitPush.PushBack(emptyRule.Copy())
488-
t.PushBack(emptyRule)
489-
t.RulesCount++
490-
491-
t.Rules[name] = emptyRule
492-
t.RuleNames = append(t.RuleNames, emptyRule)
493-
countsByRule = append(countsByRule, &[TypeLast]uint{})
494-
case TypeName:
495-
name := n.String()
496-
if _, ok := t.Rules[name]; !ok {
497-
emptyRule := &node{Type: TypeRule, string: name, id: t.RulesCount}
498-
implicitPush := &node{Type: TypeImplicitPush}
499-
emptyRule.PushBack(implicitPush)
500-
implicitPush.PushBack(&node{Type: TypeNil, string: "<nil>"})
501-
implicitPush.PushBack(emptyRule.Copy())
502-
t.PushBack(emptyRule)
503-
t.RulesCount++
504-
505-
t.Rules[name] = emptyRule
506-
t.RuleNames = append(t.RuleNames, emptyRule)
507-
countsByRule = append(countsByRule, &[TypeLast]uint{})
508-
}
509-
case TypePush:
510-
cp := rule.Copy()
511-
name := "PegText"
512-
cp.SetString(name)
513-
if _, ok := t.Rules[name]; !ok {
514-
emptyRule := &node{Type: TypeRule, string: name, id: t.RulesCount}
515-
emptyRule.PushBack(&node{Type: TypeNil, string: "<nil>"})
516-
t.PushBack(emptyRule)
517-
t.RulesCount++
518-
519-
t.Rules[name] = emptyRule
520-
t.RuleNames = append(t.RuleNames, emptyRule)
521-
countsByRule = append(countsByRule, &[TypeLast]uint{})
522-
}
523-
n.PushBack(cp)
524-
fallthrough
525-
case TypeImplicitPush:
526-
link(countsForRule, n.Front())
527-
case TypeRule, TypeAlternate, TypeUnorderedAlternate, TypeSequence,
528-
TypePeekFor, TypePeekNot, TypeQuery, TypeStar, TypePlus:
529-
for _, node := range n.Slice() {
530-
link(countsForRule, node)
531-
}
532-
}
533-
}
534-
/* first pass */
535-
for _, n := range t.Slice() {
536-
switch n.GetType() {
537-
case TypePackage:
538-
t.PackageName = n.String()
539-
case TypeImport:
540-
t.Imports = append(t.Imports, n.String())
541-
case TypePeg:
542-
t.StructName = n.String()
543-
t.StructVariables = n.Front().String()
544-
case TypeRule:
545-
if _, ok := t.Rules[n.String()]; !ok {
546-
expression := n.Front()
547-
cp := expression.Copy()
548-
expression.Init()
549-
expression.SetType(TypeImplicitPush)
550-
expression.PushBack(cp)
551-
expression.PushBack(n.Copy())
552-
553-
t.Rules[n.String()] = n
554-
t.RuleNames = append(t.RuleNames, n)
555-
}
556-
}
557-
}
558-
/* sort imports to satisfy gofmt */
559-
slices.Sort(t.Imports)
560532

561-
/* second pass */
562-
for _, n := range t.Slice() {
563-
if n.GetType() == TypeRule {
564-
rule = n
565-
counts := [TypeLast]uint{}
566-
countsByRule[n.GetID()] = &counts
567-
link(&counts, n)
533+
var rule *node
534+
535+
/* first pass */
536+
for _, n := range t.Slice() {
537+
switch n.GetType() {
538+
case TypePackage:
539+
t.PackageName = n.String()
540+
case TypeImport:
541+
t.Imports = append(t.Imports, n.String())
542+
case TypePeg:
543+
t.StructName = n.String()
544+
t.StructVariables = n.Front().String()
545+
case TypeRule:
546+
if _, ok := t.Rules[n.String()]; !ok {
547+
expression := n.Front()
548+
cp := expression.Copy()
549+
expression.Init()
550+
expression.SetType(TypeImplicitPush)
551+
expression.PushBack(cp)
552+
expression.PushBack(n.Copy())
553+
554+
t.Rules[n.String()] = n
555+
t.RuleNames = append(t.RuleNames, n)
568556
}
569557
}
570558
}
559+
/* sort imports to satisfy gofmt */
560+
slices.Sort(t.Imports)
561+
562+
/* second pass */
563+
for _, n := range t.Slice() {
564+
if n.GetType() == TypeRule {
565+
rule = n
566+
countsForRule := [TypeLast]uint{}
567+
countsByRule[n.GetID()] = &countsForRule
568+
t.link(&countsForRule, n, &counts, &countsByRule, rule)
569+
}
570+
}
571571

572572
usage := [TypeLast]uint{}
573573

@@ -655,48 +655,50 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) {
655655
intersects bool
656656
s *set.Set
657657
}, n.Len())
658-
c := 0
658+
659659
for i := range properties {
660660
properties[i].s = set.NewSet()
661661
}
662-
for _, element := range n.Slice() {
663-
consumes, properties[c].s = optimizeAlternates(element)
664-
s = s.Union(properties[c].s)
665-
c++
662+
for i, element := range n.Slice() {
663+
consumes, properties[i].s = optimizeAlternates(element)
664+
s = s.Union(properties[i].s)
666665
}
667666

668667
if firstPass {
669668
break
670669
}
671670

672671
intersections := 2
673-
compare:
674672
for ai, a := range properties[:len(properties)-1] {
675673
for _, b := range properties[ai+1:] {
676674
if a.s.Intersects(b.s) {
677675
intersections++
678676
properties[ai].intersects = true
679-
continue compare
677+
break
680678
}
681679
}
682680
}
683681
if intersections >= len(properties) {
684682
break
685683
}
686684

687-
c, unordered, ordered, maxVal := 0, &node{Type: TypeUnorderedAlternate}, &node{Type: TypeAlternate}, 0
688-
for _, element := range n.Slice() {
689-
if properties[c].intersects {
685+
unordered := &node{Type: TypeUnorderedAlternate}
686+
ordered := &node{Type: TypeAlternate}
687+
maxVal := 0
688+
for i, element := range n.Slice() {
689+
if properties[i].intersects {
690690
ordered.PushBack(element.Copy())
691691
} else {
692692
class := &node{Type: TypeUnorderedAlternate}
693693
for d := range unicode.MaxRune {
694-
if properties[c].s.Has(d) {
694+
if properties[i].s.Has(d) {
695695
class.PushBack(&node{Type: TypeCharacter, string: string(d)})
696696
}
697697
}
698698

699-
sequence, predicate, length := &node{Type: TypeSequence}, &node{Type: TypePeekFor}, properties[c].s.Len()
699+
sequence := &node{Type: TypeSequence}
700+
predicate := &node{Type: TypePeekFor}
701+
length := properties[i].s.Len()
700702
if length == 0 {
701703
class.PushBack(&node{Type: TypeNil, string: "<nil>"})
702704
}
@@ -713,7 +715,6 @@ func (t *Tree) Compile(file string, args []string, out io.Writer) (err error) {
713715
unordered.PushFront(sequence)
714716
}
715717
}
716-
c++
717718
}
718719
n.Init()
719720
if ordered.Front() == nil {

0 commit comments

Comments
 (0)