158
158
159
159
160
160
class VhdlObject (object ):
161
- ''' Base class for parsed VHDL objects
161
+ """ Base class for parsed VHDL objects
162
162
163
163
Args:
164
164
name (str): Name of the object
165
165
desc (str): Description from object metacomments
166
- '''
166
+ """
167
167
def __init__ (self , name , desc = None ):
168
168
self .name = name
169
169
self .kind = 'unknown'
170
170
self .desc = desc
171
171
172
172
class VhdlParameter (object ):
173
- ''' Parameter to subprograms, ports, and generics
173
+ """ Parameter to subprograms, ports, and generics
174
174
175
175
Args:
176
176
name (str): Name of the object
@@ -179,7 +179,7 @@ class VhdlParameter(object):
179
179
default_value (str): Default value of the parameter
180
180
desc (str): Description from object metacomments
181
181
param_desc (str): Description of the parameter
182
- '''
182
+ """
183
183
def __init__ (self , name , mode = None , data_type = None , default_value = None , desc = None , param_desc = None ):
184
184
self .name = name
185
185
self .mode = mode
@@ -203,15 +203,15 @@ def __repr__(self):
203
203
return "VhdlParameter('{}', '{}', '{}')" .format (self .name , self .mode , self .data_type .name + self .data_type .arange )
204
204
205
205
class VhdlParameterType (object ):
206
- ''' Parameter type definition
206
+ """ Parameter type definition
207
207
208
- Args:
208
+ Args:
209
209
name (str): Name of the type
210
210
direction(str): "to" or "downto"
211
211
r_bound (str): A simple expression based on digits or variable names
212
212
l_bound (str): A simple expression based on digits or variable names
213
213
arange (str): Original array range string
214
- '''
214
+ """
215
215
def __init__ (self , name , direction = "" , r_bound = "" , l_bound = "" , arange = "" ):
216
216
self .name = name
217
217
self .direction = direction .strip ()
@@ -223,25 +223,25 @@ def __repr__(self):
223
223
return "VhdlParameterType('{}','{}')" .format (self .name , self .arange )
224
224
225
225
class VhdlPackage (VhdlObject ):
226
- ''' Package declaration
226
+ """ Package declaration
227
227
228
228
Args:
229
229
name (str): Name of the package
230
230
desc (str): Description from object metacomments
231
- '''
231
+ """
232
232
def __init__ (self , name , desc = None ):
233
233
VhdlObject .__init__ (self , name , desc )
234
234
self .kind = 'package'
235
235
236
236
class VhdlType (VhdlObject ):
237
- ''' Type definition
237
+ """ Type definition
238
238
239
239
Args:
240
240
name (str): Name of the type
241
241
package (str): Package containing the type
242
242
type_of (str): Object type of this type definition
243
243
desc (str, optional): Description from object metacomments
244
- '''
244
+ """
245
245
def __init__ (self , name , package , type_of , desc = None ):
246
246
VhdlObject .__init__ (self , name , desc )
247
247
self .kind = 'type'
@@ -252,14 +252,14 @@ def __repr__(self):
252
252
253
253
254
254
class VhdlSubtype (VhdlObject ):
255
- ''' Subtype definition
255
+ """ Subtype definition
256
256
257
257
Args:
258
258
name (str): Name of the subtype
259
259
package (str): Package containing the subtype
260
260
base_type (str): Base type name derived from
261
261
desc (str, optional): Description from object metacomments
262
- '''
262
+ """
263
263
def __init__ (self , name , package , base_type , desc = None ):
264
264
VhdlObject .__init__ (self , name , desc )
265
265
self .kind = 'subtype'
@@ -270,14 +270,14 @@ def __repr__(self):
270
270
271
271
272
272
class VhdlConstant (VhdlObject ):
273
- ''' Constant definition
273
+ """ Constant definition
274
274
275
275
Args:
276
276
name (str): Name of the constant
277
277
package (str): Package containing the constant
278
278
base_type (str): Type fo the constant
279
279
desc (str, optional): Description from object metacomments
280
- '''
280
+ """
281
281
def __init__ (self , name , package , base_type , desc = None ):
282
282
VhdlObject .__init__ (self , name , desc )
283
283
self .kind = 'constant'
@@ -288,15 +288,15 @@ def __repr__(self):
288
288
289
289
290
290
class VhdlFunction (VhdlObject ):
291
- ''' Function declaration
291
+ """ Function declaration
292
292
293
293
Args:
294
294
name (str): Name of the function
295
295
package (str): Package containing the function
296
296
parameters (list of VhdlParameter): Parameters to the function
297
297
return_type (str, optional): Type of the return value
298
298
desc (str, optional): Description from object metacomments
299
- '''
299
+ """
300
300
def __init__ (self , name , package , parameters , return_type = None , desc = None ):
301
301
VhdlObject .__init__ (self , name , desc )
302
302
self .kind = 'function'
@@ -309,14 +309,14 @@ def __repr__(self):
309
309
310
310
311
311
class VhdlProcedure (VhdlObject ):
312
- ''' Procedure declaration
312
+ """ Procedure declaration
313
313
314
314
Args:
315
315
name (str): Name of the procedure
316
316
package (str): Package containing the procedure
317
317
parameters (list of VhdlParameter): Parameters to the procedure
318
318
desc (str, optional): Description from object metacomments
319
- '''
319
+ """
320
320
def __init__ (self , name , package , parameters , desc = None ):
321
321
VhdlObject .__init__ (self , name , desc )
322
322
self .kind = 'procedure'
@@ -327,14 +327,14 @@ def __repr__(self):
327
327
return "VhdlProcedure('{}')" .format (self .name )
328
328
329
329
class VhdlEntity (VhdlObject ):
330
- ''' Entity declaration
330
+ """ Entity declaration
331
331
Args:
332
332
name (str): Name of the entity
333
333
ports (list of VhdlParameter): Port parameters to the entity
334
334
generics (list of VhdlParameter): Generic parameters to the entity
335
335
sections (list of str): Metacomment sections
336
336
desc (str, optional): Description from object metacomments
337
- '''
337
+ """
338
338
def __init__ (self , name , ports , generics = None , sections = None , desc = None ):
339
339
VhdlObject .__init__ (self , name , desc )
340
340
self .kind = 'entity'
@@ -351,7 +351,7 @@ def dump(self):
351
351
print ('\t {} ({}), {} ({})' .format (p .name , type (p .name ), p .data_type , type (p .data_type )))
352
352
353
353
class VhdlComponent (VhdlObject ):
354
- ''' Component declaration
354
+ """ Component declaration
355
355
356
356
Args:
357
357
name (str): Name of the component
@@ -360,7 +360,7 @@ class VhdlComponent(VhdlObject):
360
360
generics (list of VhdlParameter): Generic parameters to the component
361
361
sections (list of str): Metacomment sections
362
362
desc (str, optional): Description from object metacomments
363
- '''
363
+ """
364
364
def __init__ (self , name , package , ports , generics = None , sections = None , desc = None ):
365
365
VhdlObject .__init__ (self , name , desc )
366
366
self .kind = 'component'
@@ -379,25 +379,25 @@ def dump(self):
379
379
380
380
381
381
def parse_vhdl_file (fname ):
382
- ''' Parse a named VHDL file
382
+ """ Parse a named VHDL file
383
383
384
384
Args:
385
385
fname(str): Name of file to parse
386
386
Returns:
387
387
Parsed objects.
388
- '''
388
+ """
389
389
with open (fname , 'rt' ) as fh :
390
390
text = fh .read ()
391
391
return parse_vhdl (text )
392
392
393
393
def parse_vhdl (text ):
394
- ''' Parse a text buffer of VHDL code
394
+ """ Parse a text buffer of VHDL code
395
395
396
396
Args:
397
397
text(str): Source code to parse
398
398
Returns:
399
399
Parsed objects.
400
- '''
400
+ """
401
401
lex = VhdlLexer
402
402
403
403
name = None
@@ -606,13 +606,13 @@ def parse_vhdl(text):
606
606
607
607
608
608
def subprogram_prototype (vo ):
609
- ''' Generate a canonical prototype string
609
+ """ Generate a canonical prototype string
610
610
611
611
Args:
612
612
vo (VhdlFunction, VhdlProcedure): Subprogram object
613
613
Returns:
614
614
Prototype string.
615
- '''
615
+ """
616
616
617
617
plist = '; ' .join (str (p ) for p in vo .parameters )
618
618
@@ -628,13 +628,13 @@ def subprogram_prototype(vo):
628
628
return proto
629
629
630
630
def subprogram_signature (vo , fullname = None ):
631
- ''' Generate a signature string
631
+ """ Generate a signature string
632
632
633
633
Args:
634
634
vo (VhdlFunction, VhdlProcedure): Subprogram object
635
635
Returns:
636
636
Signature string.
637
- '''
637
+ """
638
638
639
639
if fullname is None :
640
640
fullname = vo .name
@@ -650,22 +650,22 @@ def subprogram_signature(vo, fullname=None):
650
650
651
651
652
652
def is_vhdl (fname ):
653
- ''' Identify file as VHDL by its extension
653
+ """ Identify file as VHDL by its extension
654
654
655
655
Args:
656
656
fname (str): File name to check
657
657
Returns:
658
658
True when file has a VHDL extension.
659
- '''
659
+ """
660
660
return os .path .splitext (fname )[1 ].lower () in ('.vhdl' , '.vhd' )
661
661
662
662
663
663
class VhdlExtractor (object ):
664
- ''' Utility class that caches parsed objects and tracks array type definitions
664
+ """ Utility class that caches parsed objects and tracks array type definitions
665
665
666
666
Args:
667
667
array_types(set): Initial array types
668
- '''
668
+ """
669
669
def __init__ (self , array_types = set ()):
670
670
self .array_types = set (('std_ulogic_vector' , 'std_logic_vector' ,
671
671
'signed' , 'unsigned' , 'bit_vector' ))
@@ -674,14 +674,14 @@ def __init__(self, array_types=set()):
674
674
self .object_cache = {}
675
675
676
676
def extract_objects (self , fname , type_filter = None ):
677
- ''' Extract objects from a source file
677
+ """ Extract objects from a source file
678
678
679
679
Args:
680
680
fname (str): File to parse
681
681
type_filter (class, optional): Object class to filter results
682
682
Returns:
683
683
List of parsed objects.
684
- '''
684
+ """
685
685
objects = []
686
686
if fname in self .object_cache :
687
687
objects = self .object_cache [fname ]
@@ -698,14 +698,14 @@ def extract_objects(self, fname, type_filter=None):
698
698
return objects
699
699
700
700
def extract_objects_from_source (self , text , type_filter = None ):
701
- ''' Extract object declarations from a text buffer
701
+ """ Extract object declarations from a text buffer
702
702
703
703
Args:
704
704
text (str): Source code to parse
705
705
type_filter (class, optional): Object class to filter results
706
706
Returns:
707
707
List of parsed objects.
708
- '''
708
+ """
709
709
objects = parse_vhdl (text )
710
710
self ._register_array_types (objects )
711
711
@@ -716,13 +716,13 @@ def extract_objects_from_source(self, text, type_filter=None):
716
716
717
717
718
718
def is_array (self , data_type ):
719
- ''' Check if a type is a known array type
719
+ """ Check if a type is a known array type
720
720
721
721
Args:
722
722
data_type (str): Name of type to check
723
723
Returns:
724
724
True if ``data_type`` is a known array type.
725
- '''
725
+ """
726
726
727
727
# Split off any brackets
728
728
data_type = data_type .split ('[' )[0 ].strip ()
@@ -731,20 +731,20 @@ def is_array(self, data_type):
731
731
732
732
733
733
def _add_array_types (self , type_defs ):
734
- ''' Add array data types to internal registry
734
+ """ Add array data types to internal registry
735
735
736
736
Args:
737
737
type_defs (dict): Dictionary of type definitions
738
- '''
738
+ """
739
739
if 'arrays' in type_defs :
740
740
self .array_types |= set (type_defs ['arrays' ])
741
741
742
742
def load_array_types (self , fname ):
743
- ''' Load file of previously extracted data types
743
+ """ Load file of previously extracted data types
744
744
745
745
Args:
746
746
fname (str): Name of file to load array database from
747
- '''
747
+ """
748
748
type_defs = ''
749
749
with open (fname , 'rt' ) as fh :
750
750
type_defs = fh .read ()
@@ -757,21 +757,21 @@ def load_array_types(self, fname):
757
757
self ._add_array_types (type_defs )
758
758
759
759
def save_array_types (self , fname ):
760
- ''' Save array type registry to a file
760
+ """ Save array type registry to a file
761
761
762
762
Args:
763
763
fname (str): Name of file to save array database to
764
- '''
764
+ """
765
765
type_defs = {'arrays' : sorted (list (self .array_types ))}
766
766
with open (fname , 'wt' ) as fh :
767
767
pprint (type_defs , stream = fh )
768
768
769
769
def _register_array_types (self , objects ):
770
- ''' Add array type definitions to internal registry
770
+ """ Add array type definitions to internal registry
771
771
772
772
Args:
773
773
objects (list of VhdlType or VhdlSubtype): Array types to track
774
- '''
774
+ """
775
775
# Add all array types directly
776
776
types = [o for o in objects if isinstance (o , VhdlType ) and o .type_of == 'array_type' ]
777
777
for t in types :
@@ -787,11 +787,11 @@ def _register_array_types(self, objects):
787
787
self .array_types .add (k )
788
788
789
789
def register_array_types_from_sources (self , source_files ):
790
- ''' Add array type definitions from a file list to internal registry
790
+ """ Add array type definitions from a file list to internal registry
791
791
792
792
Args:
793
793
source_files (list of str): Files to parse for array definitions
794
- '''
794
+ """
795
795
for fname in source_files :
796
796
if is_vhdl (fname ):
797
797
self ._register_array_types (self .extract_objects (fname ))
0 commit comments