vhdl_parser 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,20 @@
1
1
  module VHDL_Parser
2
2
  class Entity
3
3
 
4
- attr_accessor :name
5
- attr_reader :ports, :generics
4
+ # An array of all the Ports.
5
+ # @return [Array<Port>] An array of all the Ports on this entity.
6
+ attr_reader :ports
6
7
 
7
- def initialize
8
+ # An array of all the Generics.
9
+ # @return [Array<Generic>] An array of all the Generics on this entity.
10
+ attr_reader :generics
11
+
12
+ # The name of the Entity.
13
+ # @return [String] The name of the Entity
14
+ attr_reader :name
15
+
16
+ def initialize(name)
17
+ @name = name
8
18
  @ports = Array.new
9
19
  @generics = Array.new
10
20
  end
@@ -19,6 +29,13 @@ module VHDL_Parser
19
29
  end
20
30
 
21
31
 
32
+ # Applies constants from a package to the entity. For example, if
33
+ # the Entity has an input whose size is defined with a constant, like
34
+ # "std_logic_vector(MY_SIZE-1 downto 0)", and "MY_SIZE" is defined in
35
+ # the package, this method will evaluate the size to a real number.
36
+ #
37
+ # @param [package] Package The package instance to merge.
38
+ # @return [nil] Nothing
22
39
  def merge_package(package)
23
40
  # TODO: do some type checking
24
41
  @generics.each do |g|
@@ -35,6 +52,7 @@ module VHDL_Parser
35
52
  g.right = Utility.sub_constants(g.right, k, v)
36
53
  end
37
54
  end
55
+ nil
38
56
  end
39
57
 
40
58
  @ports.each do |p|
@@ -50,6 +68,8 @@ module VHDL_Parser
50
68
  end
51
69
  end
52
70
 
71
+ # Re-processes Generics. May not be needed, so don't worry about it.
72
+ # @visibility private
53
73
  def process_generics
54
74
  @ports.each do |p|
55
75
  @generics.each do |g|
@@ -1,4 +1,7 @@
1
1
  module VHDL_Parser
2
+
3
+ # Helper class with methods that extract a portion of interest from
4
+ # a given String.
2
5
  class Extractor
3
6
 
4
7
  def self.extract_name(string)
@@ -1,6 +1,39 @@
1
1
  module VHDL_Parser
2
+
3
+ # Describes a VHDL Generic.
2
4
  class Generic
3
- attr_accessor :name, :value, :type, :size, :left, :right, :size_dir, :comment
5
+
6
+ # Name of the Generic.
7
+ # @return [String]
8
+ attr_accessor :name
9
+
10
+ # Value of the Generic.
11
+ # @return [String]
12
+ attr_accessor :value
13
+
14
+ # Type of the Generic.
15
+ # @return [String]
16
+ attr_accessor :type
17
+
18
+ # Size String of the Generic.
19
+ # @return [String]
20
+ attr_accessor :size
21
+
22
+ # The left part of the size definition, if there is one.
23
+ # @return [String]
24
+ attr_accessor :left
25
+
26
+ # The right part of the size definition, if there is one.
27
+ # @return [String]
28
+ attr_accessor :right
29
+
30
+ # The direction of the size, i.e. "to" or "downto"
31
+ # @return [String]
32
+ attr_accessor :size_dir
33
+
34
+ # Any inline comments of that Generic.
35
+ # @return [String]
36
+ attr_accessor :comment
4
37
 
5
38
  def initialize
6
39
  @left = 0
@@ -8,6 +41,8 @@ module VHDL_Parser
8
41
  @size_dir = "downto"
9
42
  end
10
43
 
44
+ # Basic String representation of the Generic
45
+ # @return [String]
11
46
  def to_s
12
47
  name.ljust(15) + "\t" +
13
48
  type + " " +
@@ -1,4 +1,8 @@
1
1
  class String
2
+
3
+ # Addition to String class that determines if a String is
4
+ # numeric.
5
+ # @return [boolean] Whether the String is numeric.
2
6
  def is_numeric?
3
7
  true if Float(self) rescue false
4
8
  end
@@ -1,16 +1,23 @@
1
1
  module VHDL_Parser
2
+ # Represents a VHDL Package
2
3
  class Package
3
4
 
5
+ # A Hash of all the constants defined in this Package.
6
+ # @return [Hash]
4
7
  attr_accessor :constants
5
8
 
6
9
  def initialize
7
10
  @constants = Hash.new
8
11
  end
9
12
 
13
+ # @return [String]
10
14
  def to_s
11
15
  @constants.to_s
12
16
  end
13
17
 
18
+ # Processes the constants defined in the package. This resolves
19
+ # constants being dependent on other constants in this package.
20
+ # @return [nil] Nothing
14
21
  def process
15
22
  @constants.each do |k,v|
16
23
  @constants.each do |k2,v2|
@@ -1,6 +1,39 @@
1
1
  module VHDL_Parser
2
+ # Defines an input or output on the Entity
2
3
  class Port
3
- attr_accessor :name, :direction, :comment, :type, :size, :left, :right, :size_dir
4
+ # The name of the Port
5
+ # @return [String]
6
+ attr_accessor :name
7
+
8
+ # The direction of the port.
9
+ # I.e. "in", "out", "inout"
10
+ # @return [String]
11
+ attr_accessor :direction
12
+
13
+ # Any inline comment on the Port
14
+ # @return [String]
15
+ attr_accessor :comment
16
+
17
+ # The type of the Port
18
+ # @return [String]
19
+ attr_accessor :type
20
+
21
+ # String representation of the size,
22
+ # e.g. "(7 downto 0)"
23
+ # @return [String]
24
+ attr_accessor :size
25
+
26
+ # Left part of the size, e.g. "7"
27
+ # @return [String]
28
+ attr_accessor :left
29
+
30
+ # Right part of the size, e.g. "0"
31
+ # @return [String]
32
+ attr_accessor :right
33
+
34
+ # Left part of the size, e.g. "downto"
35
+ # @return [String]
36
+ attr_accessor :size_dir
4
37
 
5
38
  def initialize
6
39
  @left = 0
@@ -8,6 +41,7 @@ module VHDL_Parser
8
41
  @size_dir = "downto"
9
42
  end
10
43
 
44
+ # @return [String]
11
45
  def to_s
12
46
  name.ljust(15) + "\t" +
13
47
  direction + "\t" +
@@ -16,6 +50,8 @@ module VHDL_Parser
16
50
  comment + "\n"
17
51
  end
18
52
 
53
+ # Returns a string based on the left, right, and size_dir attributes.
54
+ # @return [String] The size formatted as a String.
19
55
  def size
20
56
  unless @left.nil?
21
57
  "(#{@left} #{@size_dir} #{@right})"
@@ -1,5 +1,16 @@
1
1
  module VHDL_Parser
2
+ # Utility class with helpers
2
3
  class Utility
4
+
5
+ # Substitutes a constant with an actual value, and does so smartly.
6
+ # This method uses the `eval` method to evaluate the final Math expression
7
+ # into a single value. It does some basic input checking and replaces everything
8
+ # that's not a number or mathematical operator with "x". That should prevent
9
+ # some abuse.
10
+ #
11
+ # @param [target] String The size string, e.g. "MY_VALUE-1"
12
+ # @param [key] String The key string, e.g. "MY_VALUE". This gets compared to the target.
13
+ # @param [value] String The value string, e.g. "8". That's what MY_VALUE gets replaced with.
3
14
  def self.sub_constants(target, key, value)
4
15
  # first get all the words of the size. That could be
5
16
  # a number or a constant.
data/lib/vhdl_parser.rb CHANGED
@@ -7,16 +7,24 @@ require 'vhdl_parser/generic'
7
7
  require 'vhdl_parser/package'
8
8
 
9
9
 
10
+ # VHDL Parser Module. It parses a given VHDL entity into Ruby objects that can
11
+ # then be used further in code, e.g. for visualization or automation.
10
12
  module VHDL_Parser
11
13
 
12
14
  class << self
13
15
  end
14
16
 
17
+ # Parses the given file and returns an Entity object.
18
+ # @param [filename] String the path to the file.
19
+ # @return [Entity] Entity object
15
20
  def self.parse_file(filename)
16
21
  string = File.read(filename)
17
22
  self.parse(string)
18
23
  end
19
24
 
25
+ # Parses the given string and returns an Entity object.
26
+ # @param [vhdl_string] String The VHDL entity as a String
27
+ # @return [Entity] Entity object
20
28
  def self.parse(vhdl_string)
21
29
  info = vhdl_string.match /entity\s+(.*)\s+is\s+(?:generic\s*\((.*)\);)?\s*port\s*\((.*)\);\s*end\s*\1;/im
22
30
 
@@ -28,8 +36,7 @@ module VHDL_Parser
28
36
  ports.map! { |s| s.strip!}
29
37
  ports.delete_if { |l| l.match /^--/}
30
38
 
31
- @entity = Entity.new
32
- @entity.name = info[1]
39
+ @entity = Entity.new(info[1])
33
40
 
34
41
 
35
42
  self.parse_generics(generics)
@@ -40,11 +47,17 @@ module VHDL_Parser
40
47
  return @entity
41
48
  end
42
49
 
50
+ # Parses the given VHDL package file and returns a Package object.
51
+ # @param [filename] String the path to the file.
52
+ # @return [Package] Package object
43
53
  def self.parse_package_file(filename)
44
54
  string = File.read(filename)
45
55
  self.parse_package(string)
46
56
  end
47
57
 
58
+ # Parses the given VHDL package string and returns a Package object.
59
+ # @param [string] String The VHDL package as a String
60
+ # @return [Package] Package object
48
61
  def self.parse_package(string)
49
62
  package = Package.new
50
63
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vhdl_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -51,3 +51,4 @@ signing_key:
51
51
  specification_version: 3
52
52
  summary: VHDL Entity Parser
53
53
  test_files: []
54
+ has_rdoc: