Skip to content
Snippets Groups Projects
Select Git revision
1 result Searching

introduction.tex

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    lexer.py 1.08 KiB
    import re
    
    
    class Token:
        def __init__(self, key, value):
            self.k = key
            self.v = value
    
    
    class Lexer:
        def __init__(self, regex_to_token, program):
            self.regex_to_token = regex_to_token
            self.program = program
            self.current_position = 0
    
        def next(self):
            new_position = 0
            next_token = None
            if self.current_position < len(self.program):
                for pattern, value in self.regex_to_token:
                    match = pattern.match(self.program, self.current_position)
                    if match:
                        next_token = Token(value, match.group())
                        new_position = match.span()[1]
                        break
                if self.current_position == new_position:
                    msg = ['Fehler in Zeile ' + str(self.program.count("\n", 0, self.current_position) + 1) + ':',
                           'Unbekannter String: ' + re.compile(r'[^\n]*').match(self.program, self.current_position).group()]
                    raise SyntaxError("\n".join(msg))
                self.current_position = new_position
            return next_token