Match attempts to match the rule, returns true if it matches, and the match positions, along with any update to the scope
(ps *State, parAST *AST, scope lexer.Reg, depth int, optMap lexer.TokenMap)
| 743 | // Match attempts to match the rule, returns true if it matches, and the |
| 744 | // match positions, along with any update to the scope |
| 745 | func (pr *Rule) Match(ps *State, parAST *AST, scope lexer.Reg, depth int, optMap lexer.TokenMap) (bool, lexer.Reg, Matches) { |
| 746 | if pr.Off { |
| 747 | return false, scope, nil |
| 748 | } |
| 749 | |
| 750 | if depth > DepthLimit { |
| 751 | ps.Error(scope.St, "depth limit exceeded -- parser rules error -- look for recursive cases", pr) |
| 752 | return false, scope, nil |
| 753 | } |
| 754 | |
| 755 | if ps.IsNonMatch(scope, pr) { |
| 756 | return false, scope, nil |
| 757 | } |
| 758 | |
| 759 | if pr.StackMatch != "" { |
| 760 | if ps.Stack.Top() != pr.StackMatch { |
| 761 | return false, scope, nil |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // mprf := profile.Start("Match") |
| 766 | // defer mprf.End() |
| 767 | // Note: uncomment the following to see which rules are taking the most |
| 768 | // time -- very helpful for focusing effort on optimizing those rules. |
| 769 | // prf := profile.Start(pr.Nm) |
| 770 | // defer prf.End() |
| 771 | |
| 772 | nr := len(pr.Rules) |
| 773 | if pr.tokenMatchGroup || nr == 0 { // Group |
| 774 | return pr.MatchGroup(ps, parAST, scope, depth, optMap) |
| 775 | } |
| 776 | |
| 777 | // prf := profile.Start("IsMatch") |
| 778 | if mst, match := ps.IsMatch(pr, scope); match { |
| 779 | // prf.End() |
| 780 | return true, scope, mst.Regs |
| 781 | } |
| 782 | // prf.End() |
| 783 | |
| 784 | var mpos Matches |
| 785 | match := false |
| 786 | |
| 787 | if pr.noTokens { |
| 788 | match, mpos = pr.MatchNoToks(ps, parAST, scope, depth, optMap) |
| 789 | } else if pr.onlyTokens { |
| 790 | match, mpos = pr.MatchOnlyToks(ps, parAST, scope, depth, optMap) |
| 791 | } else { |
| 792 | match, mpos = pr.MatchMixed(ps, parAST, scope, depth, optMap) |
| 793 | } |
| 794 | if !match { |
| 795 | ps.AddNonMatch(scope, pr) |
| 796 | return false, scope, nil |
| 797 | } |
| 798 | |
| 799 | if len(pr.ExclFwd) > 0 || len(pr.ExclRev) > 0 { |
| 800 | ktpos := mpos[pr.ExclKeyIndex] |
| 801 | if pr.MatchExclude(ps, scope, ktpos, depth, optMap) { |
| 802 | if ps.Trace.On { |
no test coverage detected