def push_state(): global restrict_stacks, prodname_log, scopestack, parsescope statestack.append([restrict_stacks, prodname_log, glob.copy(), scopestack, parsescope]) restrict_stacks = {"noline": []} prodname_log = [] scopestack = [] parsescope = {} glob.reset()
def js_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 js_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 + len(buf) 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 js_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
def js_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 js_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 + len(buf) 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 js_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
def js_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] == "$": if safe_get(data, i + 1) == "$": #handle '$$' data = data[:i] + data[i + 1:] i += 1 continue 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 js_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 + len(buf) i += 1 glob.reset() glob.g_exit_on_err = exit_on_err glob.g_lexer = plexer linemap = glob.g_lexer.linemap glob.g_lexdata = data 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 glob.g_inside_js_parse = True plexer.lineno = plexer.lexer.lineno = 0 plexer.input(data) ret = None try: ret = parser.parse(data, lexer=plexer) except: pass glob.g_inside_js_parse = False glob.g_lexer.linemap = linemap if glob.g_error: ret = None if ret is None: ls = data.split("\n") s2 = "" for i in range(len(ls)): s2 += str(i + 1) + ": " + ls[i] + "\n" print(s2) col = glob.g_lexpos i = min(col, len(data) - 1) while i >= 0 and i < len(data) and data[i] != "\n": i -= 1 if i > 0: col -= i pass i = max(i, 0) i2 = i + 1 while i2 < len(data) and data[i2] != "\n": i2 += 1 #line = data.split("\n")[glob.g_line] if data[i] == "\n": line = data[i + 1:i2] else: line = data[i:i2] while col < len(line) and col >= 0 and line[col] in [ "\n", "\r", "\t", " " ]: col += 1 if col > 0 and col < len(line): line = line[:col] + termColor(line[col], 41) + line[col + 1:] raise SyntaxError("(js_parse intern):" + str(glob.g_line + 1) + ":" + str(col + 1) + "\n\t" + line) if ret: flatten_statementlists(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 js_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