From aedba83693cf1437e0d3295a2f160e43ffcc55ea Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Sun, 18 Jul 2021 01:24:34 +0900 Subject: Refactor --- spec/parser.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'spec/parser.go') diff --git a/spec/parser.go b/spec/parser.go index 8b825bf..e749d72 100644 --- a/spec/parser.go +++ b/spec/parser.go @@ -8,8 +8,9 @@ import ( ) type RootNode struct { - Productions []*ProductionNode - Fragments []*FragmentNode + Productions []*ProductionNode + LexProductions []*ProductionNode + Fragments []*FragmentNode } type ProductionNode struct { @@ -18,6 +19,13 @@ type ProductionNode struct { RHS []*AlternativeNode } +func (n *ProductionNode) isLexical() bool { + if len(n.RHS) == 1 && len(n.RHS[0].Elements) == 1 && n.RHS[0].Elements[0].Pattern != "" { + return true + } + return false +} + type AlternativeNode struct { Elements []*ElementNode Directive *DirectiveNode @@ -112,6 +120,7 @@ func (p *parser) parseRoot() *RootNode { }() var prods []*ProductionNode + var lexProds []*ProductionNode var fragments []*FragmentNode for { fragment := p.parseFragment() @@ -122,7 +131,11 @@ func (p *parser) parseRoot() *RootNode { prod := p.parseProduction() if prod != nil { - prods = append(prods, prod) + if prod.isLexical() { + lexProds = append(lexProds, prod) + } else { + prods = append(prods, prod) + } continue } @@ -132,8 +145,9 @@ func (p *parser) parseRoot() *RootNode { } return &RootNode{ - Productions: prods, - Fragments: fragments, + Productions: prods, + LexProductions: lexProds, + Fragments: fragments, } } -- cgit v1.2.3