1#!/usr/bin/env python
2# Copyright (C) 2013 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import sys
31import StringIO
32from collections import defaultdict
33
34import in_generator
35import template_expander
36
37
38def _char_dict(tags, index):
39    new_dict = defaultdict(list)
40    for tag in tags:
41        if index >= len(tag):
42            continue
43        new_dict[tag[index].lower()].append(tag)
44    return new_dict
45
46
47def _generate_if(name, index):
48    conditions = []
49    for i in range(index, len(name)):
50        conditions.append("data[%d] == '%c'" % (i, name[i].lower()))
51    return "if (%s)\n" % " && ".join(conditions)
52
53
54def _print_switch(buf, tag_list, index):
55    indent = '    ' * (index + 2)
56    data = _char_dict(tag_list, index)
57    # FIXME: No need to switch if there's only a single item in the data dict
58    buf.write(indent + 'switch (data[%d]) {\n' % index)
59    for char, tags in data.iteritems():
60        buf.write(indent + "case '%c':\n" % char)
61        if len(tags) > 1:
62            _print_switch(buf, tags, index + 1)
63        else:
64            tag = tags[0]
65            retval = indent + "    return %sTag.localName().impl();\n" % tag
66            if len(tag) == index + 1:
67                buf.write(retval)
68            else:
69                buf.write(indent + '    ' + _generate_if(tag, index + 1))
70                buf.write('    ' + retval)
71                buf.write(indent + "    return 0;\n")
72
73    buf.write(indent + '}\n')
74    buf.write(indent + "return 0;\n")
75
76
77class ElementLookupTrieWriter(in_generator.Writer):
78    # FIXME: Inherit all these from somewhere.
79    defaults = {
80        'JSInterfaceName': None,
81        'constructorNeedsCreatedByParser': None,
82        'constructorNeedsFormElement': None,
83        'contextConditional': None,
84        'interfaceName': None,
85        'noConstructor': None,
86        'runtimeEnabled': None,
87    }
88    default_parameters = {
89        'attrsNullNamespace': None,
90        'namespace': '',
91        'namespacePrefix': '',
92        'namespaceURI': '',
93        'fallbackInterfaceName': '',
94        'fallbackJSInterfaceName': '',
95    }
96
97    def __init__(self, in_file_paths):
98        super(ElementLookupTrieWriter, self).__init__(in_file_paths)
99        self._tags = [entry['name'] for entry in self.in_file.name_dictionaries]
100        self._namespace = self.in_file.parameters['namespace'].strip('"')
101        self._outputs = {
102            (self._namespace + "ElementLookupTrie.h"): self.generate_header,
103            (self._namespace + "ElementLookupTrie.cpp"): self.generate_implementation,
104        }
105
106    @template_expander.use_jinja('ElementLookupTrie.h.tmpl')
107    def generate_header(self):
108        return {
109            'namespace': self._namespace,
110        }
111
112    @template_expander.use_jinja('ElementLookupTrie.cpp.tmpl')
113    def generate_implementation(self):
114        size_dict = defaultdict(list)
115        for tag in self._tags:
116            size_dict[len(tag)].append(tag)
117
118        buf = StringIO.StringIO()
119        for length, tags in size_dict.iteritems():
120            buf.write("    case %d:\n" % length)
121            _print_switch(buf, tags, 0)
122        return {
123            'namespace': self._namespace,
124            'body': buf.getvalue(),
125        }
126
127
128if __name__ == "__main__":
129    in_generator.Maker(ElementLookupTrieWriter).main(sys.argv)
130