def swank_autodoc(self, sexp, _=None, line_width=None): """ (:emacs-rex (swank:autodoc '("ql:quickload" "" swank::%cursor-marker%) :print-right-margin 102) "COMMON-LISP-USER" :repl-thread 19) (:return (:ok ("(quickload ===> systems <=== &key (verbose quicklisp-client:*quickload-verbose*) silent\n (prompt quicklisp-client:*quickload-prompt*) explain &allow-other-keys)" t)) 19) """ try: # unquote if isinstance(sexp, Quoted): # For instance in emacs 27 sexp = sexp.value() else: sexp = sexp[1] # [Symbol('quote'), [ ... ]] scope, cursor = current_scope(sexp) log.debug("scope: %s, cursor: %s" % (scope, cursor)) assert cursor > 0 func = scope[0] scope = scope[:-1] # remove marker result = self.arglist(func, cursor, scope) if result: yield EuslispResult(result) else: yield EuslispResult([Symbol(":not-available"), True]) except Exception: log.error(traceback.format_exc()) yield EuslispResult([Symbol(":not-available"), True])
def swank_invoke_nth_restart_for_emacs(self, level, num): deb = self.debugger.pop(level - 1) res_dict = deb.restarts_dict def check_key(key): return key in res_dict and num == res_dict[key] if check_key('RESTART'): self.debugger = [] self.restart_euslisp_process() elif check_key('CONTINUE'): pass elif check_key('QUIT'): self.debugger = [] self.euslisp.reset() else: log.error("Restart not found!") msg = deb.message.split(self.euslisp.delim)[0] msg = repr(msg.rsplit(' in ', 1)[0]) yield [Symbol(':debug-return'), 0, level, Symbol('nil')] yield [Symbol(':return'), {'abort': 'NIL'}, self.command_id] for val in self.maybe_new_prompt(): yield val yield [Symbol(':return'), {'abort': msg}, deb.id]
def swank_load_file(self, filename): lock = self.euslisp.euslime_connection_lock log.debug('Acquiring lock: %s' % lock) lock.acquire() yield [Symbol(":write-string"), "Loading file: %s ...\n" % filename] try: cmd = '(lisp:load "{0}")'.format(qstr(filename)) for r in self.euslisp.eval(cmd): if isinstance(r, list): yield r yield [Symbol(":write-string"), "Loaded.\n"] yield EuslispResult(True) lock.release() except AbortEvaluation as e: if lock.locked(): lock.release() log.info('Aborting evaluation...') # Force-print the message, which is # by default only displayed on the minibuffer if e.message: yield [ Symbol(":write-string"), "; Evaluation aborted on {}\n".format(e.message), Symbol(":repl-result") ] yield EuslispResult(None) except Exception: if lock.locked(): lock.release() raise
def _transform(self, smtlib): if isinstance(smtlib, Symbol) or isinstance(smtlib, int): return smtlib elif isinstance(smtlib, str): smtlib = smtlib.replace('\\', '\\\\') smtlib = smtlib.replace('\\\\v', '\\v') smtlib = smtlib.replace('\\\\x', '\\x') return smtlib if isinstance(smtlib[0], list): return [self._transform(subsmtlib) for subsmtlib in smtlib] stmtname = smtlib[0].value() if stmtname in ('set-logic', 'set-option'): return None elif stmtname == 'get-value': return Symbol('get-model'), elif stmtname == 'declare-fun': declare, name, func, smttype = smtlib return Symbol('declare-variable'), name, smttype elif stmtname == 'str.prefixof': stmtname, prefix, string = smtlib return Symbol('StartsWith'), self._transform( string), self._transform(prefix) elif stmtname in Z3Str2Wrapper.rename: return [Symbol(Z3Str2Wrapper.rename[stmtname])] + [ self._transform(subsmtlib) for subsmtlib in smtlib[1:] ] elif len(smtlib) > 1: return [smtlib[0]] + [ self._transform(subsmtlib) for subsmtlib in smtlib[1:] ] return smtlib
def get_output(self, recursive=False): while True: try: out = self.output.get(timeout=self.rate) has_token = out.rsplit(self.token, 1) if has_token[0]: yield has_token[0] if len(has_token) >= 2 or not has_token[0]: # Check for Errors gen = self.get_socket_response(recursive=recursive) # Print Results # Do not use :repl-result presentation # to enable copy-paste of previous results, # which are signilized as swank objects otherwise # e.g. #.(swank:lookup-presented-object-or-lose 0.) if gen: # yield [Symbol(":presentation-start"), 0, # Symbol(":repl-result")] for r in gen: # Colors are not allowed in :repl-result formatting yield [ Symbol(":write-string"), no_color(r), Symbol(":repl-result") ] # yield [Symbol(":presentation-end"), 0, # Symbol(":repl-result")] yield [ Symbol(":write-string"), '\n', Symbol(":repl-result") ] return except Empty: self.check_poll() continue
def get_socket_response(self, connection, recursive=False): command, data = self.recv_socket_data(connection) log.debug('Socket Request Type: %s' % command) if command == 'result': return data # Process generator to avoid pendant messages data = gen_to_string(data) if command == 'read': if self.read_busy: return self.read_busy = True return [Symbol(":read-string"), 0, 1] if command == 'read-mode': log.debug("Entering read mode...") self.read_mode = True return [Symbol(":read-string"), 0, 1] if command == 'error': if recursive: log.debug('Waiting for repl output...') self.finished_output.wait() return msg = loads(data) stack = self.get_callstack() if connection == self.euslime_internal_connection: raise EuslispInternalError(msg, stack) else: raise EuslispError(msg, stack) if command == 'abort': msg = loads(data) if msg: msg = "'{}'".format(msg) # Better formatting raise AbortEvaluation(msg) raise Exception("Unhandled Socket Request Type: %s" % command)
def exec_internal(self, cmd_str, force_repl_socket=False): if force_repl_socket: # When the command must be evaluated in the main thread due to # e.g. Thread Special variables # Locks are performed from outside to yield results before release connection = self.euslime_connection log.info('exec_internal(repl): %s' % cmd_str) else: connection = self.euslime_internal_connection log.info('exec_internal: %s' % cmd_str) lock = self.euslime_internal_connection_lock log.debug('Acquiring lock: %s' % lock) lock.acquire() try: self.clear_socket_stack(connection) connection.send(cmd_str + self.delim) gen = self.get_socket_result(connection) res = gen_to_string(gen) if not force_repl_socket: lock.release() # Keep nil as a symbol to be dump-reversible res = loads(res, nil=None) # But return python-false objects if the response is solely 'nil' if res == Symbol("lisp:nil") or res == Symbol("nil"): return [] return res except Exception: if not force_repl_socket and lock.locked(): lock.release() raise
def parse_net(node): if isinstance(node, list): for n in node: if isinstance(n, list): if n[0] == Symbol("components"): for c in n[1:]: if c[0] == Symbol("comp"): parse_comp(c[1:])
def mod_path_file(modpath): if modpath[0] == Symbol("MPdot"): return mod_path_file(modpath[1]) elif modpath[0] == Symbol("MPfile"): return ".".join([symbol2str(x[1]) for x in modpath[1][1]][::-1]) else: assert modpath[0] == Symbol("MPbound") return ""
def mod_path_file(modpath): if modpath[0] == Symbol('MPdot'): return mod_path_file(modpath[1]) elif modpath[0] == Symbol('MPfile'): return '.'.join([symbol2str(x[1]) for x in modpath[1][1]][::-1]) else: assert modpath[0] == Symbol('MPbound') return ''
def interrupt(self): yield self.dumps([Symbol(":read-aborted"), 0, 1]) self.handler.euslisp.process.send_signal(signal.SIGINT) self.handler.euslisp.reset() yield self.dumps([ Symbol(':return'), { 'abort': "'Keyboard Interrupt'" }, self.handler.command_id ])
def print_mod_path(modpath): if modpath[0] == Symbol('MPdot'): return print_mod_path(modpath[1]) + '.' + symbol2str(modpath[2][1]) elif modpath[0] == Symbol('MPfile'): return '.'.join([symbol2str(x[1]) for x in modpath[1][1]][::-1]) else: assert modpath[0] == Symbol('MPbound') return '.'.join([symbol2str(x[1]) for x in modpath[1][2][1]][::-1] + [symbol2str(modpath[1][1][1])])
def print_mod_path(modpath): if modpath[0] == Symbol("MPdot"): return print_mod_path(modpath[1]) + "." + symbol2str(modpath[2][1]) elif modpath[0] == Symbol("MPfile"): return ".".join([symbol2str(x[1]) for x in modpath[1][1]][::-1]) else: assert modpath[0] == Symbol("MPbound") return ".".join([symbol2str(x[1]) for x in modpath[1][2][1]][::-1] + [symbol2str(modpath[1][1][1])])
def ppCommand(cmd) -> str: if get_cmd_type(cmd) == Symbol("StmAdd"): return "(*{}:*) {}".format(get_id(cmd), getAddBody(cmd)) elif get_cmd_type(cmd) == Symbol("StmCancel"): return "(*CANCEL {}*)".format(getCancelDest(cmd)) elif get_cmd_type(cmd) == Symbol("Failed"): return "(*FAILED {}*)".format(getCancelDest(cmd)) else: assert get_cmd_type(cmd) == Symbol("StmObserve") return ""
def send(self, cmd): 'Send a command to SerAPI and retrieve the responses' #print(cmd) assert '\n' not in cmd self.proc.sendline(cmd) try: self.proc.expect([ '\(Answer \d+ Ack\)\x00.*\(Answer \d+ Completed\)\x00', '\(Answer \d+ Ack\)\x00.*\(Answer \d+\(CoqExn.*\)\x00' ]) except pexpect.TIMEOUT as ex: print(self.proc.before) raise CoqTimeout raw_responses = self.proc.after #print(raw_responses) ack_num = int( re.search(r'^\(Answer (?P<num>\d+)', raw_responses)['num']) for num in re.findall(r'(?<=\(Answer) \d+', raw_responses): assert int(num) == ack_num responses = [] msg_str = [] for item in raw_responses.split('\x00'): item = item.strip() if item == '': continue if not item.startswith('(Feedback') and not item.startswith( '(Answer'): m = re.search(r'\(Feedback|\(Answer', item) if m is None: continue item = item[m.span()[0]:] assert item.endswith(')') parsed_item = sexpdata.loads(item, nil=None, true=None) if 'CoqExn' in item: # an error occured in Coq assert parsed_item[2][0] == Symbol('CoqExn') raise CoqExn(sexpdata.dumps(parsed_item[2][4]), sexpdata.dumps(parsed_item[2])) if item.startswith('(Feedback'): # ignore Feedback for now try: msg = parsed_item[1][3][1] if isinstance(msg, list) and msg != [] and msg[0] == Symbol( 'Message'): msg_sexp, _ = self.send( '(Print ((pp_format PpStr)) (CoqPp %s))' % sexpdata.dumps(msg[3])) msg_str.extend( [symbol2str(x[1]) for x in msg_sexp[1][2][1]]) except IndexError: pass continue responses.append(parsed_item) msg_str = '\n'.join(msg_str) return responses, raw_responses
def loadPBETasks(directory="PBE_Strings_Track"): """ Processes sygus benchmarks into task objects For these benchmarks, all of the constant strings are given to us. In a sense this is cheating (nb: the production release of flashfill does something equivalent to this "cheating") Returns (tasksWithoutCheating, tasksWithCheating) """ import os from sexpdata import loads, Symbol def findStrings(s): if isinstance(s, list): return [y for x in s for y in findStrings(x)] if isinstance(s, str): return [s] return [] tasks = [] cheatingTasks = [] for f in os.listdir(directory): if not f.endswith('.sl'): continue with open(directory + "/" + f, "r") as handle: message = "(%s)" % (handle.read()) expression = loads(message) constants = [] name = f examples = [] for e in expression: if len(e) == 0: continue if e[0] == Symbol('constraint'): e = e[1] assert e[0] == Symbol('=') inputs = e[1] assert inputs[0] == Symbol('f') inputs = inputs[1:] output = e[2] examples.append((inputs, output)) elif e[0] == Symbol('synth-fun'): assert e[1] == Symbol('f') constants += findStrings(e) task = Task(name, arrow(*[tstr] * (len(examples[0][0]) + 1)), [(tuple(xs), y) for xs, y in examples]) cheat = Task( name + "_cheating", arrow(*[tstr] * (len(examples[0][0]) + 1 + len(constants))), [(tuple(constants + xs), y) for xs, y in examples]) tasks.append(task) print name print "\n".join(map(str, examples[:3])) cheatingTasks.append(cheat) return tasks, cheatingTasks
def swank_eval(self, sexp): yield [Symbol(":read-string"), 0, 1] try: for out in self.euslisp.eval(sexp): if isinstance(out, EuslispResult): yield [Symbol(":read-aborted"), 0, 1] for val in self.maybe_new_prompt(): yield val yield out except Exception as e: yield [Symbol(":read-aborted"), 0, 1] raise e
def expression(e, environment): for n, v in enumerate(environment): if e == v: return Index(n) if isinstance(e,int): return Program.parse(str(e)) assert isinstance(e,list) if e[0] == Symbol('+'): return Application(Application(_addition, expression(e[1], environment)), expression(e[2], environment)) if e[0] == Symbol('-'): return Application(Application(_subtraction, expression(e[1], environment)), expression(e[2], environment)) assert False
def send(self, cmd): "Send a command to SerAPI and retrieve the responses" # print(cmd) assert "\n" not in cmd self.proc.sendline(cmd) try: self.proc.expect([ "\(Answer \d+ Ack\)\x00.*\(Answer \d+ Completed\)\x00", "\(Answer \d+ Ack\)\x00.*\(Answer \d+\(CoqExn.*\)\x00", ]) except pexpect.TIMEOUT as ex: print(self.proc.before) raise CoqTimeout raw_responses = self.proc.after # print(raw_responses) ack_num = int( re.search(r"^\(Answer (?P<num>\d+)", raw_responses)["num"]) for num in re.findall(r"(?<=\(Answer) \d+", raw_responses): assert int(num) == ack_num responses = [] msg_str = [] for item in raw_responses.split("\x00"): item = item.strip() if item == "": continue if not item.startswith("(Feedback") and not item.startswith( "(Answer"): m = re.search(r"\(Feedback|\(Answer", item) if m is None: continue item = item[m.span()[0]:] assert item.endswith(")") parsed_item = sexpdata.loads(item, nil=None, true=None) if "CoqExn" in item: # an error occured in Coq assert parsed_item[2][0] == Symbol("CoqExn") raise CoqExn(sexpdata.dumps(parsed_item[2][4]), sexpdata.dumps(parsed_item[2])) if item.startswith("(Feedback"): # ignore Feedback for now try: msg = parsed_item[1][3][1] if (isinstance(msg, list) and msg != [] and msg[0] == Symbol("Message")): msg_sexp, _ = self.send( "(Print ((pp_format PpStr)) (CoqPp %s))" % sexpdata.dumps(msg[3])) msg_str.extend( [symbol2str(x[1]) for x in msg_sexp[1][2][1]]) except IndexError: pass continue responses.append(parsed_item) msg_str = "\n".join(msg_str) return responses, raw_responses
def parse_module(module): if isinstance(module[1], Symbol): package = module[1]._val else: package = module[1] x = "" y = "" a = "" ref = "" layer = "" attr = "" for n in module[2:]: if isinstance(n, list): if n[0] == Symbol('layer'): if isinstance(n[1], Symbol): layer = n[1]._val else: layer = n[1] if layer == "F.Cu": layer = "top" else: layer = "bottom" if n[0] == Symbol('at'): x = n[1] y = n[2] if len(n) > 3: a = n[3] else: a = "0" if n[0] == Symbol('attr'): if isinstance(n[1], Symbol): attr = n[1]._val else: attr = n[1] if n[0] == Symbol('fp_text'): if n[1] == Symbol('reference'): if isinstance(n[2], Symbol): ref = n[2]._val else: ref = n[2] for p in rot_package: if p[0] == package: a = float(a) + p[1] for p in bom_all: if p[0] == ref: for lp in rot_part: if lp[0] == p[3]: a = float(a) + lp[1] #print("rotate", p[3], p[0]) place_all.append((ref, package, layer, attr, x, y, a))
def command(k, environment, continuation): if k == Symbol("1x3") or k == Symbol("v"): return Application(_13, continuation) if k == Symbol("3x1") or k == Symbol("h"): return Application(_31, continuation) assert isinstance(k, list) if k[0] == Symbol("r"): return Application(Application(_r, expression(k[1], environment)), continuation) if k[0] == Symbol("l"): return Application(Application(_l, expression(k[1], environment)), continuation) if k[0] == Symbol("for"): v = k[1] b = expression(k[2], environment) newEnvironment = [None, v] + environment body = block(k[3:], newEnvironment, Index(0)) return Application( Application(Application(_lp, b), Abstraction(Abstraction(body))), continuation) if k[0] == Symbol("embed"): body = block(k[1:], [None] + environment, Index(0)) return Application(Application(_e, Abstraction(body)), continuation) assert False
def test_no_such_method(self): with logging_to_stdout(self.server.logger): self.client_send('(call 3 no_such_method nil)') reply = self.receive_message() self.assertEqual(reply[0], Symbol('epc-error')) self.assertEqual(reply[1], 3) assert 'No such method' in reply[2]
def process(self, data): data = loads(data) if data[0] == Symbol(":emacs-rex"): cmd, form, pkg, thread, comm_id = data self.handler.command_id = comm_id self.handler.package = pkg else: form = data comm_id = None func = form[0].value().replace(':', '_').replace('-', '_') args = form[1:] log.info("func: %s" % func) log.info("args: %s" % args) try: gen = getattr(self.handler, func)(*args) if not gen: if comm_id: for r in self.make_response(comm_id, None): yield r return for resp in gen: if isinstance(resp, EuslispResult): for r in self.make_response(self.handler.command_id, resp.value): yield r else: yield self.dumps(resp) except Exception as e: log.error(traceback.format_exc()) for r in self.make_error(self.handler.command_id, e): yield r
def get_callstack(self, end=10): self.output = [] self.finished_output.clear() self.accumulate_output = True self.clear_socket_stack(self.euslime_connection) cmd_str = '(slime:print-callstack {})'.format(end + 4) log.info('exec: %s' % cmd_str) self.euslime_connection.send(cmd_str + self.delim) self.get_socket_response(self.euslime_connection, recursive=True) self.accumulate_output = False stack = gen_to_string(self.output) stack = [x.strip() for x in stack.split(self.delim)] # Remove 'Call Stack' and dummy error messages # 'Call Stack (max depth: 10):', # '0: at (slime:print-callstack 10)', # '1: at slime:slime-error', # '2: at slime:slime-error' stack = stack[4:] strace = [] for i, line in enumerate(stack): split_line = line.split(": at ", 1) if len(split_line) == 2: strace.append( [i, split_line[1], [Symbol(":restartable"), False]]) else: break self.euslime_connection.send( '(lisp:reset lisp:*replevel*)' + self.delim) return strace
def assoc(key, sexp): if not isinstance(sexp, list): return None for entry in sexp: if isinstance(entry, list) and entry[0] == Symbol(key): return entry[1] return None
def make_response(self, id, sexp): try: res = [Symbol(':return'), {'ok': sexp}, id] yield self.dumps(res) except Exception as e: for r in self.make_error(id, e): yield r
def parseSexpOneLevel(sexp_str: str) -> Union[List[str], int, Symbol]: sexp_str = sexp_str.strip() if sexp_str[0] == '(': items = [] cur_pos = 1 item_start_pos = 1 paren_level = 0 while True: next_match = parsePat.search(sexp_str, cur_pos) if not next_match: break cur_pos = next_match.end() if sexp_str[cur_pos - 1] == "(": paren_level += 1 elif sexp_str[cur_pos - 1] == ")": paren_level -= 1 if paren_level == 0: items.append(sexp_str[item_start_pos:cur_pos]) item_start_pos = cur_pos else: assert sexp_str[cur_pos - 1] == " " if paren_level == 0: items.append(sexp_str[item_start_pos:cur_pos]) item_start_pos = cur_pos elif re.fullmatch(r"\d+", sexp_str): return int(sexp_str) elif re.fullmatch(r"\w+", sexp_str): return Symbol(sexp_str) else: assert False, f"Couldn't parse {sexp_str}" return items
def test_error_in_method(self): with logging_to_stdout(self.server.logger): self.client_send('(call 2 bad_method nil)') result = self.client.recv(1024) expected = encode_object([ Symbol('return-error'), 2, repr(self.error_to_throw)]) self.assertEqual(result, expected)
def test_call_client_methods_info(self): self.handler.methods(self.callback) (methods, uid) = self.receive_message() self.assertEqual(methods.value(), 'methods') self.client_send('(return {0} ((dummy () "")))'.format(uid)) reply = self.callback_called_with.get(True, 1) self.assertEqual(reply, [[Symbol('dummy'), [], ""]])
def test_invalid_sexp(self): with logging_to_stdout(self.server.logger): self.client_send('(((invalid sexp!') reply = self.receive_message() self.assertEqual(reply[0].value(), Symbol('epc-error').value()) self.assertEqual(reply[1], []) # uid assert 'Not enough closing brackets.' in reply[2]
def test_parse_special_symbols(self): for s in [r'\\', r"\'", r"\`", r'\"', r'\(', r'\)', r'\[', r'\]', r'\ ', r'\.', r'\,', r'\?', r'\;', r'\#']: self.assert_parse(s, Symbol(Symbol.unquote(s)))