Module: Sourcify::Common::Parser::RawScanner::Extensions

Included in:
Method::Parser::RawScanner::Extensions, Proc::Parser::RawScanner::Extensions
Defined in:
lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb

Defined Under Namespace

Classes: Escape

Instance Method Summary collapse

Instance Method Details

#codified_tokens(fix_heredoc = false) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 86

def codified_tokens(fix_heredoc = false)
  (
    if fix_heredoc
      @tokens.map do |key, val|
        case key
        when :heredoc
          %(\n%"#{
            val.sub(/^(?:[^\n]+\n)(.*\n)(?:[^\n]+)$/m, '\1').
              gsub(/\\|"/) {|c| "\\#{c}" }
          }")
        else val
        end
      end
    else
      @tokens.map(&:last)
    end
  ).join
end

#data_frag(range) ⇒ Object



29
30
31
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 29

def data_frag(range)
  @data[range].pack('c*')
end

#increment_linenoObject

Raises:



80
81
82
83
84
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 80

def increment_lineno
  @lineno += 1
  raise Escape \
    if @stop_on_newline || !@results.empty? || (@results.empty? && @rejecting_block)
end

#offset_attributesObject



110
111
112
113
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 110

def offset_attributes
  @lineno = 1 # Fixing JRuby's lineno bug (see http://jira.codehaus.org/browse/JRUBY-5014)
  @tokens = [@tokens[-1]] unless @tokens.empty?
end

#preceded_with?(*args) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 75

def preceded_with?(*args)
  prev_token = @tokens[-1][0] == :space ? @tokens[-2] : @tokens[-1]
  !([args].flatten & prev_token).empty? rescue nil
end

#process(data, opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 13

def process(data, opts={})
  begin
    @encoding = data.encoding
    @start_pattern = opts[:start_pattern] || /.*/
    @body_matcher = opts[:body_matcher] || lambda{|_| true }
    @stop_on_newline = opts[:stop_on_newline]
    @results, @data = [], data.unpack("c*")
    reset_attributes
    execute!
  rescue Escape
    @results
  else
    @results
  end
end

#push(key, ts, te) ⇒ Object



33
34
35
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 33

def push(key, ts, te)
  @tokens << [key, data_frag(ts .. te.pred)]
end

#push_comment(ts, te) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 46

def push_comment(ts, te)
  data = data_frag(ts .. te.pred)
  @comment ||= Comment.new
  @comment << data
  return true unless @comment.closed?
  @tokens << [:comment, @comment.to_s]
  @comment = nil
end

#push_dstring(ts, te) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 37

def push_dstring(ts, te)
  data = data_frag(ts .. te.pred)
  @dstring ||= DString.new(data[%r{^("|`|/|%(?:Q|W|r|x|)(?:\W|_))},1], @encoding)
  @dstring << data
  return true unless @dstring.closed?
  @tokens << [:dstring, @dstring.to_s]
  @dstring = nil
end

#push_heredoc(ts, te) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 55

def push_heredoc(ts, te)
  data = data_frag(ts .. te.pred)
  unless @heredoc
    indented, tag = data.match(/\<\<(\-?)['"]?(\w+)['"]?$/)[1..3]
    @heredoc = Heredoc.new(tag, !indented.empty?, @encoding)
  end
  @heredoc << data
  return true unless @heredoc.closed?(data_frag(te .. te))
  @tokens << [:heredoc, @heredoc.to_s]
  @heredoc = nil
end

#push_label(ts, te) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 67

def push_label(ts, te)
  # NOTE: 1.9.* supports label key, which RubyParser cannot handle, thus
  # conversion is needed.
  @tokens << [:symbol, ':' + data_frag(ts .. te - 2)]
  @tokens << [:space, ' ']
  @tokens << [:assoc, '=>']
end

#reset_attributesObject



105
106
107
108
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 105

def reset_attributes
  @tokens, @lineno = [], 1
  @heredoc, @dstring, @comment, @rejecting_block = nil
end

#valid?(snippet, _ = nil) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
# File 'lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb', line 117

def valid?(snippet, validate_as = nil)
  sexp = Ripper.sexp(snippet)
  return false unless sexp

  case validate_as
  when :hash then sexp[-1][0][0] == :hash
  when nil then true
  else raise ArgumentError
  end
end