from sly import Lexer as SlyLexer from Models.CCLang.TokenDefinition import TokenDefinition class Lexer(SlyLexer): # Ignored pattern ignore = '\t ' # ignore_comment = r'(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)|([#].*)' ignore_comment = r'([#].*|(//.*))' ignore_newline = r'\n+' tokens = { LIBRARY, CLASS, FUNCTION, VARIABLE, USE, FROM, OUTPUT, INPUT, LENGTH, RANGE, EXIT, IF, ELSEIF, ELSE, CONTINUE, IN, RETURN, WHILE, FOR, PUBLIC, THIS, LBRACE, RBRACE, LPARAN, RPARAN, LBRACKET, RBRACKET, SEMICOLON, COLON, COMMA, POINT, PLUS, MINUS, ASTERIK, SLASH, EQUAL, CARET, TRUE, FALSE, STRING, NUMBER, EMPTY, NAME } # token definition LIBRARY = TokenDefinition.Library.value CLASS = TokenDefinition.Class.value FUNCTION = TokenDefinition.Function.value VARIABLE = TokenDefinition.Variable.value USE = TokenDefinition.Use.value FROM = TokenDefinition.From.value OUTPUT = TokenDefinition.Output.value INPUT = TokenDefinition.Input.value LENGTH = TokenDefinition.Length.value RANGE = TokenDefinition.Range.value EXIT = TokenDefinition.Exit.value IF = TokenDefinition.If.value ELSEIF = TokenDefinition.ElseIf.value ELSE = TokenDefinition.Else.value CONTINUE = TokenDefinition.Continue.value IN = TokenDefinition.In.value RETURN = TokenDefinition.Return.value WHILE = TokenDefinition.While.value FOR = TokenDefinition.For.value PUBLIC = TokenDefinition.Public.value THIS = TokenDefinition.This.value LBRACE = TokenDefinition.LeftBrace.value RBRACE = TokenDefinition.RightBrace.value LPARAN = TokenDefinition.LeftParenthesis.value RPARAN = TokenDefinition.RightParenthesis.value LBRACKET = TokenDefinition.LeftBracket.value RBRACKET = TokenDefinition.RightBracket.value SEMICOLON = TokenDefinition.Semicolon.value COLON = TokenDefinition.Colon.value COMMA = TokenDefinition.Comma.value POINT = TokenDefinition.Point.value PLUS = TokenDefinition.Plus.value MINUS = TokenDefinition.Minus.value ASTERIK = TokenDefinition.Asterisk.value SLASH = TokenDefinition.Slash.value EQUAL = TokenDefinition.Equal.value CARET = TokenDefinition.Caret.value TRUE = TokenDefinition.BoolTrue.value FALSE = TokenDefinition.BoolFalse.value STRING = TokenDefinition.String.value NUMBER = TokenDefinition.Number.value EMPTY = TokenDefinition.Empty.value NAME = TokenDefinition.Name.value def error(self, t): print("Illegal character '%s'" % t.value[0]) # self.index += 1