Example #1
0
def parse_intern(data, create_logger=False, expand_loops=True, expand_generators=True):
    glob.g_lines = data.split("\n")

    if glob.g_preprocess_code:
        data = preprocess_text(data, glob.g_file)

    if glob.g_print_tokens:
        plexer.input(data)
        tok = plexer.token()
        while tok != None:
            print(tok)
            tok = plexer.token()
        plexer.input(data)

    glob.g_lexer = plexer
    result = parser.parse(data, lexer=plexer)

    if result == None:
        if glob.g_error_pre != None:
            glob.g_error = True

        result = StatementList()

    if glob.g_error:
        print_err(glob.g_error_pre)

    typespace = TypeSpace()

    """
  if glob.g_write_manifest and glob.g_outfile != "":
    buf = gen_manifest_file(result, typespace);
    file = open(glob.g_outfile+".manifest", "w")
    file.write(buf)
    file.close()
  """

    expand_typed_classes(result, typespace)

    if glob.g_clear_slashr:
        print("\n")

    flatten_statementlists(result, typespace)

    if glob.g_combine_ifelse_nodes:
        combine_if_else_nodes(result)

    if glob.g_print_nodes:
        print("nodes: ", result)
        pass

    # if glob.g_enable_static_vars:
    #  process_static_vars(result, typespace)

    buf = gen_cpp(result, typespace)

    if glob.g_outfile == "":
        print("\n")
        print(buf)

    return buf, result
Example #2
0
def parse_intern(data,
                 create_logger=False,
                 expand_loops=True,
                 expand_generators=True):
    glob.g_lines = data.split("\n")

    if glob.g_preprocess_code:
        data = preprocess_text(data, glob.g_file)

    if glob.g_print_tokens:
        plexer.input(data)
        tok = plexer.token()
        while tok != None:
            print(tok)
            tok = plexer.token()
        plexer.input(data)

    glob.g_lexer = plexer
    result = parser.parse(data, lexer=plexer)

    if result == None:
        if glob.g_error_pre != None:
            glob.g_error = True

        result = StatementList()

    if glob.g_error:
        print_err(glob.g_error_pre)

    typespace = TypeSpace()
    """
  if glob.g_write_manifest and glob.g_outfile != "":
    buf = gen_manifest_file(result, typespace);
    file = open(glob.g_outfile+".manifest", "w")
    file.write(buf)
    file.close()
  """

    expand_typed_classes(result, typespace)

    if glob.g_clear_slashr:
        print("\n")

    flatten_statementlists(result, typespace)

    if glob.g_combine_ifelse_nodes:
        combine_if_else_nodes(result)

    if glob.g_print_nodes:
        print("nodes: ", result)
        pass

    #if glob.g_enable_static_vars:
    #  process_static_vars(result, typespace)

    buf = gen_cpp(result, typespace)

    if glob.g_outfile == "":
        print("\n")
        print(buf)

    return buf, result
Example #3
0
def dt_parse(data,
             args=None,
             file="",
             flatten=True,
             print_stack=True,
             start_node=None,
             print_warnings=False,
             exit_on_err=True,
             log_productions=False,
             validate=False):
    back = glob.copy()

    def safe_get(data, i):
        if i < 0: return ""
        elif i >= len(data): return ""

        return data[i]

    if args != None:
        if not isinstance(args, tuple) and not isinstance(args, list):
            if caniter(args) and not isinstance(args, Node) \
                 and type(args) not in [str, bytes]:
                args = list(args)
            else:
                args = (args, )  #encapsulate single arguments in a tuple

        i = 0
        ai = 0
        while i < len(data) - 2:
            if data[i] == "$" and safe_get(data, i - 1) != "$":
                i1 = i
                t = data[i + 1]
                i += 2

                arg, i = fetch_int(data, i)
                if arg == None:
                    arg = ai
                    ai += 1
                else:
                    arg -= 1
                    ai = max(arg, ai)

                if arg >= len(args):
                    raise RuntimeError(
                        "Not enough args for format conversion in parse()")

                if t == "n":
                    buf = args[arg].gen_js(0)
                elif t == "s":
                    buf = str(args[arg])
                elif t in ["d", "i", "u"]:
                    buf = str(int(args[arg]))
                elif t in ["f", "lf"]:
                    buf = str(float(args[arg]))
                elif t == "x":
                    buf = hex(int(args[arg]))
                else:
                    buf = data[i1:i]

                data = data[:i1] + buf + data[i:]

                i = i1
            i += 1

    glob.reset()
    glob.g_exit_on_err = exit_on_err
    glob.g_lexer = plexer
    glob.g_production_debug = False
    glob.g_file = file
    glob.g_print_stack = print_stack
    glob.g_print_warnings = print_warnings
    glob.g_log_productions = log_productions
    glob.g_validate_mode = validate

    plexer.lineno = plexer.lexer.lineno = 0
    plexer.input(data)

    ret = parser.parse(data, lexer=plexer)
    if glob.g_error:
        print("------------LLLLLLLLLLLLLLLLLLL yeek!!!")
        ret = None

    if glob.g_clear_slashr:
        print("\n")

    def fix_parents(node, lastnode=None):
        if node.parent in [0, None]:
            node.parent = lastnode

        for c in node.children:
            fix_parents(c, node)

    if ret != None:
        fix_parents(ret)
        if flatten:
            ret = flatten_statementlists(ret, None)
            if ret == None:
                traceback.print_stack()
                sys.stderr.write("error: internal parse error within parse\n")
                sys.exit(-1)

    if start_node != None and ret != None:

        def visit(n):
            if type(n) == start_node:
                return n

            for c in n.children:
                c2 = visit(c)
                if c2 != None:
                    return c2

        ret = visit(ret)

    if ret != None:
        combine_try_nodes(ret)

    glob.load(back)

    return ret
Example #4
0
def dt_parse(
    data,
    args=None,
    file="",
    flatten=True,
    print_stack=True,
    start_node=None,
    print_warnings=False,
    exit_on_err=True,
    log_productions=False,
    validate=False,
):
    back = glob.copy()

    def safe_get(data, i):
        if i < 0:
            return ""
        elif i >= len(data):
            return ""

        return data[i]

    if args != None:
        if not isinstance(args, tuple) and not isinstance(args, list):
            if caniter(args) and not isinstance(args, Node) and type(args) not in [str, bytes]:
                args = list(args)
            else:
                args = (args,)  # encapsulate single arguments in a tuple

        i = 0
        ai = 0
        while i < len(data) - 2:
            if data[i] == "$" and safe_get(data, i - 1) != "$":
                i1 = i
                t = data[i + 1]
                i += 2

                arg, i = fetch_int(data, i)
                if arg == None:
                    arg = ai
                    ai += 1
                else:
                    arg -= 1
                    ai = max(arg, ai)

                if arg >= len(args):
                    raise RuntimeError("Not enough args for format conversion in parse()")

                if t == "n":
                    buf = args[arg].gen_js(0)
                elif t == "s":
                    buf = str(args[arg])
                elif t in ["d", "i", "u"]:
                    buf = str(int(args[arg]))
                elif t in ["f", "lf"]:
                    buf = str(float(args[arg]))
                elif t == "x":
                    buf = hex(int(args[arg]))
                else:
                    buf = data[i1:i]

                data = data[:i1] + buf + data[i:]

                i = i1
            i += 1

    glob.reset()
    glob.g_exit_on_err = exit_on_err
    glob.g_lexer = plexer
    glob.g_production_debug = False
    glob.g_file = file
    glob.g_print_stack = print_stack
    glob.g_print_warnings = print_warnings
    glob.g_log_productions = log_productions
    glob.g_validate_mode = validate

    plexer.lineno = plexer.lexer.lineno = 0
    plexer.input(data)

    ret = parser.parse(data, lexer=plexer)
    if glob.g_error:
        print("------------LLLLLLLLLLLLLLLLLLL yeek!!!")
        ret = None

    if glob.g_clear_slashr:
        print("\n")

    def fix_parents(node, lastnode=None):
        if node.parent in [0, None]:
            node.parent = lastnode

        for c in node.children:
            fix_parents(c, node)

    if ret != None:
        fix_parents(ret)
        if flatten:
            ret = flatten_statementlists(ret, None)
            if ret == None:
                traceback.print_stack()
                sys.stderr.write("error: internal parse error within parse\n")
                sys.exit(-1)

    if start_node != None and ret != None:

        def visit(n):
            if type(n) == start_node:
                return n

            for c in n.children:
                c2 = visit(c)
                if c2 != None:
                    return c2

        ret = visit(ret)

    if ret != None:
        combine_try_nodes(ret)

    glob.load(back)

    return ret