Exemple #1
0
 def getOrAdd(self, config:ATNConfig):
     h = config.hashCodeForConfigSet()
     l = self.configLookup.get(h, None)
     if l is not None:
         r = next((cfg for cfg in l if config.equalsForConfigSet(cfg)), None)
         if r is not None:
             return r
     if l is None:
         l = [config]
         self.configLookup[h] = l
     else:
         l.append(config)
     return config
Exemple #2
0
 def getOrAdd(self, config:ATNConfig):
     h = config.hashCodeForConfigSet()
     l = self.configLookup.get(h, None)
     if l is not None:
         r = next((cfg for cfg in l if config.equalsForConfigSet(cfg)), None)
         if r is not None:
             return r
     if l is None:
         l = [config]
         self.configLookup[h] = l
     else:
         l.append(config)
     return config
Exemple #3
0
 def getOrAdd(self, config:ATNConfig):
     h = config.hashCodeForConfigSet()
     l = self.configLookup.get(h, None)
     if l is not None:
         for c in l:
             if config.equalsForConfigSet(c):
                 return c
     if l is None:
         l = [config]
         self.configLookup[h] = l
     else:
         l.append(config)
     return config
Exemple #4
0
    def hasSLLConflictTerminatingPrediction(cls, mode, configs):
        # Configs in rule stop states indicate reaching the end of the decision
        # rule (local context) or end of start rule (full context). If all
        # configs meet this condition, then none of the configurations is able
        # to match additional input so we terminate prediction.
        #
        if cls.allConfigsInRuleStopStates(configs):
            return True

        # pure SLL mode parsing
        if mode == PredictionMode.SLL:
            # Don't bother with combining configs from different semantic
            # contexts if we can fail over to full LL; costs more time
            # since we'll often fail over anyway.
            if configs.hasSemanticContext:
                # dup configs, tossing out semantic predicates
                dup = ATNConfigSet()
                for c in configs:
                    c = ATNConfig(config=c, semantic=SemanticContext.NONE)
                    dup.add(c)
                configs = dup
            # now we have combined contexts for configs with dissimilar preds

        # pure SLL or combined SLL+LL mode parsing
        altsets = cls.getConflictingAltSubsets(configs)
        return cls.hasConflictingAltSet(
            altsets) and not cls.hasStateAssociatedWithOneAlt(configs)
Exemple #5
0
    def _LOOK(self, s, stopState , ctx, look, lookBusy, \
                     calledRuleStack, seeThruPreds, addEOF):
        c = ATNConfig(s, 0, ctx)

        if c in lookBusy:
            return
        lookBusy.add(c)

        if s == stopState:
            if ctx is None:
                look.addOne(Token.EPSILON)
                return
            elif ctx.isEmpty() and addEOF:
                look.addOne(Token.EOF)
                return

        if isinstance(s, RuleStopState ):
            if ctx is None:
                look.addOne(Token.EPSILON)
                return
            elif ctx.isEmpty() and addEOF:
                look.addOne(Token.EOF)
                return

            if ctx != PredictionContext.EMPTY:
                # run thru all possible stack tops in ctx
                for i in range(0, len(ctx)):
                    returnState = self.atn.states[ctx.getReturnState(i)]
                    removed = returnState.ruleIndex in calledRuleStack
                    try:
                        calledRuleStack.discard(returnState.ruleIndex)
                        self._LOOK(returnState, stopState, ctx.getParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
                    finally:
                        if removed:
                            calledRuleStack.add(returnState.ruleIndex)
                return

        for t in s.transitions:
            if type(t) == RuleTransition:
                if t.target.ruleIndex in calledRuleStack:
                    continue

                newContext = SingletonPredictionContext.create(ctx, t.followState.stateNumber)

                try:
                    calledRuleStack.add(t.target.ruleIndex)
                    self._LOOK(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
                finally:
                    calledRuleStack.remove(t.target.ruleIndex)
            elif isinstance(t, AbstractPredicateTransition ):
                if seeThruPreds:
                    self._LOOK(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
                else:
                    look.addOne(self.HIT_PRED)
            elif t.isEpsilon:
                self._LOOK(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF)
            elif type(t) == WildcardTransition:
                look.addRange( Interval(Token.MIN_USER_TOKEN_TYPE, self.atn.maxTokenType + 1) )
            else:
                set = t.label
                if set is not None:
                    if isinstance(t, NotSetTransition):
                        set = set.complement(Token.MIN_USER_TOKEN_TYPE, self.atn.maxTokenType)
                    look.addSet(set)