1#
2# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3# Use of this file is governed by the BSD 3-clause license that
4# can be found in the LICENSE.txt file in the project root.
5#/
6
7# A tuple: (ATN state, predicted alt, syntactic, semantic context).
8#  The syntactic context is a graph-structured stack node whose
9#  path(s) to the root is the rule invocation(s)
10#  chain used to arrive at the state.  The semantic context is
11#  the tree of semantic predicates encountered before reaching
12#  an ATN state.
13#/
14from io import StringIO
15from antlr4.PredictionContext import PredictionContext
16from antlr4.atn.ATNState import ATNState, DecisionState
17from antlr4.atn.LexerActionExecutor import LexerActionExecutor
18from antlr4.atn.SemanticContext import SemanticContext
19
20# need a forward declaration
21ATNConfig = None
22
23class ATNConfig(object):
24
25    def __init__(self, state:ATNState=None, alt:int=None, context:PredictionContext=None, semantic:SemanticContext=None, config:ATNConfig=None):
26        if config is not None:
27            if state is None:
28                state = config.state
29            if alt is None:
30                alt = config.alt
31            if context is None:
32                context = config.context
33            if semantic is None:
34                semantic = config.semanticContext
35        if semantic is None:
36            semantic = SemanticContext.NONE
37        # The ATN state associated with this configuration#/
38        self.state = state
39        # What alt (or lexer rule) is predicted by this configuration#/
40        self.alt = alt
41        # The stack of invoking states leading to the rule/states associated
42        #  with this config.  We track only those contexts pushed during
43        #  execution of the ATN simulator.
44        self.context = context
45        self.semanticContext = semantic
46        # We cannot execute predicates dependent upon local context unless
47        # we know for sure we are in the correct context. Because there is
48        # no way to do this efficiently, we simply cannot evaluate
49        # dependent predicates unless we are in the rule that initially
50        # invokes the ATN simulator.
51        #
52        # closure() tracks the depth of how far we dip into the
53        # outer context: depth > 0.  Note that it may not be totally
54        # accurate depth since I don't ever decrement. TODO: make it a boolean then
55        self.reachesIntoOuterContext = 0 if config is None else config.reachesIntoOuterContext
56        self.precedenceFilterSuppressed = False if config is None else config.precedenceFilterSuppressed
57
58    # An ATN configuration is equal to another if both have
59    #  the same state, they predict the same alternative, and
60    #  syntactic/semantic contexts are the same.
61    #/
62    def __eq__(self, other):
63        if self is other:
64            return True
65        elif not isinstance(other, ATNConfig):
66            return False
67        else:
68            return self.state.stateNumber==other.state.stateNumber \
69                and self.alt==other.alt \
70                and ((self.context is other.context) or (self.context==other.context)) \
71                and self.semanticContext==other.semanticContext \
72                and self.precedenceFilterSuppressed==other.precedenceFilterSuppressed
73
74    def __hash__(self):
75        return hash((self.state.stateNumber, self.alt, self.context, self.semanticContext))
76
77    def hashCodeForConfigSet(self):
78        return hash((self.state.stateNumber, self.alt, hash(self.semanticContext)))
79
80    def equalsForConfigSet(self, other):
81        if self is other:
82            return True
83        elif not isinstance(other, ATNConfig):
84            return False
85        else:
86            return self.state.stateNumber==other.state.stateNumber \
87                and self.alt==other.alt \
88                and self.semanticContext==other.semanticContext
89
90    def __str__(self):
91        with StringIO() as buf:
92            buf.write('(')
93            buf.write(str(self.state))
94            buf.write(",")
95            buf.write(str(self.alt))
96            if self.context is not None:
97                buf.write(",[")
98                buf.write(str(self.context))
99                buf.write("]")
100            if self.semanticContext is not None and self.semanticContext is not SemanticContext.NONE:
101                buf.write(",")
102                buf.write(str(self.semanticContext))
103            if self.reachesIntoOuterContext>0:
104                buf.write(",up=")
105                buf.write(str(self.reachesIntoOuterContext))
106            buf.write(')')
107            return buf.getvalue()
108
109# need a forward declaration
110LexerATNConfig = None
111
112class LexerATNConfig(ATNConfig):
113
114    def __init__(self, state:ATNState, alt:int=None, context:PredictionContext=None, semantic:SemanticContext=SemanticContext.NONE,
115                 lexerActionExecutor:LexerActionExecutor=None, config:LexerATNConfig=None):
116        super().__init__(state=state, alt=alt, context=context, semantic=semantic, config=config)
117        if config is not None:
118            if lexerActionExecutor is None:
119                lexerActionExecutor = config.lexerActionExecutor
120        # This is the backing field for {@link #getLexerActionExecutor}.
121        self.lexerActionExecutor = lexerActionExecutor
122        self.passedThroughNonGreedyDecision = False if config is None else self.checkNonGreedyDecision(config, state)
123
124    def __hash__(self):
125        return hash((self.state.stateNumber, self.alt, self.context,
126                self.semanticContext, self.passedThroughNonGreedyDecision,
127                self.lexerActionExecutor))
128
129    def __eq__(self, other):
130        if self is other:
131            return True
132        elif not isinstance(other, LexerATNConfig):
133            return False
134        if self.passedThroughNonGreedyDecision != other.passedThroughNonGreedyDecision:
135            return False
136        if not(self.lexerActionExecutor == other.lexerActionExecutor):
137            return False
138        return super().__eq__(other)
139
140
141
142    def hashCodeForConfigSet(self):
143        return hash(self)
144
145
146
147    def equalsForConfigSet(self, other):
148        return self==other
149
150
151
152    def checkNonGreedyDecision(self, source:LexerATNConfig, target:ATNState):
153        return source.passedThroughNonGreedyDecision \
154            or isinstance(target, DecisionState) and target.nonGreedy
155