Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: tools/idl_parser/idl_node.py

Issue 13498002: Add WebIDL compliant parser plus tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge using updated lexer, fix error msgs Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
32 if type(item) is not type([]):
33 item = [item]
34
35 # Make a copy we can modify
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 def GetLogLine(self, msg):
101 filename, lineno = self.GetFileAndLine()
102 return '%s(%d) : %s\n' % (filename, lineno, msg)
103
104 # Log an error for this object
105 def Error(self, msg):
106 filename, lineno = self.GetFileAndLine()
107 self._properties['ERRORS'].append(msg)
108 ErrOut.LogLine(filename, lineno, 0, ' %s %s' % (str(self), msg))
109
110 # Log a warning for this object
111 def Warning(self, msg):
112 filename, lineno = self.GetFileAndLine()
113 self._properties['WARNINGS'].append(msg)
114 WarnOut.LogLine(self.filename, self.lineno, 0, ' %s %s' %
115 (str(self), msg))
116
117 # Return file and line number for where node was defined
118 def GetFileAndLine(self):
119 return self._properties['FILENAME'], self._properties['LINENO']
sehr 2013/04/12 15:54:57 sometimes you use _properties, and sometimes GetPr
noelallen1 2013/04/12 16:52:15 Done.
120
121 def GetName(self):
122 return self.GetProperty('NAME')
123
124 def GetParent(self):
125 return self._parent
126
127 def Traverse(self, search, filter_nodes):
128 if self._cls in filter_nodes:
129 return ''
130
131 search.Enter(self)
132 search.depth += 1
133 for child in self._children:
134 child.Traverse(search, filter_nodes)
135 search.depth -= 1
136 search.Exit(self)
137
138
139 def Tree(self, filter_nodes=None, accept_props=None):
140 class DumpTreeSearch(IDLSearch):
141 def __init__(self, props):
142 IDLSearch.__init__(self)
143 self.out = []
144 self.props = props
145
146 def Enter(self, node):
147 tab = ''.rjust(self.depth * 2)
148 self.out.append(tab + str(node))
149 if self.props:
150 for key, value in node._properties.iteritems():
151 proplist = []
152 if key in self.props:
153 proplist.append(tab + ' %s: %s' %
154 (key, str(node._properties[key])))
155 if proplist:
156 self.out.append(tab + ' PROPERTIES')
157 self.out.extend(proplist)
158
159 if filter_nodes == None:
160 filter_nodes = ['Comment', 'Copyright']
161
162 search = DumpTreeSearch(accept_props)
163 self.Traverse(search, filter_nodes)
164 return search.out
165
166 #
167 # Search related functions
168 #
169 # Check if node is of a given type
170 def IsA(self, *typelist):
171 if self._cls in typelist: return True
172 return False
173
174 # Get a list of all children
175 def GetChildren(self):
176 return self._children
177
178 def GetListOf(self, *keys):
179 out = []
180 for child in self._children:
181 if child._cls in keys: out.append(child)
182 return out
183
184 def GetOneOf(self, *keys):
185 out = self.GetListOf(*keys)
186 if out: return out[0]
187 return None
188
189 def AddChildren(self, children):
190 children = CopyToList(children)
191 for child in children:
192 if not child:
193 continue
194 if type(child) == IDLAttribute:
195 self.SetProperty(child._name, child._value)
196 continue
197 if type(child) == IDLNode:
198 child._parent = self
199 self._children.append(child)
200 continue
201 raise RuntimeError('Adding child of type .\n' % type(child).__name__)
202
203
204 #
205 # Property Functions
206 #
207 def SetProperty(self, name, val):
208 self._properties[name] = val
209
210 def GetProperty(self, name, default=None):
211 return self._properties.get(name, default)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698