Ejemplo n.º 1
0
 def stateItemRemainder(cls, parserState: ParserState):
     token = parserState.Token
     if isinstance(token, CharacterToken):
         if (token == "("):
             parserState.NewToken = OpeningRoundBracketToken(token)
             parserState.Counter += 1
         elif (token == ")"):
             parserState.Counter -= 1
             if (parserState.Counter == 0):
                 parserState.NewToken = BoundaryToken(token)
                 parserState.NewBlock = ItemBlock(
                     parserState.LastBlock,
                     parserState.TokenMarker,
                     endToken=parserState.NewToken.PreviousToken)
                 parserState.Pop()
                 parserState.TokenMarker = parserState.NewToken
             else:
                 parserState.NewToken = ClosingRoundBracketToken(token)
         elif (token == ";"):
             if (parserState.Counter == 1):
                 parserState.NewToken = DelimiterToken(token)
                 parserState.NewBlock = ItemBlock(
                     parserState.LastBlock,
                     parserState.TokenMarker,
                     endToken=parserState.NewToken.PreviousToken)
                 _ = DelimiterBlock(parserState.NewBlock,
                                    parserState.NewToken)
                 parserState.NextState = DelimiterBlock.stateItemDelimiter
             else:
                 raise TokenParserException(
                     "Mismatch in opening and closing parenthesis: open={0}"
                     .format(parserState.Counter), token)
Ejemplo n.º 2
0
    def stateExpression(cls, parserState: ParserState):
        token = parserState.Token
        if isinstance(token, FusedCharacterToken):
            parserState.NewToken = cls.FUSED_CHARACTER_TRANSLATION[
                token.Value](token)
            return
        elif isinstance(token, CharacterToken):
            if (token == "("):
                parserState.NewToken = OpeningRoundBracketToken(token)
                parserState.Counter += 1
                return
            elif (token == ")"):
                parserState.NewToken = ClosingRoundBracketToken(token)
                parserState.Counter -= 1
                return
            else:
                parserState.NewToken = cls.CHARACTER_TRANSLATION[token.Value](
                    token)
                return
        elif isinstance(token, StringToken):
            if (token <= cls.EXIT_KEYWORD.__KEYWORD__):
                parserState.NewToken = cls.EXIT_KEYWORD(token)
                parserState.NewBlock = cls(
                    parserState.LastBlock,
                    parserState.TokenMarker,
                    endToken=parserState.NewToken.PreviousToken)
                _ = cls.EXIT_BLOCK(parserState.NewBlock, parserState.NewToken)
                parserState.Pop()
                # parserState.TokenMarker = parserState.NewToken
                return
            else:
                try:
                    parserState.NewToken = cls.OPERATOR_TRANSLATIONS[
                        token.Value](token)
                except KeyError:
                    parserState.NewToken = IdentifierToken(token)
                return
        elif isinstance(token, LiteralToken):
            return
        elif isinstance(token, SpaceToken):
            parserState.NextState = cls.stateWhitespace1
            return
        elif isinstance(token, (LinebreakToken, CommentToken)):
            block = LinebreakBlock if isinstance(
                token, LinebreakToken) else CommentBlock
            parserState.NewBlock = cls(parserState.LastBlock,
                                       parserState.TokenMarker,
                                       endToken=token.PreviousToken,
                                       multiPart=True)
            _ = block(parserState.NewBlock, token)
            parserState.TokenMarker = None
            parserState.NextState = cls.stateWhitespace1
            return

        raise TokenParserException(
            "Expected '(' or whitespace after keyword GENERIC.", token)
Ejemplo n.º 3
0
	def stateExpression(cls, parserState: ParserState):
		token = parserState.Token
		if isinstance(token, FusedCharacterToken):
			parserState.NewToken = cls.FUSED_CHARACTER_TRANSLATION[token.Value](token)
			return
		elif isinstance(token, CharacterToken):
			if (token == ";"):
				if (parserState.Counter == 0):
					parserState.NewToken =  EndToken(token)
					parserState.NewBlock =  cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken)
					_ =                     cls.END_BLOCK(parserState.NewBlock, parserState.NewToken, endToken=parserState.NewToken)
					parserState.Pop(2)
					return
				else:
					raise BlockParserException("Mismatch in opening and closing parenthesis. Counter={0}".format(parserState.Counter), token)
			elif (token == "("):
				parserState.NewToken =    OpeningRoundBracketToken(token)
				parserState.Counter +=    1
				return
			elif (token == ")"):
				if (parserState.Counter == -1):
					raise BlockParserException("Mismatch in opening and closing parenthesis. Counter={0}".format(parserState.Counter), token)
				else:
					parserState.NewToken =  ClosingRoundBracketToken(token)
					parserState.Counter -=  1
					return
			else:
				parserState.NewToken =    cls.CHARACTER_TRANSLATION[token.Value](token)
				return
		elif isinstance(token, WordToken):
			try:
				parserState.NewToken =    cls.OPERATOR_TRANSLATIONS[token.Value](token)
			except KeyError:
				parserState.NewToken =    IdentifierToken(token)
			return
		elif isinstance(token, LiteralToken):
			return
		elif isinstance(token, SpaceToken):
			parserState.NextState =     cls.stateWhitespace1
			return
		elif isinstance(token, (LinebreakToken, CommentToken)):
			block =                     LinebreakBlock if isinstance(token, LinebreakToken) else CommentBlock
			parserState.NewBlock =      cls(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True)
			_ =                         block(parserState.NewBlock, token)
			parserState.TokenMarker =   None
			parserState.NextState =     cls.stateWhitespace1
			return

		raise BlockParserException("Expected operator, '(', ')', ';' or whitespace.", token)
Ejemplo n.º 4
0
    def stateBeforeExpression(cls, parserState: ParserState):
        token = parserState.Token
        if isinstance(token, CharacterToken):
            if (token == "("):
                parserState.NewToken = OpeningRoundBracketToken(token)
                parserState.Counter += 1
                parserState.NextState = cls.stateExpression
                return
            else:
                parserState.NewToken = cls.CHARACTER_TRANSLATION[token.Value](
                    token)
                parserState.NextState = cls.stateExpression
                return
        elif isinstance(token, StringToken):
            try:
                parserState.NewToken = cls.OPERATOR_TRANSLATIONS[token.Value](
                    token)
            except KeyError:
                parserState.NewToken = IdentifierToken(token)
            parserState.NextState = cls.stateExpression
            return
        elif isinstance(token, LiteralToken):
            parserState.NextState = cls.stateExpression
            return
        elif isinstance(token, LinebreakToken):
            parserState.NewBlock = LinebreakBlock(parserState.LastBlock, token)
            parserState.TokenMarker = None
            return
        elif isinstance(token, CommentToken):
            parserState.NewBlock = CommentBlock(parserState.NewBlock, token)
            parserState.TokenMarker = None
            return
        elif (isinstance(token, IndentationToken)
              and isinstance(token.PreviousToken,
                             (LinebreakToken, SingleLineCommentToken))):
            return
        elif isinstance(token, SpaceToken):
            parserState.NextState = cls.stateWhitespace1
            return

        raise TokenParserException(
            "Expected '(', unary operator, identifier, literal or whitespace.",
            token)
Ejemplo n.º 5
0
	def stateWhitespace1(cls, parserState: ParserState):
		token = parserState.Token
		if isinstance(token, FusedCharacterToken):
			parserState.NewToken =    cls.FUSED_CHARACTER_TRANSLATION[token.Value](token)
			parserState.NextState =   cls.stateExpression
			return
		elif isinstance(token, CharacterToken):
			if (token == "("):
				parserState.NewToken =    OpeningRoundBracketToken(token)
				parserState.Counter +=    1
				parserState.NextState =   cls.stateExpression
				return
			elif (token == ")"):
				parserState.NewToken =  ClosingRoundBracketToken(token)
				parserState.Counter -=  1
				parserState.NextState = cls.stateExpression
				return
			else:
				parserState.NewToken =    cls.CHARACTER_TRANSLATION[token.Value](token)
				parserState.NextState =   cls.stateExpression
				return
		elif isinstance(token, WordToken):
			tokenValue = token.Value.lower()
			if (tokenValue == cls.EXIT_KEYWORD.__KEYWORD__):
				parserState.NewToken =    LoopKeyword(token)
				parserState.NewBlock =    cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken)
				_ =                       cls.EXIT_BLOCK(parserState.NewBlock, parserState.NewToken)
				parserState.Pop(1, parserState.NewToken)
				return
			elif (tokenValue == "to"):
				from pyVHDLParser.Blocks.ControlStructure.ForLoop import LoopIterationDirectionBlock

				parserState.NewToken =    ToKeyword(token)
				parserState.NewBlock =    cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken)
				_ =                       LoopIterationDirectionBlock(parserState.NewBlock, parserState.NewToken)
				parserState.Pop()
				return
			elif (tokenValue == "downto"):
				from pyVHDLParser.Blocks.ControlStructure.ForLoop import LoopIterationDirectionBlock

				parserState.NewToken =    DowntoKeyword(token)
				parserState.NewBlock =    cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken)
				_ =                       LoopIterationDirectionBlock(parserState.NewBlock, parserState.NewToken)
				parserState.Pop()
				return
			else:
				try:
					parserState.NewToken =  cls.OPERATOR_TRANSLATIONS[token.Value](token)
				except KeyError:
					parserState.NewToken =  IdentifierToken(token)
				parserState.NextState =   cls.stateExpression
				return
		elif isinstance(token, LiteralToken):
			parserState.NextState =     cls.stateExpression
			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):
			if (not isinstance(parserState.LastBlock, LinebreakBlock)):
				parserState.NewBlock =    cls(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True)
				_ =                       CommentBlock(parserState.NewBlock, token)
			else:
				parserState.NewBlock =    CommentBlock(parserState.LastBlock, 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 BlockParserException("Expected ????????????.", token)
Ejemplo n.º 6
0
	def stateWhitespace1(cls, parserState: ParserState):
		token = parserState.Token
		if isinstance(token, FusedCharacterToken):
			try:
				parserState.NewToken =    cls.FUSED_CHARACTER_TRANSLATION[token.Value](token)
				parserState.NextState =   cls.stateExpression
				return
			except KeyError:
				if (token == cls.EXIT_CHAR):
					if (parserState.Counter == 0):
						parserState.NewToken =  cls.EXIT_TOKEN(token)
						parserState.NewBlock =  cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken)
						_ =                     cls.EXIT_BLOCK(parserState.NewBlock, parserState.NewToken, endToken=parserState.NewToken)
						parserState.Pop(1)
						return
					else:
						raise BlockParserException("Mismatch in opening and closing parenthesis. Counter={0}".format(parserState.Counter), token)
		elif isinstance(token, CharacterToken):
			if (token == cls.EXIT_CHAR):
				if (parserState.Counter == 0):
					parserState.NewToken =  cls.EXIT_TOKEN(token)
					parserState.NewBlock =  cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken)
					_ =                     cls.EXIT_BLOCK(parserState.NewBlock, parserState.NewToken, endToken=parserState.NewToken)
					parserState.Pop(2)
					return
				else:
					raise BlockParserException("Mismatch in opening and closing parenthesis. Counter={0}".format(parserState.Counter), token)
			elif (token == "("):
				parserState.NewToken =    OpeningRoundBracketToken(token)
				parserState.Counter +=    1
				parserState.NextState =   cls.stateExpression
				return
			elif (token == ")"):
				if (parserState.Counter == 0):
					parserState.NewToken =    BoundaryToken(token)
					parserState.NewBlock =    cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken)

					parserState.Pop(3, tokenMarker=parserState.NewToken)
					return
				else:
					parserState.NewToken =    ClosingRoundBracketToken(token)
					parserState.Counter -=    1
					parserState.NextState =   cls.stateExpression
					return
			else:
				parserState.NewToken =    cls.CHARACTER_TRANSLATION[token.Value](token)
				parserState.NextState =   cls.stateExpression
				return
		elif isinstance(token, WordToken):
			try:
				parserState.NewToken =    cls.OPERATOR_TRANSLATIONS[token.Value](token)
			except KeyError:
				parserState.NewToken =    IdentifierToken(token)
			parserState.NextState =     cls.stateExpression
			return
		elif isinstance(token, LiteralToken):
			parserState.NextState =     cls.stateExpression
			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):
			if (not isinstance(parserState.LastBlock, LinebreakBlock)):
				parserState.NewBlock =    cls(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True)
				_ =                       CommentBlock(parserState.NewBlock, token)
			else:
				parserState.NewBlock =    CommentBlock(parserState.LastBlock, 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 BlockParserException("Expected ????????????.", token)