Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
embeed the javasphinx module
[simgrid.git] / docs / source / _ext / javasphinx / formatter.py
1 #
2 # Copyright 2012-2015 Bronto Software, Inc. and contributors
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 """
18 Convert Java syntax tree nodes to string representations.
19
20 """
21
22 import javalang
23
24 from .util import StringBuilder
25
26 # The order for displaying modifiers
27 __modifiers_order = ('public', 'protected', 'private', 'static', 'abstract', 'final',
28                      'native', 'synchronized', 'transient', 'volatile', 'strictfp')
29
30 def formatter(f):
31     def _f(node, output=None, **kwargs):
32         if output is None:
33             output = StringBuilder()
34
35         f(node, output, **kwargs)
36         return output
37     return _f
38
39 def output_list(f, items, output=None, sep=', '):
40     if items:
41         f(items[0], output)
42         for item in items[1:]:
43             output.append(sep)
44             f(item, output)
45
46 @formatter
47 def output_annotation(annotation, output):
48     output.append('@')
49     output.append(annotation.name)
50     output.append(' ')
51
52 @formatter
53 def output_type(type, output, with_generics=True):
54     if not type:
55         output.append('void')
56         return
57
58     if type.dimensions:
59         dim = '[]' * len(type.dimensions)
60     else:
61         dim = ''
62
63     if isinstance(type, javalang.tree.BasicType):
64         output.append(type.name)
65     else:
66         while type:
67             output.append(type.name)
68
69             if with_generics:
70                 output_type_args(type.arguments, output)
71
72             type = type.sub_type
73
74             if type:
75                 output.append('.')
76     output.append(dim)
77
78 @formatter
79 def output_exception(exception, output):
80     output.append(exception)
81
82 @formatter
83 def output_type_arg(type_arg, output):
84     if type_arg.pattern_type == '?':
85         output.append('?')
86     else:
87         if type_arg.pattern_type:
88             output.append('? ')
89             output.append(type_arg.pattern_type)
90             output.append(' ')
91
92         output_type(type_arg.type, output)
93
94 @formatter
95 def output_type_args(type_args, output):
96     if type_args:
97         output.append('<')
98         output_list(output_type_arg, type_args, output, ', ')
99         output.append('>')
100
101 @formatter
102 def output_type_param(type_param, output):
103     output.append(type_param.name)
104
105     if type_param.extends:
106         output.append(' extends ')
107         output_list(output_type, type_param.extends, output, ' & ')
108
109 @formatter
110 def output_type_params(type_params, output):
111     if type_params:
112         output.append('<')
113         output_list(output_type_param, type_params, output, ', ')
114         output.append('>')
115
116 @formatter
117 def output_declaration(declaration, output):
118     for annotation in declaration.annotations:
119         output_annotation(annotation, output)
120
121     output_modifiers(declaration.modifiers, output)
122     output.append(' ')
123
124     if isinstance(declaration, javalang.tree.ClassDeclaration):
125         output.append('class ')
126     elif isinstance(declaration, javalang.tree.EnumDeclaration):
127         output.append('enum ')
128     elif isinstance(declaration, javalang.tree.InterfaceDeclaration):
129         output.append('interface ')
130     elif isinstance(declaration, javalang.tree.AnnotationDeclaration):
131         output.append('@interface ')
132
133     output.append(declaration.name)
134
135     if isinstance(declaration, (javalang.tree.ClassDeclaration, javalang.tree.InterfaceDeclaration)):
136         output_type_params(declaration.type_parameters, output)
137
138     if isinstance(declaration, javalang.tree.ClassDeclaration) and declaration.extends:
139         output.append(' extends ')
140         output_type(declaration.extends, output)
141
142     if isinstance(declaration, javalang.tree.InterfaceDeclaration) and declaration.extends:
143         output.append(' extends ')
144         output_list(output_type, declaration.extends, output, ', ')
145
146     if isinstance(declaration, (javalang.tree.ClassDeclaration, javalang.tree.EnumDeclaration)) and declaration.implements:
147         output.append(' implements ')
148         output_list(output_type, declaration.implements, output, ', ')
149
150 @formatter
151 def output_formal_param(param, output):
152     output_type(param.type, output)
153
154     if param.varargs:
155         output.append('...')
156
157     output.append(' ')
158     output.append(param.name)
159
160 @formatter
161 def output_modifiers(modifiers, output):
162     ordered_modifiers = [mod for mod in __modifiers_order if mod in modifiers]
163     output_list(lambda mod, output: output.append(mod), ordered_modifiers, output, ' ')