def rbraces(self): if self.getChar() == "{": self.indentPosition() return parser.Token(ast.TokenType.leftBrace, "", self.line) if self.getChar() == "}": self.indentPosition() return parser.Token(ast.TokenType.rightBrace, "", self.line)
def rparenthesis(self): if self.getChar() == "(": self.indentPosition() return parser.Token(ast.TokenType.leftParenthesis, "", self.line) if self.getChar() == ")": self.indentPosition() return parser.Token(ast.TokenType.rightParenthesis, "", self.line)
def rstring(self): esq = "" self.indentPosition() try: while self.getChar() != "\"": if self.getChar() == "~": esq += self.getChar() self.appendToString() esq += self.getChar() if esq not in self.escapeSeq: self.printError("UNKNOWN ESCAPE SEQUENCE") sys.exit() self.appendToString() else: self.appendToString() except IndexError: self.printError("NO STRING TERMINATION FOUND") else: self.indentPosition() counter = 0 for x in self.tempString: if x == "~n" or x == "\n": self.indentLine() counter += 1 memeString = self.tempString for x in self.escapeSeq: memeString = memeString.replace(x, self.escapeSeq[x]) return parser.Token(ast.TokenType.stringLiteral, memeString, self.line - counter)
def rindentifier(self): while self.checkLenght(): if not (self.getChar().isalpha() or self.getChar().isdigit() or self.getChar() == "_"): break else: self.appendToString() if self.tempString in self.keywords: return parser.Token(ast.TokenType.keyword, self.tempString, self.line) elif self.tempString == "truth" or self.tempString == "lie": return parser.Token(ast.TokenType.booleanLiteral, self.tempString, self.line) elif self.tempString == "and": return parser.Token(ast.TokenType.logicalAnd, "", self.line) elif self.tempString == "comment_start": self.rcomment() elif self.tempString == "or": return parser.Token(ast.TokenType.logicalOr, "", self.line) else: return parser.Token(ast.TokenType.identifier, self.tempString, self.line)
def rint(self): while self.checkLenght() and self.getChar().isdigit(): self.appendToString() if self.position == self.getLenght(): break if self.getChar() == "." or self.getChar() == "e": self.appendToString() return self.rfloat() if self.getChar().isalpha() or self.getChar() == "_": self.printError("INVALID SUFFIX AFTER INTEGER") sys.exit() return parser.Token(ast.TokenType.integerLiteral, self.tempString, self.line)
def rfloat(self): if self.getChar() == "-": self.appendToString() if not self.getChar().isdigit(): self.printError("INVALID SUFFIX AFTER FLOAT") sys.exit() while self.checkLenght() and self.getChar().isdigit(): self.appendToString() if self.position == self.getLenght(): break if self.getChar().isalpha() or self.getChar() == "_": self.printError("INVALID SUFFIX AFTER FLOAT") sys.exit() return parser.Token(ast.TokenType.floatLiteral, self.tempString, self.line)
def rnewline(self): self.indentLine() self.indentPosition() return parser.Token(ast.TokenType.newLine, "", self.line - 1)
def roperator(self): if self.getChar() == "<": self.appendToString() if self.checkLenght() and self.getChar() == "=": self.appendToString() return parser.Token(ast.TokenType.lessThanOrEqual, "", self.line) return parser.Token(ast.TokenType.lessThan, "", self.line) if self.getChar() == ">": self.appendToString() if self.position < self.getLenght() and self.getChar() == "=": self.appendToString() return parser.Token(ast.TokenType.greaterThanOrEqual, "", self.line) return parser.Token(ast.TokenType.greaterThan, "", self.line) if self.getChar() == "!": self.appendToString() if self.checkLenght() and self.getChar() == "=": self.appendToString() return parser.Token(ast.TokenType.notEqual, "", self.line) return parser.Token(ast.TokenType.notSomething, "", self.line) if self.getChar() == "=": self.appendToString() if self.checkLenght() and self.getChar() == "=": self.appendToString() return parser.Token(ast.TokenType.equal, "", self.line) return parser.Token(ast.TokenType.assign, "", self.line) if self.getChar() == "+": self.indentPosition() return parser.Token(ast.TokenType.plus, "", self.line) if self.getChar() == "-": self.indentPosition() if self.getChar() == ">": self.indentPosition() return parser.Token(ast.TokenType.assign, "", self.line) return parser.Token(ast.TokenType.minus, "", self.line) if self.getChar() == "*": self.indentPosition() return parser.Token(ast.TokenType.mult, "", self.line) if self.getChar() == "/": self.indentPosition() return parser.Token(ast.TokenType.div, "", self.line)
def rcomma(self): if self.getChar() == ",": self.indentPosition() return parser.Token(ast.TokenType.comma, "", self.line)
def reof(self): return parser.Token(ast.TokenType.endOfFile, "", self.line)