Class: WR::ParseScript

Inherits:
Ripper::Filter
  • Object
show all
Defined in:
lib/wrb/documents/make_doc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, fname = nil, lnno = 1) ⇒ ParseScript

Returns a new instance of ParseScript.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/wrb/documents/make_doc.rb', line 9

def initialize(src, fname=nil, lnno=1)
  super(src, fname, lnno)
  @result = []
  @depth = 0
  @comments = []
  @modules = {:depth=>0}
  @current = @modules
  @methods = {}
  @required = {}
  @lines = src.lines
end

Instance Attribute Details

#modulesObject (readonly)

Ripper::SCANNER_EVENTS -> [:CHAR, :__end__, :backref, :backtick, :comma, :comment, :const, :cvar, :embdoc, :embdoc_beg, :embdoc_end, :embexpr_beg, :embexpr_end, :embvar, :float, :gvar, :heredoc_beg, :heredoc_end, :ident, :ignored_nl, :imaginary, :int, :ivar, :kw, :label, :label_end, :lbrace, :lbracket, :lparen, :nl, :op, :period, :qsymbols_beg, :qwords_beg, :rational, :rbrace, :rbracket, :regexp_beg, :regexp_end, :rparen, :semicolon, :sp, :symbeg, :symbols_beg, :tlambda, :tlambeg, :tstring_beg, :tstring_content, :tstring_end, :words_beg, :words_sep]



8
9
10
# File 'lib/wrb/documents/make_doc.rb', line 8

def modules
  @modules
end

#resultObject (readonly)

Ripper::SCANNER_EVENTS -> [:CHAR, :__end__, :backref, :backtick, :comma, :comment, :const, :cvar, :embdoc, :embdoc_beg, :embdoc_end, :embexpr_beg, :embexpr_end, :embvar, :float, :gvar, :heredoc_beg, :heredoc_end, :ident, :ignored_nl, :imaginary, :int, :ivar, :kw, :label, :label_end, :lbrace, :lbracket, :lparen, :nl, :op, :period, :qsymbols_beg, :qwords_beg, :rational, :rbrace, :rbracket, :regexp_beg, :regexp_end, :rparen, :semicolon, :sp, :symbeg, :symbols_beg, :tlambda, :tlambeg, :tstring_beg, :tstring_content, :tstring_end, :words_beg, :words_sep]



8
9
10
# File 'lib/wrb/documents/make_doc.rb', line 8

def result
  @result
end

Instance Method Details

#on_comma(token, pd) ⇒ Object

dpp @getting_array_data, lineno, pd



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/wrb/documents/make_doc.rb', line 162

def on_comma(token, pd) #dpp @getting_array_data, lineno, pd
  if @getting_hash_data && pd=~/\A\s*\:?\w+\s*=>\s*(\:?\w+|\[.+\])/
    k, v =pd.split('=>') #; dpp k.strip, v.strip
    @getting_hash_data[k.strip] = v.strip
    nil
  elsif @getting_array_data && pd=~/\A\s*:?[\w_]+/
    @getting_array_data[pd.strip] = ""
    nil
  else
    pd.to_s + token
  end
end

#on_comment(token, pd) ⇒ Object

; dpp lineno, pd, token #if @getting_array_data



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/wrb/documents/make_doc.rb', line 175

def on_comment(token, pd) #; dpp lineno, pd, token #if @getting_array_data
  if pd=~/(\w+|[\)'"`])\s*\Z/
    perform_nl(token, pd)
  elsif @getting_hash_data
    if pd=~/\A\s*\:?\w+\s*=>\s*(\:?\w+|\[.+\])/
      k, v =pd.split('=>') #; dpp k.strip, v.strip
      @getting_hash_data[k.strip] = v.strip
    end
    s = @getting_hash_data[@getting_hash_data.keys.last] #; dpp s, s=~/.+#/
    s << token unless s=~/.+\#/
    return nil
  elsif @getting_array_data
    @getting_array_data[@getting_array_data.keys.last] = token.strip
    return nil
  end
  @comments << token
  nil
end

#on_default(event, token, pd) ⇒ Object

; dpp lineno, event, token, pd



87
88
89
90
91
92
93
94
95
96
# File 'lib/wrb/documents/make_doc.rb', line 87

def on_default(event, token, pd) # ; dpp lineno, event, token, pd
  case event
  when :on_nl, :on_semicolon #; dpp lineno, pd
    perform_nl(token, pd)
  when :on_ignored_nl #; dpp lineno, pd
     perform_nl(token, pd)
  else
    pd.to_s + token
  end
end

#on_embdoc(token, pd) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/wrb/documents/make_doc.rb', line 98

def on_embdoc(token, pd)
  return @_current_rd=nil if token.empty?
  case token
  when /^\s*(self\.\s*[\w_]+[=?]?|\[\]=?|<<)(\(.*\))/ #; dpp token
    @_current_rd = (@current[:singletons][$1.strip] = [$2.strip, []])
  when /^\s*([\w_]+[=?]?|\[\]=?|<<)(\(.*\)(:?\{.+\})?)/
    @_current_rd = (@current[:methods][$1.strip] = [$2.strip, []])
  when /^\s*$/
    @_current_rd = nil
  else
    @_current_rd[1] << token if @_current_rd
  end
  nil
end

#on_ident(token, pd) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/wrb/documents/make_doc.rb', line 209

def on_ident(token, pd) # 
  case pd
  when /\s*def\s+/
#        dpp lineno, pd, token
  end
  pd.to_s + token
end

#on_kw(token, pd) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/wrb/documents/make_doc.rb', line 194

def on_kw(token, pd)
  case token
  when 'module', 'class';# dpp lineno, @depth, token
    @depth += 1
  when 'begin', 'case', 'def', 'do', 'while';# dpp lineno, @depth, token
    @depth += 1
  when 'if', 'unless', 'until', 'while' #;dpp lineno, token, pd #, pd=~/\A\s+\z/
    @depth += 1 if pd =~ /\A\s+\z/ || pd=~/\n\s*\z/ || pd.empty? || pd=~/[\=\+\-\*\/]\s*\z/
  when 'end'# dpp lineno, @depth, @current[:depth]
    @current = @current[:parent] if @depth == @current[:depth]
    @depth -= 1
  end
  pd.to_s + token
end

#on_lbrace(token, pd) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/wrb/documents/make_doc.rb', line 122

def on_lbrace(token, pd)
  case pd
  when /\A\s*PreDefinedEvents\s*=\s*/
    @getting_hash_data = @current[:predefinedevents]
    @getting_hash_data[:__COMMENTS__] = @comments.dup
    @comments.clear
    nil
  when /\A\s*WinStyles\s*=\s*/
    @getting_hash_data = @current[:styles]
    @getting_hash_data[:__COMMENTS__] = @comments.dup
    @comments.clear
    nil
  else
    pd + token
  end
end

#on_lbracket(token, pd) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/wrb/documents/make_doc.rb', line 145

def on_lbracket(token, pd)
  case pd
  when /\A\s*DefaultEvents\s*=\s*/#; dpp pd
    @getting_array_data = @current[:defaultevents]
    @getting_array_data[:__COMMENTS__] = @comments.dup
    @comments.clear
    return nil
  end
  pd + token
end

#on_rbrace(token, pd) ⇒ Object

pp @getting_hash_data



138
139
140
141
142
143
# File 'lib/wrb/documents/make_doc.rb', line 138

def on_rbrace(token, pd) #pp @getting_hash_data
  if @getting_hash_data
    return @getting_hash_data = nil
  end
  pd + token
end

#on_rbracket(token, pd) ⇒ Object



155
156
157
158
159
160
# File 'lib/wrb/documents/make_doc.rb', line 155

def on_rbracket(token, pd)
  if @getting_array_data
    return @getting_array_data = nil
  end
  pd + token
end

#on_rparen(token, pd) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/wrb/documents/make_doc.rb', line 113

def on_rparen(token, pd)
#      if pd=~/def (:?self\.)?[\w_]+[=?]?\s*\(.*/ #; dpp pd
  if pd=~/\A\s*def\s+/ #; dpp pd
     perform_nl(token, pd+token)
    return nil
  end
  pd.to_s + token
end

#perform_nl(token, pd) ⇒ Object

dpp lineno, pd



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wrb/documents/make_doc.rb', line 32

def perform_nl(token, pd)  #dpp lineno, pd
  if @getting_hash_data && pd=~/\A\s*\:?\w+\s*=>\s*(\:?\w+|\[.+\])/
    if pd
      k, v = pd.split('=>')
      @getting_hash_data[k.strip] = v.strip
      @getting_hash_data = nil
    end
    return nil
  elsif @getting_array_data #; dpp pd, token, @getting_array_data
    if pd
      @getting_array_data[pd.strip] = token.strip
      @getting_array_data = nil
    end
    return nil
  end
  case pd 
  when /\A\s*(module|class)\s+([A-Z]\w*.*)/ #@comments
    nm = $2.strip
    k = nm.split('<')[0].strip
    @current[k] ||= {:type=>$1, :fullname=>nm, :parent=>@current, :depth=>@depth,
      :comments=>@comments.dup, :singletons=>{}, :methods=>{}, :aliases=>{},
      :styles=>{}, :predefinedevents=>{}, :defaultevents=>{}}
    @current = @current[k]
  when /\A\s*def\s+(self.[A-Za-z_]\w*[?\=]?|self\.\[\]=?)(.+)/ # singleton method
    return nil if @depth>4
    @current[:singletons][$1.strip] = [$2, @comments.dup]
  when /\A\s*def\s+([A-Za-z_]\w*[?\=!]?|\[\]=?|[+-<>|&]+)(.+)/ # instance method
    return nil if @depth>4 #; dpp @current[:fullname], $1
    @current[:methods][$1.strip] = [$2, @comments.dup]
  when /\A\s*alias\s+(\:?[\w_]+[=?]?|[\[\]+-<>=|]+)\s+(\:?[\w_]+[=?]?|[\[\]+-<>=|]+)/
    #        dpp lineno, $1, $2.tr(':',''), @current[:methods][$2.tr(':','')]
    if @current[:methods][$2.tr(': ','')]
      if @current[:aliases][$2] 
        @current[:aliases][$2] << $1
      else
        @current[:aliases][$2] = [$1]
      end
    end
  when /\A\s*require\W+['"]([\w\/]+)/
    mod=perform_require($1);#dpp  mod['WR'].keys if mod && mod['WR']
    if @required.empty?
      @required.update mod if mod
    else
      @required['WR'].update(mod['WR']) if mod && mod['WR']
    end
  when /\A\s*include\W+([A-Z][\w_]*)/
    if (mod1=@required['WR']) && (h=mod1[$1])
      @current[:methods].update(h[:methods])
      @current[:aliases].update(h[:aliases])
    end
  end
  @comments.clear
  nil
end

#perform_require(feature) ⇒ Object

event handers ##############



23
24
25
26
27
28
29
30
# File 'lib/wrb/documents/make_doc.rb', line 23

def perform_require(feature)
  if feature =~ /wrb\/([\w_]+)/
    s = open("../#{$1}.rb"){|f| f.read}
    ps = ParseScript.new(s)
    ps.parse
    ps.modules
  end
end