def p_body_do(self, ss, semi, e): assert ast.isloc(ss) if e is None: e = ast.update_value(semi, val.symbol('pass')) assert ast.isloc(e) do = ast.locmerge(ss, e, val.symbol('do')) return ast.locmerge(ss, e, [do] + ss.value + [e])
def p_primary_if(self, k, po, p, pc, co, c, cc, ke, ao, a, ac): assert ast.isloc(p) assert ast.isloc(c) assert ast.isloc(a) return ast.locmerge( k, ac, [ast.map_value(val.symbol, ast.update_value(k, 'if')), p, c, a])
def p_primary_language(self, ll): assert ast.isloc(ll), '%r' % (ll, ) l = ll.value (start, _end) = ll.loc assert ast.isloc(l), '%r' % (l, ) l_ = adjlocust(l, start) return l_
def _p_binop(self, l, op, r): assert ast.isloc(l) assert ast.isloc(r) if op.value in operators: # Perform operator substitution new_op = ast.update_value(op, operators[op.value]) else: # Leave it new_op = op app = [ast.map_value(val.symbol, new_op), l, r] return ast.locmerge(l, r, app)
def p_arrow_tuple(self, po, params, pc, op, body): assert ast.isloc(body) return ast.locmerge(po, body, [ ast.map_value(val.symbol, ast.update_value(op, 'lambda')), ast.locmerge(po, pc, params), body, ])
def p_primary_proc(self, k, po, params, pc, bo, body, bc): assert ast.isloc(body) return ast.locmerge(k, bc, [ ast.map_value(val.symbol, ast.update_value(k, 'lambda')), ast.locmerge(po, pc, params), body, ])
def p_arrow_one(self, param, op, body): assert ast.isloc(body) param = ast.map_value(val.symbol, param) return ast.locmerge(param, body, [ ast.map_value(val.symbol, ast.update_value(op, 'lambda')), ast.update_value(param, [param]), body, ])
def delocust(l): # XXX Why do we bother with tuples in the first place? if isinstance(l, dict) and sorted(l.keys()) == ['loc', 'value']: return delocust(l['value']) elif ast.isloc(l): return delocust(l.value) elif isinstance(l, list) or isinstance(l, tuple): return [delocust(v) for v in l] elif isinstance(l, dict): return dict((k, delocust(v)) for k, v in l.iteritems()) else: return l
def parse(f, context, languages=None): scanner = scan.Scanner(f, context, languages) semantics = Semantics() parser = grammar.Parser(semantics) while True: token = scanner.read() if token[0] == -1: # error semantics.syntax_error(token) else: if token[0] == 0: # EOF # Implicit ; at EOF. loc = (scanner.cur_pos, scanner.cur_pos) parser.feed((grammar.T_SEMI, ast.Located(loc, ';'))) parser.feed(token) if token[0] == 0: # EOF break assert isinstance(semantics.answer, list) for i in semantics.answer: assert ast.isloc(i) return semantics.answer
def p_applicative_lookup(self, a, o, index, c): assert ast.isloc(a) assert ast.isloc(index) lookup = ast.update_value(o, val.sym('lookup')) return ast.locmerge(a, c, [lookup, a, index])
def p_applicative_none(self, e): assert ast.isloc(e) return e
def p_path_expression_some(self, more, slash, s): # XXX Is this just _p_binop with the "slash" operator? assert ast.isloc(s) intersect = ast.update_value(slash, val.symbol('by_walk')) app = [intersect, more, s] return ast.locmerge(more, s, app)
def p_applicative_app(self, fn, o, args, c): assert ast.isloc(fn) for arg in args: assert ast.isloc(arg) return ast.locmerge(fn, c, [fn] + args)
def p_body_exp(self, e): assert ast.isloc(e) return e
def p_expression_opt_some(self, e): assert ast.isloc(e) return e
def p_action_sample(self, k, e): assert ast.isloc(e) i = ast.update_value(k, val.symbol('sample')) app = [i, e] return ast.locmerge(i, e, app)
def p_statements_one(self, s): assert ast.isloc(s) return ast.update_value(s, [s])
def p_action_directive(self, d): assert ast.isloc(d) return d
def p_statement_letvalues(self, l, po, names, pc, eq, e): assert ast.isloc(e) assert all(map(ast.isloc, names)) let = ast.update_value(l, val.symbol('let_values')) names = ast.locmerge(po, pc, names) return ast.locmerge(let, e, [let, names, e])
def p_do_bind_labelled(self, n, op, l): assert ast.isloc(l) # XXX Yes, this remains infix, for the macro expander to handle... # XXX Convert <~ to <- for the macro expander's sake expression = [n, ast.update_value(op, val.symbol("<-")), l] return ast.locmerge(n, l, expression)
def _p_exp(self, e): assert ast.isloc(e) return e
def p_statement_labelled(self, d): assert ast.isloc(d) return d
def p_primary_brace(self, o, e, c): assert ast.isloc(e) return ast.locmerge(o, c, e.value)
def p_statement_mutrec(self, l, n, eq, e): assert ast.isloc(e) let = ast.update_value(l, val.symbol('mutrec')) n = ast.map_value(val.symbol, n) return ast.locmerge(let, e, [let, n, e])
def p_statements_many(self, ss, semi, s): assert ast.isloc(s) assert ast.isloc(s) ss.value.append(s) return ast.locmerge(ss, s, ss.value)
def p_statement_assign(self, n, eq, e): assert ast.isloc(e) let = ast.update_value(eq, val.symbol('let')) n = ast.map_value(val.symbol, n) return ast.locmerge(n, e, [let, n, e])
def p_primary_literal(self, l): assert ast.isloc(l) return l
def p_path_expression_one(self, slash, s): assert ast.isloc(s) top = ast.update_value(slash, val.symbol('by_top')) intersect = ast.update_value(slash, val.symbol('by_walk')) app = [intersect, ast.loclist([top]), s] return ast.locmerge(top, s, app)
def p_action_force(self, k, e1, eq, e2): assert ast.isloc(e1) assert ast.isloc(e2) i = ast.update_value(k, val.symbol('force')) app = [i, e1, e2] return ast.locmerge(i, e2, app)
def p_statement_none(self, e): assert ast.isloc(e) return e