コード例 #1
0
ファイル: ATN.py プロジェクト: CodeGrimoire/antlr4
 def getExpectedTokens(self, stateNumber: int, ctx: RuleContext):
     if stateNumber < 0 or stateNumber >= len(self.states):
         raise Exception("Invalid state number.")
     s = self.states[stateNumber]
     following = self.nextTokens(s)
     if Token.EPSILON not in following:
         return following
     expected = IntervalSet()
     expected.addSet(following)
     expected.removeOne(Token.EPSILON)
     while ctx != None and ctx.invokingState >= 0 and Token.EPSILON in following:
         invokingState = self.states[ctx.invokingState]
         rt = invokingState.transitions[0]
         following = self.nextTokens(rt.followState)
         expected.addSet(following)
         expected.removeOne(Token.EPSILON)
         ctx = ctx.parentCtx
     if Token.EPSILON in following:
         expected.addOne(Token.EOF)
     return expected
コード例 #2
0
ファイル: ATN.py プロジェクト: geektcp/alpha-antlr4
 def getExpectedTokens(self, stateNumber: int, ctx: RuleContext):
     if stateNumber < 0 or stateNumber >= len(self.states):
         raise Exception("Invalid state number.")
     s = self.states[stateNumber]
     following = self.nextTokens(s)
     if Token.EPSILON not in following:
         return following
     expected = IntervalSet()
     expected.addSet(following)
     expected.removeOne(Token.EPSILON)
     while (ctx != None and ctx.invokingState >= 0
            and Token.EPSILON in following):
         invokingState = self.states[ctx.invokingState]
         rt = invokingState.transitions[0]
         following = self.nextTokens(rt.followState)
         expected.addSet(following)
         expected.removeOne(Token.EPSILON)
         ctx = ctx.parentCtx
     if Token.EPSILON in following:
         expected.addOne(Token.EOF)
     return expected
コード例 #3
0
 def makeLabel(self):
     s = IntervalSet()
     s.addOne(self.label_)
     return s
コード例 #4
0
 def makeLabel(self):
     s = IntervalSet()
     s.addOne(self.label_)
     return s
コード例 #5
0
ファイル: LL1Analyzer.py プロジェクト: DanMcLaughlin/antlr4
    def _LOOK(self, s:ATNState, stopState:ATNState , ctx:PredictionContext, look:IntervalSet, lookBusy:set,
                     calledRuleStack:set, seeThruPreds:bool, addEOF:bool):
        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( range(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_)
コード例 #6
0
    def _LOOK(self, s: ATNState, stopState: ATNState, ctx: PredictionContext,
              look: IntervalSet, lookBusy: set, calledRuleStack: set,
              seeThruPreds: bool, addEOF: bool):
        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:
                removed = s.ruleIndex in calledRuleStack
                try:
                    calledRuleStack.discard(s.ruleIndex)
                    # run thru all possible stack tops in ctx
                    for i in range(0, len(ctx)):
                        returnState = self.atn.states[ctx.getReturnState(i)]
                        self._LOOK(returnState, stopState, ctx.getParent(i),
                                   look, lookBusy, calledRuleStack,
                                   seeThruPreds, addEOF)
                finally:
                    if removed:
                        calledRuleStack.add(s.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(
                    range(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_)
コード例 #7
0
    def getControlRecoverySet(self, recognizer: Parser):
        ctx = recognizer._ctx
        recoverSet = IntervalSet()

        while ctx is not None and ctx.invokingState >= 0:

            # If statement (for Else we only want EndIf):
            if isinstance(
                    ctx,
                (wizardParser.IfStmtContext, wizardParser.ElifStmtContext)):
                recoverSet.addOne(wizardLexer.Elif)
                recoverSet.addOne(wizardLexer.Else)
                recoverSet.addOne(wizardLexer.EndIf)
            elif isinstance(ctx, wizardParser.ElseStmtContext):
                recoverSet.addOne(wizardLexer.EndIf)

            # For/While statement:
            elif isinstance(ctx, wizardParser.ForStmtContext):
                recoverSet.addOne(wizardLexer.EndFor)
            elif isinstance(ctx, wizardParser.WhileStmtContext):
                recoverSet.addOne(wizardLexer.EndWhile)

            # Case/Default context:
            elif isinstance(
                    ctx,
                (wizardParser.CaseStmtContext,
                 wizardParser.DefaultStmtContext),
            ):
                recoverSet.addOne(wizardLexer.Break)

            # Select context:
            elif isinstance(ctx, wizardParser.SelectStmtContext):
                recoverSet.addOne(wizardLexer.EndSelect)

            ctx = ctx.parentCtx
        recoverSet.removeOne(Token.EPSILON)

        return recoverSet