Module: Wirb
- Defined in:
- lib/wirb/colors.rb,
lib/wirb/schema.rb,
lib/wirb/version.rb
Constant Summary collapse
- COLORS =
{ :nothing => '0;0', # light :black => '0;30', :red => '0;31', :green => '0;32', :brown => '0;33', :blue => '0;34', :purple => '0;35', :cyan => '0;36', :light_gray => '0;37', # bold :dark_gray => '1;30', :light_red => '1;31', :light_green => '1;32', :yellow => '1;33', :light_blue => '1;34', :light_purple => '1;35', :light_cyan => '1;36', :white => '1;37', # underline :black_underline => '4;30', :red_underline => '4;31', :green_underline => '4;32', :brown_underline => '4;33', :blue_underline => '4;34', :purple_underline => '4;35', :cyan_underline => '4;36', :white_underline => '4;37', # background :black_background => '7;30', :red_background => '7;31', :green_background => '7;32', :brown_background => '7;33', :blue_background => '7;34', :purple_background => '7;35', :cyan_background => '7;36', :white_background => '7;37', }
- VERSION =
'0.2.5'
Class Method Summary collapse
-
.tokenize(str) ⇒ Object
This is an extended version of the original wirble tokenizer.
Class Method Details
.tokenize(str) ⇒ Object
This is an extended version of the original wirble tokenizer. Almost everyone would say that 300 lines long case statements need refactoring, but …sometimes it just doesn’t matter ;)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/wirb/tokenizer.rb', line 5 def tokenize(str) raise ArgumentError, 'Tokenizer needs an inspect-string' unless str.is_a? String return enum_for(:tokenize, str) unless block_given? chars = str.split(//) @state, @token, i = [], '', 0 @passed, snapshot = '', nil # exception handling # helpers pass_custom_state = lambda{ |kind, *| yield kind, @token unless @token.empty? @passed << @token @state.pop if .include?(:remove) @token = '' unless .include?(:keep_token) @repeat = true if .include?(:repeat) } pass_state = lambda{ |*| pass_custom_state[ @state[-1], * ] } pass = lambda{ |kind, string| @passed << string; yield kind, string } set_state = lambda{ |state| @state[-1] = state } get_state = lambda{ |state| @state[-1] == state } get_previous_state = lambda{ |state| @state[-2] == state } push_state = lambda{ |state, *| @state << state @repeat = true if .include? :repeat } pop_state = lambda{ |*| @state.pop @repeat = true if .include? :repeat } # action! while i <= chars.size @repeat = false llc, lc, c, nc = lc, c, chars[i], chars[i+1] # warn "char = #{c} state = #{@state*':'}" case @state[-1] when nil, :hash, :array, :enumerator, :set # "default" state case c when ':' then push_state[:symbol] when '"' then push_state[:string] when '/' then push_state[:regexp] when '#' then push_state[:object] when /[A-Z]/ then push_state[:class, :repeat] when /[a-z]/ then push_state[:keyword, :repeat] when /[0-9-]/ then push_state[:number, :repeat] when '.' then push_state[:range, :repeat] when /\s/ then pass[:whitespace, c] when ',' then pass[:comma, ','] when '>' if lc == '=' if get_state[:hash] pass[:refers, '=>'] else # FIXME remove this buggy <=> cheat pass[:symbol, '=>'] end end when '(' if nc =~ /[0-9-]/ push_state[:rational, :repeat] else push_state[:object_description, :repeat] open_brackets = 0 end when '{' if get_state[:set] pass[:open_set, '{']; push_state[nil] else pass[:open_hash, '{']; push_state[:hash] end when '[' if get_state[:enumerator] pass[:open_enumerator, '[']; push_state[nil] else pass[:open_array, '[']; push_state[:array] end when ']' if get_state[:array] pass[:close_array, ']']; pop_state[] elsif get_previous_state[:enumerator] pass[:close_enumerator, ']']; pop_state[] set_state[:object_description] end when '}' if get_state[:hash] pass[:close_hash, '}']; pop_state[] elsif get_previous_state[:set] pass[:close_set, '}']; pop_state[] set_state[:object_description] end # else # warn "ignoring char #{c.inspect}" if @debug end when :class case c when /[a-z0-9_]/i @token << c else if c ==':' && nc == ':' pass_state[] pass[:class_separator, '::'] elsif !(c == ':' && lc == ':') pass_state[:remove, :repeat] end end when :symbol case c when /"/ pass[:symbol_prefix, ':'] set_state[:symbol_string] @token = '' when /[^"., }\])=]/ @token << c else if c == ']' && lc == '[' @token << c elsif c == '=' && nc != '>' # FIXME: spaceship error @token << c else pass[:symbol_prefix, ':'] pass_state[:remove, :repeat] end end when :symbol_string if c == '"' && ( !( @token =~ /\\+$/; $& ) || $&.size % 2 == 0 ) # see string pass[:open_symbol_string, '"'] pass_state[:remove] pop_state[] pass[:close_symbol_string, '"'] else @token << c end when :string if c == '"' && ( !( @token =~ /\\+$/; $& ) || $&.size % 2 == 0 ) # allow escaping of " and pass[:open_string, '"'] # work around \\ pass_state[:remove] pass[:close_string, '"'] else @token << c end when :regexp if c == '/' && ( !( @token =~ /\\+$/; $& ) || $&.size % 2 == 0 ) # see string pass[:open_regexp, '/'] pass_state[:remove] pass[:close_regexp, '/'] push_state[:regexp_flags] else @token << c end when :regexp_flags if c =~ /[a-z]/i #*%w[m i x o n e u s] @token << c else pass_state[:remove, :repeat] end when :keyword if c =~ /[a-z0-9_]/i @token << c pass_custom_state[@token.to_sym, :remove] if %w[nil false true].include?(@token) else pass_state[:remove, :repeat] end when :number if c =~ /[0-9e+.-]/ && !(c == '.' && nc == '.') @token << c elsif c == '/' # ruby 1.8 mathn pass_state[] pass[:rational_separator, '/'] else pass_state[:remove, :repeat] end when :range if c == '.' @token << c else pass_state[:remove, :repeat] end when :rational case c when '(' pass[:open_rational, '('] when /[0-9-]/ push_state[:number, :repeat] when '/', ',' pass[:rational_separator, c] when ' ' pass[:whitespace, c] when ')' pass[:close_rational, ')'] pop_state[] end when :object case c when '<' pass[:open_object, '#<'] push_state[:object_class] open_brackets = 0 when '>' pass[:close_object, '>'] pop_state[]; @token = '' end when :object_class case c when /[a-z0-9_]/i @token << c when '>' pass_state[:remove, :repeat] else if c == ':' && nc == ':' pass_state[] pass[:class_separator, '::'] elsif !(c == ':' && lc == ':') pass_state[:keep_token] pass[:object_description_prefix, c] if %w[set].include? @token = @token.downcase set_state[@token.to_sym] else set_state[:object_description] end @token = '' end end when :object_description case c when '>', nil if open_brackets == 0 pass_state[:remove, :repeat] else open_brackets -= 1 @token << c end when '<' open_brackets += 1 @token << c when '[' pass_state[:repeat] set_state[:enumerator] when '"' pass_state[] push_state[:string] when /[0-9]/ if c == '0' && nc == 'x' pass_state[:repeat] push_state[:object_addr] else # push_state[:number, :repeat] @token << c end else @token << c end when :object_addr if c =~ /[x0-9a-f]/ @token << c else if c == '@' pass_state[:remove] push_state[:object_line] pass[:object_line_prefix, '@'] else pass_state[:remove, :repeat] end end when :object_line if c == ':' && nc =~ /[0-9]/ @token << ':' pass_state[:remove] push_state[:object_line_number] else @token << c end when :object_line_number if c =~ /[0-9]/ @token << c else pass_state[:remove, :repeat] end # else # raise "unknown state #{@state[-1]} #{@state.inspect}" end # next round :) if !@repeat i += 1 elsif snapshot && Marshal.load(snapshot) == [@state, @token, llc, lc, c, nc] # loop protection raise 'Wirb Bug :/' end snapshot = Marshal.dump([@state, @token, llc, lc, c, nc]) end rescue # p$!, $!.backtrace[0] pass[:default, str.sub(@passed, '')] end |