Beispiel #1
0
	def stateDeclarativeRegion(cls, parserState: ParserState):
		token = parserState.Token
		if isinstance(token, SpaceToken):
			blockType =                 IndentationBlock if isinstance(token, IndentationToken) else WhitespaceBlock
			parserState.NewBlock =      blockType(parserState.LastBlock, token)
			return
		elif isinstance(token, (LinebreakToken, CommentToken)):
			block =                     LinebreakBlock if isinstance(token, LinebreakToken) else CommentBlock
			parserState.NewBlock =      block(parserState.LastBlock, token)
			parserState.TokenMarker =   None
			return
		elif isinstance(token, WordToken):
			tokenValue = token.Value.lower()
			for keyword in cls.KEYWORDS:
				if (tokenValue == keyword.__KEYWORD__):
					newToken =                keyword(token)
					parserState.PushState =   cls.KEYWORDS[keyword]
					parserState.NewToken =    newToken
					parserState.TokenMarker = newToken
					return

			if (tokenValue == "end"):
				parserState.NewToken =    EndKeyword(token)
				parserState.TokenMarker = parserState.NewToken
				parserState.NextState =   EndBlock.stateEndKeyword
				return

		raise BlockParserException(
			"Expected one of these keywords: END, {keywords}. Found: '{tokenValue}'.".format(
				keywords=", ".join(
					[kw.__KEYWORD__.upper() for kw in cls.KEYWORDS]
				),
				tokenValue=token.Value
			), token)
Beispiel #2
0
    def stateWhitespace1(cls, parserState: ParserState):
        token = parserState.Token
        if isinstance(token, StringToken):
            tokenValue = token.Value.lower()
            if (tokenValue == "when"):
                newToken = WhenKeyword(token)
                parserState.NewToken = newToken
                parserState.TokenMarker = newToken
                parserState.NextState = WhenBlock.stateWhenKeyword
                return
            elif (tokenValue == "end"):
                parserState.NewToken = EndKeyword(token)
                parserState.NextState = cls.END_BLOCK.stateEndKeyword
                return
        elif isinstance(token, LinebreakToken):
            if (not (isinstance(parserState.LastBlock, CommentBlock)
                     and isinstance(parserState.LastBlock.StartToken,
                                    MultiLineCommentToken))):
                parserState.NewBlock = cls(parserState.LastBlock,
                                           parserState.TokenMarker,
                                           endToken=token.PreviousToken,
                                           multiPart=True)
                _ = LinebreakBlock(parserState.NewBlock, token)
            else:
                parserState.NewBlock = LinebreakBlock(parserState.LastBlock,
                                                      token)
            parserState.TokenMarker = None
            return
        elif isinstance(token, CommentToken):
            parserState.NewBlock = cls(parserState.LastBlock,
                                       parserState.TokenMarker,
                                       endToken=token.PreviousToken,
                                       multiPart=True)
            _ = CommentBlock(parserState.NewBlock, token)
            parserState.TokenMarker = None
            return
        elif (isinstance(token, IndentationToken)
              and isinstance(token.PreviousToken,
                             (LinebreakToken, SingleLineCommentToken))):
            return
        elif (isinstance(token, SpaceToken) and
              (isinstance(parserState.LastBlock, CommentBlock) and isinstance(
                  parserState.LastBlock.StartToken, MultiLineCommentToken))):
            parserState.NewToken = BoundaryToken(token)
            parserState.NewBlock = WhitespaceBlock(parserState.LastBlock,
                                                   parserState.NewToken)
            parserState.TokenMarker = None
            return

        raise TokenParserException(
            "Expected one of these keywords: WHEN or END. Found: '{tokenValue}'."
            .format(tokenValue=token.Value), token)
Beispiel #3
0
    def stateDeclarativeRegion(cls, parserState: ParserState):
        errorMessage = "Expected one of these keywords: generic, port, begin, end."
        token = parserState.Token
        if isinstance(parserState.Token, CharacterToken):
            if (token == "\n"):
                parserState.NewToken = LinebreakToken(token)
                parserState.NewBlock = LinebreakBlock(parserState.LastBlock,
                                                      parserState.NewToken)
                parserState.TokenMarker = parserState.NewToken
                return
            elif (token == "-"):
                parserState.PushState = SingleLineCommentBlock.statePossibleCommentStart
                parserState.TokenMarker = token
                return
            elif (token == "/"):
                parserState.PushState = MultiLineCommentBlock.statePossibleCommentStart
                parserState.TokenMarker = token
                return
        elif isinstance(token, SpaceToken):
            parserState.NewToken = IndentationToken(token)
            parserState.NewBlock = IndentationBlock(parserState.LastBlock,
                                                    parserState.NewToken)
            return
        elif isinstance(token, StringToken):
            keyword = token.Value.lower()
            if (keyword == "generic"):
                newToken = GenericKeyword(token)
                parserState.PushState = GenericList.OpenBlock.stateGenericKeyword
            elif (keyword == "port"):
                newToken = PortKeyword(token)
                parserState.PushState = PortList.OpenBlock.statePortKeyword
            elif (keyword == "end"):
                newToken = EndKeyword(token)
                parserState.NextState = EndGenerateBlock.stateEndKeyword
            elif (keyword == "begin"):
                parserState.NewToken = BeginKeyword(token)
                parserState.NewBlock = ElseGenerateBeginBlock(
                    parserState.LastBlock, parserState.NewToken)
                parserState.NextState = ElseGenerateBeginBlock.stateBeginKeyword
                return
            else:
                raise TokenParserException(errorMessage, token)

            parserState.NewToken = newToken
            parserState.TokenMarker = newToken
            return

        raise TokenParserException(errorMessage, token)
Beispiel #4
0
    def stateBeginKeyword(cls, parserState: ParserState):
        token = parserState.Token
        errorMessage = "Expected label or one of these keywords: assert, process."
        if isinstance(token, CharacterToken):
            if (token == "\n"):
                parserState.NewToken = LinebreakToken(token)
                parserState.NewBlock = LinebreakBlock(parserState.LastBlock,
                                                      parserState.NewToken)
                parserState.TokenMarker = parserState.NewToken
                return
            elif (token == "-"):
                parserState.PushState = SingleLineCommentBlock.statePossibleCommentStart
                parserState.TokenMarker = token
                return
            elif (token == "/"):
                parserState.PushState = MultiLineCommentBlock.statePossibleCommentStart
                parserState.TokenMarker = token
                return
        elif isinstance(token, SpaceToken):
            return
        # 	parserState.NewToken = IndentationToken(token)
        # 	parserState.NewBlock = IndentationBlock(parserState.LastBlock, parserState.NewToken)
        # 	return
        elif isinstance(token, StringToken):
            keyword = token.Value.lower()
            if (keyword == "process"):
                newToken = ProcessKeyword(token)
                parserState.PushState = Process.OpenBlock.stateProcessKeyword
            elif (keyword == "assert"):
                newToken = AssertKeyword(token)
                parserState.PushState = AssertBlock.stateAssertKeyword
            elif (keyword == "end"):
                newToken = EndKeyword(token)
                parserState.NextState = EndGenerateBlock.stateEndKeyword
            else:
                raise TokenParserException(errorMessage, token)

            parserState.NewToken = newToken
            parserState.TokenMarker = newToken
            return

        raise TokenParserException(errorMessage, token)