def stateWhitespace1(cls, parserState: ParserState): token = parserState.Token if isinstance(token, StringToken): if (token <= "body"): parserState.NewToken = BodyKeyword(token) parserState.NextState = PackageBody.NameBlock.stateBodyKeyword return else: parserState.NewToken = IdentifierToken(token) parserState.NextState = cls.statePackageName 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 TokenParserException("Expected package name (identifier).", token)
def stateWhitespace5(cls, parserState: ParserState): token = parserState.Token if isinstance(token, WordToken): parserState.NewToken = IdentifierToken(token) parserState.NextState = cls.stateSeverityLevel 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 BlockParserException( "Expected severity level (expression) after keyword SEVERITY.", token)
def stateTypeMarkName(cls, parserState: ParserState): token = parserState.Token errorMessage = "Expected ':=' or whitespace after type mark." if isinstance(token, CharacterToken): if (token == ":"): parserState.NewToken = BoundaryToken(token) parserState.NextState = cls.statePossibleVariableAssignment return elif (token == "\n"): parserState.NewBlock = TypeBlock(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True) parserState.NewToken = LinebreakToken(token) _ = LinebreakBlock(parserState.NewBlock, parserState.NewToken) parserState.TokenMarker = None parserState.NextState = cls.stateWhitespace4 parserState.PushState = LinebreakBlock.stateLinebreak return elif (token == "-"): parserState.NewBlock = TypeBlock(parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True) parserState.TokenMarker = None parserState.NextState = cls.stateWhitespace4 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.NextState = cls.stateWhitespace4 parserState.PushState = MultiLineCommentBlock.statePossibleCommentStart parserState.TokenMarker = token return elif isinstance(token, SpaceToken): parserState.NextState = cls.stateWhitespace4 return raise TokenParserException(errorMessage, token)
def stateAttributeDeclarationKeyword(cls, parserState: ParserState): token = parserState.Token errorMessage = "Expected whitespace after keyword ATTRIBUTE." if isinstance(token, CharacterToken): if (token == "\n"): parserState.NewBlock = AttributeDeclarationBlock( parserState.LastBlock, parserState.TokenMarker, endToken=token.PreviousToken, multiPart=True) parserState.NewToken = LinebreakToken(token) _ = LinebreakBlock(parserState.NewBlock, parserState.NewToken) parserState.TokenMarker = None parserState.NextState = cls.stateWhitespace1 parserState.PushState = LinebreakBlock.stateLinebreak return elif (token == "-"): parserState.NewBlock = AttributeDeclarationBlock( 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 = AttributeDeclarationBlock( 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.NewToken = BoundaryToken(token) parserState.NextState = cls.stateWhitespace1 return raise TokenParserException(errorMessage, token)
def stateAfterSensitivityList(cls, parserState: ParserState): token = parserState.Token if isinstance(token, StringToken): tokenValue = token.Value.lower() for keyword in OpenBlock2.KEYWORDS: if (tokenValue == keyword.__KEYWORD__): newToken = keyword(token) parserState.NextState = DeclarativeRegion.stateDeclarativeRegion parserState.PushState = OpenBlock2.KEYWORDS[keyword] parserState.NewToken = newToken parserState.TokenMarker = newToken return if (tokenValue == "begin"): parserState.NewToken = BeginKeyword(token) parserState.NewBlock = BeginBlock(parserState.LastBlock, parserState.NewToken) parserState.TokenMarker = None parserState.NextState = BeginBlock.stateSequentialRegion return elif (tokenValue == "is"): parserState.NewToken = IsKeyword(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, parserState.NewToken) parserState.TokenMarker = None parserState.NextState = DeclarativeRegion.stateDeclarativeRegion return elif isinstance(token, SpaceToken): parserState.NewToken = BoundaryToken(token) parserState.TokenMarker = parserState.NewToken parserState.NextState = cls.stateWhitespace1 return elif isinstance(token, (LinebreakToken, CommentToken)): block = LinebreakBlock if isinstance( token, LinebreakToken) else CommentBlock parserState.NewBlock = block(parserState.LastBlock, token) parserState.TokenMarker = None parserState.NextState = cls.stateWhitespace1 return raise TokenParserException("Expected whitespace after keyword ENTITY.", token)
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) parserState.TokenMarker = None return elif isinstance(token, LinebreakToken): 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, 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 == "begin"): parserState.NewToken = BeginKeyword(token) parserState.NewBlock = cls.BEGIN_BLOCK(parserState.LastBlock, parserState.NewToken) parserState.NextState = cls.BEGIN_BLOCK.stateStatementRegion return elif (tokenValue == "end"): parserState.NewToken = EndKeyword(token) parserState.NextState = cls.END_BLOCK.stateEndKeyword return raise BlockParserException( "Expected one of these keywords: BEGIN, END, {keywords}. Found: '{tokenValue}'.".format( keywords=", ".join( [kw.__KEYWORD__.upper() for kw in cls.KEYWORDS] ), tokenValue=token.Value ), token)
def stateWhitespace2(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, WordToken) and (token <= "report")): parserState.NewToken = ReportKeyword(token) parserState.NextState = cls.stateReportKeyword 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 BlockParserException("Expected keyword REPORT after assertion.", token)
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)
def stateWhitespace1(cls, parserState: ParserState): token = parserState.Token if isinstance(token, StringToken): parserState.NewToken = IdentifierToken(token) parserState.NextState = cls.stateObjectName return elif isinstance(token, ExtendedIdentifier): parserState.NextState = cls.stateObjectName 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, 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 interface {0} name (identifier).".format( cls.OBJECT_KIND), token)
def stateWhitespace3(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, CharacterToken) and (token == ".")): parserState.NewToken = DelimiterToken(token) parserState.NextState = cls.stateDot2 return elif isinstance(token, WordToken): parserState.NewToken = IdentifierToken(token) parserState.TokenMarker = parserState.NewToken parserState.NextState = cls.stateDot2 return elif isinstance(token, ExtendedIdentifier): parserState.NextState = cls.stateDot2 return elif isinstance(token, LinebreakToken): parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=token, multiPart=True) 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 BlockParserException("Expected '.'.", token)
def stateProcedureName(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, SpaceToken): parserState.NewToken = BoundaryToken(token) parserState.NextState = cls.stateWhitespace2 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.stateWhitespace2 return raise BlockParserException( "Expected ';', '(' or whitespace after procedure name.", token)
def stateWhitespace1(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, StringToken): if (token <= "when"): parserState.NewToken = WhenKeyword(token) parserState.NextState = cls.stateWhenKeyword return else: parserState.NewToken = IdentifierToken(token) # parserState.TokenMarker = parserState.NewToken parserState.NextState = cls.stateNextLoopLabel return elif isinstance(token, ExtendedIdentifier): parserState.NextState = cls.stateNextLoopLabel 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, 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 ';', keyword WHEN or loop label.", token)
def stateWhitespace2(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, StringToken) and (token <= "is")): parserState.NewToken = IsKeyword(token) parserState.NewBlock = cls(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) parserState.TokenMarker = None parserState.NextState = DeclarativeRegion.stateDeclarativeRegion 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 TokenParserException("Expected keyword IS after package name.", token)
def stateSequentialRegion(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, StringToken) and (token <= "when")): newToken = WhenKeyword(token) parserState.NewToken = newToken parserState.TokenMarker = newToken parserState.NextState = WhenBlock.stateWhenKeyword return super().stateSequentialRegion(parserState)
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 TokenParserException(errorMessage, token)
def stateWhitespace4(cls, parserState: ParserState): token = parserState.Token if isinstance(token, StringToken): parserState.NewToken = IdentifierToken(token) parserState.NextState = cls.stateSubtypeIndication return elif isinstance(token, ExtendedIdentifier): parserState.NextState = cls.stateSubtypeIndication 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, 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 subtype indication (name).", token)
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)
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, WordToken): 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 = EndBlock.stateEndKeyword else: raise BlockParserException(errorMessage, token) parserState.NewToken = newToken parserState.TokenMarker = newToken return raise BlockParserException(errorMessage, token)
def stateOpeningParenthesis(cls, parserState: ParserState): token = parserState.Token errorMessage = "Expected generic name (identifier)." if isinstance(token, CharacterToken): if (token == ")"): # if (parserState.TokenMarker != token): # parserState.NewBlock = IndentationBlock(parserState.LastBlock, parserState.TokenMarker, token.PreviousToken) parserState.Pop() parserState.TokenMarker = token return elif (token == "\n"): parserState.NewToken = LinebreakToken(token) parserState.NewBlock = LinebreakBlock(parserState.LastBlock, parserState.NewToken) parserState.TokenMarker = None parserState.PushState = LinebreakBlock.stateLinebreak return elif (token == "-"): parserState.TokenMarker = None parserState.PushState = SingleLineCommentBlock.statePossibleCommentStart parserState.TokenMarker = token return elif (token == "/"): parserState.TokenMarker = None 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): parserState.NewToken = IdentifierToken(token) parserState.TokenMarker = parserState.NewToken parserState.NextState = ItemBlock.stateItemRemainder # if (parserState.TokenMarker != token): # parserState.NewBlock = IndentationBlock(parserState.LastBlock, parserState.TokenMarker, token) return raise TokenParserException(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 stateSequentialRegion(cls, parserState: ParserState): token = parserState.Token if isinstance(token, WordToken): tokenValue = token.Value.lower() if (tokenValue == "elsif"): newToken = ElsIfKeyword(token) parserState.NewToken = newToken parserState.TokenMarker = newToken parserState.NextState = ElsIfConditionBlock.stateElsIfKeyword return elif (tokenValue == "else"): newToken = ElseKeyword(token) parserState.NewToken = newToken parserState.NewBlock = ElseBlock(parserState.LastBlock, newToken) parserState.TokenMarker = None parserState.NextState = ElseBlock.stateElseKeyword return super().stateSequentialRegion(parserState)
def statePossibleCommentStart(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, CharacterToken) and (token == "*")): parserState.NewToken = MultiLineCommentStartKeyword(parserState.TokenMarker) parserState.TokenMarker = parserState.NewToken parserState.NextState = cls.stateConsumeComment return else: parserState.Pop() # print(" {DARK_GREEN}re-issue: {GREEN}{state!s} {DARK_GREEN}token={GREEN}{token}{NOCOLOR}".format(state=parserState, token=parserState.Token, **Console.Foreground)) parserState.NextState(parserState)
def stateAfterParameterList(cls, parserState: ParserState): token = parserState.Token if isinstance(token, StringToken): if (token <= "return"): parserState.NewToken = ReturnKeyword(token) # parserState.NewBlock = NameBlock2(parserState.LastBlock, parserState.TokenMarker, parserState.NewToken) parserState.NextState = cls.stateReturnKeyword 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 = block(parserState.LastBlock, token) parserState.TokenMarker = None parserState.NextState = cls.stateWhitespace1 return raise TokenParserException("Expected keyword RETURN.", token)
def statePossibleVariableAssignment(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, CharacterToken) and (token == "=")): parserState.NewToken = VariableAssignmentKeyword( parserState.TokenMarker) parserState.TokenMarker = parserState.NewToken parserState.NextState = cls.stateVariableAssignment return raise NotImplementedError( "State=PossibleCommentStart: {0!r}".format(token))
def stateReturnKeyword(cls, parserState: ParserState): token = parserState.Token errorMessage = "Expected whitespace after keyword RETURN." if isinstance(token, SpaceToken): parserState.NewToken = BoundaryToken(token) parserState.NextState = cls.stateWhitespace2 return elif isinstance(token, SpaceToken): parserState.NewToken = BoundaryToken(token) parserState.NextState = cls.stateWhitespace2 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.stateWhitespace2 return raise TokenParserException(errorMessage, token)
def statePossibleCommentEnd(cls, parserState: ParserState): token = parserState.Token if (isinstance(token, CharacterToken) and (token == "/")): parserState.NewToken = MultiLineCommentEndKeyword(parserState.TokenMarker) parserState.Pop() parserState.NewBlock = MultiLineCommentBlock(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken) parserState.Pop() return else: parserState.Pop() parserState.NextState(parserState)
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 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)
def stateWhitespace2(cls, parserState: ParserState): token = parserState.Token errorMessage = "Expected type name (identifier)." if isinstance(token, CharacterToken): if (token == ":"): parserState.NewToken = BoundaryToken(token) parserState.NextState = cls.stateColon1 return if (token == "\n"): parserState.NewToken = LinebreakToken(token) if (not isinstance(parserState.LastBlock, MultiLineCommentBlock)): parserState.NewBlock = TypeBlock(parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken, multiPart=True) _ = LinebreakBlock(parserState.NewBlock, parserState.NewToken) else: parserState.NewBlock = LinebreakBlock(parserState.LastBlock, parserState.NewToken) parserState.TokenMarker = None parserState.PushState = LinebreakBlock.stateLinebreak 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 elif isinstance(token, WordToken): parserState.NewToken = IdentifierToken(token) parserState.NextState = cls.stateColon1() return elif (isinstance(token, SpaceToken) and isinstance(parserState.LastBlock, MultiLineCommentBlock)): parserState.NewToken = BoundaryToken(token) parserState.NewBlock = WhitespaceBlock(parserState.LastBlock, parserState.NewToken) parserState.TokenMarker = None return raise TokenParserException(errorMessage, token)
def stateTypeName(cls, parserState: ParserState): token = parserState.Token if isinstance(token, CharacterToken): if (token == ';'): parserState.NewToken = DelimiterToken(token) parserState.NewBlock = cls( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) _ = cls.DELIMITER_BLOCK(parserState.NewBlock, parserState.NewToken) parserState.Pop() return elif (token == ')'): parserState.NewToken = BoundaryToken(token) parserState.NewBlock = cls( parserState.LastBlock, parserState.TokenMarker, endToken=parserState.NewToken.PreviousToken) parserState.Pop(2) parserState.TokenMarker = parserState.NewToken return elif isinstance(token, SpaceToken): parserState.NewToken = BoundaryToken(token) parserState.NextState = cls.stateWhitespace2 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.stateWhitespace2 return raise TokenParserException( "Expected ';', ')' or whitespace after interface type name.", token)