Example #1
0
 def make_attr_node(self, attr):
     node_list = []
     new_attr_name = attr.localName
     attr_ast = util.parse(attr.nodeValue, 'rhs_expression')
     node_list.append(ast.TextNode(u' %(new_attr_name)s="' % vars()))
     # fixme: need to guarantee good output - escape sequences etc
     node_list.append(ast.PlaceholderSubstitutionNode(attr_ast))
     node_list.append(ast.TextNode('"'))
     return node_list
Example #2
0
 def make_attr_node(self, attr):
     node_list = []
     new_attr_name = attr.localName
     attr_ast = util.parse(attr.nodeValue, 'rhs_expression')
     node_list.append(ast.TextNode(u' %(new_attr_name)s="' % vars()))
     # fixme: need to guarantee good output - escape sequences etc
     node_list.append(ast.PlaceholderSubstitutionNode(attr_ast))
     node_list.append(ast.TextNode('"'))
     return node_list
Example #3
0
 def handle_condition(self, dom_node, attr_name):
     expr_ast = util.parse(dom_node.getAttribute(attr_name),
                           'rhs_expression')
     node_list = []
     if_node = ast.IfNode(expr_ast)
     node_list.append(if_node)
     if_node.append(self.make_tag_node(dom_node))
     for n in dom_node.childNodes:
         if_node.extend(self.build_ast(n))
     if_node.extend(self.make_tag_node(dom_node, close=True))
     return node_list
Example #4
0
 def handle_condition(self, dom_node, attr_name):
     expr_ast = util.parse(
         dom_node.getAttribute(attr_name), 'rhs_expression')
     node_list = []
     if_node = ast.IfNode(expr_ast)
     node_list.append(if_node)
     if_node.append(self.make_tag_node(dom_node))
     for n in dom_node.childNodes:
         if_node.extend(self.build_ast(n))
     if_node.extend(self.make_tag_node(dom_node, close=True))
     return node_list
Example #5
0
 def handle_define(self, dom_node, attr_name):
     node_list = []
     node_name = dom_node.nodeName
     # print "handle_define", node_name
     # fixme: this is a nasty temp hack, it will generate the correct code
     # for 1 define, but multiple expressions won't work
     expr_ast = util.parse(dom_node.getAttribute(attr_name), 'argument_list')
     dom_node.removeAttribute(attr_name)
     node_list.extend(expr_ast)
     node_list.extend(self.build_ast(dom_node))
     return node_list
Example #6
0
 def handle_define(self, dom_node, attr_name):
     node_list = []
     node_name = dom_node.nodeName
     # print "handle_define", node_name
     # fixme: this is a nasty temp hack, it will generate the correct code
     # for 1 define, but multiple expressions won't work
     expr_ast = util.parse(dom_node.getAttribute(attr_name),
                           'argument_list')
     dom_node.removeAttribute(attr_name)
     node_list.extend(expr_ast)
     node_list.extend(self.build_ast(dom_node))
     return node_list
Example #7
0
    def handleMacro(self, pnode, macro_function, parse_rule):
        if isinstance(pnode, ast.MacroNode):
            kargs_map = pnode.parameter_list.get_arg_map()
        elif isinstance(pnode, ast.CallFunctionNode):
            kargs_map = pnode.arg_list.get_arg_map()
        else:
            self.compiler.error(SemanticAnalyzerError(
                "unexpected node type '%s' for macro" % type(pnode)),
                                pos=pnode.pos)

        macro_output = macro_function(pnode, kargs_map, self.compiler)
        # fixme: bad place to import, difficult to put at the top due to
        # cyclic dependency
        try:
            if parse_rule:
                fragment_ast = util.parse(macro_output, parse_rule)
            elif isinstance(pnode, ast.MacroNode):
                fragment_ast = util.parse(macro_output, 'fragment_goal')
            elif isinstance(pnode, ast.CallFunctionNode):
                fragment_ast = util.parse(macro_output, 'rhs_expression')
        except Exception, e:
            self.compiler.error(MacroParseError(e), pos=pnode.pos)
Example #8
0
    def handle_omit_tag(self, dom_node, attr_name):
        debug("handle_omit_tag", dom_node)
        node_list = []
        node_name = dom_node.nodeName
        raw_expression = dom_node.getAttribute(attr_name)
        if raw_expression:
            expr_ast = util.parse(raw_expression, 'argument_list')
        else:
            expr_ast = None

        dom_node.removeAttribute(attr_name)
        setattr(dom_node, 'omit_tag', True)
        setattr(dom_node, 'omit_tag_ast', expr_ast)
        return node_list
Example #9
0
def macro_i18n(macro_node, arg_map, compiler):
    # fixme: parse the parameter list into something usable
    # macro_node.parameter_list

    # generate a fake translation for now to verify this is working
    # most apps will have to stub this part out somehow i think
    macro_content_ast = util.parse(macro_node.value, 'i18n_goal')
    i18n_msg = make_i18n_message(macro_node.value, macro_content_ast)
    i18n_msg_utf8 = i18n_msg.encode(sys.getdefaultencoding())
    #print "macro_content_ast"
    #print "orginal:", macro_node.value
    #print "i18n:", i18n_msg_utf8
    #visitor.print_tree(macro_content_ast)
    return i18n_msg
Example #10
0
  def handleMacro(self, pnode, macro_function, parse_rule):
    if isinstance(pnode, ast.MacroNode):
      kargs_map = pnode.parameter_list.get_arg_map()
    elif isinstance(pnode, ast.CallFunctionNode):
      kargs_map = pnode.arg_list.get_arg_map()
    else:
      self.compiler.error(
          SemanticAnalyzerError("unexpected node type '%s' for macro" %
                                type(pnode)),
          pos=pnode.pos)

    macro_output = macro_function(pnode, kargs_map, self.compiler)
    # fixme: bad place to import, difficult to put at the top due to
    # cyclic dependency
    try:
      if parse_rule:
        fragment_ast = util.parse(macro_output, parse_rule)
      elif isinstance(pnode, ast.MacroNode):
        fragment_ast = util.parse(macro_output, 'fragment_goal')
      elif isinstance(pnode, ast.CallFunctionNode):
        fragment_ast = util.parse(macro_output, 'rhs_expression')
    except Exception, e:
      self.compiler.error(MacroParseError(e), pos=pnode.pos)
Example #11
0
    def handle_omit_tag(self, dom_node, attr_name):
        debug("handle_omit_tag", dom_node)
        node_list = []
        node_name = dom_node.nodeName
        raw_expression = dom_node.getAttribute(attr_name)
        if raw_expression:
            expr_ast = util.parse(raw_expression, 'argument_list')
        else:
            expr_ast = None

        dom_node.removeAttribute(attr_name)
        setattr(dom_node, 'omit_tag', True)
        setattr(dom_node, 'omit_tag_ast', expr_ast)
        return node_list
Example #12
0
 def handle_content(self, dom_node, attr_name):
     debug("handle_content", dom_node)
     #traceback.print_stack()
     expr_ast = util.parse(
         dom_node.getAttribute(attr_name), 'rhs_expression')
     dom_node.removeAttribute(attr_name)
     setattr(dom_node, 'has_child_stuff', True)
     node_list = []
     debug("handle_content start", dom_node)
     node_list.extend(self.make_tag_node(dom_node))
     node_list.append(ast.PlaceholderSubstitutionNode(expr_ast))
     debug("handle_content end", dom_node)
     node_list.extend(self.make_tag_node(dom_node, close=True))
     debug("handle_content return", dom_node)
     return node_list
Example #13
0
 def analyzeMacroNode(self, pnode):
   # fixme: better error handler
   macro_handler_name = 'macro_%s' % pnode.name
   try:
     macro_function, macro_parse_rule = self.compiler.macro_registry[
         macro_handler_name]
   except KeyError:
     self.compiler.error(SemanticAnalyzerError("no handler registered for '%s'"
                                               % macro_handler_name),
                         pos=pnode.pos)
   try:
     temp_fragment = util.parse(pnode.value,
                                macro_parse_rule or 'fragment_goal')
   except Exception, e:
     self.compiler.error(MacroParseError(e), pos=pnode.pos)
Example #14
0
 def analyzeMacroNode(self, pnode):
     # fixme: better error handler
     macro_handler_name = 'macro_%s' % pnode.name
     try:
         macro_function, macro_parse_rule = self.compiler.macro_registry[
             macro_handler_name]
     except KeyError:
         self.compiler.error(SemanticAnalyzerError(
             "no handler registered for '%s'" % macro_handler_name),
                             pos=pnode.pos)
     try:
         temp_fragment = util.parse(pnode.value, macro_parse_rule
                                    or 'fragment_goal')
     except Exception, e:
         self.compiler.error(MacroParseError(e), pos=pnode.pos)
Example #15
0
 def handle_content(self, dom_node, attr_name):
     debug("handle_content", dom_node)
     #traceback.print_stack()
     expr_ast = util.parse(dom_node.getAttribute(attr_name),
                           'rhs_expression')
     dom_node.removeAttribute(attr_name)
     setattr(dom_node, 'has_child_stuff', True)
     node_list = []
     debug("handle_content start", dom_node)
     node_list.extend(self.make_tag_node(dom_node))
     node_list.append(ast.PlaceholderSubstitutionNode(expr_ast))
     debug("handle_content end", dom_node)
     node_list.extend(self.make_tag_node(dom_node, close=True))
     debug("handle_content return", dom_node)
     return node_list
Example #16
0
    def handle_repeat(self, dom_node, attr_name):
        debug("handle_repeat", dom_node)
        expr_pieces = dom_node.getAttribute(attr_name).split()
        dom_node.removeAttribute(attr_name)
        target = expr_pieces[0]
        expr_ast = util.parse(' '.join(expr_pieces[1:]), 'rhs_expression')
        node_list = []
        # hack - assumes python syntax
        fn = ast.ForNode(
            ast.TargetListNode([
                ast.IdentifierNode("self.repeat['%s']" % target),
                ast.IdentifierNode(target)
            ]),
            ast.ExpressionListNode([
                ast.CallFunctionNode(ast.IdentifierNode('enumerate'),
                                     ast.ArgListNode([expr_ast]))
            ]))

        if self.has_child_stuff(dom_node):
            debug("has_child_stuff:", dom_node)
            fn.extend(self.build_ast(dom_node))
            #fn.append(self.make_tag_node(dom_node))
            #for n in dom_node.childNodes:
            #  fn.extend(self.build_ast(n))
        else:
            # print "no children"
            fn.extend(self.build_ast(dom_node))

        if (dom_node.previousSibling and dom_node.previousSibling.nodeType
                == xml.dom.minidom.Node.TEXT_NODE
                and not dom_node.previousSibling.nodeValue.strip()):
            # inject the previous whitespace sibling to keep the output looking
            # ok fixme: a conditional is probably required here - you only want
            # to execute this if it's not the last execution of the loop
            fn.prepend(self.build_ast(dom_node.previousSibling))

            # now remove the previous sibling
            #print "node", dom_node
            #print "parent", dom_node.parentNode
            #print ' '.join("previous", dom_node.previousSibling,
            #               id(dom_node.previousSibling))
            #print "next", dom_node.nextSibling, id(dom_node.nextSibling)
            #dom_node.parentNode.removeChild(dom_node.previousSibling)
            node_list.append(ast.EatPrevious())

        node_list.append(fn)
        #fn.extend(self.make_tag_node(dom_node, close=True))
        return node_list
Example #17
0
    def handle_repeat(self, dom_node, attr_name):
        debug("handle_repeat", dom_node)
        expr_pieces = dom_node.getAttribute(attr_name).split()
        dom_node.removeAttribute(attr_name)
        target = expr_pieces[0]
        expr_ast = util.parse(' '.join(expr_pieces[1:]), 'rhs_expression')
        node_list = []
        # hack - assumes python syntax
        fn = ast.ForNode(
            ast.TargetListNode([ast.IdentifierNode(
                "self.repeat['%s']" % target), ast.IdentifierNode(target)]),
            ast.ExpressionListNode([ast.CallFunctionNode(
                ast.IdentifierNode('enumerate'), ast.ArgListNode([expr_ast]))]))

        if self.has_child_stuff(dom_node):
            debug("has_child_stuff:", dom_node)
            fn.extend(self.build_ast(dom_node))
            #fn.append(self.make_tag_node(dom_node))
            #for n in dom_node.childNodes:
            #  fn.extend(self.build_ast(n))
        else:
            # print "no children"
            fn.extend(self.build_ast(dom_node))

        if (dom_node.previousSibling and dom_node.previousSibling.nodeType ==
                xml.dom.minidom.Node.TEXT_NODE and
                not dom_node.previousSibling.nodeValue.strip()):
            # inject the previous whitespace sibling to keep the output looking
            # ok fixme: a conditional is probably required here - you only want
            # to execute this if it's not the last execution of the loop
            fn.prepend(self.build_ast(dom_node.previousSibling))

            # now remove the previous sibling
            #print "node", dom_node
            #print "parent", dom_node.parentNode
            #print ' '.join("previous", dom_node.previousSibling,
            #               id(dom_node.previousSibling))
            #print "next", dom_node.nextSibling, id(dom_node.nextSibling)
            #dom_node.parentNode.removeChild(dom_node.previousSibling)
            node_list.append(ast.EatPrevious())

        node_list.append(fn)
        #fn.extend(self.make_tag_node(dom_node, close=True))
        return node_list
Example #18
0
 def handle_replace(self, dom_node, attr_name):
     expr_ast = util.parse(
         dom_node.getAttribute(attr_name), 'rhs_expression')
     dom_node.removeAttribute(attr_name)
     return [ast.PlaceholderSubstitutionNode(expr_ast)]
Example #19
0
 def handle_replace(self, dom_node, attr_name):
     expr_ast = util.parse(dom_node.getAttribute(attr_name),
                           'rhs_expression')
     dom_node.removeAttribute(attr_name)
     return [ast.PlaceholderSubstitutionNode(expr_ast)]