def ParseAndEval(code_str): w_parser, _ = parse_lib.MakeParserForCompletion(code_str) #spec = arith_parse.MakeShellSpec() #a_parser = tdop.TdopParser(spec, w_parser) # Calls ReadWord(LexMode.ARITH) #anode = a_parser.Parse() anode = w_parser._ReadArithExpr() # need the right lex state? if not anode: raise ExprSyntaxError("failed %s" % w_parser.Error()) print('node:', anode) mem = cmd_exec.Mem('', []) exec_opts = cmd_exec.ExecOpts() ev = word_eval.CompletionWordEvaluator(mem, exec_opts) arith_ev = expr_eval.ArithEvaluator(mem, ev) ok = arith_ev.Eval(anode) if ok: value = arith_ev.Result() print('value:', value) else: raise AssertionError(code_str) return value
def _MakeParser(code_str): # NOTE: We need the extra ]] token w_parser, _ = parse_lib.MakeParserForCompletion(code_str + ' ]]') w_parser._Next(LexMode.DBRACKET) # for tests only p = bool_parse.BoolParser(w_parser) if not p._Next(): raise AssertionError return p
def _MakeParser(code_str): # NOTE: We need the extra ]] token arena = test_lib.MakeArena('<bool_parse_test.py>') w_parser, _ = parse_lib.MakeParserForCompletion(code_str + ' ]]', arena) w_parser._Next(lex_mode_e.DBRACKET) # for tests only p = bool_parse.BoolParser(w_parser) if not p._Next(): raise AssertionError return p
def Matches(self, buf, status_out): arena = alloc.CompletionArena(self.pool) w_parser, c_parser = parse_lib.MakeParserForCompletion(buf, arena) comp_type, prefix, comp_words = _GetCompletionType( w_parser, c_parser, self.ev, status_out) comp_type, prefix, comp_words = _GetCompletionType1(self.parser, buf) # TODO: I don't get bash -D vs -E. Might need to write a test program. if comp_type == ECompletionType.VAR_NAME: # Non-user chain chain = self.var_comp elif comp_type == ECompletionType.HASH_KEY: # Non-user chain chain = 'TODO' elif comp_type == ECompletionType.REDIR_FILENAME: # Non-user chain chain = 'TODO' elif comp_type == ECompletionType.FIRST: chain = self.comp_lookup.GetFirstCompleter() elif comp_type == ECompletionType.REST: chain = self.comp_lookup.GetCompleterForName(comp_words[0]) elif comp_type == ECompletionType.NONE: # Null chain? No completion? For example, # ${a:- <TAB> -- we have no idea what to put here chain = 'TODO' else: raise AssertionError(comp_type) status_out.Write(0, 'Completing %r ... (Ctrl-C to cancel)', buf) start_time = time.time() index = len(comp_words) - 1 # COMP_CWORD -1 when it's empty i = 0 for m in chain.Matches(comp_words, index, prefix): # TODO: need to dedupe these yield m i += 1 elapsed = time.time() - start_time plural = '' if i == 1 else 'es' status_out.Write(0, '... %d match%s for %r in %.2f seconds (Ctrl-C to cancel)', i, plural, buf, elapsed) elapsed = time.time() - start_time plural = '' if i == 1 else 'es' status_out.Write(0, 'Found %d match%s for %r in %.2f seconds', i, plural, buf, elapsed)
def ParseAndEval(code_str): arena = test_lib.MakeArena('<arith_parse_test.py>') w_parser, _ = parse_lib.MakeParserForCompletion(code_str, arena) anode = w_parser._ReadArithExpr() # need the right lex state? if not anode: raise ExprSyntaxError("failed %s" % w_parser.Error()) print('node:', anode) mem = state.Mem('', [], {}, None) exec_opts = state.ExecOpts(mem) splitter = legacy.SplitContext(mem) ev = word_eval.CompletionWordEvaluator(mem, exec_opts, splitter) arith_ev = expr_eval.ArithEvaluator(mem, exec_opts, ev) value = arith_ev.Eval(anode) return value
def _TestGetCompletionType(buf): ev = _MakeTestEvaluator() arena = test_lib.MakeArena('<completion_test.py>') w_parser, c_parser = parse_lib.MakeParserForCompletion(buf, arena) print('---', buf) return completion._GetCompletionType(w_parser, c_parser, ev, STATUS)
def _TestGetCompletionType(buf): ev = _MakeTestEvaluator() w_parser, c_parser = parse_lib.MakeParserForCompletion(buf) print('---', buf) return completion._GetCompletionType(w_parser, c_parser, ev, STATUS)