cauterize 0.0.1.pre5 → 0.0.1.pre7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/README.md +9 -0
  2. data/Rakefile +1 -1
  3. data/bin/cauterize +1 -1
  4. data/cauterize.gemspec +1 -0
  5. data/example/Cauterize +15 -9
  6. data/example/build.sh +3 -0
  7. data/example/doc_cauterize_output/example_project.txt +124 -0
  8. data/example/ruby_ex.rb +14 -0
  9. data/lib/cauterize.rb +117 -0
  10. data/lib/cauterize/base_type.rb +3 -2
  11. data/lib/cauterize/builders.rb +18 -2
  12. data/lib/cauterize/builders/c/group.rb +2 -3
  13. data/lib/cauterize/builders/cs/group.rb +2 -3
  14. data/lib/cauterize/builders/doc/buildable.rb +30 -0
  15. data/lib/cauterize/builders/doc/builtin.rb +21 -0
  16. data/lib/cauterize/builders/doc/composite.rb +23 -0
  17. data/lib/cauterize/builders/doc/enumeration.rb +24 -0
  18. data/lib/cauterize/builders/doc/fixed_array.rb +26 -0
  19. data/lib/cauterize/builders/doc/group.rb +29 -0
  20. data/lib/cauterize/builders/doc/scalar.rb +21 -0
  21. data/lib/cauterize/builders/doc/variable_array.rb +26 -0
  22. data/lib/cauterize/builtin.rb +2 -2
  23. data/lib/cauterize/c_builder.rb +5 -0
  24. data/lib/cauterize/cauterize.rb +7 -2
  25. data/lib/cauterize/composite.rb +8 -7
  26. data/lib/cauterize/cs_builder.rb +8 -5
  27. data/lib/cauterize/doc_builder.rb +34 -0
  28. data/lib/cauterize/enumeration.rb +5 -5
  29. data/lib/cauterize/fixed_array.rb +3 -3
  30. data/lib/cauterize/group.rb +18 -8
  31. data/lib/cauterize/scalar.rb +3 -3
  32. data/lib/cauterize/variable_array.rb +3 -3
  33. data/lib/cauterize/version.rb +1 -1
  34. data/spec/base_type_spec.rb +10 -0
  35. data/spec/builders/c/group_spec.rb +2 -2
  36. data/spec/builders/cs/group_spec.rb +1 -1
  37. data/spec/builders/doc/buildable_spec.rb +25 -0
  38. data/spec/builders_spec.rb +4 -2
  39. data/spec/builtin_spec.rb +2 -0
  40. data/spec/c_builder_spec.rb +12 -3
  41. data/spec/composite_spec.rb +11 -0
  42. data/spec/cs_builder_spec.rb +6 -2
  43. data/spec/doc_builder_spec.rb +247 -0
  44. data/spec/enumeration_spec.rb +2 -0
  45. data/spec/fixed_array_spec.rb +2 -0
  46. data/spec/group_spec.rb +4 -2
  47. data/spec/scalar_spec.rb +2 -0
  48. data/spec/spec_helper.rb +4 -0
  49. data/support/c/src/cauterize.h +1 -1
  50. data/support/cs/src/CauterizeFormatter.cs +7 -5
  51. data/support/cs/src/CauterizeTypes.cs +45 -5
  52. metadata +50 -18
@@ -0,0 +1,21 @@
1
+ module Cauterize
2
+ module Builders
3
+ module Doc
4
+ class BuiltIn < Buildable
5
+ def heading
6
+ [
7
+ "Type Name: #{@blueprint.name}",
8
+ "Cauterize Class: built-in",
9
+ "Description: #{(@blueprint.description ? " - #{@blueprint.description}" : "<none>")}",
10
+ ].join("\n")
11
+ end
12
+
13
+ def body
14
+ ["data - size: #{@blueprint.byte_length} bytes"]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Cauterize::Builders.register(:doc, Cauterize::BuiltIn, Cauterize::Builders::Doc::BuiltIn)
@@ -0,0 +1,23 @@
1
+ module Cauterize
2
+ module Builders
3
+ module Doc
4
+ class Composite < Buildable
5
+ def heading
6
+ [
7
+ "Type Name: #{@blueprint.name}",
8
+ "Cauterize Class: composite",
9
+ "Description: #{(@blueprint.description ? @blueprint.description : "<none>")}",
10
+ ].join("\n")
11
+ end
12
+
13
+ def body
14
+ @blueprint.fields.values.map do |v|
15
+ "#{v.name} - type: #{v.type.name}" + (v.description ? " - description: #{v.description}" : "")
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Cauterize::Builders.register(:doc, Cauterize::Composite, Cauterize::Builders::Doc::Composite)
@@ -0,0 +1,24 @@
1
+ module Cauterize
2
+ module Builders
3
+ module Doc
4
+ class Enumeration < Buildable
5
+ def heading
6
+ [
7
+ "Type Name: #{@blueprint.name}",
8
+ "Cauterize Class: enumeration",
9
+ "Description: #{@blueprint.description ? @blueprint.description : "<none>"}",
10
+ "Encoding: #{@blueprint.representation.name}",
11
+ ].join("\n")
12
+ end
13
+
14
+ def body
15
+ @blueprint.values.values.map {|v| " #{v.name} = #{v.value}"}
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ Cauterize::Builders.register(:doc, Cauterize::Enumeration, Cauterize::Builders::Doc::Enumeration)
23
+
24
+
@@ -0,0 +1,26 @@
1
+ module Cauterize
2
+ module Builders
3
+ module Doc
4
+ class FixedArray < Buildable
5
+ def heading
6
+ [
7
+ "Type Name: #{@blueprint.name}",
8
+ "Cauterize Class: fixed-array",
9
+ "Description: #{@blueprint.description ? @blueprint.description : "<none>"}",
10
+ "Stored Type: #{@blueprint.array_type.name}",
11
+ "Value Count: #{@blueprint.array_size}",
12
+ ].join("\n")
13
+ end
14
+
15
+ def body
16
+ [
17
+ "data - #{@blueprint.array_size} values of type #{@blueprint.array_type.name}",
18
+ ]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ Cauterize::Builders.register(:doc, Cauterize::FixedArray, Cauterize::Builders::Doc::FixedArray)
26
+
@@ -0,0 +1,29 @@
1
+ module Cauterize
2
+ module Builders
3
+ module Doc
4
+ class Group < Buildable
5
+ def heading
6
+ [
7
+ "Type Name: #{@blueprint.name}",
8
+ "Cauterize Class: group",
9
+ "Description: #{(@blueprint.description ? @blueprint.description : "<none>")}",
10
+ ].join("\n")
11
+ end
12
+
13
+ def body
14
+ vals = @blueprint.fields.values.map do |v|
15
+ " #{v.name} - payload: #{v.type ? v.type.name : "<no payload>"}" + (v.description ? " - description: #{v.description}" : "")
16
+ end
17
+
18
+ [
19
+ "kind tag: #{@blueprint.tag_enum.name}",
20
+ "kinds:"
21
+ ] + vals
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ Cauterize::Builders.register(:doc, Cauterize::Group, Cauterize::Builders::Doc::Group)
29
+
@@ -0,0 +1,21 @@
1
+ module Cauterize
2
+ module Builders
3
+ module Doc
4
+ class Scalar < Buildable
5
+ def heading
6
+ [
7
+ "Type Name: #{@blueprint.name}",
8
+ "Cauterize Class: scalar",
9
+ "Description: #{(@blueprint.description ? " - #{@blueprint.description}" : "<none>")}",
10
+ ].join("\n")
11
+ end
12
+
13
+ def body
14
+ ["data - type: #{@blueprint.type_name.name}"]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Cauterize::Builders.register(:doc, Cauterize::Scalar, Cauterize::Builders::Doc::Scalar)
@@ -0,0 +1,26 @@
1
+ module Cauterize
2
+ module Builders
3
+ module Doc
4
+ class VariableArray < Buildable
5
+ def heading
6
+ [
7
+ "Type Name: #{@blueprint.name}",
8
+ "Cauterize Class: variable-array",
9
+ "Description: #{@blueprint.description ? @blueprint.description : "<none>"}",
10
+ "Maximum Value Count: #{@blueprint.array_size}",
11
+ ].join("\n")
12
+ end
13
+
14
+ def body
15
+ [
16
+ "length - type #{@blueprint.size_type.name}",
17
+ "data - 0 to #{@blueprint.array_size} values of type #{@blueprint.array_type.name}",
18
+ ]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ Cauterize::Builders.register(:doc, Cauterize::VariableArray, Cauterize::Builders::Doc::VariableArray)
26
+
@@ -18,7 +18,7 @@ module Cauterize
18
18
 
19
19
  def create_builtins
20
20
  BUILT_IN_TYPES.each do |b|
21
- _b = BuiltIn.new(b[:name])
21
+ _b = BuiltIn.new(b[:name], nil)
22
22
  _b.byte_length(b[:size])
23
23
  _b.is_signed(b[:signed])
24
24
  builtins[b[:name]] = _b
@@ -26,7 +26,7 @@ module Cauterize
26
26
  end
27
27
 
28
28
  class BuiltIn < BaseType
29
- def initialize(name)
29
+ def initialize(name, desc=nil)
30
30
  super
31
31
  end
32
32
 
@@ -21,6 +21,8 @@ module Cauterize
21
21
  f = default_formatter
22
22
 
23
23
  excluder = @name.up_snake + "_H_#{Time.now.to_i}"
24
+ f << "/* WARNING: This is generated code. Do not edit this file directly. */"
25
+ f << ""
24
26
  f << "#ifndef #{excluder}"
25
27
  f << "#define #{excluder}"
26
28
  f.blank_line
@@ -51,6 +53,7 @@ module Cauterize
51
53
 
52
54
  f.blank_line
53
55
  f << "#endif /* #{excluder} */"
56
+ f << "\n"
54
57
 
55
58
  File.open(@h, "wb") do |fh|
56
59
  fh.write(f.to_s)
@@ -60,6 +63,8 @@ module Cauterize
60
63
  def build_c
61
64
  f = default_formatter
62
65
 
66
+ f << "/* WARNING: This is generated code. Do not edit this file directly. */"
67
+ f << ""
63
68
  f << %Q{#include <cauterize_util.h>}
64
69
  f << %Q{#include "#{@name}.h"}
65
70
 
@@ -29,6 +29,12 @@ module Cauterize
29
29
  Cauterize::CSBuilder.new(cs_file, output_prefix)
30
30
  end
31
31
 
32
+ def make_builder_doc(target_dir, output_prefix)
33
+ doc_file = File.join(target_dir, "#{output_prefix}.txt")
34
+
35
+ Cauterize::DocBuilder.new(doc_file, output_prefix)
36
+ end
37
+
32
38
  def get_name
33
39
  @@description_name
34
40
  end
@@ -49,8 +55,7 @@ module Cauterize
49
55
  @@version = version
50
56
  end
51
57
 
52
- private
53
- def self.parse_dsl(desc_file)
58
+ def parse_dsl(desc_file)
54
59
  Object.new.extend(Cauterize).instance_exec do
55
60
  # this magic allows us to emit useful exception messages when evaling the
56
61
  # file. if your description file has errors, you'll be able to find them
@@ -5,8 +5,8 @@
5
5
  module Cauterize
6
6
  module_function
7
7
 
8
- def composite(name)
9
- c = Cauterize.composites[name] || Cauterize.composites[name] = Composite.new(name)
8
+ def composite(name, desc=nil)
9
+ c = Cauterize.composites[name] || Cauterize.composites[name] = Composite.new(name, desc)
10
10
  yield c if block_given?
11
11
  return c
12
12
  end
@@ -24,26 +24,27 @@ module Cauterize
24
24
  end
25
25
 
26
26
  class CompositeField
27
- attr_reader :name, :type
28
- def initialize(field_name, type_name)
27
+ attr_reader :name, :type, :description
28
+ def initialize(field_name, type_name, desc=nil)
29
29
  @name = field_name
30
30
  @type = BaseType.find_type!(type_name)
31
+ @description = desc
31
32
  end
32
33
  end
33
34
 
34
35
  class Composite < BaseType
35
36
  attr_reader :fields
36
37
 
37
- def initialize(name)
38
+ def initialize(name, desc=nil)
38
39
  super
39
40
  @fields = {}
40
41
  end
41
42
 
42
- def field(name, type)
43
+ def field(name, type, desc=nil)
43
44
  if @fields[name]
44
45
  raise Exception.new("Field name #{name} already used.")
45
46
  else
46
- @fields[name] = CompositeField.new(name, type)
47
+ @fields[name] = CompositeField.new(name, type, desc)
47
48
  end
48
49
  end
49
50
  end
@@ -18,19 +18,22 @@ module Cauterize
18
18
  def build_cs
19
19
  f = four_space_formatter
20
20
 
21
+ f << "/* WARNING: This is generated code. Do not edit this file directly. */"
22
+ f << ""
21
23
  f << "using System;"
22
24
  f << "using Cauterize;"
23
25
  f.blank_line
24
26
  f << "namespace #{@name}"
25
27
  f.braces do
26
- f << "public class #{@name}CauterizeInfo : CauterizeInfo"
28
+ f << "public static class #{@name}Cauterize"
27
29
  f.braces do
28
- f << "static #{@name}CauterizeInfo()"
30
+ f << "public static CauterizeInfo Info = new CauterizeInfo"
29
31
  f.braces do
30
- f << "Name = \"#{@name}\";"
31
- f << "GeneratedVersion = \"#{Cauterize.get_version}\";"
32
- f << "GeneratedDate = \"#{Cauterize.get_version}\";"
32
+ f << "Name = \"#{@name}\","
33
+ f << "GeneratedVersion = \"#{Cauterize.get_version}\","
34
+ f << "GeneratedDate = \"#{DateTime.now.to_s}\""
33
35
  end
36
+ f << ";"
34
37
  end
35
38
  f.blank_line
36
39
 
@@ -0,0 +1,34 @@
1
+ module Cauterize
2
+ class DocBuilder
3
+ attr_reader :doc_path, :name
4
+ def initialize(doc_path, name="cauterize")
5
+ @doc_path = doc_path
6
+ @name = name
7
+ end
8
+
9
+ def build
10
+ build_doc
11
+ end
12
+
13
+ private
14
+
15
+ def build_doc
16
+ File.open(@doc_path, "wb") do |fh|
17
+ doc_sections = []
18
+
19
+ instances = BaseType.all_instances
20
+ builders = instances.map {|i| Builders.get(:doc, i)}
21
+
22
+ builders.each do |b|
23
+ body_lines = (b.body || []).map {|l| " " + l}
24
+ lines = ([b.heading] + body_lines)
25
+
26
+ doc_sections << lines.join("\n") + "\n"
27
+ end
28
+
29
+ fh.write(doc_sections.join("\n"))
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -6,17 +6,17 @@ require 'set'
6
6
 
7
7
  module Cauterize
8
8
  module_function
9
- def enumeration(name)
10
- e = Cauterize.enumerations[name] || Cauterize.enumerations[name] = Enumeration.new(name)
9
+ def enumeration(name, desc=nil)
10
+ e = Cauterize.enumerations[name] || Cauterize.enumerations[name] = Enumeration.new(name, desc)
11
11
  yield e if block_given?
12
12
  return e
13
13
  end
14
14
 
15
- def enumeration!(name, &blk)
15
+ def enumeration!(name, desc=nil, &blk)
16
16
  if Cauterize.enumerations[name]
17
17
  raise Exception.new("Enumeration with name #{name} already exists.")
18
18
  else
19
- Cauterize.enumeration(name, &blk)
19
+ Cauterize.enumeration(name, desc, &blk)
20
20
  end
21
21
  end
22
22
 
@@ -36,7 +36,7 @@ module Cauterize
36
36
  class Enumeration < BaseType
37
37
  attr_reader :values
38
38
 
39
- def initialize(name)
39
+ def initialize(name, desc=nil)
40
40
  super
41
41
  @values = {}
42
42
  @value_id = 0
@@ -1,8 +1,8 @@
1
1
  module Cauterize
2
2
  module_function
3
3
 
4
- def fixed_array(name)
5
- a = Cauterize.fixed_arrays[name] || Cauterize.fixed_arrays[name] = FixedArray.new(name)
4
+ def fixed_array(name, desc=nil)
5
+ a = Cauterize.fixed_arrays[name] || Cauterize.fixed_arrays[name] = FixedArray.new(name, desc)
6
6
  yield a if block_given?
7
7
  return a
8
8
  end
@@ -20,7 +20,7 @@ module Cauterize
20
20
  end
21
21
 
22
22
  class FixedArray < BaseType
23
- def initialize(name)
23
+ def initialize(name, desc=nil)
24
24
  super
25
25
  end
26
26
 
@@ -1,8 +1,8 @@
1
1
  module Cauterize
2
2
  module_function
3
3
 
4
- def group(name)
5
- a = Cauterize.groups[name] || Cauterize.groups[name] = Group.new(name)
4
+ def group(name, desc=nil)
5
+ a = Cauterize.groups[name] || Cauterize.groups[name] = Group.new(name, desc)
6
6
  yield a if block_given?
7
7
  return a
8
8
  end
@@ -20,32 +20,42 @@ module Cauterize
20
20
  end
21
21
 
22
22
  class GroupField
23
- attr_reader :name, :type
23
+ attr_reader :name, :type, :description
24
24
 
25
- def initialize(name, type)
25
+ def initialize(name, type, desc=nil)
26
26
  @name = name
27
27
  @type = BaseType.find_type!(type) if type
28
+ @description = desc
28
29
  end
29
30
  end
30
31
 
31
32
  class Group < BaseType
32
33
  attr_reader :fields, :tag_enum
33
34
 
34
- def initialize(name)
35
+ def initialize(name, desc=nil)
36
+ # This technically should be defined before the group itself is defined
37
+ # because the group depends on the enumeration. Technically, this snippet
38
+ # of code doesn't care, but others may depend on the ordering.
39
+ @tag_enum = Cauterize.enumeration!("group_#{name}_type".to_sym)
40
+
41
+ # Make sure that this is called *AFTER* the enumeration is created.
35
42
  super
36
43
  @fields = {}
37
- @tag_enum = Cauterize.enumeration!("group_#{name}_type".to_sym)
38
44
  end
39
45
 
40
- def field(name, type=nil)
46
+ def field(name, type, desc=nil)
41
47
  if @fields[name]
42
48
  raise Exception.new("Field name #{name} already used.")
43
49
  else
44
- @fields[name] = GroupField.new(name, type)
50
+ @fields[name] = GroupField.new(name, type, desc)
45
51
  @tag_enum.value(enum_sym(name))
46
52
  end
47
53
  end
48
54
 
55
+ def dataless(name, desc=nil)
56
+ field(name, nil, desc)
57
+ end
58
+
49
59
  def enum_sym(fname)
50
60
  "group_#{@name}_type_#{fname}".up_snake.to_sym
51
61
  end