parser/parsing/parser.py
def find_closing(exp, expression, language, prev = ''):
    """
    Function for parsing expression that has a closing

    :param exp: protobuf Expression objectregex expression
    :param expression: regex expression
    :param language: language this expression is written
    :param prev: what's before this expression that need to consider
    :return: length of this expression, bool indicating if the opening is literal character
    """
    opening = expression[0]
    closing = ""
    if opening == "[":
        closing = "]"
    elif opening == "{":
        closing = "}"
    elif opening == "<":
        closing = ">"
    elif opening == "(":
        closing = ")"
    if opening == "/":
        end = expression.rfind('/')
        if end == 0:
            raise ValueError('Wrong regex.')
        parserHelper.append_token(exp, "/", root.TokenType.Anchor,\
            root.AnchorType.ForwardSlash)
        parse(expression[1 : end], exp, language)
        parserHelper.append_token(exp, "/", root.TokenType.Anchor,\
            root.AnchorType.ForwardSlash)
        parse_flag(exp, expression[end + 1:])
        return len(expression), False

    if closing:
        # Do a quick search of the expression for the appearances of closing without an escape
        i = find_balanced_closing(opening, closing, expression)
        if expression[i] == closing:
            if closing == "}":
                if prev == "\p":
                    parserHelper.append_token(exp, expression[:i+1], root.TokenType.CharacterClass,\
                        root.CharacterClassType.UnicodePropertyEscape)
                elif prev == "\P":
                    parserHelper.append_token(exp, expression[:i+1], root.TokenType.CharacterClass,\
                        root.CharacterClassType.NotUnicodePropertyEscape)
                elif prev == r"\u":
                    parserHelper.append_token(exp, expression[:i+1], root.TokenType.Escape,\
                        root.EscapeType.UnicodeEscape)
                elif prev == "":
                    if re.match("\d+,\d*$", expression[1:i]) or expression[1:i].isdigit():
                        parserHelper.append_token(exp, expression[:i+1], root.TokenType.QuantifierModifier,\
                            root.QuantifierModifierType.SpecifiedQuantifier)
                        return i + 1, False
                    return 1, True  # { be interpreted literally
                raise ValueError('Wrong regex.')
            elif closing == "]":
                parserHelper.append_token(exp, opening, root.TokenType.CharacterClass,\
                    root.CharacterClassType.OpenSet)
                parse(expression[1: i], exp, language, in_set=True)
                parserHelper.append_token(exp, closing, root.TokenType.CharacterClass,\
                    root.CharacterClassType.CloseSet)
                return i + 1, False
            elif closing == ")":
                parserHelper.append_token(exp, opening, root.TokenType.GroupReference,\
                    root.GroupReferenceType.OpenCapture)
                parse(expression[1: i], exp, language)
                parserHelper.append_token(exp, closing, root.TokenType.GroupReference,\
                    root.GroupReferenceType.CloseCapture)
                return i + 1, False
            elif closing == ">":
                if prev == "?":
                    parserHelper.append_token(exp, '?' + expression[:i + 1], root.TokenType.GroupReference,\
                    root.GroupReferenceType.GroupName)
                    return i + 2, False

    if opening == "{": # { be interpreted literally
        return 1, True
    raise ValueError('Wrong regex.')

    Function for parsing expression that has a closing

    :param exp: protobuf Expression objectregex expression
    :param expression: regex expression
    :param language: language this expression is written
    :param prev: what's before this expression that need to consider
    :return: length of this expression, bool indicating if the opening is literal character
    
Function for parsing expression that has a closing
Function for parsing expression that has a closing
def find_closing(exp, expression, language, prev = ''):
    opening = expression[0]
    closing = ""
    if opening == "[":
        closing = "]"
    elif opening == "{":
        closing = "}"
    elif opening == "<":
        closing = ">"
    elif opening == "(":
        closing = ")"
    if opening == "/":
        end = expression.rfind('/')
        if end == 0:
            raise ValueError('Wrong regex.')
        parserHelper.append_token(exp, "/", root.TokenType.Anchor,\
            root.AnchorType.ForwardSlash)
        parse(expression[1 : end], exp, language)
        parserHelper.append_token(exp, "/", root.TokenType.Anchor,\
            root.AnchorType.ForwardSlash)
        parse_flag(exp, expression[end + 1:])
        return len(expression), False
    if closing:
        i = find_balanced_closing(opening, closing, expression)
        if expression[i] == closing:
            if closing == "}":
                if prev == "\p":
                    parserHelper.append_token(exp, expression[:i+1], root.TokenType.CharacterClass,\
                        root.CharacterClassType.UnicodePropertyEscape)
                elif prev == "\P":
                    parserHelper.append_token(exp, expression[:i+1], root.TokenType.CharacterClass,\
                        root.CharacterClassType.NotUnicodePropertyEscape)
                elif prev == r"\u":
                    parserHelper.append_token(exp, expression[:i+1], root.TokenType.Escape,\
                        root.EscapeType.UnicodeEscape)
                elif prev == "":
                    if re.match("\d+,\d*$", expression[1:i]) or expression[1:i].isdigit():
                        parserHelper.append_token(exp, expression[:i+1], root.TokenType.QuantifierModifier,\
                            root.QuantifierModifierType.SpecifiedQuantifier)
                        return i + 1, False
                    return 1, True  
                raise ValueError('Wrong regex.')
            elif closing == "]":
                parserHelper.append_token(exp, opening, root.TokenType.CharacterClass,\
                    root.CharacterClassType.OpenSet)
                parse(expression[1: i], exp, language, in_set=True)
                parserHelper.append_token(exp, closing, root.TokenType.CharacterClass,\
                    root.CharacterClassType.CloseSet)
                return i + 1, False
            elif closing == ")":
                parserHelper.append_token(exp, opening, root.TokenType.GroupReference,\
                    root.GroupReferenceType.OpenCapture)
                parse(expression[1: i], exp, language)
                parserHelper.append_token(exp, closing, root.TokenType.GroupReference,\
                    root.GroupReferenceType.CloseCapture)
                return i + 1, False
            elif closing == ">":
                if prev == "?":
                    parserHelper.append_token(exp, '?' + expression[:i + 1], root.TokenType.GroupReference,\
                    root.GroupReferenceType.GroupName)
                    return i + 2, False
    if opening == "{": 
        return 1, True
    raise ValueError('Wrong regex.')
def find_closing ( exp , expression , language , prev = '' ) : opening = expression [ 0 ] closing = "" if opening == "[" : closing = "]" elif opening == "{" : closing = "}" elif opening == "<" : closing = ">" elif opening == "(" : closing = ")" if opening == "/" : end = expression . rfind ( '/' ) if end == 0 : raise ValueError ( 'Wrong regex.' ) parserHelper . append_token ( exp , "/" , root . TokenType . Anchor , root . AnchorType . ForwardSlash ) parse ( expression [ 1 : end ] , exp , language ) parserHelper . append_token ( exp , "/" , root . TokenType . Anchor , root . AnchorType . ForwardSlash ) parse_flag ( exp , expression [ end + 1 : ] ) return len ( expression ) , False if closing : i = find_balanced_closing ( opening , closing , expression ) if expression [ i ] == closing : if closing == "}" : if prev == "\p" : parserHelper . append_token ( exp , expression [ : i + 1 ] , root . TokenType . CharacterClass , root . CharacterClassType . UnicodePropertyEscape ) elif prev == "\P" : parserHelper . append_token ( exp , expression [ : i + 1 ] , root . TokenType . CharacterClass , root . CharacterClassType . NotUnicodePropertyEscape ) elif prev == r"\u" : parserHelper . append_token ( exp , expression [ : i + 1 ] , root . TokenType . Escape , root . EscapeType . UnicodeEscape ) elif prev == "" : if re . match ( "\d+,\d*$" , expression [ 1 : i ] ) or expression [ 1 : i ] . isdigit ( ) : parserHelper . append_token ( exp , expression [ : i + 1 ] , root . TokenType . QuantifierModifier , root . QuantifierModifierType . SpecifiedQuantifier ) return i + 1 , False return 1 , True raise ValueError ( 'Wrong regex.' ) elif closing == "]" : parserHelper . append_token ( exp , opening , root . TokenType . CharacterClass , root . CharacterClassType . OpenSet ) parse ( expression [ 1 : i ] , exp , language , in_set = True ) parserHelper . append_token ( exp , closing , root . TokenType . CharacterClass , root . CharacterClassType . CloseSet ) return i + 1 , False elif closing == ")" : parserHelper . append_token ( exp , opening , root . TokenType . GroupReference , root . GroupReferenceType . OpenCapture ) parse ( expression [ 1 : i ] , exp , language ) parserHelper . append_token ( exp , closing , root . TokenType . GroupReference , root . GroupReferenceType . CloseCapture ) return i + 1 , False elif closing == ">" : if prev == "?" : parserHelper . append_token ( exp , '?' + expression [ : i + 1 ] , root . TokenType . GroupReference , root . GroupReferenceType . GroupName ) return i + 2 , False if opening == "{" : return 1 , True raise ValueError ( 'Wrong regex.' )
Function for parsing expression that has a closing
Function for parsing expression that has a closing
"""
    Function for parsing expression that has a closing

    :param exp: protobuf Expression objectregex expression
    :param expression: regex expression
    :param language: language this expression is written
    :param prev: what's before this expression that need to consider
    :return: length of this expression, bool indicating if the opening is literal character
    """ # Do a quick search of the expression for the appearances of closing without an escape # { be interpreted literally # { be interpreted literally
length of this expression, bool indicating if the opening is literal character
length of this expression bool indicating if the opening is literal character
exp
protobuf Expression objectregex expression
protobuf Expression objectregex expression
expression
None
language
language this expression is written
language this expression is written
prev
None