antlr3 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ group Sample
2
+
3
+ class_definition ::=
4
+ <<<
5
+ class <%= @name %><% if @superclass %> < <%= @superclass %><% end %>
6
+ % if @attributes
7
+
8
+ % for attr in @attributes
9
+ <%= attribute( *attr ).to_s.chomp %>
10
+ % end
11
+ % end
12
+ % if @methods
13
+ % for method in ( @methods || [] )
14
+ <%= method( method ) %>
15
+ % end
16
+ % end
17
+ end
18
+ >>>
19
+
20
+ attribute( name, access = 'rw' ) ::=
21
+ <<<
22
+ % case @access.to_s.downcase
23
+ % when 'r'
24
+ attr_reader :<%= @name %>
25
+ % when 'w'
26
+ attr_writer :<%= @name %>
27
+ % else
28
+ attr_accessor :<%= @name %>
29
+ % end
30
+ >>>
31
+
32
+ method ::=
33
+ <<<
34
+
35
+ def <%= @name %><% if @arguments and not @arguments.empty? %>( <%= @arguments.join( ', ' ) %> )<% end %>
36
+ <%= @body.gsub( /^/, ' ' ) %>
37
+ end
38
+ >>>
@@ -0,0 +1,248 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+
4
+ require 'spec'
5
+ require 'antlr3/template'
6
+ require 'antlr3/util'
7
+
8
+ include ANTLR3
9
+ MethodDescription = Struct.new( :name, :body, :arguments )
10
+ TEMPLATE_NAMES = %w( method class_definition attribute )
11
+ SAMPLE_GROUP_FILE = File.join(
12
+ File.dirname( __FILE__ ), 'sample-input', 'template-group'
13
+ )
14
+
15
+ describe Template do
16
+
17
+ describe Template::Context do
18
+ example "creating an empty context" do
19
+ context = Template::Context.new
20
+ context.instance_variables.should be_empty
21
+ end
22
+
23
+ example "creating a context with a variable map" do
24
+ context = Template::Context.new(
25
+ :a => 1, :b => 2
26
+ )
27
+
28
+ vars = context.instance_variables.map { | i | i.to_s }
29
+ vars.should include( '@a' )
30
+ vars.should include( '@b' )
31
+
32
+ context.instance_variable_get( '@a' ).should == 1
33
+ context.instance_variable_get( '@b' ).should == 2
34
+ end
35
+
36
+ example "fetching variable values from []" do
37
+ context = Template::Context.new(
38
+ :a => 1, :b => 2
39
+ )
40
+
41
+ context[ :a ].should == 1
42
+ context[ 'a' ].should == 1
43
+ context[ :b ].should == 2
44
+ context[ 'b' ].should == 2
45
+ end
46
+
47
+ example "defining variables with []=" do
48
+ context = Template::Context.new( :a => 3 )
49
+ context[ :a ] = 1
50
+ context[ 'b' ] = 2
51
+
52
+ context.instance_variable_get( '@a' ).should == 1
53
+ context.instance_variable_get( '@b' ).should == 2
54
+ end
55
+
56
+ example "using method missing to assign values" do
57
+ context = Template::Context.new( :a => 3 )
58
+ context.a = 1
59
+ context.b = 2
60
+
61
+ context.instance_variable_get( '@a' ).should == 1
62
+ context.instance_variable_get( '@b' ).should == 2
63
+ end
64
+
65
+ example "using method missing to get variable values" do
66
+ context = Template::Context.new( :a => 1, :b => 2)
67
+
68
+ context.a.should == 1
69
+ context.b.should == 2
70
+ end
71
+
72
+ end
73
+
74
+
75
+ shared_examples_for "template groups" do
76
+ include ANTLR3::Util
77
+
78
+ example "template definitions" do
79
+ templates = @group.templates
80
+ templates.should_not be_empty
81
+ templates.should equal @group::TEMPLATES
82
+
83
+ names = templates.keys
84
+
85
+ names.should have(3).things
86
+ for template_name in TEMPLATE_NAMES
87
+ names.should include template_name
88
+ template_class = templates[ template_name ]
89
+ template_class.should be_a_kind_of Class
90
+ template_class.superclass.should equal Template::Context
91
+ template_class.should be < @group # it should include the group module
92
+ end
93
+ end
94
+
95
+ example "template_defined?( name ) should verify whether a template is defined in a group" do
96
+ for name in TEMPLATE_NAMES
97
+ @group.template_defined?( name ).should be_true
98
+ @group.template_defined?( name.to_s ).should be_true
99
+ end
100
+
101
+ @group.template_defined?( :somethign_else ).should be_false
102
+ end
103
+
104
+ example "template method definitions" do
105
+ for name in TEMPLATE_NAMES
106
+ @group.should respond_to( name )
107
+ @group.should respond_to( "#{ name }!" )
108
+ @group.private_instance_methods.should include name.to_s
109
+ @group.private_instance_methods.should include "#{ name }!"
110
+ end
111
+ end
112
+
113
+ example "template method operation" do
114
+ value = @group.class_definition
115
+ value.should be_a_kind_of Template::Context
116
+
117
+ value = @group.class_definition!
118
+ value.should be_a_kind_of String
119
+
120
+ value = @group.attribute( :name => 'a' )
121
+ value.should be_a_kind_of Template::Context
122
+ end
123
+ end
124
+
125
+ describe Template::Group, "dynamic template definition" do
126
+ include ANTLR3::Util
127
+
128
+ before :each do
129
+ @group = Template::Group.new do
130
+ extend ANTLR3::Util
131
+ define_template( :class_definition, tidy( <<-'END'.chomp ) )
132
+ | class <%= @name %><% if @superclass %> < <%= @superclass %><% end %>
133
+ | % if @attributes
134
+ |
135
+ | % for attr, access in @attributes
136
+ | <%= attribute( :name => attr, :access => ( access || 'rw' ) ).to_s.chomp %>
137
+ | % end
138
+ | % end
139
+ | % if @methods
140
+ | % for method in ( @methods || [] )
141
+ | <%= method( method ) %>
142
+ | % end
143
+ | % end
144
+ | end
145
+ END
146
+
147
+ define_template( :attribute, tidy( <<-'END'.chomp ) )
148
+ | % case @access.to_s.downcase
149
+ | % when 'r'
150
+ | attr_reader :<%= @name %>
151
+ | % when 'w'
152
+ | attr_writer :<%= @name %>
153
+ | % else
154
+ | attr_accessor :<%= @name %>
155
+ | % end
156
+ END
157
+
158
+ define_template( :method, tidy( <<-'END'.chomp ) )
159
+ |
160
+ | def <%= @name %><% if @arguments and not @arguments.empty? %>( <%= @arguments.join( ', ' ) %> )<% end %>
161
+ | <%= @body.gsub( /^/, ' ' ) %>
162
+ | end
163
+ END
164
+ end
165
+ end
166
+
167
+ it_should_behave_like "template groups"
168
+
169
+ example "template object string rendering" do
170
+ attributes = [
171
+ %w( family ),
172
+ %w( name r )
173
+ ]
174
+
175
+ methods = [
176
+ MethodDescription.new( 'eat', %q[puts( "ate %s %s" % [ number, @name ] )], %w( number ) ),
177
+ MethodDescription.new( :to_s, '@name.to_s.dup' )
178
+ ]
179
+
180
+ vegetable = @group.class_definition(
181
+ :name => 'Vegetable',
182
+ :superclass => 'Food',
183
+ :attributes => attributes,
184
+ :methods => methods
185
+ )
186
+
187
+ vegetable.to_s.should == tidy( <<-END.chomp )
188
+ | class Vegetable < Food
189
+ |
190
+ | attr_accessor :family
191
+ | attr_reader :name
192
+ |
193
+ | def eat( number )
194
+ | puts( "ate %s %s" % [ number, @name ] )
195
+ | end
196
+ |
197
+ | def to_s
198
+ | @name.to_s.dup
199
+ | end
200
+ | end
201
+ END
202
+ end
203
+ end
204
+
205
+ describe Template::Group, "loading a template definition file" do
206
+ before :each do
207
+ @group = Template::Group.load( SAMPLE_GROUP_FILE )
208
+ end
209
+
210
+ it_should_behave_like "template groups"
211
+
212
+ example "template object string rendering" do
213
+ attributes = [
214
+ %w( family ),
215
+ %w( name r )
216
+ ]
217
+
218
+ methods = [
219
+ MethodDescription.new( 'eat', %q[puts( "ate %s %s" % [ number, @name ] )], %w( number ) ),
220
+ MethodDescription.new( :to_s, '@name.to_s.dup' )
221
+ ]
222
+
223
+ vegetable = @group.class_definition(
224
+ :name => 'Vegetable',
225
+ :superclass => 'Food',
226
+ :attributes => attributes,
227
+ :methods => methods
228
+ )
229
+
230
+ vegetable.to_s.should == tidy( <<-END.chomp )
231
+ | class Vegetable < Food
232
+ |
233
+ | attr_accessor :family
234
+ | attr_reader :name
235
+ |
236
+ | def eat( number )
237
+ | puts( "ate %s %s" % [ number, @name ] )
238
+ | end
239
+ |
240
+ | def to_s
241
+ | @name.to_s.dup
242
+ | end
243
+ | end
244
+ END
245
+ end
246
+
247
+ end
248
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: antlr3
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Yetter
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-07 00:00:00 -05:00
12
+ date: 2010-01-17 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -76,8 +76,12 @@ files:
76
76
  - lib/antlr3/test/call-stack.rb
77
77
  - lib/antlr3/test/grammar.rb
78
78
  - lib/antlr3/test/core-extensions.rb
79
+ - lib/antlr3/template.rb
79
80
  - lib/antlr3/dfa.rb
80
81
  - lib/antlr3/profile.rb
82
+ - lib/antlr3/template/parameter.rb
83
+ - lib/antlr3/template/group-lexer.rb
84
+ - lib/antlr3/template/group-parser.rb
81
85
  - lib/antlr3/tree/visitor.rb
82
86
  - lib/antlr3/tree/debug.rb
83
87
  - lib/antlr3/tree/wizard.rb
@@ -88,6 +92,7 @@ files:
88
92
  - lib/antlr3.rb
89
93
  - test/unit/test-dfa.rb
90
94
  - test/unit/sample-input/file-stream-1
95
+ - test/unit/sample-input/template-group
91
96
  - test/unit/sample-input/teststreams.input2
92
97
  - test/unit/test-tree-wizard.rb
93
98
  - test/unit/test-streams.rb
@@ -95,6 +100,7 @@ files:
95
100
  - test/unit/test-trees.rb
96
101
  - test/unit/test-exceptions.rb
97
102
  - test/unit/test-scheme.rb
103
+ - test/unit/test-template.rb
98
104
  - test/functional/delegation/import.rb
99
105
  - test/functional/lexer/xml.rb
100
106
  - test/functional/lexer/properties.rb
@@ -122,11 +128,13 @@ files:
122
128
  - test/functional/parser/calc.rb
123
129
  - test/functional/parser/ll-star.rb
124
130
  - test/functional/parser/scopes.rb
131
+ - test/functional/template-output/template-output.rb
125
132
  - test/functional/token-rewrite/via-parser.rb
126
133
  - test/functional/token-rewrite/basic.rb
127
134
  - templates/ASTDbg.stg
128
135
  - templates/AST.stg
129
136
  - templates/Ruby.stg
137
+ - templates/ST.stg
130
138
  - templates/Dbg.stg
131
139
  - templates/ASTParser.stg
132
140
  - templates/ASTTreeParser.stg
@@ -173,6 +181,7 @@ test_files:
173
181
  - test/unit/test-trees.rb
174
182
  - test/unit/test-exceptions.rb
175
183
  - test/unit/test-scheme.rb
184
+ - test/unit/test-template.rb
176
185
  - test/functional/delegation/import.rb
177
186
  - test/functional/lexer/xml.rb
178
187
  - test/functional/lexer/properties.rb
@@ -200,5 +209,6 @@ test_files:
200
209
  - test/functional/parser/calc.rb
201
210
  - test/functional/parser/ll-star.rb
202
211
  - test/functional/parser/scopes.rb
212
+ - test/functional/template-output/template-output.rb
203
213
  - test/functional/token-rewrite/via-parser.rb
204
214
  - test/functional/token-rewrite/basic.rb