Beispiel #1
0
def read_till_next_dot(stream):
    charlist = []
    tlist = ["%", "", "end_of_file"]
    whitespace = True
    ignore = False
    while True:
        char, _ = read_unicode_char(stream)
        if char == "%":
            ignore = True
        if char == "\n":
            ignore = False
            continue
        if char == "end_of_file":
            ignore = False
        if rstring.strip_spaces(char) == "":
            continue
        if not ignore:
            if char == "end_of_file":
                if whitespace:
                    return "end_of_file."
                else:
                    error.throw_syntax_error("Unexpected end of file")
            else:
                whitespace = False
            charlist.append(char)
            if char == ".":
                nextchar, n = read_unicode_char(stream)
                stream.seek(-n, 1)
                if rstring.strip_spaces(nextchar) in tlist:
                    return "".join(charlist)
Beispiel #2
0
def read_till_next_dot(stream):
    charlist = []
    tlist = ["%", "", "end_of_file"]
    whitespace = True
    ignore = False
    while True:
        char, _ = read_unicode_char(stream)
        if char == "%":
            ignore = True
        if char == "\n":
            ignore = False
            continue
        if char == "end_of_file":
            ignore = False
        if rstring.strip_spaces(char) == "":
            continue
        if not ignore:
            if char == "end_of_file":
                if whitespace:
                    return "end_of_file."
                else:
                    error.throw_syntax_error("Unexpected end of file")
            else:
                whitespace = False
            charlist.append(char)
            if char == ".":
                nextchar, n = read_unicode_char(stream)
                stream.seek(-n, 1)
                if rstring.strip_spaces(nextchar) in tlist:
                    return "".join(charlist)
Beispiel #3
0
def _fromstring_text(space, s, count, sep, length, dtype):
    sep_stripped = strip_spaces(sep)
    skip_bad_vals = len(sep_stripped) == 0

    items = []
    num_items = 0
    idx = 0

    while (num_items < count or count == -1) and idx < len(s):
        nextidx = s.find(sep, idx)
        if nextidx < 0:
            nextidx = length
        piece = strip_spaces(s[idx:nextidx])
        if len(piece) > 0 or not skip_bad_vals:
            if len(piece) == 0 and not skip_bad_vals:
                val = dtype.itemtype.default_fromstring(space)
            else:
                try:
                    val = dtype.coerce(space, space.newtext(piece))
                except OperationError as e:
                    if not e.match(space, space.w_ValueError):
                        raise
                    gotit = False
                    while not gotit and len(piece) > 0:
                        piece = piece[:-1]
                        try:
                            val = dtype.coerce(space, space.newtext(piece))
                            gotit = True
                        except OperationError as e:
                            if not e.match(space, space.w_ValueError):
                                raise
                    if not gotit:
                        val = dtype.itemtype.default_fromstring(space)
                    nextidx = length
            items.append(val)
            num_items += 1
        idx = nextidx + 1

    if count > num_items:
        raise oefmt(space.w_ValueError,
                    "string is smaller than requested size")

    a = W_NDimArray.from_shape(space, [num_items], dtype=dtype)
    ai, state = a.create_iter()
    for val in items:
        ai.setitem(state, val)
        state = ai.next(state)

    return a
Beispiel #4
0
def _fromstring_text(space, s, count, sep, length, dtype):
    sep_stripped = strip_spaces(sep)
    skip_bad_vals = len(sep_stripped) == 0

    items = []
    num_items = 0
    idx = 0

    while (num_items < count or count == -1) and idx < len(s):
        nextidx = s.find(sep, idx)
        if nextidx < 0:
            nextidx = length
        piece = strip_spaces(s[idx:nextidx])
        if len(piece) > 0 or not skip_bad_vals:
            if len(piece) == 0 and not skip_bad_vals:
                val = dtype.itemtype.default_fromstring(space)
            else:
                try:
                    val = dtype.coerce(space, space.wrap(piece))
                except OperationError as e:
                    if not e.match(space, space.w_ValueError):
                        raise
                    gotit = False
                    while not gotit and len(piece) > 0:
                        piece = piece[:-1]
                        try:
                            val = dtype.coerce(space, space.wrap(piece))
                            gotit = True
                        except OperationError as e:
                            if not e.match(space, space.w_ValueError):
                                raise
                    if not gotit:
                        val = dtype.itemtype.default_fromstring(space)
                    nextidx = length
            items.append(val)
            num_items += 1
        idx = nextidx + 1

    if count > num_items:
        raise oefmt(space.w_ValueError,
                    "string is smaller than requested size")

    a = W_NDimArray.from_shape(space, [num_items], dtype=dtype)
    ai, state = a.create_iter()
    for val in items:
        ai.setitem(state, val)
        state = ai.next(state)

    return space.wrap(a)
Beispiel #5
0
def string_to_int(s, base=10):
    """Utility to converts a string to an integer.
    If base is 0, the proper base is guessed based on the leading
    characters of 's'.  Raises ParseStringError in case of error.
    Raises ParseStringOverflowError in case the result does not fit.
    """
    from rpython.rlib.rstring import (NumberStringParser,
                                      ParseStringOverflowError, strip_spaces)
    s = literal = strip_spaces(s)
    p = NumberStringParser(s, literal, base, 'int')
    base = p.base
    result = 0
    while True:
        digit = p.next_digit()
        if digit == -1:
            return result

        if p.sign == -1:
            digit = -digit

        try:
            result = ovfcheck(result * base)
            result = ovfcheck(result + digit)
        except OverflowError:
            raise ParseStringOverflowError(p)
Beispiel #6
0
def string_to_int(s, base=10):
    """Utility to converts a string to an integer.
    If base is 0, the proper base is guessed based on the leading
    characters of 's'.  Raises ParseStringError in case of error.
    Raises ParseStringOverflowError in case the result does not fit.
    """
    from rpython.rlib.rstring import (
        NumberStringParser, ParseStringOverflowError, strip_spaces)
    s = literal = strip_spaces(s)
    p = NumberStringParser(s, literal, base, 'int')
    base = p.base
    result = 0
    while True:
        digit = p.next_digit()
        if digit == -1:
            return result

        if p.sign == -1:
            digit = -digit

        try:
            result = ovfcheck(result * base)
            result = ovfcheck(result + digit)
        except OverflowError:
            raise ParseStringOverflowError(p)
Beispiel #7
0
def string_to_float(s):
    """
    Conversion of string to float.
    This version tries to only raise on invalid literals.
    Overflows should be converted to infinity whenever possible.

    Expects an unwrapped string and return an unwrapped float.
    """
    from rpython.rlib.rstring import strip_spaces, ParseStringError

    s = strip_spaces(s)

    if not s:
        raise ParseStringError("empty string for float()")


    low = s.lower()
    if low == "-inf" or low == "-infinity":
        return -INFINITY
    elif low == "inf" or low == "+inf":
        return INFINITY
    elif low == "infinity" or low == "+infinity":
        return INFINITY
    elif low == "nan" or low == "+nan":
        return NAN
    elif low == "-nan":
        return -NAN

    try:
        return rstring_to_float(s)
    except ValueError:
        raise ParseStringError("invalid literal for float(): '%s'" % s)
Beispiel #8
0
def string_to_float(s):
    """
    Conversion of string to float.
    This version tries to only raise on invalid literals.
    Overflows should be converted to infinity whenever possible.

    Expects an unwrapped string and return an unwrapped float.
    """
    from rpython.rlib.rstring import strip_spaces, ParseStringError

    s = strip_spaces(s)
    if not s:
        raise ParseStringError(INVALID_MSG)

    low = s.lower()
    if low == "-inf" or low == "-infinity":
        return -INFINITY
    elif low == "inf" or low == "+inf":
        return INFINITY
    elif low == "infinity" or low == "+infinity":
        return INFINITY
    elif low == "nan" or low == "+nan":
        return NAN
    elif low == "-nan":
        return -NAN

    try:
        return rstring_to_float(s)
    except ValueError:
        raise ParseStringError(INVALID_MSG)
Beispiel #9
0
def parse(input):
    program = []
    lines   = [rstring.strip_spaces(i) for i in rstring.split(input, '\n')]
    labels  = get_labels(lines)
    for line in lines:
        line = [i for i in rstring.split(line, ' ') if i]
        if not line:
            continue
        ins, args = line[0].upper(), [convert_arg(i, labels) for i in line[1:]]
        if ins[-1] == ':':
            continue
        elif ins == "COPY":
            val = Copy(args[0])
        elif ins == "CONST":
            val = Const(args[0])
        elif ins == "CALL":
            val = Call(args[0], args[1:])
        elif ins == "JUMP":
            val = Jump(args[0])
        elif ins == "RETURN":
            val = Return(args)
        elif ins == "PICK":
            val = Pick(args[0], args[1], args[2])
        elif ins in Binop.classes:
            val = Binop.get_cls(ins)(args[0], args[1])
        else:
            raise Exception("Unparsable instruction %s" % ins)
        program.append(val)
    return program[:]
Beispiel #10
0
def convert_arg(val, labels):
    val = rstring.strip_spaces(val)
    if val in labels:
        return labels[val]
    val = val[1:] if val[0] == 'b' or val[0] == 'B' else val
    int_rep = int(val, 10)
    return int_rep
Beispiel #11
0
def parse_bytecode_file(bytecode_file):
    """Parse a file of bytecode.

    The argument should be the text of the original file, with
    mnemonic bytecodes. The result will be a rcsp.box.ProgramBox
    object.
    """
    from rcsp.box import CodeBox, ProgramBox
    bytecode = []
    # Split bytecode into individual strings.
    # Throw away comments and whitespace.
    for line_ in bytecode_file.split('\n'):
        if DEBUG:
            line = line_.strip()
        else:
            line = rstring.strip_spaces(line_)
        if line.startswith('#'):
            continue
        else:
            if DEBUG:
                for code in line.split():
                    bytecode.append(code)
            else:
                bytecode.extend(rstring.split(line))
    # Parse bytecodes into numeric opcodes.
    functions = {}  # Dict of str names -> CodeBox objects
    counter = 0
    while counter < len(bytecode):
        opcodes, strings, integers, bools = [], [], [], []
        name = ''  # Name of each function which is parsed.
        code = bytecode[counter]
        # Parse a list of functions.
        if code == 'DEF':
            counter += 1  # Skip DEF.
            name = bytecode[counter]
            counter += 1  # Skip name of function.
            code = bytecode[counter]
            # TODO: Enable nested functions.
            while code != 'ENDDEF':
                # Handle general instructions.
                if code in opcode_mnemonics():
                    opcodes.append(opcode(code))
                # Handle literals.
                else:
                    if code.isdigit():
                        integers.append(int(code))
                        opcodes.append(len(integers) - 1)
                    elif code == 'True' or code == 'False':
                        bools.append(bool(code))
                        opcodes.append(len(bools) - 1)
                    else:
                        strings.append(code)
                        opcodes.append(len(strings) - 1)
                counter += 1
                code = bytecode[counter]
            functions[name] = CodeBox(opcodes, strings, integers, bools)
            # Ignore ENDDEF
            counter += 1
    return ProgramBox(functions)
Beispiel #12
0
def _fromstring_text(space, s, count, sep, length, dtype):
    sep_stripped = strip_spaces(sep)
    skip_bad_vals = len(sep_stripped) == 0

    items = []
    num_items = 0
    idx = 0

    while (num_items < count or count == -1) and idx < len(s):
        nextidx = s.find(sep, idx)
        if nextidx < 0:
            nextidx = length
        piece = strip_spaces(s[idx:nextidx])
        if len(piece) > 0 or not skip_bad_vals:
            if len(piece) == 0 and not skip_bad_vals:
                val = dtype.itemtype.default_fromstring(space)
            else:
                try:
                    val = dtype.coerce(space, space.wrap(piece))
                except OperationError, e:
                    if not e.match(space, space.w_ValueError):
                        raise
                    gotit = False
                    while not gotit and len(piece) > 0:
                        piece = piece[:-1]
                        try:
                            val = dtype.coerce(space, space.wrap(piece))
                            gotit = True
                        except OperationError, e:
                            if not e.match(space, space.w_ValueError):
                                raise
                    if not gotit:
                        val = dtype.itemtype.default_fromstring(space)
                    nextidx = length
            items.append(val)
            num_items += 1
Beispiel #13
0
def _fromstring_text(space, s, count, sep, length, dtype):
    sep_stripped = strip_spaces(sep)
    skip_bad_vals = len(sep_stripped) == 0

    items = []
    num_items = 0
    idx = 0

    while (num_items < count or count == -1) and idx < len(s):
        nextidx = s.find(sep, idx)
        if nextidx < 0:
            nextidx = length
        piece = strip_spaces(s[idx:nextidx])
        if len(piece) > 0 or not skip_bad_vals:
            if len(piece) == 0 and not skip_bad_vals:
                val = dtype.itemtype.default_fromstring(space)
            else:
                try:
                    val = dtype.coerce(space, space.wrap(piece))
                except OperationError, e:
                    if not e.match(space, space.w_ValueError):
                        raise
                    gotit = False
                    while not gotit and len(piece) > 0:
                        piece = piece[:-1]
                        try:
                            val = dtype.coerce(space, space.wrap(piece))
                            gotit = True
                        except OperationError, e:
                            if not e.match(space, space.w_ValueError):
                                raise
                    if not gotit:
                        val = dtype.itemtype.default_fromstring(space)
                    nextidx = length
            items.append(val)
            num_items += 1