parser 0.9.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,228 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: ascii-8bit
3
+
4
+ require 'minitest/autorun'
5
+ require 'ruby_parser_extras'
6
+
7
+ require 'minitest/unit'
8
+
9
+ class TestStackState < MiniTest::Unit::TestCase
10
+ attr_reader :s
11
+
12
+ def setup
13
+ @s = RubyParserStuff::StackState.new :test
14
+ end
15
+
16
+ def assert_encoding str, default = false
17
+ orig_str = str.dup
18
+ p = Ruby19Parser.new
19
+ s = nil
20
+
21
+ out, err = capture_io do
22
+ s = p.handle_encoding str
23
+ end
24
+
25
+ assert_equal orig_str.sub(/\357\273\277/, ''), s
26
+
27
+ exp_err = ""
28
+
29
+ if defined?(Encoding) then
30
+ assert_equal "UTF-8", s.encoding.to_s, str.inspect
31
+ else
32
+ exp_err = "Skipping magic encoding comment\n" unless default
33
+ end
34
+
35
+ assert_equal "", out, str.inspect
36
+ assert_equal exp_err, err, str.inspect # HACK
37
+ end
38
+
39
+ def test_handle_encoding_bom
40
+ # bom support, default to utf-8
41
+ assert_encoding "\xEF\xBB\xBF# blah"
42
+ # we force_encode to US-ASCII, then encode to UTF-8 so our lexer will work
43
+ assert_encoding "\xEF\xBB\xBF# encoding: US-ASCII"
44
+ end
45
+
46
+ def test_handle_encoding_default
47
+ assert_encoding "blah", :default
48
+ end
49
+
50
+ def test_handle_encoding_emacs
51
+ # Q: how many different ways can we screw these up? A: ALL OF THEM
52
+
53
+ assert_encoding "# - encoding: utf-8 -"
54
+ assert_encoding "# - encoding:utf-8"
55
+ assert_encoding "# -* coding: UTF-8 -*-"
56
+ assert_encoding "# -*- coding: UTF-8 -*-"
57
+ assert_encoding "# -*- coding: utf-8 -*"
58
+ assert_encoding "# -*- coding: utf-8 -*-"
59
+ assert_encoding "# -*- coding: utf-8; mode: ruby -*-"
60
+ assert_encoding "# -*- coding: utf-8; mode: ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2"
61
+ assert_encoding "# -*- coding:utf-8; mode:ruby; -*-"
62
+ assert_encoding "# -*- encoding: UTF-8 -*-"
63
+ assert_encoding "# -*- encoding: utf-8 -*"
64
+ assert_encoding "# -*- encoding: utf-8 -*-"
65
+ assert_encoding "# -*- mode:ruby; coding:utf-8 -*-"
66
+ assert_encoding "# -*- ruby encoding: utf-8 -*-"
67
+ assert_encoding "# -- encoding: utf-8 --"
68
+ assert_encoding "# ~*~ encoding: utf-8 ~*~"
69
+ assert_encoding "#-*- coding: utf-8 -*-"
70
+ assert_encoding "#-*- coding:utf-8"
71
+ assert_encoding "#-- -*- mode: ruby; encoding: utf-8 -*-\n"
72
+ end
73
+
74
+ def test_handle_encoding_wtf
75
+ assert_encoding "# coding : utf-8"
76
+ assert_encoding "# Ruby 1.9: encoding: utf-8"
77
+ assert_encoding "# Encoding: UTF-8 <-- required, please leave this in."
78
+ assert_encoding "# Encoding: UTF-8"
79
+ assert_encoding "# coding: utf-8"
80
+ assert_encoding "# coding:utf-8"
81
+ assert_encoding "# coding=utf-8"
82
+ assert_encoding "# encoding: ASCII"
83
+ assert_encoding "# encoding: ASCII-8BIT"
84
+ assert_encoding "# encoding: ISO-8859-1"
85
+ assert_encoding "# encoding: UTF-8"
86
+ assert_encoding "# encoding: ascii-8bit"
87
+ assert_encoding "# encoding: cp1252"
88
+ assert_encoding "# encoding: euc-jp -*-"
89
+ assert_encoding "# encoding: utf-8 # -*- ruby -*-"
90
+ assert_encoding "# encoding: utf-8 require 'github_api/utils/url'"
91
+ assert_encoding "# encoding: utf-8!"
92
+ assert_encoding "# encoding: utf-8"
93
+ assert_encoding "#<Encoding:UTF-8>"
94
+ assert_encoding "#Encoding: UTF-8"
95
+ assert_encoding "#coding:utf-8"
96
+ assert_encoding "#encoding: UTF-8!"
97
+ assert_encoding "#encoding: UTF-8"
98
+ assert_encoding "#encoding: cp1252"
99
+ assert_encoding "#encoding: sjis"
100
+ assert_encoding "#encoding: utf-8"
101
+ end
102
+
103
+ def test_handle_encoding_normal
104
+ assert_encoding "# encoding: UTF-8"
105
+ assert_encoding "# encoding: UTF-8\r\n" # UGH I hate windoze
106
+ assert_encoding "# coding: UTF-8"
107
+ assert_encoding "# encoding = UTF-8"
108
+ assert_encoding "# coding = UTF-8"
109
+ end
110
+
111
+ def test_handle_encoding_vim
112
+ assert_encoding "# vim: set fileencoding=utf-8 filetype=ruby ts=2 : "
113
+ assert_encoding "# vim: fileencoding=UTF-8 ft=ruby syn=ruby ts=2 sw=2 ai eol et si"
114
+ assert_encoding "# vim: fileencoding=UTF-8 nobomb sw=2 ts=2 et"
115
+ assert_encoding "# vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2"
116
+ assert_encoding "# vim: set fileencoding=utf-8"
117
+ assert_encoding "# vim:encoding=UTF-8:"
118
+ assert_encoding "# vim:fileencoding=UTF-8:"
119
+ assert_encoding "# vim:set fileencoding=utf-8 filetype=ruby"
120
+ assert_encoding "# vim:set fileencoding=utf-8:"
121
+ end
122
+
123
+ def test_stack_state
124
+ s.push true
125
+ s.push false
126
+ s.lexpop
127
+ assert_equal [false, true], s.stack
128
+ end
129
+
130
+ def test_is_in_state
131
+ assert_equal false, s.is_in_state
132
+ s.push false
133
+ assert_equal false, s.is_in_state
134
+ s.push true
135
+ assert_equal true, s.is_in_state
136
+ s.push false
137
+ assert_equal false, s.is_in_state
138
+ end
139
+
140
+ def test_lexpop
141
+ assert_equal [false], s.stack
142
+ s.push true
143
+ s.push false
144
+ assert_equal [false, true, false], s.stack
145
+ s.lexpop
146
+ assert_equal [false, true], s.stack
147
+ end
148
+
149
+ def test_pop
150
+ assert_equal [false], s.stack
151
+ s.push true
152
+ assert_equal [false, true], s.stack
153
+ assert_equal true, s.pop
154
+ assert_equal [false], s.stack
155
+ end
156
+
157
+ def test_push
158
+ assert_equal [false], s.stack
159
+ s.push true
160
+ s.push false
161
+ assert_equal [false, true, false], s.stack
162
+ end
163
+ end
164
+
165
+ class TestEnvironment < MiniTest::Unit::TestCase
166
+ def deny t
167
+ assert ! t
168
+ end
169
+
170
+ def setup
171
+ @env = RubyParserStuff::Environment.new
172
+ @env[:blah] = 42
173
+ assert_equal 42, @env[:blah]
174
+ end
175
+
176
+ def test_var_scope_dynamic
177
+ @env.extend :dynamic
178
+ assert_equal 42, @env[:blah]
179
+ @env.unextend
180
+ assert_equal 42, @env[:blah]
181
+ end
182
+
183
+ def test_var_scope_static
184
+ @env.extend
185
+ assert_equal nil, @env[:blah]
186
+ @env.unextend
187
+ assert_equal 42, @env[:blah]
188
+ end
189
+
190
+ def test_all_dynamic
191
+ expected = { :blah => 42 }
192
+
193
+ @env.extend :dynamic
194
+ assert_equal expected, @env.all
195
+ @env.unextend
196
+ assert_equal expected, @env.all
197
+ end
198
+
199
+ def test_all_static
200
+ @env.extend
201
+ expected = { }
202
+ assert_equal expected, @env.all
203
+
204
+ @env.unextend
205
+ expected = { :blah => 42 }
206
+ assert_equal expected, @env.all
207
+ end
208
+
209
+ def test_all_static_deeper
210
+ expected0 = { :blah => 42 }
211
+ expected1 = { :blah => 42, :blah2 => 24 }
212
+ expected2 = { :blah => 27 }
213
+
214
+ @env.extend :dynamic
215
+ @env[:blah2] = 24
216
+ assert_equal expected1, @env.all
217
+
218
+ @env.extend
219
+ @env[:blah] = 27
220
+ assert_equal expected2, @env.all
221
+
222
+ @env.unextend
223
+ assert_equal expected1, @env.all
224
+
225
+ @env.unextend
226
+ assert_equal expected0, @env.all
227
+ end
228
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Peter Zotov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sexp_processor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '4.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: racc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.6
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.6
69
+ - !ruby/object:Gem::Dependency
70
+ name: hoe
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.5'
83
+ description: |-
84
+ ruby_parser (RP) is a ruby parser written in pure ruby (utilizing
85
+ racc--which does by default use a C extension). RP's output is
86
+ the same as ParseTree's output: s-expressions using ruby's arrays and
87
+ base types.
88
+
89
+ As an example:
90
+
91
+ def conditional1 arg1
92
+ return 1 if arg1 == 0
93
+ return 0
94
+ end
95
+
96
+ becomes:
97
+
98
+ s(:defn, :conditional1, s(:args, :arg1),
99
+ s(:if,
100
+ s(:call, s(:lvar, :arg1), :==, s(:lit, 0)),
101
+ s(:return, s(:lit, 1)),
102
+ nil),
103
+ s(:return, s(:lit, 0)))
104
+ email:
105
+ - whitequark@whitequark.org
106
+ executables:
107
+ - ruby_parse
108
+ - ruby_parse_extract_error
109
+ extensions: []
110
+ extra_rdoc_files:
111
+ - History.txt
112
+ - Manifest.txt
113
+ - README.txt
114
+ files:
115
+ - .autotest
116
+ - History.txt
117
+ - Manifest.txt
118
+ - README.txt
119
+ - Rakefile
120
+ - bin/ruby_parse
121
+ - bin/ruby_parse_extract_error
122
+ - lib/gauntlet_rubyparser.rb
123
+ - lib/ruby18_parser.rb
124
+ - lib/ruby18_parser.y
125
+ - lib/ruby19_parser.rb
126
+ - lib/ruby19_parser.y
127
+ - lib/ruby_lexer.rb
128
+ - lib/ruby_parser.rb
129
+ - lib/ruby_parser_extras.rb
130
+ - test/test_ruby_lexer.rb
131
+ - test/test_ruby_parser.rb
132
+ - test/test_ruby_parser_extras.rb
133
+ - .gemtest
134
+ homepage: https://github.com/seattlerb/ruby_parser
135
+ licenses: []
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options:
139
+ - --main
140
+ - README.txt
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>'
151
+ - !ruby/object:Gem::Version
152
+ version: 1.3.1
153
+ requirements: []
154
+ rubyforge_project: parser
155
+ rubygems_version: 2.0.0
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: ruby_parser (RP) is a ruby parser written in pure ruby (utilizing racc--which
159
+ does by default use a C extension)
160
+ test_files:
161
+ - test/test_ruby_lexer.rb
162
+ - test/test_ruby_parser.rb
163
+ - test/test_ruby_parser_extras.rb