Commit 7b05d5cd authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Improve ListNode to insert head and tail if not empty

Makes ListNode insert the head and tail section iff the content
is not empty.

Bug: 839389
Change-Id: Ie154a6f252814abc3a51284f22ac04188e168e00
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1981417Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#727541}
parent 26808eeb
......@@ -432,25 +432,40 @@ class ListNode(CodeNode):
except that addition and removal of None have no effect.
"""
def __init__(self, code_nodes=None, separator="\n", separator_last=""):
def __init__(self, code_nodes=None, separator="\n", head="", tail=""):
"""
Args:
code_nodes: A list of CodeNode to be rendered.
separator: A str inserted between code nodes.
head:
tail: The head and tail sections that will be rendered iff the
content list is not empty.
"""
assert isinstance(separator, str)
assert isinstance(separator_last, str)
assert isinstance(head, str)
assert isinstance(tail, str)
element_nodes_gensym = CodeNode.gensym()
element_nodes = []
template_text = format_template(
"""\
% if {element_nodes}:
{head}\\
% endif
% for node in {element_nodes}:
${node}\\
% if not loop.last:
{separator}\\
% endif
% endfor
{separator_last}\
% if {element_nodes}:
{tail}\\
% endif\
""",
element_nodes=element_nodes_gensym,
separator=separator,
separator_last=separator_last)
head=head,
tail=tail)
template_vars = {element_nodes_gensym: element_nodes}
CodeNode.__init__(
......@@ -530,12 +545,13 @@ class SequenceNode(ListNode):
and provides the points where SymbolDefinitionNodes can be inserted.
"""
def __init__(self, code_nodes=None, separator="\n", separator_last=""):
def __init__(self, code_nodes=None, separator="\n", head="", tail=""):
ListNode.__init__(
self,
code_nodes=code_nodes,
separator=separator,
separator_last=separator_last)
head=head,
tail=tail)
def _render(self, renderer, last_render_state):
duplicates = []
......@@ -558,12 +574,13 @@ class SymbolScopeNode(SequenceNode):
insert corresponding SymbolDefinitionNodes appropriately.
"""
def __init__(self, code_nodes=None, separator="\n", separator_last=""):
def __init__(self, code_nodes=None, separator="\n", head="", tail=""):
SequenceNode.__init__(
self,
code_nodes=code_nodes,
separator=separator,
separator_last=separator_last)
head=head,
tail=tail)
self._likeliness = Likeliness.ALWAYS
self._registered_code_symbols = set()
......
......@@ -33,7 +33,7 @@ class CodeNodeCxxTest(unittest.TestCase):
self.assertEqual(actual, expected)
def test_symbol_definition_with_branches(self):
root = SymbolScopeNode(separator_last="\n")
root = SymbolScopeNode(tail="\n")
root.register_code_symbols([
SymbolNode("var1", "int ${var1} = 1;"),
......@@ -79,7 +79,7 @@ var3;
""")
def test_symbol_definition_with_nested_branches(self):
root = SymbolScopeNode(separator_last="\n")
root = SymbolScopeNode(tail="\n")
root.register_code_symbols([
SymbolNode("var1", "int ${var1} = 1;"),
......@@ -132,7 +132,7 @@ if (true) {
""")
def test_function_definition_minimum(self):
root = SymbolScopeNode(separator_last="\n")
root = SymbolScopeNode(tail="\n")
root.append(
CxxFuncDefNode(
name="blink::bindings::func", arg_decls=[],
......@@ -145,7 +145,7 @@ void blink::bindings::func() {
""")
def test_function_definition_full(self):
root = SymbolScopeNode(separator_last="\n")
root = SymbolScopeNode(tail="\n")
func_def = CxxFuncDefNode(
name="blink::bindings::func",
......
......@@ -72,6 +72,14 @@ class CodeNodeTest(unittest.TestCase):
root.remove(root[-1])
self.assertRenderResult(root, "2,3,5")
def test_list_node_head_and_tail(self):
self.assertRenderResult(ListNode(), "")
self.assertRenderResult(ListNode(head="head"), "")
self.assertRenderResult(ListNode(tail="tail"), "")
self.assertRenderResult(
ListNode([TextNode("-content-")], head="head", tail="tail"),
"head-content-tail")
def test_nested_sequence(self):
"""Tests nested ListNodes."""
root = ListNode(separator=",")
......@@ -93,7 +101,7 @@ class CodeNodeTest(unittest.TestCase):
Tests that use of SymbolNode inserts necessary SymbolDefinitionNode
appropriately.
"""
root = SymbolScopeNode(separator_last="\n")
root = SymbolScopeNode(tail="\n")
root.register_code_symbols([
SymbolNode("var1", "int ${var1} = ${var2} + ${var3};"),
......
......@@ -637,7 +637,7 @@ def generate_dictionary_cc_file(dictionary):
make_dict_member_set_def(cg_context.make_copy(dict_member=member)),
])
root_node = SymbolScopeNode(separator_last="\n")
root_node = ListNode(tail="\n")
root_node.set_accumulator(CodeGenAccumulator())
root_node.set_renderer(MakoRenderer())
......@@ -681,7 +681,7 @@ def generate_dictionary_h_file(dictionary):
make_dict_class_def(cg_context),
])
root_node = SymbolScopeNode(separator_last="\n")
root_node = ListNode(tail="\n")
root_node.set_accumulator(CodeGenAccumulator())
root_node.set_renderer(MakoRenderer())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment