# Generated from Tree.g4 by ANTLR 4.9.2
# encoding: utf-8
from antlr4 import *
from io import StringIO
import sys
if sys.version_info[1] > 5:
	from typing import TextIO
else:
	from typing.io import TextIO


def serializedATN():
    with StringIO() as buf:
        buf.write("\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\6")
        buf.write("\26\4\2\t\2\4\3\t\3\3\2\3\2\3\2\3\3\3\3\3\3\3\3\6\3\16")
        buf.write("\n\3\r\3\16\3\17\3\3\3\3\5\3\24\n\3\3\3\2\2\4\2\4\2\2")
        buf.write("\2\25\2\6\3\2\2\2\4\23\3\2\2\2\6\7\5\4\3\2\7\b\7\2\2\3")
        buf.write("\b\3\3\2\2\2\t\24\7\5\2\2\n\13\7\3\2\2\13\r\7\5\2\2\f")
        buf.write("\16\5\4\3\2\r\f\3\2\2\2\16\17\3\2\2\2\17\r\3\2\2\2\17")
        buf.write("\20\3\2\2\2\20\21\3\2\2\2\21\22\7\4\2\2\22\24\3\2\2\2")
        buf.write("\23\t\3\2\2\2\23\n\3\2\2\2\24\5\3\2\2\2\4\17\23")
        return buf.getvalue()


class TreeParser ( Parser ):

    grammarFileName = "Tree.g4"

    atn = ATNDeserializer().deserialize(serializedATN())

    decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ]

    sharedContextCache = PredictionContextCache()

    literalNames = [ "<INVALID>", "'('", "')'" ]

    symbolicNames = [ "<INVALID>", "<INVALID>", "<INVALID>", "INT", "WS" ]

    RULE_int_tree_top = 0
    RULE_int_tree = 1

    ruleNames =  [ "int_tree_top", "int_tree" ]

    EOF = Token.EOF
    T__0=1
    T__1=2
    INT=3
    WS=4

    def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
        super().__init__(input, output)
        self.checkVersion("4.9.2")
        self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache)
        self._predicates = None




    class Int_tree_topContext(ParserRuleContext):
        __slots__ = 'parser'

        def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
            super().__init__(parent, invokingState)
            self.parser = parser


        def getRuleIndex(self):
            return TreeParser.RULE_int_tree_top

     
        def copyFrom(self, ctx:ParserRuleContext):
            super().copyFrom(ctx)



    class TopContext(Int_tree_topContext):

        def __init__(self, parser, ctx:ParserRuleContext): # actually a TreeParser.Int_tree_topContext
            super().__init__(parser)
            self.copyFrom(ctx)

        def int_tree(self):
            return self.getTypedRuleContext(TreeParser.Int_treeContext,0)

        def EOF(self):
            return self.getToken(TreeParser.EOF, 0)

        def enterRule(self, listener:ParseTreeListener):
            if hasattr( listener, "enterTop" ):
                listener.enterTop(self)

        def exitRule(self, listener:ParseTreeListener):
            if hasattr( listener, "exitTop" ):
                listener.exitTop(self)

        def accept(self, visitor:ParseTreeVisitor):
            if hasattr( visitor, "visitTop" ):
                return visitor.visitTop(self)
            else:
                return visitor.visitChildren(self)



    def int_tree_top(self):

        localctx = TreeParser.Int_tree_topContext(self, self._ctx, self.state)
        self.enterRule(localctx, 0, self.RULE_int_tree_top)
        try:
            localctx = TreeParser.TopContext(self, localctx)
            self.enterOuterAlt(localctx, 1)
            self.state = 4
            self.int_tree()
            self.state = 5
            self.match(TreeParser.EOF)
        except RecognitionException as re:
            localctx.exception = re
            self._errHandler.reportError(self, re)
            self._errHandler.recover(self, re)
        finally:
            self.exitRule()
        return localctx


    class Int_treeContext(ParserRuleContext):
        __slots__ = 'parser'

        def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1):
            super().__init__(parent, invokingState)
            self.parser = parser


        def getRuleIndex(self):
            return TreeParser.RULE_int_tree

     
        def copyFrom(self, ctx:ParserRuleContext):
            super().copyFrom(ctx)



    class NodeContext(Int_treeContext):

        def __init__(self, parser, ctx:ParserRuleContext): # actually a TreeParser.Int_treeContext
            super().__init__(parser)
            self.copyFrom(ctx)

        def INT(self):
            return self.getToken(TreeParser.INT, 0)
        def int_tree(self, i:int=None):
            if i is None:
                return self.getTypedRuleContexts(TreeParser.Int_treeContext)
            else:
                return self.getTypedRuleContext(TreeParser.Int_treeContext,i)


        def enterRule(self, listener:ParseTreeListener):
            if hasattr( listener, "enterNode" ):
                listener.enterNode(self)

        def exitRule(self, listener:ParseTreeListener):
            if hasattr( listener, "exitNode" ):
                listener.exitNode(self)

        def accept(self, visitor:ParseTreeVisitor):
            if hasattr( visitor, "visitNode" ):
                return visitor.visitNode(self)
            else:
                return visitor.visitChildren(self)


    class LeafContext(Int_treeContext):

        def __init__(self, parser, ctx:ParserRuleContext): # actually a TreeParser.Int_treeContext
            super().__init__(parser)
            self.copyFrom(ctx)

        def INT(self):
            return self.getToken(TreeParser.INT, 0)

        def enterRule(self, listener:ParseTreeListener):
            if hasattr( listener, "enterLeaf" ):
                listener.enterLeaf(self)

        def exitRule(self, listener:ParseTreeListener):
            if hasattr( listener, "exitLeaf" ):
                listener.exitLeaf(self)

        def accept(self, visitor:ParseTreeVisitor):
            if hasattr( visitor, "visitLeaf" ):
                return visitor.visitLeaf(self)
            else:
                return visitor.visitChildren(self)



    def int_tree(self):

        localctx = TreeParser.Int_treeContext(self, self._ctx, self.state)
        self.enterRule(localctx, 2, self.RULE_int_tree)
        self._la = 0 # Token type
        try:
            self.state = 17
            self._errHandler.sync(self)
            token = self._input.LA(1)
            if token in [TreeParser.INT]:
                localctx = TreeParser.LeafContext(self, localctx)
                self.enterOuterAlt(localctx, 1)
                self.state = 7
                self.match(TreeParser.INT)
                pass
            elif token in [TreeParser.T__0]:
                localctx = TreeParser.NodeContext(self, localctx)
                self.enterOuterAlt(localctx, 2)
                self.state = 8
                self.match(TreeParser.T__0)
                self.state = 9
                self.match(TreeParser.INT)
                self.state = 11 
                self._errHandler.sync(self)
                _la = self._input.LA(1)
                while True:
                    self.state = 10
                    self.int_tree()
                    self.state = 13 
                    self._errHandler.sync(self)
                    _la = self._input.LA(1)
                    if not (_la==TreeParser.T__0 or _la==TreeParser.INT):
                        break

                self.state = 15
                self.match(TreeParser.T__1)
                pass
            else:
                raise NoViableAltException(self)

        except RecognitionException as re:
            localctx.exception = re
            self._errHandler.reportError(self, re)
            self._errHandler.recover(self, re)
        finally:
            self.exitRule()
        return localctx





