def getScientificNotationToken(self, sourceCode): try: if self.getFloatNumberToken(sourceCode) != None: floatTK = self.getFloatNumberToken(sourceCode)[0] index = len(floatTK.lexeme) if sourceCode[index] == 'e' or sourceCode[index] == 'E': index = index + 1 if sourceCode[index] == "+" or sourceCode[index] == "-": index = index + 1 if self.getFloatNumberToken(sourceCode[index:]) != None: floatTK = self.getFloatNumberToken(sourceCode[index:])[0] totalIndex = index + len(floatTK.lexeme) tokenval = TokenVal.SCIENTIFIC_NOTATION.value tokentype = TokenType.SCIENTIFIC_NOTATION.value lexeme = sourceCode[0:totalIndex] tk = token(tokentype, tokenval, lexeme) slicedSourceCode = sourceCode[totalIndex:] return tk, slicedSourceCode elif self.getIntegerNumberToken(sourceCode[index:]) != None: intTK = self.getIntegerNumberToken(sourceCode[index:])[0] totalIndex = index + len(intTK.lexeme) tokenval = TokenVal.SCIENTIFIC_NOTATION.value tokentype = TokenType.SCIENTIFIC_NOTATION.value lexeme = sourceCode[0:totalIndex] tk = token(tokentype, tokenval, lexeme) slicedSourceCode = sourceCode[totalIndex:] return tk, slicedSourceCode elif self.getIntegerNumberToken(sourceCode) != None: intTK = self.getIntegerNumberToken(sourceCode)[0] index = len(intTK.lexeme) if sourceCode[index] == 'e' or sourceCode[index] == 'E': index = index + 1 if sourceCode[index] == "+" or sourceCode[index] == "-": index = index + 1 if self.getFloatNumberToken(sourceCode[index:]) != None: floatTK = self.getFloatNumberToken(sourceCode[index:])[0] totalIndex = index + len(floatTK.lexeme) tokenval = TokenVal.SCIENTIFIC_NOTATION.value tokentype = TokenType.SCIENTIFIC_NOTATION.value lexeme = sourceCode[0:totalIndex] tk = token(tokentype, tokenval, lexeme) slicedSourceCode = sourceCode[totalIndex:] return tk, slicedSourceCode elif self.getIntegerNumberToken(sourceCode[index:]) != None: intTK = self.getIntegerNumberToken(sourceCode[index:])[0] totalIndex = index + len(intTK.lexeme) tokenval = TokenVal.SCIENTIFIC_NOTATION.value tokentype = TokenType.SCIENTIFIC_NOTATION.value lexeme = sourceCode[0:totalIndex] tk = token(tokentype, tokenval, lexeme) slicedSourceCode = sourceCode[totalIndex:] return tk, slicedSourceCode else: return None except IndexError: return None
def getFloatNumberToken(self, sourceCode): if self.__isNumber__(sourceCode): try: index = 0 for i in range(len(sourceCode)): if self.__isNumber__(sourceCode[i]): index = i else: break index = index + 1 if sourceCode[index] == ".": newIndex = index + 1 if newIndex > len(sourceCode): index = newIndex else: for j in range(newIndex, len(sourceCode)): if self.__isNumber__(sourceCode[j]): index = j else: break index = index + 1 if index > len(sourceCode): index = len(sourceCode) - 1 tokentype = TokenType.FLOAT_NUMBER tokenval = TokenVal.FLOAT_NUMBER lexeme = sourceCode[0:index] tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[index:] return tk, slicedSourceCode except IndexError: return None else: return None
def getLogicEqualsToken(self, sourceCode): if self.__isEqualityLexeme__(sourceCode): tokentype = TokenType.LOGIC_EQUALS tokenval = TokenVal.LOGIC_EQUALS lexeme = "=" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getLogicOrToken(self, sourceCode): if self.__isLogicOrLexeme__(sourceCode): tokentype = TokenType.LOGIC_OR tokenval = TokenVal.LOGIC_OR lexeme = "||" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[2:] return tk, slicedSourceCode else: return None
def getOpenBracketsToken(self, sourceCode): if self.__isOpenBracketsLexeme__(sourceCode): tokentype = TokenType.OPEN_BRACKETS tokenval = TokenVal.OPEN_BRACKETS lexeme = "[" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getCloseParenthesesToken(self, sourceCode): if self.__isCloseParenthesesLexeme__(sourceCode): tokentype = TokenType.CLOSE_PARENTHESES tokenval = TokenVal.CLOSE_PARENTHESES lexeme = ")" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getHigherEqualsToken(self, sourceCode): if self.__isHigherEqualsLexeme__(sourceCode): tokentype = TokenType.HIGHER_EQUALS tokenval = TokenVal.HIGHER_EQUALS lexeme = ">=" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[2:] return tk, slicedSourceCode else: return None
def getHigherToken(self, sourceCode): if self.__isHigherLexeme__(sourceCode): tokentype = TokenType.HIGHER tokenval = TokenVal.HIGHER lexeme = ">" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getReadToken(self, sourceCode): if self.__isReadLexeme__(sourceCode): tokentype = TokenType.READ tokenval = TokenVal.READ lexeme = "leia" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[4:] return tk, slicedSourceCode else: return None
def getUntilToken(self, sourceCode): if self.__isUntilLexeme__(sourceCode): tokentype = TokenType.UNTIL tokenval = TokenVal.UNTIL lexeme = "até" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[3:] return tk, slicedSourceCode else: return None
def getReturnToken(self, sourceCode): if self.__isReturnLexeme__(sourceCode): tokentype = TokenType.RETURN tokenval = TokenVal.RETURN lexeme = "retorna" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[7:] return tk, slicedSourceCode else: return None
def getForToken(self, sourceCode): if self.__isForLexeme__(sourceCode): tokentype = TokenType.FOR tokenval = TokenVal.FOR lexeme = "repita" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[6:] return tk, slicedSourceCode else: return None
def getEndToken(self, sourceCode): if self.__isEndLexeme__(sourceCode): tokentype = TokenType.END tokenval = TokenVal.END lexeme = "fim" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[3:] return tk, slicedSourceCode else: return None
def getThenToken(self, sourceCode): if self.__isThenLexeme__(sourceCode): tokentype = TokenType.THEN tokenval = TokenVal.THEN lexeme = "então" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[5:] return tk, slicedSourceCode else: return None
def getElseToken(self, sourceCode): if self.__isElseLexeme__(sourceCode): tokentype = TokenType.ELSE tokenval = TokenVal.ELSE lexeme = "senão" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[5:] return tk, slicedSourceCode else: return None
def getAssignmentToken(self, sourceCode): if self.__isAssignmentLexeme__(sourceCode): tokentype = TokenType.ASSIGNMENT tokenval = TokenVal.ASSIGNMENT lexeme = ":=" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[2:] return tk, slicedSourceCode else: return None
def getWriteToken(self, sourceCode): if self.__isWriteLexeme__(sourceCode): tokentype = TokenType.WRITE tokenval = TokenVal.WRITE lexeme = "escreve" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[7:] return tk, slicedSourceCode else: return None
def getLessEqualsToken(self, sourceCode): if self.__isLessEqualsLexeme__(sourceCode): tokentype = TokenType.LESS_EQUALS tokenval = TokenVal.LESS_EQUALS lexeme = "<=" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[2:] return tk, slicedSourceCode else: return None
def getIntegerTypeToken(self, sourceCode): if self.__isIntegerTypeLexeme__(sourceCode): tokentype = TokenType.INTEGER_TYPE tokenval = TokenVal.INTEGER_TYPE lexeme = "inteiro" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[7:] return tk, slicedSourceCode else: return None
def getOpenParethesesToken(self, sourceCode): if self.__isOPenParenthesesLexeme__(sourceCode): tokentype = TokenType.OPEN_PARENTHESES tokenval = TokenVal.OPEN_PARENTHESES lexeme = "(" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getFloatTypeToken(self, sourceCode): if self.__isFloatTypeLexeme__(sourceCode): tokentype = TokenType.FLOAT_TYPE tokenval = TokenVal.FLOAT_TYPE lexeme = "flutuante" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[9:] return tk, slicedSourceCode else: return None
def getTwoDotsToken(self, sourceCode): if self.__isTwoDotsLexeme_(sourceCode): tokentype = TokenType.TWO_DOTS tokenval = TokenVal.TWO_DOTS lexeme = ":" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getMinusToken(self, sourceCode): if self.__isMinusLexeme__(sourceCode): tokentype = TokenType.MINUS tokenval = TokenVal.MINUS lexeme = "-" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getCloseBracketsToken(self, sourceCode): if self.__isCloseBracketsLexeme__(sourceCode): tokentype = TokenType.CLOSE_BRACKETS tokenval = TokenVal.CLOSE_BRACKETS lexeme = "]" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getTimesToken(self, sourceCode): if self.__isTimesLexeme__(sourceCode): tokentype = TokenType.TIMES tokenval = TokenVal.TIMES lexeme = "*" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getLogicNotToken(self, sourceCode): if self.__isNotLexeme__(sourceCode): tokentype = TokenType.LOGIC_NOT tokenval = TokenVal.LOGIC_NOT lexeme = "!" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getDivisionToken(self, sourceCode): if self.__isDivisionLexeme__(sourceCode): tokentype = TokenType.DIVISION tokenval = TokenVal.DIVISION lexeme = "/" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getCommaToken(self, sourceCode): if self.__isCommaLexeme__(sourceCode): tokentype = TokenType.COMMA tokenval = TokenVal.COMMA lexeme = "," tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[1:] return tk, slicedSourceCode else: return None
def getIfToken(self, sourceCode): if self.__isIFLexeme__(sourceCode): tokentype = TokenType.IF tokenval = TokenVal.IF lexeme = "se" tk = token(tokentype.value, tokenval.value, lexeme) slicedSourceCode = sourceCode[2:] return tk, slicedSourceCode else: return None
def getIdToken(self, sourceCode): letters = self.uppercaseLetters + self.lowercaseLetters + self.specialUpperCases + self.specialLowerCases lettersAndDigits = letters + self.naturalDigits if sourceCode[0] not in letters: return None for i in range(len(sourceCode)): if sourceCode[i] not in lettersAndDigits: tokentype = TokenType.IDENTIFICATOR.value tokenval = TokenVal.IDENTIFICATOR.value lexeme = sourceCode[0:i] tk = token(tokentype, tokenval, lexeme) slicedSourceCode = sourceCode[i:] return tk, slicedSourceCode tokentype = TokenType.IDENTIFICATOR.value tokenval = TokenVal.IDENTIFICATOR.value lexeme = sourceCode[0:] tk = token(tokentype, tokenval, lexeme) slicedSourceCode = "" return tk, slicedSourceCode