parser/parsing/parser.py
def parse_backslash(exp, expression, language):
    """
    Determine token that starts with backslash

    :param exp: Expression protobuf object
    :param expression: expression string
    :param language: language this expression is written
    :return token: Token protobuf object
    :return : length of token
    """
    character_classes = ["s", "S", "w", "W", "d", "D"]
    anchors = ["B", "b"]
    escape_reserved = ['+', '*', '?', '^', '$', '\\', '.', '[', ']', '{', '}', '(', ')', '|', '/']
    escape_other = ["t", "n", "v", "f", "r", "0"]

    if expression[0] in character_classes:
        if expression[0] == "s":
            parserHelper.append_token(exp, "\s", root.TokenType.CharacterClass,\
                root.CharacterClassType.Whitespace)
        elif expression[0] == "S":
            parserHelper.append_token(exp, "\S", root.TokenType.CharacterClass,\
                root.CharacterClassType.NotWhitespace)
        elif expression[0] == "w":
            parserHelper.append_token(exp, "\w", root.TokenType.CharacterClass,\
                root.CharacterClassType.Word)
        elif expression[0] == "W":
            parserHelper.append_token(exp, "\W", root.TokenType.CharacterClass,\
                root.CharacterClassType.NotWord)
        elif expression[0] == "d":
            parserHelper.append_token(exp, "\d", root.TokenType.CharacterClass,\
                root.CharacterClassType.Digit)
        else:
            parserHelper.append_token(exp, "\D", root.TokenType.CharacterClass,\
                root.CharacterClassType.NotDigit)
        return 2
    if expression[0] in ["p", "P"]:
        length = find_closing(exp, expression[1:], language, prev = "\\" + expression[0])
        return length
    if expression[0] in anchors:
        if expression[0] == "b":
            parserHelper.append_token(exp, "\b", root.TokenType.Anchor,\
                root.AnchorType.WordBoundary)
        elif expression[0] == "B":
            parserHelper.append_token(exp, "\B", root.TokenType.Anchor,\
                root.AnchorType.NotWordBoundary)
        return 2
    if expression[0] in escape_reserved:
        parserHelper.append_token(exp, "\\" + expression[0], root.TokenType.Escape,\
            root.EscapeType.Reserved)
        return 2
    if expression[0] in escape_other:
        if expression[0] == "t":
            parserHelper.append_token(exp, "\t", root.TokenType.Escape,\
                root.EscapeType.Tab)
        elif expression[0] == "n":
            parserHelper.append_token(exp, "\n", root.TokenType.Escape,\
                root.EscapeType.Tab)
        elif expression[0] == "v":
            parserHelper.append_token(exp, "\v", root.TokenType.Escape,\
                root.EscapeType.Tab)
        elif expression[0] == "f":
            parserHelper.append_token(exp, "\f", root.TokenType.Escape,\
                root.EscapeType.Tab)
        elif expression[0] == "r":
            parserHelper.append_token(exp, "\r", root.TokenType.Escape,\
                root.EscapeType.Tab)
        else:
            parserHelper.append_token(exp, "\n", root.TokenType.Escape,\
                root.EscapeType.Tab)
        return 2
    if len(expression) > 1 and expression[:2] == "cI":
        parserHelper.append_token(exp, "\n", root.TokenType.Escape,\
            root.EscapeType.Control)
        return 3
    if expression[0] == "u" and len(expression) >= 2:
        if expression[1] == "{":
            length = find_closing(exp, expression[1:], language, prev = r"\u") 
            return length
        if len(expression) >= 5 and all(c in string.hexdigits for c in expression[1:5]):
            parserHelper.append_token(exp, "\\" + expression[:5], root.TokenType.Escape,\
                        root.EscapeType.UnicodeEscape)
            return 6
        raise ValueError('Wrong regex.')
    if len(expression) >= 3 and all(c in string.octdigits for c in expression[:3]):
        parserHelper.append_token(exp, "\\" + expression[:3], root.TokenType.Escape,\
                        root.EscapeType.Octal)
        return 4
    elif len(expression) >= 3 and expression[0] == "x" and all(c in string.hexdigits for c in expression[1:3]):
        parserHelper.append_token(exp, "\\" + expression[:3], root.TokenType.Escape,\
                        root.EscapeType.Hexadecimal)
        return 4
    if expression[0].isdigit():
        parserHelper.append_token(exp, "\\" + expression[:0], root.TokenType.GroupReference,\
                        root.GroupReferenceType.NumericReference)
        return 2
    else:
        parserHelper.append_token(exp, "\\" + expression[0], root.TokenType.Character, expression[0])
        return 2

    Determine token that starts with backslash

    :param exp: Expression protobuf object
    :param expression: expression string
    :param language: language this expression is written
    :return token: Token protobuf object
    :return : length of token
    
Determine token that starts with backslash
Determine token that starts with backslash
def parse_backslash(exp, expression, language):
    character_classes = ["s", "S", "w", "W", "d", "D"]
    anchors = ["B", "b"]
    escape_reserved = ['+', '*', '?', '^', '$', '\\', '.', '[', ']', '{', '}', '(', ')', '|', '/']
    escape_other = ["t", "n", "v", "f", "r", "0"]
    if expression[0] in character_classes:
        if expression[0] == "s":
            parserHelper.append_token(exp, "\s", root.TokenType.CharacterClass,\
                root.CharacterClassType.Whitespace)
        elif expression[0] == "S":
            parserHelper.append_token(exp, "\S", root.TokenType.CharacterClass,\
                root.CharacterClassType.NotWhitespace)
        elif expression[0] == "w":
            parserHelper.append_token(exp, "\w", root.TokenType.CharacterClass,\
                root.CharacterClassType.Word)
        elif expression[0] == "W":
            parserHelper.append_token(exp, "\W", root.TokenType.CharacterClass,\
                root.CharacterClassType.NotWord)
        elif expression[0] == "d":
            parserHelper.append_token(exp, "\d", root.TokenType.CharacterClass,\
                root.CharacterClassType.Digit)
        else:
            parserHelper.append_token(exp, "\D", root.TokenType.CharacterClass,\
                root.CharacterClassType.NotDigit)
        return 2
    if expression[0] in ["p", "P"]:
        length = find_closing(exp, expression[1:], language, prev = "\\" + expression[0])
        return length
    if expression[0] in anchors:
        if expression[0] == "b":
            parserHelper.append_token(exp, "\b", root.TokenType.Anchor,\
                root.AnchorType.WordBoundary)
        elif expression[0] == "B":
            parserHelper.append_token(exp, "\B", root.TokenType.Anchor,\
                root.AnchorType.NotWordBoundary)
        return 2
    if expression[0] in escape_reserved:
        parserHelper.append_token(exp, "\\" + expression[0], root.TokenType.Escape,\
            root.EscapeType.Reserved)
        return 2
    if expression[0] in escape_other:
        if expression[0] == "t":
            parserHelper.append_token(exp, "\t", root.TokenType.Escape,\
                root.EscapeType.Tab)
        elif expression[0] == "n":
            parserHelper.append_token(exp, "\n", root.TokenType.Escape,\
                root.EscapeType.Tab)
        elif expression[0] == "v":
            parserHelper.append_token(exp, "\v", root.TokenType.Escape,\
                root.EscapeType.Tab)
        elif expression[0] == "f":
            parserHelper.append_token(exp, "\f", root.TokenType.Escape,\
                root.EscapeType.Tab)
        elif expression[0] == "r":
            parserHelper.append_token(exp, "\r", root.TokenType.Escape,\
                root.EscapeType.Tab)
        else:
            parserHelper.append_token(exp, "\n", root.TokenType.Escape,\
                root.EscapeType.Tab)
        return 2
    if len(expression) > 1 and expression[:2] == "cI":
        parserHelper.append_token(exp, "\n", root.TokenType.Escape,\
            root.EscapeType.Control)
        return 3
    if expression[0] == "u" and len(expression) >= 2:
        if expression[1] == "{":
            length = find_closing(exp, expression[1:], language, prev = r"\u") 
            return length
        if len(expression) >= 5 and all(c in string.hexdigits for c in expression[1:5]):
            parserHelper.append_token(exp, "\\" + expression[:5], root.TokenType.Escape,\
                        root.EscapeType.UnicodeEscape)
            return 6
        raise ValueError('Wrong regex.')
    if len(expression) >= 3 and all(c in string.octdigits for c in expression[:3]):
        parserHelper.append_token(exp, "\\" + expression[:3], root.TokenType.Escape,\
                        root.EscapeType.Octal)
        return 4
    elif len(expression) >= 3 and expression[0] == "x" and all(c in string.hexdigits for c in expression[1:3]):
        parserHelper.append_token(exp, "\\" + expression[:3], root.TokenType.Escape,\
                        root.EscapeType.Hexadecimal)
        return 4
    if expression[0].isdigit():
        parserHelper.append_token(exp, "\\" + expression[:0], root.TokenType.GroupReference,\
                        root.GroupReferenceType.NumericReference)
        return 2
    else:
        parserHelper.append_token(exp, "\\" + expression[0], root.TokenType.Character, expression[0])
        return 2
def parse_backslash ( exp , expression , language ) : character_classes = [ "s" , "S" , "w" , "W" , "d" , "D" ] anchors = [ "B" , "b" ] escape_reserved = [ '+' , '*' , '?' , '^' , '$' , '\\' , '.' , '[' , ']' , '{' , '}' , '(' , ')' , '|' , '/' ] escape_other = [ "t" , "n" , "v" , "f" , "r" , "0" ] if expression [ 0 ] in character_classes : if expression [ 0 ] == "s" : parserHelper . append_token ( exp , "\s" , root . TokenType . CharacterClass , root . CharacterClassType . Whitespace ) elif expression [ 0 ] == "S" : parserHelper . append_token ( exp , "\S" , root . TokenType . CharacterClass , root . CharacterClassType . NotWhitespace ) elif expression [ 0 ] == "w" : parserHelper . append_token ( exp , "\w" , root . TokenType . CharacterClass , root . CharacterClassType . Word ) elif expression [ 0 ] == "W" : parserHelper . append_token ( exp , "\W" , root . TokenType . CharacterClass , root . CharacterClassType . NotWord ) elif expression [ 0 ] == "d" : parserHelper . append_token ( exp , "\d" , root . TokenType . CharacterClass , root . CharacterClassType . Digit ) else : parserHelper . append_token ( exp , "\D" , root . TokenType . CharacterClass , root . CharacterClassType . NotDigit ) return 2 if expression [ 0 ] in [ "p" , "P" ] : length = find_closing ( exp , expression [ 1 : ] , language , prev = "\\" + expression [ 0 ] ) return length if expression [ 0 ] in anchors : if expression [ 0 ] == "b" : parserHelper . append_token ( exp , "\b" , root . TokenType . Anchor , root . AnchorType . WordBoundary ) elif expression [ 0 ] == "B" : parserHelper . append_token ( exp , "\B" , root . TokenType . Anchor , root . AnchorType . NotWordBoundary ) return 2 if expression [ 0 ] in escape_reserved : parserHelper . append_token ( exp , "\\" + expression [ 0 ] , root . TokenType . Escape , root . EscapeType . Reserved ) return 2 if expression [ 0 ] in escape_other : if expression [ 0 ] == "t" : parserHelper . append_token ( exp , "\t" , root . TokenType . Escape , root . EscapeType . Tab ) elif expression [ 0 ] == "n" : parserHelper . append_token ( exp , "\n" , root . TokenType . Escape , root . EscapeType . Tab ) elif expression [ 0 ] == "v" : parserHelper . append_token ( exp , "\v" , root . TokenType . Escape , root . EscapeType . Tab ) elif expression [ 0 ] == "f" : parserHelper . append_token ( exp , "\f" , root . TokenType . Escape , root . EscapeType . Tab ) elif expression [ 0 ] == "r" : parserHelper . append_token ( exp , "\r" , root . TokenType . Escape , root . EscapeType . Tab ) else : parserHelper . append_token ( exp , "\n" , root . TokenType . Escape , root . EscapeType . Tab ) return 2 if len ( expression ) > 1 and expression [ : 2 ] == "cI" : parserHelper . append_token ( exp , "\n" , root . TokenType . Escape , root . EscapeType . Control ) return 3 if expression [ 0 ] == "u" and len ( expression ) >= 2 : if expression [ 1 ] == "{" : length = find_closing ( exp , expression [ 1 : ] , language , prev = r"\u" ) return length if len ( expression ) >= 5 and all ( c in string . hexdigits for c in expression [ 1 : 5 ] ) : parserHelper . append_token ( exp , "\\" + expression [ : 5 ] , root . TokenType . Escape , root . EscapeType . UnicodeEscape ) return 6 raise ValueError ( 'Wrong regex.' ) if len ( expression ) >= 3 and all ( c in string . octdigits for c in expression [ : 3 ] ) : parserHelper . append_token ( exp , "\\" + expression [ : 3 ] , root . TokenType . Escape , root . EscapeType . Octal ) return 4 elif len ( expression ) >= 3 and expression [ 0 ] == "x" and all ( c in string . hexdigits for c in expression [ 1 : 3 ] ) : parserHelper . append_token ( exp , "\\" + expression [ : 3 ] , root . TokenType . Escape , root . EscapeType . Hexadecimal ) return 4 if expression [ 0 ] . isdigit ( ) : parserHelper . append_token ( exp , "\\" + expression [ : 0 ] , root . TokenType . GroupReference , root . GroupReferenceType . NumericReference ) return 2 else : parserHelper . append_token ( exp , "\\" + expression [ 0 ] , root . TokenType . Character , expression [ 0 ] ) return 2
Determine token that starts with backslash
Determine token that starts with backslash
"""
    Determine token that starts with backslash

    :param exp: Expression protobuf object
    :param expression: expression string
    :param language: language this expression is written
    :return token: Token protobuf object
    :return : length of token
    """
Token protobuf object
Token protobuf object
token
length of token
length of token
exp
Expression protobuf object
Expression protobuf object
expression
None
language
language this expression is written
language this expression is written