def goal(rb_name, rule_name, goal_info, pred_plan_lines, python_lines): # returns plan_lines, goal_fn_head, goal_fn_tail, goal_decl_lines goal, goal_name, pattern_args, taking, start_lineno, end_lineno = goal_info assert goal == 'goal' goal_fn_head = ( "", "def %s(rule, arg_patterns, arg_context):" % rule_name, ("INDENT", 2), "engine = rule.rule_base.engine", "patterns = rule.goal_arg_patterns()", "if len(arg_patterns) == len(patterns):", ("INDENT", 2), "context = contexts.bc_context(rule)", "try:", ("INDENT", 2), ("STARTING_LINENO", start_lineno), "if all(map(lambda pat, arg:", ("INDENT", 2), ("INDENT", 9), ("INDENT", 2), "pat.match_pattern(context, context,", ("INDENT", 18), "arg, arg_context),", "POPINDENT", "POPINDENT", "patterns,", "arg_patterns)):", ("ENDING_LINENO", end_lineno), "POPINDENT", "rule.rule_base.num_bc_rules_matched += 1", ) goal_fn_tail = ( "POPINDENT", "POPINDENT", "finally:", ("INDENT", 2), "context.done()", "POPINDENT", "POPINDENT", "POPINDENT", ) if not taking and not pred_plan_lines and not python_lines: plan_fn_name = "None" plan_lines = () else: plan_fn_name = "%s_plans.%s" % (rb_name, rule_name) def_start = "def %s" % rule_name taking = [line.strip() for line in taking if line.strip()] if not taking: def_head = def_start + '(context):' else: if taking[0][0] != '(' or taking[-1][-1] != ')': from pyke.krb_compiler import scanner end = scanner.lexer.lexpos taking_start = scanner.lexer.lexdata.rfind('taking', 0, end) if taking_start < 0: raise SyntaxError("'taking' clause: missing parenthesis", scanner.syntaxerror_params()) taking_start += len('taking') while taking_start < len(scanner.lexdata) and \ scanner.lexdata[taking_start].isspace(): taking_start += 1 lineno = scanner.lexer.lineno - \ scanner.lexer.lexdata.count('\n', taking_start, scanner.lexer.lexpos) raise SyntaxError( "'taking' clause: missing parenthesis", scanner.syntaxerror_params(taking_start, lineno)) taking[0] = def_start + "(context, " + taking[0][1:] taking[-1] += ':' if len(taking) == 1: def_head = taking[0] else: def_head = ( taking[0], ('INDENT', 4), tuple(taking[1:]), "POPINDENT", ) plan_lines = ( "", def_head, ('INDENT', 2), pred_plan_lines, python_lines, "POPINDENT", ) goal_decl_lines = ( "", "bc_rule.bc_rule(%r, This_rule_base, %r," % (rule_name, goal_name), ("INDENT", 16), "%s, %s," % (rule_name, plan_fn_name), ) + list_format(pattern_args, "(", "),") return plan_lines, goal_fn_head, goal_fn_tail, goal_decl_lines
def syntax_error(msg, lineno, pos): raise SyntaxError(msg, scanner.syntaxerror_params(pos, lineno))
def goal(rb_name, rule_name, goal_info, pred_plan_lines, python_lines): # returns plan_lines, goal_fn_head, goal_fn_tail, goal_decl_lines goal, goal_name, pattern_args, taking, start_lineno, end_lineno = goal_info assert goal == 'goal' goal_fn_head = ( "", "def %s(rule, arg_patterns, arg_context):" % rule_name, ("INDENT", 2), "engine = rule.rule_base.engine", "patterns = rule.goal_arg_patterns()", "if len(arg_patterns) == len(patterns):", ("INDENT", 2), "context = contexts.bc_context(rule)", "try:", ("INDENT", 2), ("STARTING_LINENO", start_lineno), "if all(itertools.imap(lambda pat, arg:", ("INDENT", 2), ("INDENT", 20), ("INDENT", 2), "pat.match_pattern(context, context,", ("INDENT", 18), "arg, arg_context),", "POPINDENT", "POPINDENT", "patterns,", "arg_patterns)):", ("ENDING_LINENO", end_lineno), "POPINDENT", "rule.rule_base.num_bc_rules_matched += 1", ) goal_fn_tail = ( "POPINDENT", "POPINDENT", "finally:", ("INDENT", 2), "context.done()", "POPINDENT", "POPINDENT", "POPINDENT", ) if not taking and not pred_plan_lines and not python_lines: plan_fn_name = "None" plan_lines = () else: plan_fn_name = "%s_plans.%s" % (rb_name, rule_name) def_start = "def %s" % rule_name taking = [line.strip() for line in taking if line.strip()] if not taking: def_head = def_start + '(context):' else: if taking[0][0] != '(' or taking[-1][-1] != ')': from pyke.krb_compiler import scanner end = scanner.lexer.lexpos taking_start = scanner.lexer.lexdata.rfind('taking', 0, end) if taking_start < 0: raise SyntaxError("'taking' clause: missing parenthesis", scanner.syntaxerror_params()) taking_start += len('taking') while taking_start < len(scanner.lexdata) and \ scanner.lexdata[taking_start].isspace(): taking_start += 1 lineno = scanner.lexer.lineno - \ scanner.lexer.lexdata.count('\n', taking_start, scanner.lexer.lexpos) raise SyntaxError("'taking' clause: missing parenthesis", scanner.syntaxerror_params(taking_start, lineno)) taking[0] = def_start + "(context, " + taking[0][1:] taking[-1] += ':' if len(taking) == 1: def_head = taking[0] else: def_head = (taking[0], ('INDENT', 4), tuple(taking[1:]), "POPINDENT", ) plan_lines = ("", def_head, ('INDENT', 2), pred_plan_lines, python_lines, "POPINDENT", ) goal_decl_lines = ( "", "bc_rule.bc_rule(%r, This_rule_base, %r," % (rule_name, goal_name), ("INDENT", 16), "%s, %s," % (rule_name, plan_fn_name), ) + list_format(pattern_args, "(", "),") return plan_lines, goal_fn_head, goal_fn_tail, goal_decl_lines
def syntax_error(msg, lineno, pos): raise SyntaxError(msg, scanner.syntaxerror_params(pos, lineno))
def p_error(t): if t is None: raise SyntaxError("invalid syntax", scanner.syntaxerror_params()) else: raise SyntaxError("invalid syntax", scanner.syntaxerror_params(t.lexpos, t.lineno))
def p_error(t): if t is None: raise SyntaxError("invalid syntax", scanner.syntaxerror_params()) else: raise SyntaxError("invalid syntax", scanner.syntaxerror_params(t.lexpos, t.lineno))