MCPcopy Create free account
hub / github.com/cogentcore/core / ParseRules

Method ParseRules

parse/parser/rule.go:662–704  ·  view source on GitHub ↗

ParseRules parses rules and returns this rule if it matches, nil if not

(ps *State, parent *Rule, parAST *AST, scope lexer.Reg, optMap lexer.TokenMap, depth int)

Source from the content-addressed store, hash-verified

660
661// ParseRules parses rules and returns this rule if it matches, nil if not
662func (pr *Rule) ParseRules(ps *State, parent *Rule, parAST *AST, scope lexer.Reg, optMap lexer.TokenMap, depth int) *Rule {
663 ok := false
664 if pr.setsScope {
665 scope, ok = pr.Scope(ps, parAST, scope)
666 if !ok {
667 return nil
668 }
669 } else if GUIActive {
670 if scope == lexer.RegZero {
671 ps.Error(scope.St, "scope is empty and no EOS in rule -- invalid rules -- starting rules must all have EOS", pr)
672 return nil
673 }
674 }
675 match, nscope, mpos := pr.Match(ps, parAST, scope, 0, optMap)
676 if !match {
677 return nil
678 }
679
680 rparent := parent.Parent.(*Rule)
681
682 if parent.AST != NoAST && parent.IsGroup() {
683 if parAST.Name != parent.Name {
684 mreg := mpos.StartEndExcl(ps)
685 newAST := ps.AddAST(parAST, parent.Name, mreg)
686 if parent.AST == AnchorAST {
687 parAST = newAST
688 }
689 }
690 } else if parent.IsGroup() && rparent.AST != NoAST && rparent.IsGroup() { // two-level group...
691 if parAST.Name != rparent.Name {
692 mreg := mpos.StartEndExcl(ps)
693 newAST := ps.AddAST(parAST, rparent.Name, mreg)
694 if rparent.AST == AnchorAST {
695 parAST = newAST
696 }
697 }
698 }
699 valid := pr.DoRules(ps, parent, parAST, nscope, mpos, optMap, depth) // returns validity but we don't care once matched..
700 if !valid {
701 return nil
702 }
703 return pr
704}
705
706// Scope finds the potential scope region for looking for tokens -- either from
707// EOS position or State ScopeStack pushed from parents.

Callers 1

ParseMethod · 0.95

Calls 7

ScopeMethod · 0.95
MatchMethod · 0.95
DoRulesMethod · 0.95
IsGroupMethod · 0.80
StartEndExclMethod · 0.80
AddASTMethod · 0.80
ErrorMethod · 0.65

Tested by

no test coverage detected