Commit 6a8335a5 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Optimize rendering of TextNode and introduce EmptyNode

Bug: 839389
Change-Id: I583f47fc7f065489fb4f5a2473898fb95c9c7bb8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1980604
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#728404}
parent 4eb261c2
...@@ -339,10 +339,21 @@ class CodeNode(object): ...@@ -339,10 +339,21 @@ class CodeNode(object):
symbol_node, set()).add(symbol_scope_chain) symbol_node, set()).add(symbol_scope_chain)
class EmptyNode(CodeNode):
"""Represents the zero-length text and renders nothing."""
def __init__(self):
CodeNode.__init__(self)
def _render(self, renderer, last_render_state):
pass
class LiteralNode(CodeNode): class LiteralNode(CodeNode):
""" """
Represents a literal text, which will be rendered as is without any template Represents a literal text, which will be rendered as is without any template
magic applied. magic applied. The given literal text object will be stringified on each
rendering.
""" """
def __init__(self, literal_text): def __init__(self, literal_text):
...@@ -358,7 +369,28 @@ class LiteralNode(CodeNode): ...@@ -358,7 +369,28 @@ class LiteralNode(CodeNode):
renderer.pop_caller() renderer.pop_caller()
class TextNode(CodeNode): def TextNode(template_text):
"""
Represents a template text node.
TextNode is designed to be a leaf node of a code node tree. TextNode
represents a template text while LiteralNode represents a literal text.
All template magics will be applied to |template_text|.
This function is pretending to be a CodeNode subclass and instantiates one
of text-ish code node subclass depending on the content of |template_text|.
"""
assert isinstance(template_text, str)
if "$" in template_text or "%" in template_text:
return _TextNode(template_text)
elif template_text:
return LiteralNode(template_text)
else:
return EmptyNode()
class _TextNode(CodeNode):
""" """
Represents a template text node. Represents a template text node.
......
...@@ -7,6 +7,7 @@ import web_idl ...@@ -7,6 +7,7 @@ import web_idl
from . import name_style from . import name_style
from .clang_format import clang_format from .clang_format import clang_format
from .code_node import CodeNode from .code_node import CodeNode
from .code_node import EmptyNode
from .code_node import LiteralNode from .code_node import LiteralNode
from .code_node import SequenceNode from .code_node import SequenceNode
from .codegen_accumulator import CodeGenAccumulator from .codegen_accumulator import CodeGenAccumulator
...@@ -69,9 +70,9 @@ def enclose_with_header_guard(code_node, header_guard): ...@@ -69,9 +70,9 @@ def enclose_with_header_guard(code_node, header_guard):
return SequenceNode([ return SequenceNode([
LiteralNode("#ifndef {}".format(header_guard)), LiteralNode("#ifndef {}".format(header_guard)),
LiteralNode("#define {}".format(header_guard)), LiteralNode("#define {}".format(header_guard)),
LiteralNode(""), EmptyNode(),
code_node, code_node,
LiteralNode(""), EmptyNode(),
LiteralNode("#endif // {}".format(header_guard)), LiteralNode("#endif // {}".format(header_guard)),
]) ])
...@@ -82,9 +83,9 @@ def enclose_with_namespace(code_node, namespace): ...@@ -82,9 +83,9 @@ def enclose_with_namespace(code_node, namespace):
return SequenceNode([ return SequenceNode([
LiteralNode("namespace {} {{".format(namespace)), LiteralNode("namespace {} {{".format(namespace)),
LiteralNode(""), EmptyNode(),
code_node, code_node,
LiteralNode(""), EmptyNode(),
LiteralNode("}} // namespace {}".format(namespace)), LiteralNode("}} // namespace {}".format(namespace)),
]) ])
......
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