def repl(hook): lineno = 0 tokens = [] while True: try: log = pyson.Log(pyson.get_logger(__name__), 3) if not tokens: line = pyson.util.prompt("pyson.parser >>> ") else: line = pyson.util.prompt("pyson.parser ... ") lineno += 1 tokens.extend(pyson.lexer.tokenize(pyson.StringSource("<stdin>", line), log, lineno)) while tokens: token_stream = iter(tokens) try: agent = parse_agent("<stdin>", token_stream, log, frozenset()) except StopIteration: log.throw() break else: log.throw() hook(agent) tokens = list(token_stream) except pyson.AggregatedError as error: print(str(error), file=sys.stderr) tokens = [] except KeyboardInterrupt: print() sys.exit(0)
def test_unexpected_eof(self): src = pyson.StringSource("<test>", "a") log = pyson.Log(pyson.get_logger(__name__), 3) tokens = pyson.lexer.TokenStream(src, log) with self.assertRaises(pyson.AggregatedError): pyson.parser.parse(tokens, log, frozenset())
def test_unify_return_value(self): src = pyson.StringSource("<test>", "+!p <- (X = 2) + 1 > 0.") log = pyson.Log(pyson.get_logger(__name__), 3) tokens = pyson.lexer.TokenStream(src, log) with self.assertRaises(pyson.AggregatedError): pyson.parser.parse(tokens, log, frozenset()) log.throw()
def test_rule_head_not_unifiable(self): src = pyson.StringSource("<test>", "rule(X + 1) :- true.") log = pyson.Log(pyson.get_logger(__name__), 3) tokens = pyson.lexer.TokenStream(src, log) pyson.parser.parse(tokens, log, frozenset()) with self.assertRaises(pyson.AggregatedError): log.throw()
def test_formula_type(self): src = pyson.StringSource("<test>", "+!plan <- ?true.") log = pyson.Log(pyson.get_logger(__name__), 3) tokens = pyson.lexer.TokenStream(src, log) pyson.parser.parse(tokens, log, frozenset()) with self.assertRaises(pyson.AggregatedError): log.throw()
def repl(agent, env, actions): lineno = 0 tokens = [] env = Environment() variables = {} intention = Intention() while True: try: log = pyson.Log(LOGGER, 3) try: if not tokens: line = pyson.util.prompt("%s >>> " % agent.name) else: line = pyson.util.prompt("%s ... " % agent.name) except KeyboardInterrupt: print() sys.exit(0) lineno += 1 tokens.extend( pyson.lexer.tokenize(pyson.StringSource("<stdin>", line), log, lineno)) while tokens: token_stream = iter(tokens) try: tok = next(token_stream) tok, body = pyson.parser.parse_plan_body( tok, token_stream, log) except StopIteration: log.throw() break else: log.throw() tokens = list(token_stream) intention.instr = Instruction(noop) body.accept( BuildInstructionsVisitor(variables, actions, intention.instr, log)) log.throw() agent.intentions.append(collections.deque([intention])) env.run_agent(agent) dump_variables(variables, intention.scope) except pyson.AggregatedError as error: print(str(error), file=sys.stderr) tokens = [] except pyson.PysonError as error: LOGGER.error("%s", error) tokens = []
def repl(): lineno = 1 while True: try: line = pyson.util.prompt("pyson.lexer >>> ") main(pyson.StringSource("<stdin>", line), lineno) lineno += 1 except pyson.AggregatedError as error: print(str(error), file=sys.stderr) except KeyboardInterrupt: print() sys.exit(0)
def _wait(agent, term, intention): # Handle optional arguments. args = [pyson.grounded(arg, intention.scope) for arg in term.args] if len(args) == 2: event, millis = args else: if pyson.is_number(args[0]): millis = args[0] event = None else: millis = None event = args[0] # Type checks. if not (millis is None or pyson.is_number(millis)): raise pyson.PysonError("expected timeout for .wait to be numeric") if not (event is None or pyson.is_string(event)): raise pyson.PysonError("expected event for .wait to be a string") # Event. if event is not None: # Parse event. if not event.endswith("."): event += "." log = pyson.Log(LOGGER, 1) tokens = pyson.lexer.TokenStream(pyson.StringSource("<.wait>", event), log) tok, ast_event = pyson.parser.parse_event(tokens.next(), tokens, log) if tok.lexeme != ".": raise log.error( "expected no further tokens after event for .wait, got: '%s'", tok.lexeme, loc=tok.loc) # Build term. event = ast_event.accept(pyson.runtime.BuildEventVisitor(log)) # Timeout. if millis is None: until = None else: until = agent.env.time() + millis / 1000 # Create waiter. intention.waiter = pyson.runtime.Waiter(event=event, until=until) yield