Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 import sys | |
| 7 | |
| 8 """Nodes for PPAPI IDL AST""" | |
| 9 | |
| 10 # | |
| 11 # IDL Node | |
| 12 # | |
| 13 # IDL Node defines the IDLAttribute and IDLNode objects which are constructed | |
| 14 # by the parser as it processes the various 'productions'. The IDLAttribute | |
| 15 # objects are assigned to the IDLNode's property dictionary instead of being | |
| 16 # applied as children of The IDLNodes, so they do not exist in the final tree. | |
| 17 # The AST of IDLNodes is the output from the parsing state and will be used | |
| 18 # as the source data by the various generators. | |
| 19 # | |
| 20 | |
| 21 | |
| 22 # | |
| 23 # CopyToList | |
| 24 # | |
| 25 # Takes an input item, list, or None, and returns a new list of that set. | |
| 26 def CopyToList(item): | |
| 27 # If the item is 'Empty' make it an empty list | |
| 28 if not item: | |
| 29 item = [] | |
| 30 | |
| 31 # If the item is not a list | |
|
sehr
2013/04/08 18:37:07
add something like: "return a list containing one
| |
| 32 if type(item) is not type([]): | |
| 33 item = [item] | |
| 34 | |
| 35 # Make a copy we can modify | |
|
sehr
2013/04/08 18:37:07
Please make sure that comments end with a period c
| |
| 36 return list(item) | |
| 37 | |
| 38 | |
| 39 # IDLSearch | |
| 40 # | |
| 41 # A temporary object used by the parsing process to hold an Extended Attribute | |
| 42 # which will be passed as a child to a standard IDLNode. | |
| 43 # | |
| 44 class IDLSearch(object): | |
| 45 def __init__(self): | |
| 46 self.depth = 0 | |
| 47 | |
| 48 def Enter(self, node): | |
| 49 pass | |
| 50 | |
| 51 def Exit(self, node): | |
| 52 pass | |
| 53 | |
| 54 | |
| 55 # IDLAttribute | |
| 56 # | |
| 57 # A temporary object used by the parsing process to hold an Extended Attribute | |
| 58 # which will be passed as a child to a standard IDLNode. | |
| 59 # | |
| 60 class IDLAttribute(object): | |
| 61 def __init__(self, name, value): | |
| 62 self._cls = 'Property' | |
| 63 self._name = name | |
| 64 self._value = value | |
| 65 | |
| 66 def __str__(self): | |
| 67 return '%s=%s' % (self._name, self._value) | |
| 68 | |
| 69 # | |
| 70 # IDLNode | |
| 71 # | |
| 72 # This class implements the AST tree, providing the associations between | |
| 73 # parents and children. It also contains a namepsace and propertynode to | |
| 74 # allow for look-ups. IDLNode is derived from IDLRelease, so it is | |
| 75 # version aware. | |
| 76 # | |
| 77 class IDLNode(object): | |
| 78 def __init__(self, cls, filename, lineno, pos, children=None): | |
| 79 self._cls = cls | |
| 80 self._properties = { | |
| 81 'ERRORS' : [], | |
| 82 'WARNINGS': [], | |
| 83 'FILENAME': filename, | |
| 84 'LINENO' : lineno, | |
| 85 'POSSITION' : pos, | |
| 86 } | |
| 87 | |
| 88 self._children = [] | |
| 89 self._parent = None | |
| 90 self.AddChildren(children) | |
| 91 | |
| 92 # | |
| 93 # | |
| 94 # | |
| 95 # Return a string representation of this node | |
| 96 def __str__(self): | |
| 97 name = self.GetProperty('NAME','') | |
| 98 return '%s(%s)' % (self._cls, name) | |
| 99 | |
| 100 | |
| 101 # Log an error for this object | |
| 102 def Error(self, msg): | |
| 103 filename, lineno = self.GetFileAndLine() | |
| 104 self._properties['ERRORS'].append(msg) | |
| 105 ErrOut.LogLine(filename, lineno, 0, ' %s %s' % (str(self), msg)) | |
| 106 | |
| 107 # Log a warning for this object | |
| 108 def Warning(self, msg): | |
| 109 filename, lineno = self.GetFileAndLine() | |
| 110 self._properties['WARNINGS'].append(msg) | |
| 111 WarnOut.LogLine(self.filename, self.lineno, 0, ' %s %s' % | |
| 112 (str(self), msg)) | |
| 113 | |
| 114 # Return file and line number for where node was defined | |
| 115 def GetFileAndLine(self): | |
| 116 return self._properties['FILENAME'], self._properties['LINENO'] | |
|
sehr
2013/04/08 18:37:07
use self.GetProperty here.
| |
| 117 | |
| 118 def GetName(self): | |
| 119 return self.GetProperty('NAME') | |
| 120 | |
| 121 def GetParent(self): | |
| 122 return self._parent | |
| 123 | |
| 124 def Traverse(self, search, filter_nodes): | |
| 125 if self._cls in filter_nodes: | |
| 126 return '' | |
| 127 | |
| 128 search.Enter(self) | |
| 129 search.depth += 1 | |
| 130 for child in self._children: | |
| 131 child.Traverse(search, filter_nodes) | |
| 132 search.depth -= 1 | |
| 133 search.Exit(self) | |
| 134 | |
| 135 | |
| 136 def Tree(self, filter_nodes=None, accept_props=None): | |
| 137 class DumpTreeSearch(IDLSearch): | |
| 138 def __init__(self, props): | |
| 139 IDLSearch.__init__(self) | |
| 140 self.out = [] | |
| 141 self.props = props | |
| 142 | |
| 143 def Enter(self, node): | |
| 144 tab = ''.rjust(self.depth * 2) | |
| 145 self.out.append(tab + str(node)) | |
| 146 if self.props: | |
| 147 for key, value in node._properties: | |
| 148 proplist = [] | |
| 149 if key in self.props: | |
| 150 proplist.append(tab + ' %s: %s' % | |
| 151 (prop, str(node.properties[prop]))) | |
| 152 if proplist: | |
| 153 self.out.append(tab + ' PROPERTIES') | |
| 154 self.out.extend(proplist) | |
| 155 | |
| 156 if filter_nodes == None: | |
| 157 filter_nodes = ['Comment', 'Copyright'] | |
| 158 | |
| 159 search = DumpTreeSearch(accept_props) | |
| 160 self.Traverse(search, filter_nodes) | |
| 161 return search.out | |
| 162 | |
| 163 # | |
| 164 # Search related functions | |
| 165 # | |
| 166 # Check if node is of a given type | |
| 167 def IsA(self, *typelist): | |
| 168 if self._cls in typelist: return True | |
|
sehr
2013/04/08 18:37:07
Does this work?
return self._cls in typelist
| |
| 169 return False | |
| 170 | |
| 171 # Get a list of all children | |
| 172 def GetChildren(self): | |
| 173 return self._children | |
| 174 | |
| 175 def GetListOf(self, *keys): | |
| 176 out = [] | |
| 177 for child in self._children: | |
| 178 if child._cls in keys: out.append(child) | |
| 179 return out | |
| 180 | |
| 181 def GetOneOf(self, *keys): | |
| 182 out = self.GetListOf(*keys) | |
| 183 if out: return out[0] | |
| 184 return None | |
| 185 | |
| 186 def AddChildren(self, children): | |
| 187 children = CopyToList(children) | |
| 188 for child in children: | |
| 189 if not child: | |
| 190 continue | |
| 191 if type(child) == IDLAttribute: | |
| 192 self.SetProperty(child._name, child._value) | |
| 193 continue | |
| 194 if type(child) == IDLNode: | |
| 195 child._parent = self | |
| 196 self._children.append(child) | |
| 197 continue | |
| 198 raise RuntimeError('Adding child of type .\n' % type(child).__name__) | |
| 199 | |
| 200 | |
| 201 # | |
| 202 # Property Functions | |
| 203 # | |
| 204 def SetProperty(self, name, val): | |
| 205 self._properties[name] = val | |
| 206 | |
| 207 def GetProperty(self, name, default=None): | |
| 208 return self._properties.get(name, default) | |
| OLD | NEW |