OLD | NEW |
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import json | 6 import json |
7 import os.path | 7 import os.path |
8 import sys | 8 import sys |
| 9 import re |
9 | 10 |
10 # This file is a peer to json_schema.py. Each of these files understands a | 11 # This file is a peer to json_schema.py. Each of these files understands a |
11 # certain format describing APIs (either JSON or IDL), reads files written | 12 # certain format describing APIs (either JSON or IDL), reads files written |
12 # in that format into memory, and emits them as a Python array of objects | 13 # in that format into memory, and emits them as a Python array of objects |
13 # corresponding to those APIs, where the objects are formatted in a way that | 14 # corresponding to those APIs, where the objects are formatted in a way that |
14 # the JSON schema compiler understands. compiler.py drives both idl_schema.py | 15 # the JSON schema compiler understands. compiler.py drives both idl_schema.py |
15 # and json_schema.py. | 16 # and json_schema.py. |
16 | 17 |
17 # idl_parser expects to be able to import certain files in its directory, | 18 # idl_parser expects to be able to import certain files in its directory, |
18 # so let's set things up the way it wants. | 19 # so let's set things up the way it wants. |
19 idl_generators_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), | 20 idl_generators_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
20 os.pardir, os.pardir, 'ppapi', 'generators') | 21 os.pardir, os.pardir, 'ppapi', 'generators') |
21 if idl_generators_path not in sys.path: | 22 if idl_generators_path not in sys.path: |
22 sys.path.insert(0, idl_generators_path) | 23 sys.path.insert(0, idl_generators_path) |
23 import idl_parser | 24 import idl_parser |
24 | 25 |
| 26 def ProcessComment(comment): |
| 27 ''' |
| 28 Given the string from a Comment node, parse it into a tuple that looks |
| 29 like: |
| 30 ( |
| 31 "The processed comment, minus all |parameter| mentions.", |
| 32 { |
| 33 'parameter_name_1': "The comment that followed |parameter_name_1|:", |
| 34 ... |
| 35 } |
| 36 ) |
| 37 ''' |
| 38 # Find all the parameter comments of the form "|name|: comment". |
| 39 parameter_comments = re.findall(r'\n *\|([^|]*)\| *: *(.*)', comment) |
| 40 # Get the parent comment (everything before the first parameter comment. |
| 41 parent_comment = re.sub(r'\n *\|.*', '', comment) |
| 42 parent_comment = parent_comment.replace('\n', '').strip() |
| 43 |
| 44 parsed = {} |
| 45 for (name, comment) in parameter_comments: |
| 46 parsed[name] = comment.replace('\n', '').strip() |
| 47 return (parent_comment, parsed) |
| 48 |
25 class Callspec(object): | 49 class Callspec(object): |
26 ''' | 50 ''' |
27 Given a Callspec node representing an IDL function declaration, converts into | 51 Given a Callspec node representing an IDL function declaration, converts into |
28 a name/value pair where the value is a list of function parameters. | 52 a name/value pair where the value is a list of function parameters. |
29 ''' | 53 ''' |
30 def __init__(self, callspec_node): | 54 def __init__(self, callspec_node, comment): |
31 self.node = callspec_node | 55 self.node = callspec_node |
| 56 self.comment = comment |
32 | 57 |
33 def process(self, callbacks): | 58 def process(self, callbacks): |
34 parameters = [] | 59 parameters = [] |
35 for node in self.node.children: | 60 for node in self.node.children: |
36 parameters.append(Param(node).process(callbacks)) | 61 parameter = Param(node).process(callbacks) |
| 62 if parameter['name'] in self.comment: |
| 63 parameter['description'] = self.comment[parameter['name']] |
| 64 parameters.append(parameter) |
37 return self.node.GetName(), parameters | 65 return self.node.GetName(), parameters |
38 | 66 |
39 class Param(object): | 67 class Param(object): |
40 ''' | 68 ''' |
41 Given a Param node representing a function parameter, converts into a Python | 69 Given a Param node representing a function parameter, converts into a Python |
42 dictionary that the JSON schema compiler expects to see. | 70 dictionary that the JSON schema compiler expects to see. |
43 ''' | 71 ''' |
44 def __init__(self, param_node): | 72 def __init__(self, param_node): |
45 self.node = param_node | 73 self.node = param_node |
46 | 74 |
(...skipping 30 matching lines...) Expand all Loading... |
77 self.node = member_node | 105 self.node = member_node |
78 | 106 |
79 def process(self, callbacks): | 107 def process(self, callbacks): |
80 properties = {} | 108 properties = {} |
81 name = self.node.GetName() | 109 name = self.node.GetName() |
82 if self.node.GetProperty('OPTIONAL'): | 110 if self.node.GetProperty('OPTIONAL'): |
83 properties['optional'] = True | 111 properties['optional'] = True |
84 if self.node.GetProperty('nodoc'): | 112 if self.node.GetProperty('nodoc'): |
85 properties['nodoc'] = True | 113 properties['nodoc'] = True |
86 is_function = False | 114 is_function = False |
| 115 parameter_comments = {} |
| 116 for node in self.node.children: |
| 117 if node.cls == 'Comment': |
| 118 (parent_comment, parameter_comments) = ProcessComment(node.GetName()) |
| 119 properties['description'] = parent_comment |
87 for node in self.node.children: | 120 for node in self.node.children: |
88 if node.cls == 'Callspec': | 121 if node.cls == 'Callspec': |
89 is_function = True | 122 is_function = True |
90 name, parameters = Callspec(node).process(callbacks) | 123 name, parameters = Callspec(node, parameter_comments).process(callbacks) |
91 properties['parameters'] = parameters | 124 properties['parameters'] = parameters |
92 properties['name'] = name | 125 properties['name'] = name |
93 if is_function: | 126 if is_function: |
94 properties['type'] = 'function' | 127 properties['type'] = 'function' |
95 else: | 128 else: |
96 properties = Typeref(self.node.GetProperty('TYPEREF'), | 129 properties = Typeref(self.node.GetProperty('TYPEREF'), |
97 self.node, properties).process(callbacks) | 130 self.node, properties).process(callbacks) |
98 return name, properties | 131 return name, properties |
99 | 132 |
100 class Typeref(object): | 133 class Typeref(object): |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 ''' | 274 ''' |
242 Dump a json serialization of parse result for the IDL files whose names | 275 Dump a json serialization of parse result for the IDL files whose names |
243 were passed in on the command line. | 276 were passed in on the command line. |
244 ''' | 277 ''' |
245 for filename in sys.argv[1:]: | 278 for filename in sys.argv[1:]: |
246 schema = Load(filename) | 279 schema = Load(filename) |
247 print json.dumps(schema, indent=2) | 280 print json.dumps(schema, indent=2) |
248 | 281 |
249 if __name__ == '__main__': | 282 if __name__ == '__main__': |
250 Main() | 283 Main() |
OLD | NEW |