func (ni *NameIndex) Initialize(g parser.Grammar) error { ni.nonterminalsByName = make(map[string]parser.GrammarParticle) ni.terminalsByName = make(map[string]parser.GrammarParticle) for i := 0; i < g.NumNonterminals(); i++ { nt := g.Nonterminal(i) ni.nonterminalsByName[nt.Name()] = nt } for i := 0; i < g.NumTerminals(); i++ { t := g.Terminal(i) ni.terminalsByName[t.Name()] = t } ni.lhsNames = make(map[string][]parser.Production) ni.rhsNames = make(map[string][]parser.Production) for _, p := range g.Productions() { var rhs, lhs []byte for i := 0; i < p.LhsLen(); i++ { lhs = append(lhs, p.Lhs(i).Name()...) if i < p.LhsLen()-1 { lhs = append(lhs, "|"...) } } if _, has := ni.lhsNames[string(lhs)]; !has { ni.lhsNames[string(lhs)] = []parser.Production{} } ni.lhsNames[string(lhs)] = append(ni.lhsNames[string(lhs)], p) for i := 0; i < p.RhsLen(); i++ { rhs = append(rhs, p.Rhs(i).Name()...) if i < p.RhsLen()-1 { rhs = append(rhs, "|"...) } } if _, has := ni.rhsNames[string(rhs)]; !has { ni.rhsNames[string(rhs)] = []parser.Production{} } ni.rhsNames[string(rhs)] = append(ni.rhsNames[string(rhs)], p) } return nil }