def stateClosingParenthesis(cls, parserState: ParserState): token = parserState.Token errorMessage = "Expected ';' or whitespace." if isinstance(token, CharacterToken): if (token == ";"): parserState.NewToken = EndToken(token) parserState.NewBlock = CloseBlock( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) parserState.Pop() return elif (token == "\n"): parserState.NewToken = LinebreakToken(token) parserState.PushState = LinebreakBlock.stateLinebreak parserState.TokenMarker = parserState.NewToken return elif (token == "-"): parserState.NewBlock = CloseBlock(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True) parserState.TokenMarker = None parserState.NextState = cls.stateWhitespace1 parserState.PushState = SingleLineCommentBlock.statePossibleCommentStart parserState.TokenMarker = token return elif (token == "/"): parserState.NewBlock = CloseBlock(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True) parserState.TokenMarker = None parserState.NextState = cls.stateWhitespace1 parserState.PushState = MultiLineCommentBlock.statePossibleCommentStart parserState.TokenMarker = token return elif isinstance(token, SpaceToken): parserState.NextState = cls.stateWhitespace1 return raise BlockParserException(errorMessage, token)
def stateMessage(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, CharacterToken)and (token == ";")): parserState.NewToken = EndToken(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) parserState.Pop() return elif isinstance(token, SpaceToken): parserState.NewToken = BoundaryToken(token) parserState.NextState = cls.stateWhitespace4 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.stateWhitespace4 return raise TokenParserException("Expected whitespace after assertion.", token)
def stateObjectName(cls, parserState: ParserState): token = parserState.Token if isinstance(token, CharacterToken): if (token == ","): parserState.NewToken = DelimiterToken(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken) _ = DelimiterBlock(parserState.NewBlock, parserState.NewToken, endToken=parserState.NewToken) parserState.NextState = DelimiterBlock.stateDelimiter return elif (token == ";"): parserState.NewToken = EndToken(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken) _ = EndBlock(parserState.NewBlock, parserState.NewToken, endToken=parserState.NewToken) parserState.Pop() return elif isinstance(token, SpaceToken): # parserState.NewToken = WhitespaceBlock(parserState.LastBlock, token) parserState.NextState = cls.stateWhitespace5 return elif isinstance(token, LinebreakToken): parserState.NextState = cls.stateWhitespace5 return elif isinstance(token, CommentToken): parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True) _ = CommentBlock(parserState.NewBlock, token) parserState.TokenMarker = None parserState.NextState = cls.stateWhitespace5 return raise BlockParserException("Expected ',', ';' or whitespace.", token)
def stateSubtypeIndication(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, CharacterToken) and (token == ';')): parserState.NewToken = EndToken(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) _ = cls.END_BLOCK(parserState.NewBlock, parserState.NewToken) parserState.Pop() return elif isinstance(token, SpaceToken): parserState.NewToken = BoundaryToken(token) parserState.NextState = cls.stateWhitespace4 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.stateWhitespace4 return raise BlockParserException("Expected ';' or whitespace after subtype indication.", token)
def stateReturnKeyword(cls, parserState: ParserState): token = parserState.Token if isinstance(token, CharacterToken): if (token == ";"): parserState.NewToken = EndToken(token) parserState.NewBlock = EndBlock( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) parserState.Pop() return elif (token == "("): parserState.NewToken = BoundaryToken(token) parserState.NewBlock = cls( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) parserState.TokenMarker = parserState.NewToken parserState.NextState = EndBlock.stateError parserState.PushState = ReturnExpressionBlock.stateExpression return elif isinstance(token, SpaceToken): parserState.NewToken = BoundaryToken(token) 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 RETURN.", token)
def stateWhitespace4(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, CharacterToken) and (token == ";")): parserState.NewToken = EndToken(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) parserState.Pop() 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 = CommentBlock(parserState.LastBlock, token) parserState.TokenMarker = None 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 ';' after subtype indication.", token)
def stateExpressionEnd(cls, parserState: ParserState): token = parserState.Token errorMessage = "Expected ';'." if isinstance(token, CharacterToken): if (token == ";"): parserState.NewToken = EndToken(token) parserState.NewBlock = TypeBlock(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) parserState.Pop() return elif (token == "-"): parserState.NewBlock = TypeBlock(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True) parserState.TokenMarker = None parserState.PushState = SingleLineCommentBlock.statePossibleCommentStart parserState.TokenMarker = token return elif (token == "/"): parserState.NewBlock = TypeBlock(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True) parserState.TokenMarker = None parserState.PushState = MultiLineCommentBlock.statePossibleCommentStart parserState.TokenMarker = token return raise TokenParserException(errorMessage, token)
def stateLibraryName(cls, parserState: ParserState): token = parserState.Token if isinstance(token, CharacterToken): if (token == ","): parserState.NewToken = DelimiterToken(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken) _ = DelimiterBlock(parserState.NewBlock, parserState.NewToken) parserState.NextState = DelimiterBlock.stateDelimiter return elif (token == ";"): parserState.NewToken = EndToken(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken) _ = EndBlock(parserState.NewBlock, parserState.NewToken) parserState.Pop() return elif isinstance(token, SpaceToken): #parserState.NewToken = BoundaryToken(token) 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 ';' after library name.", token)
def stateWhitespace1(cls, parserState: ParserState): token = parserState.Token if isinstance(token, CharacterToken): if ((cls.KEYWORD_IS_OPTIONAL is True) and (token == ";")): parserState.NewToken = EndToken(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) parserState.Pop() return elif isinstance(token, StringToken): IS_SINGLE_KEYWORD = isinstance(cls.KEYWORD, tuple) KW = cls.KEYWORD[0] if IS_SINGLE_KEYWORD else cls.KEYWORD if (token <= KW.__KEYWORD__): parserState.NewToken = KW(token) parserState.NextState = cls.stateKeyword1 elif (cls.EXPECTED_NAME_KIND == "label"): parserState.NewToken = LabelToken(token) parserState.NextState = cls.stateSimpleName else: parserState.NewToken = IdentifierToken(token) parserState.NextState = cls.stateSimpleName 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 if (cls.KEYWORD_IS_OPTIONAL is True): if (cls.EXPECTED_NAME_KIND == "label"): errorMessage = "Expected ';', {0} keyword or {1} label.".format( cls.EXPECTED_NAME.upper(), cls.EXPECTED_NAME) else: errorMessage = "Expected ';', {0} keyword or {1} name.".format( cls.EXPECTED_NAME.upper(), cls.EXPECTED_NAME) else: if (cls.EXPECTED_NAME_KIND == "label"): errorMessage = "Expected {0} keyword or {1} label.".format( cls.EXPECTED_NAME.upper(), cls.EXPECTED_NAME) else: errorMessage = "Expected {0} keyword or {1} name.".format( cls.EXPECTED_NAME.upper(), cls.EXPECTED_NAME) raise TokenParserException(errorMessage, token)
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 == ";"): 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 parserState.NextState = cls.stateExpression 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 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 identifier, literal, operator, '(', ')' or ';'.", token)
def stateWhitespace2(cls, parserState: ParserState): token = parserState.Token if isinstance(token, CharacterToken): if (token == "("): parserState.NewToken = BoundaryToken(token) parserState.NewBlock = cls( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) _ = ParameterList.OpenBlock(parserState.NewBlock, parserState.NewToken) parserState.TokenMarker = None parserState.NextState = VoidBlock.stateAfterParameterList parserState.PushState = ParameterList.OpenBlock.stateOpeningParenthesis parserState.Counter = 1 return elif (token == ";"): parserState.NewToken = EndToken(token) parserState.NewBlock = cls( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) # .PreviousToken) # _ = EndBlock(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) parserState.Pop() return elif isinstance(token, WordToken): keyword = token.Value.lower() if (keyword == "is"): parserState.NewToken = IsKeyword(token) parserState.NewBlock = cls( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) _ = VoidBlock(parserState.NewBlock, parserState.NewToken) parserState.TokenMarker = parserState.NewToken parserState.NextState = VoidBlock.stateDeclarativeRegion return elif (keyword == "generic"): parserState.NewToken = GenericKeyword(token) parserState.NewBlock = cls( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) parserState.NextState = GenericList.OpenBlock.stateGenericKeyword parserState.TokenMarker = parserState.NewToken return elif (keyword == "parameter"): parserState.NewToken = ParameterKeyword(token) parserState.NewBlock = cls( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) parserState.NextState = ParameterList.OpenBlock.stateParameterKeyword parserState.TokenMarker = parserState.NewToken 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, 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 ';', '(' or keywords GENERIC, PARAMETER or RETURN after procedure name.", token)
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 TokenParserException( "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 TokenParserException( "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, StringToken): 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 operator, '(', ')', ';' or whitespace.", token)