Class: RegExpr
- Inherits:
-
Hash
- Object
- Hash
- RegExpr
- Defined in:
- lib/regexpr.rb,
lib/regexpr.rb
Defined Under Namespace
Classes: Begin, Block, Char, Chars, End, Not, Or, Range, Regexp, Repeat, Segment, WildCard
Constant Summary collapse
- STDEXP =
Class Method Summary collapse
Instance Method Summary collapse
- #=~(x) ⇒ Object
- #def(cl = Class.new, *exp) ⇒ Object
- #match(m, exp = :main) ⇒ Object
- #parse(t, u = nil) ⇒ Object
- #to_r(exp = nil) ⇒ Object
- #to_r_next(exp) ⇒ Object
- #to_re(exp = nil) ⇒ Object
Class Method Details
.[](*vals) ⇒ Object
349 350 351 352 353 |
# File 'lib/regexpr.rb', line 349 def [] *vals ret= super *vals STDEXP.each {|k, v| ret[ k]||= v } ret end |
.new(*vals) ⇒ Object
355 356 357 358 359 |
# File 'lib/regexpr.rb', line 355 def new *vals ret= super *vals STDEXP.each {|k, v| ret[ k]||= v } ret end |
Instance Method Details
#=~(x) ⇒ Object
494 |
# File 'lib/regexpr.rb', line 494 def =~( x) to_r =~ x end |
#def(cl = Class.new, *exp) ⇒ Object
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/regexpr.rb', line 474 def def cl= Class.new, *exp exp= [ :main ] if exp.empty? exp.each do |e| re= self.to_re e names= re.names.collect('@%s'.method(:%)).join ', ' re= ::Regexp.new '^%s$'% re.to_r ev= <<-EOF def #{e}= val m= #{re.inspect}. match val raise ArgumentError, 'Unallowed Chars! (%s =~ #{re.inspect})'% val. inspect unless m #{names}= *m[ 1.. -1] end EOF cl.class_eval ev end cl end |
#match(m, exp = :main) ⇒ Object
492 |
# File 'lib/regexpr.rb', line 492 def match( m, exp= :main) to_r( exp).match m end |
#parse(t, u = nil) ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/regexpr.rb', line 384 def parse t, u= nil u||= RegExpr::Block.new until !t or t.empty? v, t= self.to_r_next t case v when ')' then return u, t when RegExpr::Repeat then v.value= u.pop end u.push v end u end |
#to_r(exp = nil) ⇒ Object
362 363 364 365 366 367 368 |
# File 'lib/regexpr.rb', line 362 def to_r exp= nil r = self.to_re exp r.optimize! h, r = r.hidden?, r.to_r r = r[ 1...-1] unless h ::Regexp.new r end |
#to_r_next(exp) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/regexpr.rb', line 397 def to_r_next exp exp.strip! /^/ =~ exp[ 1.. -1] t= case exp[ 0] when ?^ then return RegExpr::Begin.new, exp[ 1.. -1] when ?$ then return RegExpr::End.new, exp[ 1.. -1] when ?\\ h= case exp[ 1] when ?D, ?S, ?W, ?a, ?d.. ?f, ?n, ?r.. ?t, ?v, ?w return RegExpr::WildCard.new( '\%c'% exp[ 1]), exp[ 2.. -1] when ?x then 16 when ?o then 8 when ?b then 2 when ?0.. ?9 exp= 'XX'+ exp[ 1.. -1] 10 else raise ArgumentError, 'Unknown form "%s"'% exp end i= exp[ 2.. -1].to_i h return RegExpr::Char.new( i.chr), exp[ (i.to_s( h). size+ 2).. -1] when ?. return RegExpr::WildCard.new( '.'), exp[ 1.. -1] when ?0 case exp[ 1] when ?x then %r<^0x([0-9a-f]+)>i.match exp return '', $1.to_i( 16).to_s+ $' when ?o then %r<^0o([0-8]+)>.match exp return '', $1.to_i( 8).to_s+ $' when ?b then %r<^0b([01]+)>.match exp return '', $1.to_i( 2).to_s+ $' else case exp when %r<(\d+)..(\d+)> RegExpr::Range.new $1.to_i, $2.to_i when %r<^(\d+,\d+|,\d+|\d+,?)> RegExpr::Repeat.new '', *$1.split( ',') else raise ArgumentError, 'Unknown form "%s"'% exp end end when ?( return parse( exp[ 1.. -1]) when ?) then ')' when ?| then RegExpr::Or.new when ?+ then RegExpr::Repeat.new '', 1, nil when ?* then RegExpr::Repeat.new '', nil when ?? then RegExpr::Repeat.new '', 0, 1 when ?" RegExpr::Char.new %r<^"((?:[^"]|\\")*)">.match( exp)[ 1] when ?[ RegExpr::Chars.new %r<^\[((?:[^\]]|\\\])*[^\\]|)\]>.match( exp)[ 1] when ?/ _, re, f= %r<^/((?:[^/]|\\/)*)/(im?|mi)?>.match( exp) flg= $2=~ /i/ ? ::Regexp::IGNORECASE : 0 flg+= $2=~ /m/ ? ::Regexp::MULTILINE : 0 RegExpr::Regexp.new ::Regexp.new( re, flg) else case exp when %r<^([a-z_][a-z_0-9]*\b)>i self.to_re $1.to_sym when %r<(\d+)..(\d+)> RegExpr::Range.new $1.to_i, $2.to_i when %r<^(\d+,\d+|,\d+|\d+,?)> RegExpr::Repeat.new '', *$1.split( ',') else raise ArgumentError, 'Unknown form "%s"'% exp end end [ t, $' ] end |
#to_re(exp = nil) ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/regexpr.rb', line 370 def to_re exp= nil exp||= :main u= RegExpr::Block.new t, u.hidden= if Symbol === exp u.name= exp.to_sym if self[ exp] [ self[ exp], false] else [ self[ exp.to_s], true] end else [ exp.to_s, true] end parse t end |