csquare-cast 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,192 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cast'
4
+ require 'test/unit'
5
+ require 'stringio'
6
+ require 'fileutils'
7
+
8
+ # a dir to cd into for creating files and such
9
+ TEST_DIR = "#{File.dirname(__FILE__)}/var"
10
+
11
+ # --------------------------------------------------------------------
12
+ # Helpers for testing
13
+ # --------------------------------------------------------------------
14
+
15
+ class Array
16
+ def same_list?(other)
17
+ self.length == other.length or
18
+ return false
19
+ self.zip(other).all? do |mine, yours|
20
+ mine.equal? yours or
21
+ return false
22
+ end
23
+ end
24
+ end
25
+
26
+ class Integer
27
+ #
28
+ # Return a `self'-element array containing the result of the given
29
+ # block.
30
+ #
31
+ def of(&blk)
32
+ Array.new(self, &blk)
33
+ end
34
+ end
35
+
36
+ module Test::Unit::Assertions
37
+ INDENT = ' '
38
+ #
39
+ # Assert that the given string is parsed as expected. The given
40
+ # string is of the format:
41
+ #
42
+ # <program>
43
+ # ----
44
+ # <expected inspect string>
45
+ #
46
+ # The <program> part is yielded to obtain the AST.
47
+ #
48
+ def check_ast(test_data)
49
+ inp, exp = test_data.split(/^----+\n/)
50
+ ast = yield(inp)
51
+ assert_tree(ast)
52
+ assert(ast.is_a?(C::Node))
53
+ assert_equal_inspect_strs(exp, ast.inspect)
54
+ end
55
+ #
56
+ # Assert that the given Node#inspect strings are equal.
57
+ #
58
+ def assert_equal_inspect_strs(exp, out)
59
+ # remove EOL space
60
+ out = out.gsub(/ *$/, '')
61
+ exp = exp.gsub(/ *$/, '')
62
+
63
+ # normalize BOL space
64
+ exp.gsub!(%r'^#{INDENT}*') do |s|
65
+ levels = s.length / INDENT.length
66
+ C::Node::INSPECT_TAB*levels
67
+ end
68
+
69
+ # compare
70
+ msg = "Debug strings unequal:\n#{juxtapose('Expected', exp, 'Output', out)}"
71
+ assert_block(msg){out == exp}
72
+ end
73
+ #
74
+ # Return a string of `s1' and `s2' side by side with a dividing line
75
+ # in between indicating differences. `h1' and `h2' are the column
76
+ # headings.
77
+ #
78
+ def juxtapose(h1, s1, h2, s2)
79
+ s1 = s1.each_line.map{|line| line.chomp}
80
+ s2 = s2.each_line.map{|line| line.chomp}
81
+ rows = [s1.length, s2.length].max
82
+ wl = s1.map{|line| line.length}.max
83
+ wr = s2.map{|line| line.length}.max
84
+ ret = ''
85
+ ret << "#{('-'*wl)}----#{'-'*wr}\n"
86
+ ret << "#{h1.ljust(wl)} || #{h2}\n"
87
+ ret << "#{('-'*wl)}-++-#{'-'*wr}\n"
88
+ (0...rows).each do |i|
89
+ if i >= s1.length
90
+ ret << "#{' '*wl} > #{s2[i]}\n"
91
+ elsif i >= s2.length
92
+ ret << "#{s1[i].ljust(wl)} <\n"
93
+ elsif s1[i] == s2[i]
94
+ ret << "#{s1[i].ljust(wl)} || #{s2[i]}\n"
95
+ else
96
+ ret << "#{s1[i].ljust(wl)} <> #{s2[i]}\n"
97
+ end
98
+ end
99
+ ret << "#{('-'*wl)}----#{'-'*wr}\n"
100
+ return ret
101
+ end
102
+ #
103
+ # Assert that an exception of the given class is raised, and that
104
+ # the message matches the given regex.
105
+ #
106
+ def assert_error(klass, re)
107
+ ex = nil
108
+ assert_raise(klass) do
109
+ begin
110
+ yield
111
+ rescue Exception => ex
112
+ raise
113
+ end
114
+ end
115
+ assert_match(re, ex.message)
116
+ end
117
+ #
118
+ # Assert that the given ast's nodes' parents are correct, and there
119
+ # aren't non-Nodes where there shouldn't be.
120
+ #
121
+ def assert_tree(ast)
122
+ meth = 'unknown method'
123
+ caller.each do |line|
124
+ if line =~ /in `(test_.*?)'/ #`
125
+ meth = $1
126
+ break
127
+ end
128
+ end
129
+ filename = "#{self.class}_#{meth}.out"
130
+ begin
131
+ assert_tree1(ast, nil)
132
+ assert(true)
133
+ rescue BadTreeError => e
134
+ require 'pp'
135
+ open("#{filename}", 'w'){|f| PP.pp(ast, f)}
136
+ flunk("#{e.message}. Output dumped to `#{filename}'.")
137
+ end
138
+ end
139
+ #
140
+ def assert_tree1(x, parent)
141
+ if x.is_a? C::Node
142
+ parent.equal? x.parent or
143
+ raise BadTreeError, "#{x.class}:0x#{(x.id << 1).to_s(16)} has #{x.parent ? 'wrong' : 'no'} parent"
144
+ x.fields.each do |field|
145
+ next if !field.child?
146
+ val = x.send(field.reader)
147
+ next if val.nil?
148
+ val.is_a? C::Node or
149
+ raise BadTreeError, "#{x.class}:0x#{(x.id << 1).to_s(16)} is a non-Node child"
150
+ assert_tree1(val, x)
151
+ end
152
+ end
153
+ end
154
+ class BadTreeError < StandardError; end
155
+ #
156
+ # Assert that `arg' is a C::NodeList.
157
+ #
158
+ def assert_list(arg)
159
+ assert_kind_of(C::NodeList, arg)
160
+ end
161
+ #
162
+ # Assert that `arg' is an empty C::NodeList.
163
+ #
164
+ def assert_empty_list(arg)
165
+ assert_list arg
166
+ assert(arg.empty?)
167
+ end
168
+ #
169
+ # Assert that the elements of exp are the same as those of out, and
170
+ # are in the same order.
171
+ #
172
+ def assert_same_list(exp, out)
173
+ assert_equal(exp.length, out.length, "Checking length")
174
+ (0...exp.length).each do |i|
175
+ assert_same(exp[i], out[i], "At index #{i} (of 0...#{exp.length})")
176
+ end
177
+ end
178
+ #
179
+ # Assert that out is ==, but not the same as exp (i.e., it is a
180
+ # copy).
181
+ #
182
+ def assert_copy(exp, out)
183
+ assert_not_same exp, out
184
+ assert_equal exp, out
185
+ end
186
+ #
187
+ # Assert the invariants of `node'.
188
+ #
189
+ def assert_invariants(node)
190
+ node.assert_invariants(self)
191
+ end
192
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csquare-cast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - George Ogata
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &88127500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *88127500
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake-compiler
27
+ requirement: &88127290 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.7'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *88127290
36
+ - !ruby/object:Gem::Dependency
37
+ name: hoe
38
+ requirement: &88127070 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *88127070
47
+ description: ! 'CAST parses C code into an abstract syntax tree (AST), lets you break
48
+ it, then vomit it out as code. The parser does C99.
49
+
50
+
51
+ This fork supports Ruby 1.9.3, gemspec, and requires Hoe. The Rubyforge page above
52
+ is documentation for the original version, but most things should be the same.'
53
+ email:
54
+ - george.ogata@gmail.com
55
+ executables: []
56
+ extensions:
57
+ - ext/cast/extconf.rb
58
+ extra_rdoc_files:
59
+ - README.rdoc
60
+ - Manifest.txt
61
+ - History.txt
62
+ files:
63
+ - Rakefile
64
+ - README.rdoc
65
+ - Manifest.txt
66
+ - History.txt
67
+ - cast.gemspec
68
+ - lib/cast/c.tab.rb
69
+ - lib/cast/c.y
70
+ - lib/cast/c_nodes.rb
71
+ - lib/cast/inspect.rb
72
+ - lib/cast/node.rb
73
+ - lib/cast/node_list.rb
74
+ - lib/cast/parse.rb
75
+ - lib/cast/preprocessor.rb
76
+ - lib/cast/tempfile.rb
77
+ - lib/cast/to_s.rb
78
+ - lib/cast.rb
79
+ - ext/cast/cast.h
80
+ - ext/cast/cast.c
81
+ - ext/cast/extconf.rb
82
+ - ext/cast/parser.c
83
+ - ext/cast/yylex.c
84
+ - ext/cast/yylex.re
85
+ - test/test_helper.rb
86
+ - .gemtest
87
+ homepage: http://cast.rubyforge.org
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options:
91
+ - --main
92
+ - README.rdoc
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '1.9'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ segments:
108
+ - 0
109
+ hash: -610101443
110
+ requirements: []
111
+ rubyforge_project: cast
112
+ rubygems_version: 1.8.10
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: CAST parses C code into an abstract syntax tree (AST), lets you break it,
116
+ then vomit it out as code
117
+ test_files:
118
+ - test/test_helper.rb