string_literal_extractor 0.0.1 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7c72d3ff93f4a3ca4dd93f3d74bc949d1681060
4
- data.tar.gz: 16552e6fbfc788ff2f2bb37d75ef18bdf4c44d1a
3
+ metadata.gz: 543cc852e84b1bd0ef01ab0f63b6e27fac160462
4
+ data.tar.gz: 5cb35717eb6f5228d29f0be84508929712490ccf
5
5
  SHA512:
6
- metadata.gz: 1c43345413e6792721fe79fe7a2ffec837d8d57c1654ab5452f296a0f861b717805e6f0b43946d1bedfaead30339388c3250cca1fdbe9fe32baefd6fb9706f64
7
- data.tar.gz: 02e7b32e76ee2f038d176761cd78db7c2c602e67ef175db52e3bfef67ec865f22c19108dafdd71eb5f87b7a2434564b7f6900991a4bca499f279589d286e9c24
6
+ metadata.gz: 1eabe26d0a05c6479ce79f90bdd95e12af6d766b53a6bc694bcdf2a41b3946b6067014be05d472de0ad8af5b75ecfd9336cd486a15dbb025d5927e2278b5cb02
7
+ data.tar.gz: ee1607412100eed4348aeb92680d21031f6291ef8a185684662107894800de027dda78569848005906041824b6173df69e1e254685daf2f9d60ea8f98e1d5c03
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -0,0 +1,75 @@
1
+ require 'ripper'
2
+
3
+ class StringLiteralExtractor < Ripper
4
+ include Enumerable
5
+
6
+ %i(heredoc_beg tstring_beg).each do |event|
7
+ module_eval(<<-CODE, __FILE__, __LINE__ + 1)
8
+ def on_#{event}(tok)
9
+ @buf ||= []
10
+ @buf << [[lineno, column]]
11
+ end
12
+ CODE
13
+ end
14
+
15
+ def on_tstring_end(tok)
16
+ @buf.last << [lineno, column]
17
+ end
18
+
19
+ def on_heredoc_end(tok)
20
+ @buf.last << [lineno, column + tok.size-1]
21
+ end
22
+
23
+ def on_CHAR(literal)
24
+ @string_literal_each.call(literal, [[lineno, column], [lineno, column + 1]])
25
+ end
26
+
27
+ def on_string_literal(*args)
28
+ pos = @buf.pop
29
+ lineno_pos = pos.map(&:first)
30
+ column_pos = pos.map(&:last)
31
+ lines = @src.lines[lineno_pos.first-1...lineno_pos.last]
32
+ if lineno_pos.first == lineno_pos.last
33
+ lines[0] = lines[0][column_pos.first..column_pos.last]
34
+ else
35
+ lines[0] = lines[0].dup.tap { |l| l[0...column_pos.first] = '' }
36
+ lines[-1] = lines[-1].dup.tap { |l| l[column_pos.last+1..-1] = '' }
37
+ end
38
+ @string_literal_each.call(lines.join, pos)
39
+
40
+ args.unshift(:string_literal)
41
+ args
42
+ end
43
+
44
+ def initialize(src, filename="(ripper)", lineno=1)
45
+ @src = src
46
+ super
47
+ end
48
+
49
+ def each
50
+ @string_literal_each = -> (literal, pos) { yield literal, pos }
51
+ parse
52
+ end
53
+ end
54
+
55
+ return unless $0 == __FILE__
56
+
57
+ StringLiteralExtractor.new(%q{"#{'hi' + 'hoi'}"}).each do |literal, pos|
58
+ p [literal, pos]
59
+ end
60
+
61
+ StringLiteralExtractor.new(%q{"a" "b"}).each do |literal, pos|
62
+ p [literal, pos]
63
+ end
64
+
65
+ StringLiteralExtractor.new("<<EOS\n foo bar\n\nEOS").each do |literal, pos|
66
+ p [literal, pos]
67
+ end
68
+
69
+ StringLiteralExtractor.new('"#{hi}"').each do |literal, pos|
70
+ p [literal, pos]
71
+ end
72
+
73
+ StringLiteralExtractor.new('?a').each do |literal, pos|
74
+ p [literal, pos]
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_literal_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seiei Miyagi
@@ -37,7 +37,8 @@ email: hanachin@gmail.com
37
37
  executables: []
38
38
  extensions: []
39
39
  extra_rdoc_files: []
40
- files: []
40
+ files:
41
+ - string_literal_extractor.rb
41
42
  homepage: https://github.com/hanachin/string_literal_extractor
42
43
  licenses:
43
44
  - MIT
metadata.gz.sig CHANGED
Binary file