parser/parsing/parser.py
def parse(fe: typing.Union[str, FoundExpression], exp: root.Expression = None, language: str = None,
in_set: bool = False) -> root.Expression:
    """
    Main parsing function to parse expression into tokens

    :param fe: FoundExpression object
    :param exp: protobuf Expression object
    :param language: language Expression is written in
    :param in_set: whether this expression is within []
    :return: protobuf Expression object
    """
    if isinstance(fe, str):
        expression = fe
    else:
        expression = fe.expression
    if not language and not isinstance(language, int):
        language = fe.language
    if not exp:
        exp = root.Expression()
        exp.raw = expression
    index = 0
    string = ""
    while index < len(expression):
        if not in_set and (expression[index] == "[" or expression[index] == "(" or expression[index] == "{" or
                           (language == "JS" and expression[index] == "/")):
            parserHelper.append_token(exp, string, root.TokenType.Character, string)
            length, is_literal = find_closing(exp, expression[index:], language)
            if not is_literal:
                string = ""
            else:
                del exp.tokens[-1]
                string += expression[index]
            index += length
        elif expression[index] == '\\':  # this is just \, \ within a str represents an escape character
            string = parserHelper.modify_string(exp, string)
            length = parse_backslash(exp, expression[index + 1:], language)
            index += length
        elif not in_set and (expression[index] == "?"): 
            string = parserHelper.modify_string(exp, string)
            parse_question_mark(exp, expression[index:], language)
            index += len(exp.tokens[-1].token)
        elif not in_set and (expression[index] == "*"):
            string = parserHelper.modify_string(exp, string)
            parserHelper.append_token(exp, "*", root.TokenType.QuantifierModifier, root.QuantifierModifierType.Star)
            index += 1 
        elif not in_set and (expression[index] == "|"): # OR
            string = parserHelper.modify_string(exp, string)
            parserHelper.append_token(exp, "|", root.TokenType.QuantifierModifier, root.QuantifierModifierType.AlternationPipe)
            index += 1 
        elif expression[index] == "-":
            if index == 0 or len(expression) == index + 1: # - can be interpreted literally
                parserHelper.modify_string(exp, expression[index])
                index += 1
            else:
                del exp.tokens[-1]
                parserHelper.append_token(exp, expression[index - 1: index + 2], root.TokenType.CharacterClass, root.CharacterClassType.RangeSet)
                index += 2
        elif expression[index] == "^":
            string = parserHelper.modify_string(exp, string)
            if exp.tokens and exp.tokens[-1].token == "[":
                parserHelper.append_token(exp, "^", root.TokenType.CharacterClass, root.CharacterClassType.SetNegation)
            else:
                parserHelper.append_token(exp, "^", root.TokenType.Anchor, root.AnchorType.Beginning)
            index += 1
        elif not in_set and (expression[index] == "."): 
            string = parserHelper.modify_string(exp, string)
            parserHelper.append_token(exp, ".", root.TokenType.CharacterClass, root.CharacterClassType.Dot)
            index += 1
        elif not in_set and (expression[index] == "$"):
            string = parserHelper.modify_string(exp, string)
            length = parse_dollar_sign(exp, expression[index:])
            index += length
        elif not in_set and (expression[index] == "+"):
            string = parserHelper.modify_string(exp, string)
            parserHelper.append_token(exp, "+", root.TokenType.QuantifierModifier, root.QuantifierModifierType.Plus)
            index += 1
        elif in_set: # ex: [\101a-df<?h|b*.]
            parserHelper.modify_string(exp, expression[index])
            index += 1    
        else:
            string += expression[index]
            index += 1
    if string:
        if exp.tokens and exp.tokens[-1].token == "[" and string == expression:
            parserHelper.append_token(exp, expression, root.TokenType.CharacterClass, root.CharacterClassType.InclusiveSet)
        else:
            parserHelper.append_token(exp, string, root.TokenType.Character, string)
    # json_obj = MessageToJson(exp)
    # print(json_obj)
    return exp

    Main parsing function to parse expression into tokens

    :param fe: FoundExpression object
    :param exp: protobuf Expression object
    :param language: language Expression is written in
    :param in_set: whether this expression is within []
    :return: protobuf Expression object
    
Main parsing function to parse expression into tokens
Main parsing function to parse expression into tokens
def parse(fe: typing.Union[str, FoundExpression], exp: root.Expression = None, language: str = None,
in_set: bool = False) -> root.Expression:
    if isinstance(fe, str):
        expression = fe
    else:
        expression = fe.expression
    if not language and not isinstance(language, int):
        language = fe.language
    if not exp:
        exp = root.Expression()
        exp.raw = expression
    index = 0
    string = ""
    while index < len(expression):
        if not in_set and (expression[index] == "[" or expression[index] == "(" or expression[index] == "{" or
                           (language == "JS" and expression[index] == "/")):
            parserHelper.append_token(exp, string, root.TokenType.Character, string)
            length, is_literal = find_closing(exp, expression[index:], language)
            if not is_literal:
                string = ""
            else:
                del exp.tokens[-1]
                string += expression[index]
            index += length
        elif expression[index] == '\\':  
            string = parserHelper.modify_string(exp, string)
            length = parse_backslash(exp, expression[index + 1:], language)
            index += length
        elif not in_set and (expression[index] == "?"): 
            string = parserHelper.modify_string(exp, string)
            parse_question_mark(exp, expression[index:], language)
            index += len(exp.tokens[-1].token)
        elif not in_set and (expression[index] == "*"):
            string = parserHelper.modify_string(exp, string)
            parserHelper.append_token(exp, "*", root.TokenType.QuantifierModifier, root.QuantifierModifierType.Star)
            index += 1 
        elif not in_set and (expression[index] == "|"): 
            string = parserHelper.modify_string(exp, string)
            parserHelper.append_token(exp, "|", root.TokenType.QuantifierModifier, root.QuantifierModifierType.AlternationPipe)
            index += 1 
        elif expression[index] == "-":
            if index == 0 or len(expression) == index + 1: 
                parserHelper.modify_string(exp, expression[index])
                index += 1
            else:
                del exp.tokens[-1]
                parserHelper.append_token(exp, expression[index - 1: index + 2], root.TokenType.CharacterClass, root.CharacterClassType.RangeSet)
                index += 2
        elif expression[index] == "^":
            string = parserHelper.modify_string(exp, string)
            if exp.tokens and exp.tokens[-1].token == "[":
                parserHelper.append_token(exp, "^", root.TokenType.CharacterClass, root.CharacterClassType.SetNegation)
            else:
                parserHelper.append_token(exp, "^", root.TokenType.Anchor, root.AnchorType.Beginning)
            index += 1
        elif not in_set and (expression[index] == "."): 
            string = parserHelper.modify_string(exp, string)
            parserHelper.append_token(exp, ".", root.TokenType.CharacterClass, root.CharacterClassType.Dot)
            index += 1
        elif not in_set and (expression[index] == "$"):
            string = parserHelper.modify_string(exp, string)
            length = parse_dollar_sign(exp, expression[index:])
            index += length
        elif not in_set and (expression[index] == "+"):
            string = parserHelper.modify_string(exp, string)
            parserHelper.append_token(exp, "+", root.TokenType.QuantifierModifier, root.QuantifierModifierType.Plus)
            index += 1
        elif in_set: 
            parserHelper.modify_string(exp, expression[index])
            index += 1    
        else:
            string += expression[index]
            index += 1
    if string:
        if exp.tokens and exp.tokens[-1].token == "[" and string == expression:
            parserHelper.append_token(exp, expression, root.TokenType.CharacterClass, root.CharacterClassType.InclusiveSet)
        else:
            parserHelper.append_token(exp, string, root.TokenType.Character, string)
    return exp
def parse ( fe : typing . Union [ str , FoundExpression ] , exp : root . Expression = None , language : str = None , in_set : bool = False ) -> root . Expression : if isinstance ( fe , str ) : expression = fe else : expression = fe . expression if not language and not isinstance ( language , int ) : language = fe . language if not exp : exp = root . Expression ( ) exp . raw = expression index = 0 string = "" while index < len ( expression ) : if not in_set and ( expression [ index ] == "[" or expression [ index ] == "(" or expression [ index ] == "{" or ( language == "JS" and expression [ index ] == "/" ) ) : parserHelper . append_token ( exp , string , root . TokenType . Character , string ) length , is_literal = find_closing ( exp , expression [ index : ] , language ) if not is_literal : string = "" else : del exp . tokens [ - 1 ] string += expression [ index ] index += length elif expression [ index ] == '\\' : string = parserHelper . modify_string ( exp , string ) length = parse_backslash ( exp , expression [ index + 1 : ] , language ) index += length elif not in_set and ( expression [ index ] == "?" ) : string = parserHelper . modify_string ( exp , string ) parse_question_mark ( exp , expression [ index : ] , language ) index += len ( exp . tokens [ - 1 ] . token ) elif not in_set and ( expression [ index ] == "*" ) : string = parserHelper . modify_string ( exp , string ) parserHelper . append_token ( exp , "*" , root . TokenType . QuantifierModifier , root . QuantifierModifierType . Star ) index += 1 elif not in_set and ( expression [ index ] == "|" ) : string = parserHelper . modify_string ( exp , string ) parserHelper . append_token ( exp , "|" , root . TokenType . QuantifierModifier , root . QuantifierModifierType . AlternationPipe ) index += 1 elif expression [ index ] == "-" : if index == 0 or len ( expression ) == index + 1 : parserHelper . modify_string ( exp , expression [ index ] ) index += 1 else : del exp . tokens [ - 1 ] parserHelper . append_token ( exp , expression [ index - 1 : index + 2 ] , root . TokenType . CharacterClass , root . CharacterClassType . RangeSet ) index += 2 elif expression [ index ] == "^" : string = parserHelper . modify_string ( exp , string ) if exp . tokens and exp . tokens [ - 1 ] . token == "[" : parserHelper . append_token ( exp , "^" , root . TokenType . CharacterClass , root . CharacterClassType . SetNegation ) else : parserHelper . append_token ( exp , "^" , root . TokenType . Anchor , root . AnchorType . Beginning ) index += 1 elif not in_set and ( expression [ index ] == "." ) : string = parserHelper . modify_string ( exp , string ) parserHelper . append_token ( exp , "." , root . TokenType . CharacterClass , root . CharacterClassType . Dot ) index += 1 elif not in_set and ( expression [ index ] == "$" ) : string = parserHelper . modify_string ( exp , string ) length = parse_dollar_sign ( exp , expression [ index : ] ) index += length elif not in_set and ( expression [ index ] == "+" ) : string = parserHelper . modify_string ( exp , string ) parserHelper . append_token ( exp , "+" , root . TokenType . QuantifierModifier , root . QuantifierModifierType . Plus ) index += 1 elif in_set : parserHelper . modify_string ( exp , expression [ index ] ) index += 1 else : string += expression [ index ] index += 1 if string : if exp . tokens and exp . tokens [ - 1 ] . token == "[" and string == expression : parserHelper . append_token ( exp , expression , root . TokenType . CharacterClass , root . CharacterClassType . InclusiveSet ) else : parserHelper . append_token ( exp , string , root . TokenType . Character , string ) return exp
Main parsing function to parse expression into tokens
Main parsing function to parse expression into tokens
"""
    Main parsing function to parse expression into tokens

    :param fe: FoundExpression object
    :param exp: protobuf Expression object
    :param language: language Expression is written in
    :param in_set: whether this expression is within []
    :return: protobuf Expression object
    """ # this is just \, \ within a str represents an escape character # OR # - can be interpreted literally # ex: [\101a-df<?h|b*.] # json_obj = MessageToJson(exp) # print(json_obj)
protobuf Expression object
protobuf Expression object
fe
typing.Union[str, FoundExpression]
None
exp
root.Expression
protobuf Expression object
protobuf Expression object
language
str
language Expression is written in
language Expression is written in
in_set
bool
whether this expression is within []
whether this expression is within []