def __init__(self, span, dot_path, value): """ Construct an assignment AST node. :type span: Span :param span: the span of the assignment. :type dot_path: DotPath :param dot_path: the left hand side of the assignment. :type value: Component | Number | StringLit | JsonObj | DotPath | Expr :param value: the right hand side of the assignment. """ super(Assignment, self).__init__(span) if (isinstance(value, Component) or isinstance(value, JsonObj) or isinstance(value, Number) or isinstance(value, StringLit) or isinstance(value, Ident) or isinstance(value, DotPath) or isinstance(value, Expr)) and\ isinstance(dot_path, DotPath): self.lhs = dot_path self.rhs = value else: raise exception.BananaGrammarBug('Impossible assignment found with' ' left hand side: {} and' ' right hand side: {}'.format( type(dot_path), type(value)))
def __init__(self, span, value, arg_name=None): """ Construct an argument for a component ctor :type span: Span :param span: Span of the argument. :type value: Number | StringLit | JsonObj | DotPath | Expr :param value: Value for the argument :type arg_name: Ident :param arg_name: Name of the argument """ super(ComponentCtorArg, self).__init__(span) if (isinstance(value, JsonObj) or isinstance(value, Number) or isinstance(value, StringLit) or isinstance(value, Ident) or isinstance(value, DotPath) or isinstance(value, Expr)) and (isinstance(arg_name, Ident) or arg_name is None): self.arg_name = arg_name self.value = value else: # This code should be unreachable. # The grammar as defined should prevent us from # seeing an arg value or a value of the incorrect type raise exception.BananaGrammarBug( 'Impossible constructor argument found with' ' left hand side: {} and' ' right hand side: {}'.format(type(arg_name), type(value)))
def action_expr(s, l, t): if len(t) != 1: raise exception.BananaGrammarBug( 'Bug found in the grammar for expression,' ' Please report this bug.') if isinstance(t[0], ast.Expr): return t[0] return ast.Expr(ast.make_span(s, l, t), t[0])
def action_parse_comp_ctor(s, l, tokens): comp = ast.Component(ast.make_span(s, l, tokens)) for tok in tokens: if isinstance(tok, ast.Ident): comp.set_ctor(tok) elif isinstance(tok, ast.ComponentCtorArg): comp.add_arg(tok) else: raise exception.BananaGrammarBug( 'Bug found in the grammar, Please report this bug') return comp
def compute_hi(init_loc, tokens): hi = init_loc for tok in tokens: if isinstance(tok, ASTNode): hi = max(hi, tok.span.hi) elif isinstance(tok, basestring): hi += len(tok) elif isinstance(tok, p.ParseResults): hi = max(hi, compute_hi(init_loc, tok)) else: raise exception.BananaGrammarBug( "Couldn't create span for: {}".format(tok)) return hi
def action_root_ast(s, l, tokens): root = ast.BananaFile(emitter) for tok in tokens: if isinstance(tok, ast.Assignment): if isinstance(tok.rhs, ast.Component): root.add_component_ctor(tok.lhs, tok.rhs) else: root.add_assignment(tok.lhs, tok.rhs) elif isinstance(tok, ast.Connection): root.add_connections(tok) else: raise exception.BananaGrammarBug( 'Bug found in the grammar, Please report this bug.') return root
def action_unimplemented(s, l, t): raise exception.BananaGrammarBug("unimplemented code reached")