Example #1
0
def EvalCharLiteralForRegex(tok):
    # type: (Token) -> Optional[class_literal_term_t]
    """For regex char classes.

  Similar logic as below.
  """
    id_ = tok.id
    value = tok.val

    if id_ == Id.Char_OneChar:
        c = value[1]
        s = consts.LookupCharC(c)
        return class_literal_term.ByteSet(s, tok.span_id)

    elif id_ == Id.Char_Hex:
        s = value[2:]
        i = int(s, 16)
        return class_literal_term.ByteSet(chr(i), tok.span_id)

    elif id_ in (Id.Char_Unicode4, Id.Char_Unicode8):
        s = value[2:]
        i = int(s, 16)
        return class_literal_term.CodePoint(i, tok.span_id)

    elif id_ == Id.Expr_Name:  # [b B] is NOT mutated
        return None

    else:
        raise AssertionError(Id_str(id_))
Example #2
0
def EvalCharLiteralForRegex(tok):
    """For regex char classes.

  Similar logic as below.
  """
    id_ = tok.id
    value = tok.val

    if id_ == Id.Char_OneChar:
        c = value[1]
        s = _ONE_CHAR[c]
        return class_literal_term.ByteSet(s, tok.span_id)

    elif id_ == Id.Char_Hex:
        s = value[2:]
        i = int(s, 16)
        return class_literal_term.ByteSet(chr(i), tok.span_id)

    elif id_ in (Id.Char_Unicode4, Id.Char_Unicode8):
        s = value[2:]
        i = int(s, 16)
        return class_literal_term.CodePoint(i, tok.span_id)

    else:
        raise AssertionError