# Generated from Vgdl.g4 by ANTLR 4.7.2
from antlr4 import *
from io import StringIO
from typing.io import TextIO
import sys



def serializedATN():
    with StringIO() as buf:
        buf.write("\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\23")
        buf.write("\u00ec\b\1\4\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7")
        buf.write("\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t\13\4\f\t\f\4\r\t\r")
        buf.write("\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22\4\23")
        buf.write("\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\3\2\3\2")
        buf.write("\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\3\3\3\3\3\3\3\3\3\3")
        buf.write("\3\3\3\3\3\3\3\3\3\3\4\3\4\3\5\3\5\3\5\3\5\3\5\3\5\3\5")
        buf.write("\3\5\3\5\3\5\3\5\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3")
        buf.write("\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\7\3\7\3\7\3\7\3\7\3\7")
        buf.write("\3\7\3\7\3\7\3\7\3\7\3\7\3\7\3\7\3\7\3\b\3\b\3\b\3\b\3")
        buf.write("\b\3\t\3\t\3\n\3\n\3\13\3\13\7\13|\n\13\f\13\16\13\177")
        buf.write("\13\13\3\13\3\13\3\f\3\f\3\f\5\f\u0086\n\f\3\f\3\f\5\f")
        buf.write("\u008a\n\f\3\f\5\f\u008d\n\f\5\f\u008f\n\f\3\f\3\f\3\r")
        buf.write("\6\r\u0094\n\r\r\r\16\r\u0095\3\r\3\r\3\16\3\16\3\16\3")
        buf.write("\16\3\16\3\17\3\17\3\17\3\17\3\17\3\17\3\20\3\20\3\20")
        buf.write("\3\20\7\20\u00a9\n\20\f\20\16\20\u00ac\13\20\3\20\5\20")
        buf.write("\u00af\n\20\3\21\7\21\u00b2\n\21\f\21\16\21\u00b5\13\21")
        buf.write("\3\21\3\21\7\21\u00b9\n\21\f\21\16\21\u00bc\13\21\3\21")
        buf.write("\3\21\7\21\u00c0\n\21\f\21\16\21\u00c3\13\21\3\21\3\21")
        buf.write("\7\21\u00c7\n\21\f\21\16\21\u00ca\13\21\6\21\u00cc\n\21")
        buf.write("\r\21\16\21\u00cd\3\22\3\22\3\23\6\23\u00d3\n\23\r\23")
        buf.write("\16\23\u00d4\3\24\3\24\3\25\3\25\3\26\3\26\3\27\5\27\u00de")
        buf.write("\n\27\3\27\6\27\u00e1\n\27\r\27\16\27\u00e2\3\27\3\27")
        buf.write("\6\27\u00e7\n\27\r\27\16\27\u00e8\5\27\u00eb\n\27\2\2")
        buf.write("\30\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r")
        buf.write("\31\16\33\17\35\20\37\21!\22#\23%\2\'\2)\2+\2-\2\3\2\20")
        buf.write("\4\2\f\f\17\17\4\2\13\13\"\"\4\2VVvv\4\2TTtt\4\2WWww\4")
        buf.write("\2GGgg\4\2HHhh\4\2CCcc\4\2NNnn\4\2UUuu\3\2\"\"\5\2\60")
        buf.write("\61C\\c|\3\2\62;\4\2..\60\60\2\u00fa\2\3\3\2\2\2\2\5\3")
        buf.write("\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2")
        buf.write("\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2")
        buf.write("\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2\35\3\2\2\2\2")
        buf.write("\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\3/\3\2\2\2\59\3\2\2")
        buf.write("\2\7C\3\2\2\2\tE\3\2\2\2\13R\3\2\2\2\ra\3\2\2\2\17p\3")
        buf.write("\2\2\2\21u\3\2\2\2\23w\3\2\2\2\25y\3\2\2\2\27\u008e\3")
        buf.write("\2\2\2\31\u0093\3\2\2\2\33\u0099\3\2\2\2\35\u009e\3\2")
        buf.write("\2\2\37\u00ae\3\2\2\2!\u00b3\3\2\2\2#\u00cf\3\2\2\2%\u00d2")
        buf.write("\3\2\2\2\'\u00d6\3\2\2\2)\u00d8\3\2\2\2+\u00da\3\2\2\2")
        buf.write("-\u00dd\3\2\2\2/\60\7D\2\2\60\61\7c\2\2\61\62\7u\2\2\62")
        buf.write("\63\7k\2\2\63\64\7e\2\2\64\65\7I\2\2\65\66\7c\2\2\66\67")
        buf.write("\7o\2\2\678\7g\2\28\4\3\2\2\29:\7U\2\2:;\7r\2\2;<\7t\2")
        buf.write("\2<=\7k\2\2=>\7v\2\2>?\7g\2\2?@\7U\2\2@A\7g\2\2AB\7v\2")
        buf.write("\2B\6\3\2\2\2CD\7@\2\2D\b\3\2\2\2EF\7N\2\2FG\7g\2\2GH")
        buf.write("\7x\2\2HI\7g\2\2IJ\7n\2\2JK\7O\2\2KL\7c\2\2LM\7r\2\2M")
        buf.write("N\7r\2\2NO\7k\2\2OP\7p\2\2PQ\7i\2\2Q\n\3\2\2\2RS\7K\2")
        buf.write("\2ST\7p\2\2TU\7v\2\2UV\7g\2\2VW\7t\2\2WX\7c\2\2XY\7e\2")
        buf.write("\2YZ\7v\2\2Z[\7k\2\2[\\\7q\2\2\\]\7p\2\2]^\7U\2\2^_\7")
        buf.write("g\2\2_`\7v\2\2`\f\3\2\2\2ab\7V\2\2bc\7g\2\2cd\7t\2\2d")
        buf.write("e\7o\2\2ef\7k\2\2fg\7p\2\2gh\7c\2\2hi\7v\2\2ij\7k\2\2")
        buf.write("jk\7q\2\2kl\7p\2\2lm\7U\2\2mn\7g\2\2no\7v\2\2o\16\3\2")
        buf.write("\2\2pq\7y\2\2qr\7k\2\2rs\7p\2\2st\7?\2\2t\20\3\2\2\2u")
        buf.write("v\7?\2\2v\22\3\2\2\2wx\7\61\2\2x\24\3\2\2\2y}\7%\2\2z")
        buf.write("|\n\2\2\2{z\3\2\2\2|\177\3\2\2\2}{\3\2\2\2}~\3\2\2\2~")
        buf.write("\u0080\3\2\2\2\177}\3\2\2\2\u0080\u0081\b\13\2\2\u0081")
        buf.write("\26\3\2\2\2\u0082\u0083\6\f\2\2\u0083\u008f\5%\23\2\u0084")
        buf.write("\u0086\7\17\2\2\u0085\u0084\3\2\2\2\u0085\u0086\3\2\2")
        buf.write("\2\u0086\u0087\3\2\2\2\u0087\u008a\7\f\2\2\u0088\u008a")
        buf.write("\4\16\17\2\u0089\u0085\3\2\2\2\u0089\u0088\3\2\2\2\u008a")
        buf.write("\u008c\3\2\2\2\u008b\u008d\5%\23\2\u008c\u008b\3\2\2\2")
        buf.write("\u008c\u008d\3\2\2\2\u008d\u008f\3\2\2\2\u008e\u0082\3")
        buf.write("\2\2\2\u008e\u0089\3\2\2\2\u008f\u0090\3\2\2\2\u0090\u0091")
        buf.write("\b\f\3\2\u0091\30\3\2\2\2\u0092\u0094\t\3\2\2\u0093\u0092")
        buf.write("\3\2\2\2\u0094\u0095\3\2\2\2\u0095\u0093\3\2\2\2\u0095")
        buf.write("\u0096\3\2\2\2\u0096\u0097\3\2\2\2\u0097\u0098\b\r\2\2")
        buf.write("\u0098\32\3\2\2\2\u0099\u009a\t\4\2\2\u009a\u009b\t\5")
        buf.write("\2\2\u009b\u009c\t\6\2\2\u009c\u009d\t\7\2\2\u009d\34")
        buf.write("\3\2\2\2\u009e\u009f\t\b\2\2\u009f\u00a0\t\t\2\2\u00a0")
        buf.write("\u00a1\t\n\2\2\u00a1\u00a2\t\13\2\2\u00a2\u00a3\t\7\2")
        buf.write("\2\u00a3\36\3\2\2\2\u00a4\u00aa\5)\25\2\u00a5\u00a9\5")
        buf.write(")\25\2\u00a6\u00a9\5+\26\2\u00a7\u00a9\5-\27\2\u00a8\u00a5")
        buf.write("\3\2\2\2\u00a8\u00a6\3\2\2\2\u00a8\u00a7\3\2\2\2\u00a9")
        buf.write("\u00ac\3\2\2\2\u00aa\u00a8\3\2\2\2\u00aa\u00ab\3\2\2\2")
        buf.write("\u00ab\u00af\3\2\2\2\u00ac\u00aa\3\2\2\2\u00ad\u00af\5")
        buf.write("-\27\2\u00ae\u00a4\3\2\2\2\u00ae\u00ad\3\2\2\2\u00af ")
        buf.write("\3\2\2\2\u00b0\u00b2\5\31\r\2\u00b1\u00b0\3\2\2\2\u00b2")
        buf.write("\u00b5\3\2\2\2\u00b3\u00b1\3\2\2\2\u00b3\u00b4\3\2\2\2")
        buf.write("\u00b4\u00b6\3\2\2\2\u00b5\u00b3\3\2\2\2\u00b6\u00ba\5")
        buf.write("\'\24\2\u00b7\u00b9\5\31\r\2\u00b8\u00b7\3\2\2\2\u00b9")
        buf.write("\u00bc\3\2\2\2\u00ba\u00b8\3\2\2\2\u00ba\u00bb\3\2\2\2")
        buf.write("\u00bb\u00bd\3\2\2\2\u00bc\u00ba\3\2\2\2\u00bd\u00c1\7")
        buf.write("@\2\2\u00be\u00c0\5\31\r\2\u00bf\u00be\3\2\2\2\u00c0\u00c3")
        buf.write("\3\2\2\2\u00c1\u00bf\3\2\2\2\u00c1\u00c2\3\2\2\2\u00c2")
        buf.write("\u00cb\3\2\2\2\u00c3\u00c1\3\2\2\2\u00c4\u00c8\5\37\20")
        buf.write("\2\u00c5\u00c7\5\31\r\2\u00c6\u00c5\3\2\2\2\u00c7\u00ca")
        buf.write("\3\2\2\2\u00c8\u00c6\3\2\2\2\u00c8\u00c9\3\2\2\2\u00c9")
        buf.write("\u00cc\3\2\2\2\u00ca\u00c8\3\2\2\2\u00cb\u00c4\3\2\2\2")
        buf.write("\u00cc\u00cd\3\2\2\2\u00cd\u00cb\3\2\2\2\u00cd\u00ce\3")
        buf.write("\2\2\2\u00ce\"\3\2\2\2\u00cf\u00d0\13\2\2\2\u00d0$\3\2")
        buf.write("\2\2\u00d1\u00d3\t\3\2\2\u00d2\u00d1\3\2\2\2\u00d3\u00d4")
        buf.write("\3\2\2\2\u00d4\u00d2\3\2\2\2\u00d4\u00d5\3\2\2\2\u00d5")
        buf.write("&\3\2\2\2\u00d6\u00d7\n\f\2\2\u00d7(\3\2\2\2\u00d8\u00d9")
        buf.write("\t\r\2\2\u00d9*\3\2\2\2\u00da\u00db\7a\2\2\u00db,\3\2")
        buf.write("\2\2\u00dc\u00de\7/\2\2\u00dd\u00dc\3\2\2\2\u00dd\u00de")
        buf.write("\3\2\2\2\u00de\u00e0\3\2\2\2\u00df\u00e1\t\16\2\2\u00e0")
        buf.write("\u00df\3\2\2\2\u00e1\u00e2\3\2\2\2\u00e2\u00e0\3\2\2\2")
        buf.write("\u00e2\u00e3\3\2\2\2\u00e3\u00ea\3\2\2\2\u00e4\u00e6\t")
        buf.write("\17\2\2\u00e5\u00e7\t\16\2\2\u00e6\u00e5\3\2\2\2\u00e7")
        buf.write("\u00e8\3\2\2\2\u00e8\u00e6\3\2\2\2\u00e8\u00e9\3\2\2\2")
        buf.write("\u00e9\u00eb\3\2\2\2\u00ea\u00e4\3\2\2\2\u00ea\u00eb\3")
        buf.write("\2\2\2\u00eb.\3\2\2\2\26\2}\u0085\u0089\u008c\u008e\u0095")
        buf.write("\u00a8\u00aa\u00ae\u00b3\u00ba\u00c1\u00c8\u00cd\u00d4")
        buf.write("\u00dd\u00e2\u00e8\u00ea\4\b\2\2\3\f\2")
        return buf.getvalue()


class VgdlLexer(Lexer):

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

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

    T__0 = 1
    T__1 = 2
    T__2 = 3
    T__3 = 4
    T__4 = 5
    T__5 = 6
    T__6 = 7
    T__7 = 8
    T__8 = 9
    COMMENT = 10
    NEWLINE = 11
    WS = 12
    TRUE = 13
    FALSE = 14
    WORD = 15
    LEVELDEFINITION = 16
    ANY = 17

    channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ]

    modeNames = [ "DEFAULT_MODE" ]

    literalNames = [ "<INVALID>",
            "'BasicGame'", "'SpriteSet'", "'>'", "'LevelMapping'", "'InteractionSet'", 
            "'TerminationSet'", "'win='", "'='", "'/'" ]

    symbolicNames = [ "<INVALID>",
            "COMMENT", "NEWLINE", "WS", "TRUE", "FALSE", "WORD", "LEVELDEFINITION", 
            "ANY" ]

    ruleNames = [ "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", 
                  "T__7", "T__8", "COMMENT", "NEWLINE", "WS", "TRUE", "FALSE", 
                  "WORD", "LEVELDEFINITION", "ANY", "SPACES", "SYMBOL", 
                  "CHAR", "UNDERSCORE", "NUMBER" ]

    grammarFileName = "Vgdl.g4"

    def __init__(self, input=None, output:TextIO = sys.stdout):
        super().__init__(input, output)
        self.checkVersion("4.7.2")
        self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
        self._actions = None
        self._predicates = None



        # A queue where extra tokens are pushed on (see the NEWLINE lexer rule).
        self.tokens = []

        # The stack that keeps track of the indentation level.
        self.indents = []

        # The amount of opened braces, brackets and parenthesis.
        self.opened = 0

        # The most recently produced token.
        self.last_token = None

    def emitToken(self, t):
        super().emitToken(t)
        self.tokens.append(t)

    def nextToken(self):
        if self._input.LA(1) == Token.EOF and len(self.indents) > 0:
            # Remove any trailing EOF tokens from our buffer.
            while len(self.tokens) > 0 and self.tokens[-1].type == Token.EOF:
                del self.tokens[-1]

            # First emit an extra line break that serves as the end of the statement.
            self.emitToken(self.common_token(VgdlLexer.NEWLINE, "\n"));

            # Now emit as much DEDENT tokens as needed.
            while len(self.indents) != 0:
                self.emitToken(self.create_dedent())
                del self.indents[-1]

            # Put the EOF back on the token stream.
            self.emitToken(self.common_token(Token.EOF, "<EOF>"));

        next = super().nextToken();

        if next.channel == Token.DEFAULT_CHANNEL:
            # Keep track of the last token on the default channel.
            self.last_token = next

        if len(self.tokens) == 0:
            return next
        else:
            t = self.tokens[0]
            del self.tokens[0]
            return t

    def create_dedent(self):
        from grammar.VgdlParser import VgdlParser
        dedent = self.common_token(VgdlParser.DEDENT, "")
        dedent.line = self.last_token.line
        return dedent

    def common_token(self, _type,  text):
        from antlr4.Token import CommonToken
        stop = self.getCharIndex() - 1
        if len(self.text) == 0:
            start = stop
        else:
            start = stop - len(self.text) + 1
        return CommonToken(self._tokenFactorySourcePair, _type, Lexer.DEFAULT_TOKEN_CHANNEL, start, stop)

    ## Calculates the indentation of the provided spaces, taking the
    ## following rules into account:
    ##
    ## "Tabs are replaced (from left to right) by one to eight spaces
    ##  such that the total number of characters up to and including
    ##  the replacement is a multiple of eight [...]"
    ##
    ##  -- https://docs.python.org/3.1/reference/lexical_analysis.html#indentation
    def getIndentationCount(self, spaces):
        count = 0
        for ch in spaces:
            if ch == '\t':
                count += 8 - (count % 8)
            else:
                count += 1
        return count

    def atStartOfInput(self):
        return self._interp.column == 0 and self._interp.line == 1



    def action(self, localctx:RuleContext, ruleIndex:int, actionIndex:int):
        if self._actions is None:
            actions = dict()
            actions[10] = self.NEWLINE_action 
            self._actions = actions
        action = self._actions.get(ruleIndex, None)
        if action is not None:
            action(localctx, actionIndex)
        else:
            raise Exception("No registered action for:" + str(ruleIndex))


    def NEWLINE_action(self, localctx:RuleContext , actionIndex:int):
        if actionIndex == 0:

                import re
                from grammar.VgdlParser import VgdlParser
                new_line = re.sub(r"[^\r\n\f]+", "", self._interp.getText(self._input)) #.replaceAll("[^\r\n\f]+", "")
                spaces = re.sub(r"[\r\n\f]+", "", self._interp.getText(self._input)) #.replaceAll("[\r\n\f]+", "")
                next = self._input.LA(1)

                if next == VgdlParser.EOF:
                    chr_next = -1
                else:
                    chr_next = chr( next )

                if self.opened > 0 or chr_next == '\r' or chr_next == '\n' or chr_next == '\f' or chr_next == '#':
                    self.skip()
                else:
                    self.emitToken(self.common_token(self.NEWLINE, new_line))

                    indent = self.getIndentationCount(spaces)
                    if len(self.indents) == 0:
                        previous = 0
                    else:
                        previous = self.indents[-1]

                    if indent == previous:
                        self.skip()
                    elif indent > previous:
                        self.indents.append(indent)
                        self.emitToken(self.common_token(VgdlParser.INDENT, spaces))
                    else:
                        while len(self.indents) > 0 and self.indents[-1] > indent:
                            self.emitToken(self.create_dedent())
                            del self.indents[-1]

               
     

    def sempred(self, localctx:RuleContext, ruleIndex:int, predIndex:int):
        if self._predicates is None:
            preds = dict()
            preds[10] = self.NEWLINE_sempred
            self._predicates = preds
        pred = self._predicates.get(ruleIndex, None)
        if pred is not None:
            return pred(localctx, predIndex)
        else:
            raise Exception("No registered predicate for:" + str(ruleIndex))

    def NEWLINE_sempred(self, localctx:RuleContext, predIndex:int):
            if predIndex == 0:
                return self.atStartOfInput()
         


