glark 1.9.0 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. data/bin/glark +2 -2
  2. data/lib/glark.rb +1 -1
  3. data/lib/glark/app/app.rb +48 -0
  4. data/lib/glark/app/help.rb +103 -0
  5. data/lib/glark/app/info/options.rb +84 -0
  6. data/lib/glark/app/options.rb +201 -0
  7. data/lib/glark/app/rcfile.rb +49 -0
  8. data/lib/glark/app/runner.rb +140 -0
  9. data/lib/glark/app/spec.rb +19 -0
  10. data/lib/glark/input/filter/criteria_opts.rb +41 -0
  11. data/lib/glark/input/filter/dir_criteria_opts.rb +37 -0
  12. data/lib/glark/input/filter/file_criteria_opts.rb +33 -0
  13. data/lib/glark/input/filter/filter.rb +59 -0
  14. data/lib/glark/input/filter/options.rb +171 -0
  15. data/lib/glark/input/options.rb +120 -0
  16. data/lib/glark/input/range.rb +104 -0
  17. data/lib/glark/input/spec.rb +39 -0
  18. data/lib/glark/io/file/archive_file.rb +64 -0
  19. data/lib/glark/io/file/binary_file.rb +19 -0
  20. data/lib/glark/io/file/file.rb +57 -0
  21. data/lib/glark/io/file/gz_file.rb +21 -0
  22. data/lib/glark/io/file/tar_file.rb +35 -0
  23. data/lib/glark/io/file/tar_gz_file.rb +52 -0
  24. data/lib/glark/io/file/zip_file.rb +39 -0
  25. data/lib/glark/match/and.rb +83 -0
  26. data/lib/glark/match/and_distance.rb +58 -0
  27. data/lib/glark/match/compound.rb +34 -0
  28. data/lib/glark/match/expression.rb +63 -0
  29. data/lib/glark/match/factory.rb +173 -0
  30. data/lib/glark/match/ior.rb +20 -0
  31. data/lib/glark/match/options.rb +74 -0
  32. data/lib/glark/match/or.rb +41 -0
  33. data/lib/glark/match/re.rb +81 -0
  34. data/lib/glark/match/re_factory.rb +44 -0
  35. data/lib/glark/match/spec.rb +59 -0
  36. data/lib/glark/match/xor.rb +20 -0
  37. data/lib/glark/output/binary_file_summary.rb +17 -0
  38. data/lib/glark/output/common.rb +43 -0
  39. data/lib/glark/output/context.rb +57 -0
  40. data/lib/glark/output/count.rb +26 -0
  41. data/lib/glark/output/file_header.rb +20 -0
  42. data/lib/glark/output/file_name_only.rb +35 -0
  43. data/lib/glark/output/formatted.rb +22 -0
  44. data/lib/glark/output/glark_count.rb +23 -0
  45. data/lib/glark/output/glark_format.rb +62 -0
  46. data/lib/glark/output/glark_lines.rb +36 -0
  47. data/lib/glark/output/grep_count.rb +18 -0
  48. data/lib/glark/output/grep_lines.rb +42 -0
  49. data/lib/glark/output/line_status.rb +46 -0
  50. data/lib/glark/output/lines.rb +100 -0
  51. data/lib/glark/output/match_list.rb +15 -0
  52. data/lib/glark/output/options.rb +103 -0
  53. data/lib/glark/output/results.rb +23 -0
  54. data/lib/glark/output/spec.rb +105 -0
  55. data/lib/glark/output/unfiltered_lines.rb +28 -0
  56. data/lib/glark/util/colors/options.rb +143 -0
  57. data/lib/glark/util/colors/spec.rb +21 -0
  58. data/lib/glark/util/highlight.rb +108 -0
  59. data/lib/glark/util/io/depth.rb +29 -0
  60. data/lib/glark/util/io/fileset.rb +162 -0
  61. data/lib/glark/util/io/filter/criteria.rb +49 -0
  62. data/lib/glark/util/io/filter/filter.rb +10 -0
  63. data/lib/glark/util/io/lines.rb +117 -0
  64. data/lib/glark/util/option.rb +34 -0
  65. data/lib/glark/util/options.rb +12 -0
  66. data/lib/glark/util/optutil.rb +69 -0
  67. data/lib/glark/util/timestamper.rb +18 -0
  68. data/man/glark.1 +1134 -0
  69. data/test/glark/app/and_test.rb +82 -0
  70. data/test/glark/app/compound_test.rb +33 -0
  71. data/test/glark/app/context_test.rb +43 -0
  72. data/test/glark/app/count_test.rb +89 -0
  73. data/test/glark/app/dump_test.rb +16 -0
  74. data/test/glark/app/expression_file_test.rb +22 -0
  75. data/test/glark/app/extended_regexp_test.rb +17 -0
  76. data/test/glark/app/extract_matches_test.rb +27 -0
  77. data/test/glark/app/files_with_match_test.rb +32 -0
  78. data/test/glark/app/files_without_match_test.rb +26 -0
  79. data/test/glark/app/filter_test.rb +118 -0
  80. data/test/glark/app/highlight_test.rb +78 -0
  81. data/test/glark/app/ignore_case_test.rb +22 -0
  82. data/test/glark/app/invert_test.rb +49 -0
  83. data/test/glark/app/ior_test.rb +21 -0
  84. data/test/glark/app/label_test.rb +28 -0
  85. data/test/glark/app/line_number_color_test.rb +42 -0
  86. data/test/glark/app/line_numbers_test.rb +42 -0
  87. data/test/glark/app/match_limit_test.rb +49 -0
  88. data/test/glark/app/options_test.rb +722 -0
  89. data/test/glark/app/range_test.rb +101 -0
  90. data/test/glark/app/rcfile_test.rb +113 -0
  91. data/test/glark/app/record_separator_test.rb +32 -0
  92. data/test/glark/app/regexp_test.rb +48 -0
  93. data/test/glark/app/tc.rb +92 -0
  94. data/test/glark/app/text_color_test.rb +31 -0
  95. data/test/glark/app/whole_lines_test.rb +17 -0
  96. data/test/glark/app/whole_words_test.rb +42 -0
  97. data/test/glark/app/xor_test.rb +19 -0
  98. data/test/glark/input/binary_file_test.rb +0 -0
  99. data/test/glark/input/directory_test.rb +202 -0
  100. data/test/glark/input/dirname_test.rb +69 -0
  101. data/test/glark/input/exclude_matching_test.rb +20 -0
  102. data/test/glark/input/ext_test.rb +65 -0
  103. data/test/glark/input/filter/criteria_test.rb +91 -0
  104. data/test/glark/input/filter/filter_spec_test.rb +27 -0
  105. data/test/glark/input/filter/filter_test.rb +21 -0
  106. data/test/glark/input/name_test.rb +75 -0
  107. data/test/glark/input/path_test.rb +72 -0
  108. data/test/glark/input/range_test.rb +82 -0
  109. data/test/glark/input/size_limit_test.rb +51 -0
  110. data/test/glark/input/split_as_path_test.rb +28 -0
  111. data/test/glark/match_test.rb +192 -0
  112. data/test/glark/resources.rb +21 -0
  113. data/test/glark/tc.rb +37 -0
  114. data/test/resources/add.rb +10 -0
  115. data/test/resources/echo.rb +2 -0
  116. data/test/resources/greet.rb +13 -0
  117. metadata +198 -28
  118. data/README +0 -0
  119. data/bin/jlark +0 -63
  120. data/lib/glark/expression.rb +0 -440
  121. data/lib/glark/exprfactory.rb +0 -248
  122. data/lib/glark/glark.rb +0 -297
  123. data/lib/glark/help.rb +0 -85
  124. data/lib/glark/input.rb +0 -183
  125. data/lib/glark/options.rb +0 -757
  126. data/lib/glark/output.rb +0 -266
  127. data/test/lib/glark/glark_test.rb +0 -317
  128. data/test/lib/glark/options_test.rb +0 -891
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubygems'
5
+ require 'riel'
6
+
7
+ module Glark
8
+ class Resource
9
+ include Loggable
10
+
11
+ RES_DIR = '/proj/org/incava/glark/test/resources/'
12
+
13
+ def to_path basename
14
+ RES_DIR + basename
15
+ end
16
+
17
+ def readlines basename
18
+ ::IO.readlines to_path
19
+ end
20
+ end
21
+ end
data/test/glark/tc.rb ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubygems'
5
+ require 'riel'
6
+ require 'test/unit'
7
+ require 'tempfile'
8
+
9
+ STDOUT.sync = true
10
+ STDERR.sync = true
11
+
12
+ require 'glark/app/runner'
13
+
14
+ Log.verbose = true
15
+ Log.set_widths(-15, 35, -35)
16
+ Log.level = Log::DEBUG
17
+
18
+ module Glark
19
+ class TestCase < Test::Unit::TestCase
20
+ include Loggable
21
+
22
+ # Returns a list of instance methods, in sorted order, so that they are run
23
+ # predictably by the unit test framework.
24
+
25
+ class << self
26
+ alias :unsorted_instance_methods :instance_methods
27
+
28
+ def instance_methods b
29
+ unsorted_instance_methods(true).sort
30
+ end
31
+ end
32
+
33
+ def test_truth
34
+ assert true
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ class Adder
5
+ def initialize x, y
6
+ puts x.to_s + " + " + y.to_s + " = " + (x.to_i + y.to_i).to_s
7
+ end
8
+ end
9
+
10
+ Adder.new ARGV.shift || 2, ARGV.shift || 2
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/ruby -w
2
+ puts ARGV.join(' ')
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubygems'
5
+ require 'riel'
6
+
7
+ class Greet
8
+ def initialize name
9
+ puts "hello, " + name
10
+ end
11
+ end
12
+
13
+ Greet.new ARGV.shift || "world"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glark
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
5
- prerelease: false
4
+ hash: 63
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
- - 9
8
+ - 10
9
9
  - 0
10
- version: 1.9.0
10
+ version: 1.10.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeff Pace
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-07 00:00:00 -04:00
18
+ date: 2012-12-30 00:00:00 -05:00
19
19
  default_executable: glark
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,36 +26,160 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 29
29
+ hash: 51
30
30
  segments:
31
- - 0
32
- - 0
33
31
  - 1
34
- version: 0.0.1
32
+ - 1
33
+ - 16
34
+ version: 1.1.16
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
- description:
38
- email: jpace@incava.org
37
+ - !ruby/object:Gem::Dependency
38
+ name: rainbow
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 27
46
+ segments:
47
+ - 1
48
+ - 1
49
+ - 4
50
+ version: 1.1.4
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: |
54
+ Glark searches files for regular expressions, extending grep by matching complex
55
+ expressions ("and", "or", and "xor"), extracting and searching within compressed
56
+ files, and excluding .svn and .git subdirectories by default. Different projects
57
+ can have their own Glark configuration.
58
+
59
+ email: jeugenepace@gmail.com
39
60
  executables:
40
61
  - glark
41
62
  extensions: []
42
63
 
43
- extra_rdoc_files:
44
- - README
64
+ extra_rdoc_files: []
65
+
45
66
  files:
46
- - bin/jlark
47
- - bin/glark
48
- - lib/glark/input.rb
49
- - lib/glark/expression.rb
50
- - lib/glark/options.rb
51
- - lib/glark/exprfactory.rb
52
- - lib/glark/glark.rb
53
- - lib/glark/output.rb
54
- - lib/glark/help.rb
67
+ - lib/glark/app/help.rb
68
+ - lib/glark/app/options.rb
69
+ - lib/glark/app/runner.rb
70
+ - lib/glark/app/info/options.rb
71
+ - lib/glark/app/app.rb
72
+ - lib/glark/app/rcfile.rb
73
+ - lib/glark/app/spec.rb
74
+ - lib/glark/util/options.rb
75
+ - lib/glark/util/option.rb
76
+ - lib/glark/util/timestamper.rb
77
+ - lib/glark/util/io/fileset.rb
78
+ - lib/glark/util/io/filter/filter.rb
79
+ - lib/glark/util/io/filter/criteria.rb
80
+ - lib/glark/util/io/depth.rb
81
+ - lib/glark/util/io/lines.rb
82
+ - lib/glark/util/optutil.rb
83
+ - lib/glark/util/highlight.rb
84
+ - lib/glark/util/colors/options.rb
85
+ - lib/glark/util/colors/spec.rb
86
+ - lib/glark/input/options.rb
87
+ - lib/glark/input/range.rb
88
+ - lib/glark/input/filter/options.rb
89
+ - lib/glark/input/filter/criteria_opts.rb
90
+ - lib/glark/input/filter/filter.rb
91
+ - lib/glark/input/filter/file_criteria_opts.rb
92
+ - lib/glark/input/filter/dir_criteria_opts.rb
93
+ - lib/glark/input/spec.rb
94
+ - lib/glark/io/file/binary_file.rb
95
+ - lib/glark/io/file/tar_gz_file.rb
96
+ - lib/glark/io/file/zip_file.rb
97
+ - lib/glark/io/file/tar_file.rb
98
+ - lib/glark/io/file/file.rb
99
+ - lib/glark/io/file/gz_file.rb
100
+ - lib/glark/io/file/archive_file.rb
101
+ - lib/glark/match/options.rb
102
+ - lib/glark/match/and_distance.rb
103
+ - lib/glark/match/re.rb
104
+ - lib/glark/match/expression.rb
105
+ - lib/glark/match/and.rb
106
+ - lib/glark/match/factory.rb
107
+ - lib/glark/match/compound.rb
108
+ - lib/glark/match/xor.rb
109
+ - lib/glark/match/or.rb
110
+ - lib/glark/match/ior.rb
111
+ - lib/glark/match/re_factory.rb
112
+ - lib/glark/match/spec.rb
113
+ - lib/glark/output/line_status.rb
114
+ - lib/glark/output/unfiltered_lines.rb
115
+ - lib/glark/output/options.rb
116
+ - lib/glark/output/grep_lines.rb
117
+ - lib/glark/output/grep_count.rb
118
+ - lib/glark/output/glark_lines.rb
119
+ - lib/glark/output/count.rb
120
+ - lib/glark/output/glark_count.rb
121
+ - lib/glark/output/glark_format.rb
122
+ - lib/glark/output/binary_file_summary.rb
123
+ - lib/glark/output/context.rb
124
+ - lib/glark/output/lines.rb
125
+ - lib/glark/output/results.rb
126
+ - lib/glark/output/common.rb
127
+ - lib/glark/output/match_list.rb
128
+ - lib/glark/output/file_name_only.rb
129
+ - lib/glark/output/formatted.rb
130
+ - lib/glark/output/file_header.rb
131
+ - lib/glark/output/spec.rb
55
132
  - lib/glark.rb
56
- - test/lib/glark/options_test.rb
57
- - test/lib/glark/glark_test.rb
58
- - README
133
+ - man/glark.1
134
+ - bin/glark
135
+ - test/glark/app/and_test.rb
136
+ - test/glark/app/invert_test.rb
137
+ - test/glark/app/context_test.rb
138
+ - test/glark/app/files_with_match_test.rb
139
+ - test/glark/app/xor_test.rb
140
+ - test/glark/app/ignore_case_test.rb
141
+ - test/glark/app/line_number_color_test.rb
142
+ - test/glark/app/ior_test.rb
143
+ - test/glark/app/whole_words_test.rb
144
+ - test/glark/app/expression_file_test.rb
145
+ - test/glark/app/regexp_test.rb
146
+ - test/glark/app/line_numbers_test.rb
147
+ - test/glark/app/range_test.rb
148
+ - test/glark/app/options_test.rb
149
+ - test/glark/app/match_limit_test.rb
150
+ - test/glark/app/text_color_test.rb
151
+ - test/glark/app/extract_matches_test.rb
152
+ - test/glark/app/highlight_test.rb
153
+ - test/glark/app/whole_lines_test.rb
154
+ - test/glark/app/record_separator_test.rb
155
+ - test/glark/app/filter_test.rb
156
+ - test/glark/app/label_test.rb
157
+ - test/glark/app/extended_regexp_test.rb
158
+ - test/glark/app/dump_test.rb
159
+ - test/glark/app/tc.rb
160
+ - test/glark/app/rcfile_test.rb
161
+ - test/glark/app/compound_test.rb
162
+ - test/glark/app/count_test.rb
163
+ - test/glark/app/files_without_match_test.rb
164
+ - test/glark/match_test.rb
165
+ - test/glark/input/exclude_matching_test.rb
166
+ - test/glark/input/split_as_path_test.rb
167
+ - test/glark/input/filter/filter_spec_test.rb
168
+ - test/glark/input/filter/filter_test.rb
169
+ - test/glark/input/filter/criteria_test.rb
170
+ - test/glark/input/range_test.rb
171
+ - test/glark/input/size_limit_test.rb
172
+ - test/glark/input/path_test.rb
173
+ - test/glark/input/ext_test.rb
174
+ - test/glark/input/directory_test.rb
175
+ - test/glark/input/binary_file_test.rb
176
+ - test/glark/input/name_test.rb
177
+ - test/glark/input/dirname_test.rb
178
+ - test/glark/resources.rb
179
+ - test/glark/tc.rb
180
+ - test/resources/greet.rb
181
+ - test/resources/add.rb
182
+ - test/resources/echo.rb
59
183
  has_rdoc: true
60
184
  homepage: http://www.incava.org/projects/glark
61
185
  licenses: []
@@ -86,10 +210,56 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
210
  requirements: []
87
211
 
88
212
  rubyforge_project:
89
- rubygems_version: 1.3.7
213
+ rubygems_version: 1.4.2
90
214
  signing_key:
91
215
  specification_version: 3
92
216
  summary: Extended searching of text files.
93
217
  test_files:
94
- - test/lib/glark/options_test.rb
95
- - test/lib/glark/glark_test.rb
218
+ - test/glark/app/and_test.rb
219
+ - test/glark/app/invert_test.rb
220
+ - test/glark/app/context_test.rb
221
+ - test/glark/app/files_with_match_test.rb
222
+ - test/glark/app/xor_test.rb
223
+ - test/glark/app/ignore_case_test.rb
224
+ - test/glark/app/line_number_color_test.rb
225
+ - test/glark/app/ior_test.rb
226
+ - test/glark/app/whole_words_test.rb
227
+ - test/glark/app/expression_file_test.rb
228
+ - test/glark/app/regexp_test.rb
229
+ - test/glark/app/line_numbers_test.rb
230
+ - test/glark/app/range_test.rb
231
+ - test/glark/app/options_test.rb
232
+ - test/glark/app/match_limit_test.rb
233
+ - test/glark/app/text_color_test.rb
234
+ - test/glark/app/extract_matches_test.rb
235
+ - test/glark/app/highlight_test.rb
236
+ - test/glark/app/whole_lines_test.rb
237
+ - test/glark/app/record_separator_test.rb
238
+ - test/glark/app/filter_test.rb
239
+ - test/glark/app/label_test.rb
240
+ - test/glark/app/extended_regexp_test.rb
241
+ - test/glark/app/dump_test.rb
242
+ - test/glark/app/tc.rb
243
+ - test/glark/app/rcfile_test.rb
244
+ - test/glark/app/compound_test.rb
245
+ - test/glark/app/count_test.rb
246
+ - test/glark/app/files_without_match_test.rb
247
+ - test/glark/match_test.rb
248
+ - test/glark/input/exclude_matching_test.rb
249
+ - test/glark/input/split_as_path_test.rb
250
+ - test/glark/input/filter/filter_spec_test.rb
251
+ - test/glark/input/filter/filter_test.rb
252
+ - test/glark/input/filter/criteria_test.rb
253
+ - test/glark/input/range_test.rb
254
+ - test/glark/input/size_limit_test.rb
255
+ - test/glark/input/path_test.rb
256
+ - test/glark/input/ext_test.rb
257
+ - test/glark/input/directory_test.rb
258
+ - test/glark/input/binary_file_test.rb
259
+ - test/glark/input/name_test.rb
260
+ - test/glark/input/dirname_test.rb
261
+ - test/glark/resources.rb
262
+ - test/glark/tc.rb
263
+ - test/resources/greet.rb
264
+ - test/resources/add.rb
265
+ - test/resources/echo.rb
data/README DELETED
File without changes
data/bin/jlark DELETED
@@ -1,63 +0,0 @@
1
- #!/bin/sh
2
- exec ruby -w -x $0 ${1+"$@"} # -*- ruby -*-
3
- #!ruby -w
4
- # vim: set filetype=ruby : set sw=2
5
-
6
- # Extends glark into Java archives (jar and zip files)
7
-
8
- # $Id$
9
-
10
- dir = File.dirname(File.dirname(File.expand_path(__FILE__)))
11
-
12
- libpath = dir + "/share"
13
- $:.unshift libpath
14
-
15
- require 'glark/glark'
16
-
17
- $PACKAGE = "jlark"
18
-
19
- class Jlark < Glark
20
-
21
- def search(name)
22
- case name[-4 .. -1]
23
- when '.jar'
24
- search_jar_file(name)
25
- when '.zip'
26
- search_zip_file(name)
27
- end
28
- end
29
-
30
- def search_archive_file(fname, command)
31
- if skipped?(fname)
32
- log { "skipping file: #{fname}" }
33
- else
34
- IO.popen(command) do |io|
35
- ifile_args = {
36
- :after => @after,
37
- :before => @before,
38
- :output => @output
39
- }
40
-
41
- input = InputFile.new(fname, io, ifile_args)
42
- search_file(input)
43
- end
44
- end
45
- end
46
-
47
- def search_jar_file(fname)
48
- search_archive_file(fname, "jar tvf #{fname}")
49
- end
50
-
51
- def search_zip_file(fname)
52
- search_archive_file(fname, "unzip -l #{fname}")
53
- end
54
- end
55
-
56
- # and use a cached file for .jar and .zip files
57
-
58
- # the pattern is always -r ., with --classpath optional
59
- class JlarkOptions < GlarkOptions
60
-
61
- end
62
-
63
- Jlark.main
@@ -1,440 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- #!ruby -w
3
- # vim: set filetype=ruby : set sw=2
4
-
5
- # Extended regular-expression-based expressions.
6
-
7
- require 'rubygems'
8
- require 'riel'
9
-
10
- require 'glark/options'
11
-
12
- # A function object, which can be applied (processed) against a InputFile.
13
- class FuncObj
14
- include Loggable
15
-
16
- attr_accessor :match_line_number, :file, :matches, :invert_match
17
-
18
- def initialize
19
- @match_line_number = nil
20
- @matches = Array.new
21
-
22
- opts = GlarkOptions.instance
23
- @invert_match = opts.invert_match
24
- @display_matches = !opts.file_names_only && opts.filter && !opts.count
25
- @range_start = opts.range_start
26
- @range_end = opts.range_end
27
- @file_names_only = opts.file_names_only
28
- @match_limit = opts.match_limit
29
- @write_null = opts.write_null
30
- @filter = opts.filter
31
- end
32
-
33
- def add_match(lnum)
34
- @matches.push(lnum)
35
- end
36
-
37
- def start_position
38
- match_line_number
39
- end
40
-
41
- def end_position
42
- start_position
43
- end
44
-
45
- def reset_file(file)
46
- @match_line_number = nil
47
- @file = file
48
- @matches = Array.new
49
- end
50
-
51
- def range(var, infile)
52
- if var
53
- if var.index(/([\.\d]+)%/)
54
- count = infile.linecount
55
- count * $1.to_f / 100
56
- else
57
- var.to_f
58
- end
59
- else
60
- nil
61
- end
62
- end
63
-
64
- def process(infile)
65
- got_match = false
66
- reset_file(infile.fname)
67
-
68
- rgstart = range(@range_start, infile)
69
- rgend = range(@range_end, infile)
70
-
71
- lastmatch = 0
72
- nmatches = 0
73
- lnum = 0
74
- infile.each_line do |line|
75
- if ((!rgstart || lnum >= rgstart) &&
76
- (!rgend || lnum <= rgend) &&
77
- evaluate(line, lnum, infile))
78
-
79
- mark_as_match(infile)
80
- got_match = true
81
- nmatches += 1
82
-
83
- if @display_matches
84
- infile.write_matches(!@invert_match, lastmatch, lnum)
85
- lastmatch = lnum + 1
86
- elsif @file_names_only
87
- # we don't need to match more than once
88
- break
89
- end
90
-
91
- if @match_limit && nmatches >= @match_limit
92
- # we've found the match limit
93
- break
94
- end
95
- end
96
- lnum += 1
97
- end
98
-
99
- if @file_names_only
100
- if got_match != @invert_match
101
- if @write_null
102
- print infile.fname + "\0"
103
- else
104
- puts infile.fname
105
- end
106
- end
107
- elsif @filter
108
- if @invert_match
109
- infile.write_matches(false, 0, lnum)
110
- elsif got_match
111
- infile.write_matches(true, 0, lnum)
112
- end
113
- else
114
- infile.write_all
115
- end
116
- end
117
-
118
- def mark_as_match(infile)
119
- infile.mark_as_match(start_position)
120
- end
121
-
122
- def to_s
123
- str = inspect
124
- end
125
-
126
- end
127
-
128
-
129
- # -------------------------------------------------------
130
- # Regular expression function object
131
- # -------------------------------------------------------
132
-
133
- # Applies a regular expression against a InputFile.
134
- class RegexpFuncObj < FuncObj
135
-
136
- attr_reader :re
137
-
138
- def initialize(re, hlidx, args = Hash.new)
139
- @re = re
140
- @file = nil
141
- if @highlight = args[:highlight]
142
- @text_highlights = args[:text_highlights]
143
- @hlidx = if @text_highlights.length > 0 && args[:highlight] == "multi"
144
- hlidx % @text_highlights.length
145
- else
146
- 0
147
- end
148
- end
149
-
150
- @extract_matches = args[:extract_matches]
151
-
152
- super()
153
- end
154
-
155
- def <=>(other)
156
- @re <=> other.re
157
- end
158
-
159
- def ==(other)
160
- @re == other.re
161
- end
162
-
163
- def inspect
164
- @re.inspect
165
- end
166
-
167
- def match?(line)
168
- @re.match(line)
169
- end
170
-
171
- def evaluate(line, lnum, file)
172
- if Log.verbose
173
- log { "evaluating <<<#{line[0 .. -2]}>>>" }
174
- end
175
-
176
- if md = match?(line)
177
- log { "matched" }
178
- if @extract_matches
179
- if md.kind_of?(MatchData)
180
- line.replace(md[-1] + "\n")
181
- else
182
- warn "--not does not work with -v"
183
- end
184
- else
185
- # log { "NOT replacing line" }
186
- end
187
-
188
- @match_line_number = lnum
189
-
190
- if @highlight
191
- highlight_match(lnum, file)
192
- end
193
-
194
- add_match(lnum)
195
- true
196
- else
197
- false
198
- end
199
- end
200
-
201
- def explain(level = 0)
202
- " " * level + to_s + "\n"
203
- end
204
-
205
- def highlight_match(lnum, file)
206
- log { "lnum: #{lnum}; file: #{file}" }
207
-
208
- lnums = file.get_range(lnum)
209
- log { "lnums(#{lnum}): #{lnums}" }
210
- if lnums
211
- lnums.each do |ln|
212
- str = file.output.formatted[ln] || file.get_line(ln)
213
- if Log.verbose
214
- log { "file.output.formatted[#{ln}]: #{file.output.formatted[ln]}" }
215
- log { "file.get_line(#{ln}): #{file.get_line(ln)}" }
216
- log { "highlighting: #{str}" }
217
- end
218
-
219
- file.output.formatted[ln] = str.gsub(@re) do |m|
220
- lastcapts = Regexp.last_match.captures
221
- # find the index of the first non-nil capture:
222
- miidx = (0 ... lastcapts.length).find { |mi| lastcapts[mi] } || @hlidx
223
-
224
- @text_highlights[miidx].highlight(m)
225
- end
226
- end
227
- end
228
- end
229
-
230
- end
231
-
232
-
233
- # -------------------------------------------------------
234
- # Compound expression function object
235
- # -------------------------------------------------------
236
-
237
- # Associates a pair of expressions.
238
- class CompoundExpression < FuncObj
239
-
240
- attr_reader :ops
241
-
242
- def initialize(*ops)
243
- @ops = ops
244
- @file = nil
245
- super()
246
- end
247
-
248
- def reset_file(file)
249
- @ops.each do |op|
250
- op.reset_file(file)
251
- end
252
- super
253
- end
254
-
255
- def start_position
256
- @last_start
257
- end
258
-
259
- def ==(other)
260
- self.class == other.class && @ops == other.ops
261
- end
262
-
263
- end
264
-
265
-
266
- # -------------------------------------------------------
267
- # Multi-Or expression function object
268
- # -------------------------------------------------------
269
-
270
- # Evaluates both expressions.
271
- class MultiOrExpression < CompoundExpression
272
-
273
- def evaluate(line, lnum, file)
274
- matched_ops = @ops.select do |op|
275
- op.evaluate(line, lnum, file)
276
- end
277
-
278
- if is_match?(matched_ops)
279
- lastmatch = matched_ops[-1]
280
- @last_start = lastmatch.start_position
281
- @last_end = lastmatch.end_position
282
- @match_line_number = lnum
283
-
284
- add_match(lnum)
285
- true
286
- else
287
- false
288
- end
289
- end
290
-
291
- def inspect
292
- "(" + @ops.collect { |op| op.to_s }.join(" " + operator + " ") + ")"
293
- end
294
-
295
- def end_position
296
- @last_end
297
- end
298
-
299
- def explain(level = 0)
300
- str = " " * level + criteria + ":\n"
301
- str += @ops.collect { |op| op.explain(level + 4) }.join(" " * level + operator + "\n")
302
- str
303
- end
304
-
305
- end
306
-
307
-
308
- # -------------------------------------------------------
309
- # Inclusive or expression function object
310
- # -------------------------------------------------------
311
-
312
- # Evaluates the expressions, and is satisfied when one return true.
313
- class InclusiveOrExpression < MultiOrExpression
314
-
315
- def is_match?(matched_ops)
316
- return matched_ops.size > 0
317
- end
318
-
319
- def operator
320
- "or"
321
- end
322
-
323
- def criteria
324
- ops.size == 2 ? "either" : "any of"
325
- end
326
-
327
- end
328
-
329
-
330
- # -------------------------------------------------------
331
- # Exclusive or expression function object
332
- # -------------------------------------------------------
333
-
334
- # Evaluates the expressions, and is satisfied when only one returns true.
335
- class ExclusiveOrExpression < MultiOrExpression
336
-
337
- def is_match?(matched_ops)
338
- return matched_ops.size == 1
339
- end
340
-
341
- def operator
342
- "xor"
343
- end
344
-
345
- def criteria
346
- "only one of"
347
- end
348
-
349
- end
350
-
351
-
352
- # -------------------------------------------------------
353
- # And expression function object
354
- # -------------------------------------------------------
355
-
356
- # Evaluates both expressions, and is satisfied when both return true.
357
- class AndExpression < CompoundExpression
358
-
359
- def initialize(dist, op1, op2)
360
- @dist = dist
361
- super(op1, op2)
362
- end
363
-
364
- def mark_as_match(infile)
365
- infile.mark_as_match(start_position, end_position)
366
- end
367
-
368
- def match_within_distance(op, lnum)
369
- stack "op: #{op}; lnum: #{lnum}"
370
- op.matches.size > 0 and (op.matches[-1] - lnum <= @dist)
371
- end
372
-
373
- def inspect
374
- str = "("+ @ops[0].to_s
375
- if @dist == 0
376
- str += " same line as "
377
- elsif @dist.kind_of?(Float) && @dist.infinite?
378
- str += " same file as "
379
- else
380
- str += " within " + @dist.to_s + " lines of "
381
- end
382
- str += @ops[1].to_s + ")"
383
- str
384
- end
385
-
386
- def match?(line, lnum, file)
387
- matches = (0 ... @ops.length).select do |oi|
388
- @ops[oi].evaluate(line, lnum, file)
389
- end
390
-
391
- matches.each do |mi|
392
- oidx = (1 + mi) % @ops.length
393
- other = @ops[oidx]
394
- if match_within_distance(other, lnum)
395
- # search for the maximum match within the distance limit
396
- other.matches.each do |m|
397
- if lnum - m <= @dist
398
- log { "match: #{m} within range #{@dist} of #{lnum}" }
399
- @last_start = m
400
- return true
401
- end
402
- end
403
- log { "other matches out of range" }
404
- return false
405
- end
406
- end
407
-
408
- return false
409
- end
410
-
411
- def end_position
412
- @ops.collect { |op| op.end_position }.max
413
- end
414
-
415
- def evaluate(line, lnum, file)
416
- if match?(line, lnum, file)
417
- @match_line_number = lnum
418
- true
419
- else
420
- false
421
- end
422
- end
423
-
424
- def explain(level = 0)
425
- str = ""
426
- if @dist == 0
427
- str += " " * level + "on the same line:\n"
428
- elsif @dist.kind_of?(Float) && @dist.infinite?
429
- str += " " * level + "in the same file:\n"
430
- else
431
- lnstr = @dist == 1 ? "line" : "lines"
432
- str += " " * level + "within #{@dist} #{lnstr} of each other:\n"
433
- end
434
- str += @ops[0].explain(level + 4)
435
- str += " " * level + "and\n"
436
- str += @ops[1].explain(level + 4)
437
- str
438
- end
439
-
440
- end