OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os.path | 5 import os.path |
6 import sys | 6 import sys |
7 import re | |
7 | 8 |
8 # This file is a peer to json_schema.py. Each of these files understands a | 9 # This file is a peer to json_schema.py. Each of these files understands a |
9 # certain format describing APIs (either JSON or IDL), reads files written | 10 # certain format describing APIs (either JSON or IDL), reads files written |
10 # in that format into memory, and emits them as a Python array of objects | 11 # in that format into memory, and emits them as a Python array of objects |
11 # corresponding to those APIs, where the objects are formatted in a way that | 12 # corresponding to those APIs, where the objects are formatted in a way that |
12 # the JSON schema compiler understands. compiler.py drives both idl_schema.py | 13 # the JSON schema compiler understands. compiler.py drives both idl_schema.py |
13 # and json_schema.py. | 14 # and json_schema.py. |
14 | 15 |
15 # idl_parser expects to be able to import certain files in its directory, | 16 # idl_parser expects to be able to import certain files in its directory, |
16 # so let's set things up the way it wants. | 17 # so let's set things up the way it wants. |
17 idl_generators_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), | 18 idl_generators_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
18 os.pardir, os.pardir, 'ppapi', 'generators') | 19 os.pardir, os.pardir, 'ppapi', 'generators') |
19 if idl_generators_path not in sys.path: | 20 if idl_generators_path not in sys.path: |
20 sys.path.insert(0, idl_generators_path) | 21 sys.path.insert(0, idl_generators_path) |
21 import idl_parser | 22 import idl_parser |
22 | 23 |
24 def ProcessComment(comment): | |
25 ''' | |
26 Given the string from a Comment node, parse it into a dictionary that looks | |
27 like: | |
28 { | |
29 '': "The processed comment, minus all |parameter| mentions.", | |
asargent_no_longer_on_chrome
2012/04/16 17:34:11
nit: using the empty string as a key makes the cod
Matt Perry
2012/04/16 19:19:15
I had it return a tuple of (summary_comment, parse
| |
30 'parameter_name_1': "The comment that followed |parameter_name_1|: | |
31 } | |
32 ''' | |
33 # Find all the parameter comments of the form "|name|: comment". | |
34 parameter_comments = re.findall(r'\n *\|([^|]*)\| *: *(.*)', comment) | |
35 # Get the parent comment (everything before the first parameter comment. | |
36 parent_comment = re.sub(r'\n *\|.*', '', comment) | |
37 parent_comment = parent_comment.replace('\n', '').strip() | |
38 | |
39 parsed = {'': parent_comment} | |
40 for (name, comment) in parameter_comments: | |
41 parsed[name] = comment.replace('\n', '').strip() | |
42 return parsed | |
43 | |
23 class Callspec(object): | 44 class Callspec(object): |
24 ''' | 45 ''' |
25 Given a Callspec node representing an IDL function declaration, converts into | 46 Given a Callspec node representing an IDL function declaration, converts into |
26 a name/value pair where the value is a list of function parameters. | 47 a name/value pair where the value is a list of function parameters. |
27 ''' | 48 ''' |
28 def __init__(self, callspec_node): | 49 def __init__(self, callspec_node, comment): |
29 self.node = callspec_node | 50 self.node = callspec_node |
51 self.comment = comment | |
30 | 52 |
31 def process(self, refs): | 53 def process(self, refs): |
32 parameters = [] | 54 parameters = [] |
33 for node in self.node.children: | 55 for node in self.node.children: |
34 parameters.append(Param(node).process(refs)) | 56 parameter = Param(node).process(refs) |
57 if parameter['name'] in self.comment: | |
58 parameter['description'] = self.comment[parameter['name']] | |
59 parameters.append(parameter) | |
35 return self.node.GetName(), parameters | 60 return self.node.GetName(), parameters |
36 | 61 |
37 class Param(object): | 62 class Param(object): |
38 ''' | 63 ''' |
39 Given a Param node representing a function parameter, converts into a Python | 64 Given a Param node representing a function parameter, converts into a Python |
40 dictionary that the JSON schema compiler expects to see. | 65 dictionary that the JSON schema compiler expects to see. |
41 ''' | 66 ''' |
42 def __init__(self, param_node): | 67 def __init__(self, param_node): |
43 self.node = param_node | 68 self.node = param_node |
44 | 69 |
(...skipping 30 matching lines...) Expand all Loading... | |
75 self.node = member_node | 100 self.node = member_node |
76 | 101 |
77 def process(self, refs): | 102 def process(self, refs): |
78 properties = {} | 103 properties = {} |
79 name = self.node.GetName() | 104 name = self.node.GetName() |
80 if self.node.GetProperty('OPTIONAL'): | 105 if self.node.GetProperty('OPTIONAL'): |
81 properties['optional'] = True | 106 properties['optional'] = True |
82 if self.node.GetProperty('nodoc'): | 107 if self.node.GetProperty('nodoc'): |
83 properties['nodoc'] = True | 108 properties['nodoc'] = True |
84 is_function = False | 109 is_function = False |
110 comment = {} | |
111 for node in self.node.children: | |
112 if node.cls == 'Comment': | |
113 comment = ProcessComment(node.GetName()) | |
114 properties['description'] = comment[''] | |
85 for node in self.node.children: | 115 for node in self.node.children: |
86 if node.cls == 'Callspec': | 116 if node.cls == 'Callspec': |
87 is_function = True | 117 is_function = True |
88 name, parameters = Callspec(node).process(refs) | 118 name, parameters = Callspec(node, comment).process(refs) |
89 properties['parameters'] = parameters | 119 properties['parameters'] = parameters |
90 properties['name'] = name | 120 properties['name'] = name |
91 if is_function: | 121 if is_function: |
92 properties['type'] = 'function' | 122 properties['type'] = 'function' |
93 else: | 123 else: |
94 properties = Typeref(self.node.GetProperty('TYPEREF'), | 124 properties = Typeref(self.node.GetProperty('TYPEREF'), |
95 self.node, properties).process(refs) | 125 self.node, properties).process(refs) |
96 return name, properties | 126 return name, properties |
97 | 127 |
98 class Typeref(object): | 128 class Typeref(object): |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 Python dictionary in a format that the JSON schema compiler expects to see. | 257 Python dictionary in a format that the JSON schema compiler expects to see. |
228 ''' | 258 ''' |
229 | 259 |
230 f = open(filename, 'r') | 260 f = open(filename, 'r') |
231 contents = f.read() | 261 contents = f.read() |
232 f.close() | 262 f.close() |
233 | 263 |
234 idl = idl_parser.IDLParser().ParseData(contents, filename) | 264 idl = idl_parser.IDLParser().ParseData(contents, filename) |
235 idl_schema = IDLSchema(idl) | 265 idl_schema = IDLSchema(idl) |
236 return idl_schema.process() | 266 return idl_schema.process() |
OLD | NEW |